diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index 40ac9b58a..06c6d9382 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -489,9 +489,9 @@ class Exchange(object): # Combine tickers data: List = [] - for tick in tickers: - if tick[0] == pair: - data.extend(tick[1]) + for p, ticker in tickers: + if p == pair: + data.extend(ticker) # Sort data again after extending the result - above calls return in "async order" order data = sorted(data, key=lambda x: x[0]) logger.info("downloaded %s with length %s.", pair, len(data)) @@ -502,9 +502,16 @@ class Exchange(object): Refresh tickers asyncronously and return the result. """ logger.debug("Refreshing klines for %d pairs", len(pair_list)) - asyncio.get_event_loop().run_until_complete( + ticklist = asyncio.get_event_loop().run_until_complete( self.async_get_candles_history(pair_list, ticker_interval)) + for pair, ticks in ticklist: + # keeping last candle time as last refreshed time of the pair + if ticks: + self._pairs_last_refresh_time[pair] = ticks[-1][0] // 1000 + # keeping candles in cache + self.klines[pair] = ticks + async def async_get_candles_history(self, pairs: List[str], tick_interval: str) -> List[Tuple[str, List]]: """Download ohlcv history for pair-list asyncronously """ @@ -528,7 +535,7 @@ class Exchange(object): # so we fetch it from local cache if (not since_ms and self._pairs_last_refresh_time.get(pair, 0) + interval_in_sec >= - arrow.utcnow().timestamp): + arrow.utcnow().timestamp and pair in self.klines): data = self.klines[pair] logger.debug("Using cached klines data for %s ...", pair) else: @@ -542,13 +549,6 @@ class Exchange(object): if data and data[0][0] > data[-1][0]: data = sorted(data, key=lambda x: x[0]) - # keeping last candle time as last refreshed time of the pair - if data: - self._pairs_last_refresh_time[pair] = data[-1][0] // 1000 - - # keeping candles in cache - self.klines[pair] = data - logger.debug("done fetching %s ...", pair) return pair, data diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index d1f391266..b711dd3ab 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -737,7 +737,7 @@ def test_get_history(default_conf, mocker, caplog): def test_refresh_tickers(mocker, default_conf, caplog) -> None: tick = [ [ - 1511686200000, # unix timestamp ms + arrow.utcnow().timestamp * 1000, # unix timestamp ms 1, # open 2, # high 3, # low @@ -757,9 +757,16 @@ def test_refresh_tickers(mocker, default_conf, caplog) -> None: assert log_has(f'Refreshing klines for {len(pairs)} pairs', caplog.record_tuples) assert exchange.klines + assert exchange._api_async.fetch_ohlcv.call_count == 2 for pair in pairs: assert exchange.klines[pair] + # test caching + exchange.refresh_tickers(['IOTA/ETH', 'XRP/ETH'], '5m') + + assert exchange._api_async.fetch_ohlcv.call_count == 2 + assert log_has(f"Using cached klines data for {pairs[0]} ...", caplog.record_tuples) + @pytest.mark.asyncio async def test__async_get_candle_history(default_conf, mocker, caplog): @@ -788,10 +795,6 @@ async def test__async_get_candle_history(default_conf, mocker, caplog): assert res[1] == tick assert exchange._api_async.fetch_ohlcv.call_count == 1 assert not log_has(f"Using cached klines data for {pair} ...", caplog.record_tuples) - # test caching - res = await exchange._async_get_candle_history(pair, "5m") - assert exchange._api_async.fetch_ohlcv.call_count == 1 - assert log_has(f"Using cached klines data for {pair} ...", caplog.record_tuples) # exchange = Exchange(default_conf) await async_ccxt_exception(mocker, default_conf, MagicMock(),