store dataframe updated as tuple

This commit is contained in:
Matthias 2020-06-12 14:12:33 +02:00
parent fd97ad9b76
commit 9794914838
2 changed files with 12 additions and 11 deletions

View File

@ -336,4 +336,5 @@ CANCEL_REASON = {
}
# List of pairs with their timeframes
ListPairsWithTimeframes = List[Tuple[str, str]]
PairWithTimeframe = Tuple[str, str]
ListPairsWithTimeframes = List[PairWithTimeframe]

View File

@ -5,12 +5,13 @@ including ticker and orderbook data, live and historical candle (OHLCV) data
Common Interface for bot and strategy to access data.
"""
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from arrow import Arrow
from pandas import DataFrame
from freqtrade.constants import ListPairsWithTimeframes
from freqtrade.constants import ListPairsWithTimeframes, PairWithTimeframe
from freqtrade.data.history import load_pair_history
from freqtrade.exceptions import DependencyException, OperationalException
from freqtrade.exchange import Exchange
@ -25,7 +26,7 @@ class DataProvider:
self._config = config
self._exchange = exchange
self._pairlists = pairlists
self.__cached_pairs: Dict[Tuple(str, str), DataFrame] = {}
self.__cached_pairs: Dict[PairWithTimeframe, Tuple(DataFrame, datetime)] = {}
def _set_cached_df(self, pair: str, timeframe: str, dataframe: DataFrame) -> None:
"""
@ -36,10 +37,7 @@ class DataProvider:
:param timeframe: Timeframe to get data for
:param dataframe: analyzed dataframe
"""
self.__cached_pairs[(pair, timeframe)] = {
'data': dataframe,
'updated': Arrow.utcnow().datetime,
}
self.__cached_pairs[(pair, timeframe)] = (dataframe, Arrow.utcnow().datetime)
def refresh(self,
pairlist: ListPairsWithTimeframes,
@ -104,15 +102,17 @@ class DataProvider:
logger.warning(f"No data found for ({pair}, {timeframe}).")
return data
def get_analyzed_dataframe(self, pair: str, timeframe: str = None) -> DataFrame:
def get_analyzed_dataframe(self, pair: str,
timeframe: str = None) -> Tuple[DataFrame, datetime]:
"""
:param pair: pair to get the data for
:param timeframe: timeframe to get data for
:return: Analyzed Dataframe for this pair
:return: Tuple of (Analyzed Dataframe, lastrefreshed) for the requested pair / timeframe
combination
"""
# TODO: check updated time and don't return outdated data.
if (pair, timeframe) in self._set_cached_df:
return self._set_cached_df[(pair, timeframe)]['data']
if (pair, timeframe) in self.__cached_pairs:
return self.__cached_pairs[(pair, timeframe)]
else:
# TODO: this is most likely wrong...
raise ValueError(f"No analyzed dataframe found for ({pair}, {timeframe})")