reinstate caching for get_ticker

This commit is contained in:
xmatthias 2018-06-06 20:18:16 +02:00
parent 7d3eefa97a
commit e690003621

View File

@ -18,6 +18,8 @@ _API: ccxt.Exchange = None
_CONF: Dict = {} _CONF: Dict = {}
API_RETRY_COUNT = 4 API_RETRY_COUNT = 4
_CACHED_TICKER: Dict[str, Any] = {}
# Holds all open sell orders for dry_run # Holds all open sell orders for dry_run
_DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {} _DRY_RUN_OPEN_ORDERS: Dict[str, Any] = {}
@ -264,17 +266,28 @@ def get_tickers() -> Dict:
raise OperationalException(e) raise OperationalException(e)
# TODO: remove refresh argument, keeping it to keep track of where it was intended to be used
@retrier @retrier
def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict: def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
try: global _CACHED_TICKER
return _API.fetch_ticker(pair) if refresh or pair not in _CACHED_TICKER.keys():
except (ccxt.NetworkError, ccxt.ExchangeError) as e: try:
raise TemporaryError( data = _API.fetch_ticker(pair)
'Could not load ticker history due to {}. Message: {}'.format( try:
e.__class__.__name__, e)) _CACHED_TICKER[pair] = {
except ccxt.BaseError as e: 'bid': float(data['bid']),
raise OperationalException(e) }
except KeyError as e:
logger.debug("Could not cache ticker data for %s", pair)
return data
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(
'Could not load ticker history due to {}. Message: {}'.format(
e.__class__.__name__, e))
except ccxt.BaseError as e:
raise OperationalException(e)
else:
logger.info("returning cached data for %s", pair)
return _CACHED_TICKER[pair]
@retrier @retrier