From 0d980134e7f946aaae9894c0611657a54a8c0b2f Mon Sep 17 00:00:00 2001 From: iuvbio Date: Sun, 10 Mar 2019 13:30:45 +0100 Subject: [PATCH] add markets reload func --- freqtrade/exchange/exchange.py | 19 +++++++++++++++++++ freqtrade/freqtradebot.py | 3 +++ 2 files changed, 22 insertions(+) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 8eb74bdd5..30bf9a30c 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -88,6 +88,8 @@ class Exchange(object): # Holds last candle refreshed time of each pair self._pairs_last_refresh_time: Dict[Tuple[str, str], int] = {} + # Timestamp of last markets refresh + self._last_markets_refresh: int = 0 # Holds candles self._klines: Dict[Tuple[str, str], DataFrame] = {} @@ -106,7 +108,12 @@ class Exchange(object): logger.info('Using Exchange "%s"', self.name) + # Converts the interval provided in minutes in config to seconds + self.markets_refresh_interval: int = exchange_config.get( + "markets_refresh_interval", 60) * 60 + # Initial markets load self._load_markets() + # Check if all pairs are available self.validate_pairs(config['exchange']['pair_whitelist']) self.validate_ordertypes(config.get('order_types', {})) @@ -204,9 +211,21 @@ class Exchange(object): try: self._api.load_markets(reload=reload) self._load_async_markets(reload=reload) + self._last_markets_refresh = arrow.utcnow().timestamp except ccxt.BaseError as e: logger.warning('Unable to initialize markets. Reason: %s', e) + def _reload_markets(self) -> None: + """Reload markets both sync and async, if refresh interval has passed""" + # Check whether markets have to be reloaded + if (self._last_markets_refresh > 0) and ( + self._last_markets_refresh + self.markets_refresh_interval + > arrow.utcnow().timestamp): + return None + else: + logger.debug("Performing scheduled market reload..") + self._load_markets(reload=True) + def validate_pairs(self, pairs: List[str]) -> None: """ Checks if all given pairs are tradable on the current exchange. diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 939904c73..0422b0f88 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -155,6 +155,9 @@ class FreqtradeBot(object): """ state_changed = False try: + # Check whether markets have to be reloaded + self.exchange._reload_markets() + # Refresh whitelist self.pairlists.refresh_pairlist() self.active_pair_whitelist = self.pairlists.whitelist