diff --git a/freqtrade/exchange/__init__.py b/freqtrade/exchange/__init__.py index f6bc39239..1f9f147d8 100644 --- a/freqtrade/exchange/__init__.py +++ b/freqtrade/exchange/__init__.py @@ -389,6 +389,16 @@ class Exchange(object): logger.info("downloaded %s with length %s.", pair, len(data)) return data + def refresh_tickers(self, pair_list: List[str], ticker_interval: str) -> bool: + """ + Refresh tickers asyncronously and return the result. + """ + logger.debug("Refreshing klines for %d pairs", len(pair_list)) + asyncio.get_event_loop().run_until_complete( + self.async_get_candles_history(pair_list, ticker_interval)) + + return True + async def async_get_candles_history(self, pairs: List[str], tick_interval: str) -> List[Tuple[str, List]]: """Download ohlcv history for pair-list asyncronously """ diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 40c665261..3cb7ade9a 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -2,7 +2,6 @@ Freqtrade is the main module of this bot. It contains the class Freqtrade() """ -import asyncio import copy import logging import time @@ -162,20 +161,6 @@ class FreqtradeBot(object): time.sleep(duration) return result - def refresh_tickers(self, pair_list: List[str]) -> bool: - """ - Refresh tickers asyncronously and return the result. - """ - logger.debug("Refreshing klines for %d pairs", len(pair_list)) - datatups = asyncio.get_event_loop().run_until_complete( - self.exchange.async_get_candles_history(pair_list, self.strategy.ticker_interval)) - - # updating cached klines available to bot - #self.exchange.klines = {pair: data for (pair, data) in datatups} - # self.exchange.klines = datatups - - return True - def _process(self, nb_assets: Optional[int] = 0) -> bool: """ Queries the persistence layer for open trades and handles them, @@ -197,7 +182,7 @@ class FreqtradeBot(object): self.config['exchange']['pair_whitelist'] = final_list # Refreshing candles - self.refresh_tickers(final_list) + self.exchange.refresh_tickers(final_list, self.strategy.ticker_interval) # Query trades from persistence layer trades = Trade.query.filter(Trade.is_open.is_(True)).all() diff --git a/freqtrade/tests/exchange/test_exchange.py b/freqtrade/tests/exchange/test_exchange.py index ca06a4a70..a9b786cb8 100644 --- a/freqtrade/tests/exchange/test_exchange.py +++ b/freqtrade/tests/exchange/test_exchange.py @@ -599,6 +599,33 @@ def test_get_history(default_conf, mocker, caplog): assert len(ret) == 2 +def test_refresh_tickers(mocker, default_conf, caplog) -> None: + tick = [ + [ + 1511686200000, # unix timestamp ms + 1, # open + 2, # high + 3, # low + 4, # close + 5, # volume (in quote currency) + ] + ] + + caplog.set_level(logging.DEBUG) + exchange = get_patched_exchange(mocker, default_conf) + exchange._api_async.fetch_ohlcv = get_mock_coro(tick) + + pairs = ['IOTA/ETH', 'XRP/ETH'] + # empty dicts + assert not exchange.klines + exchange.refresh_tickers(['IOTA/ETH', 'XRP/ETH'], '5m') + + assert log_has(f'Refreshing klines for {len(pairs)} pairs', caplog.record_tuples) + assert exchange.klines + for pair in pairs: + assert exchange.klines[pair] + + @pytest.mark.asyncio async def test__async_get_candle_history(default_conf, mocker, caplog): tick = [ diff --git a/freqtrade/tests/test_freqtradebot.py b/freqtrade/tests/test_freqtradebot.py index 42b348892..5764c5b0f 100644 --- a/freqtrade/tests/test_freqtradebot.py +++ b/freqtrade/tests/test_freqtradebot.py @@ -44,7 +44,7 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None: """ freqtrade.strategy.get_signal = lambda e, s, t: value freqtrade.exchange.get_candle_history = lambda p, i: None - freqtrade.refresh_tickers = lambda i: True + freqtrade.exchange.refresh_tickers = lambda p, i: True def patch_RPCManager(mocker) -> MagicMock: @@ -137,36 +137,6 @@ def test_throttle_with_assets(mocker, default_conf) -> None: assert result == -1 -def test_refresh_tickers(mocker, default_conf, caplog) -> None: - tick = [ - [ - 1511686200000, # unix timestamp ms - 1, # open - 2, # high - 3, # low - 4, # close - 5, # volume (in quote currency) - ] - ] - - async def async_get_candles_history(pairlist, timeframe): - return [(pair, tick) for pair in pairlist] - - caplog.set_level(logging.DEBUG) - freqtrade = get_patched_freqtradebot(mocker, default_conf) - freqtrade.exchange.async_get_candles_history = async_get_candles_history - - pairs = ['IOTA/ETH', 'XRP/ETH'] - # empty dicts - assert not freqtrade.exchange.klines - freqtrade.refresh_tickers(['IOTA/ETH', 'XRP/ETH']) - - assert log_has(f'Refreshing klines for {len(pairs)} pairs', caplog.record_tuples) - assert freqtrade.exchange.klines - for pair in pairs: - assert freqtrade.exchange.klines[pair] - - def test_gen_pair_whitelist(mocker, default_conf, tickers) -> None: freqtrade = get_patched_freqtradebot(mocker, default_conf) mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers)