Refactor refresh_pairs to exchange and fix tests

This commit is contained in:
Matthias 2018-08-16 12:15:09 +02:00
parent e6e2799f03
commit ff8ed564f1
4 changed files with 39 additions and 47 deletions

View File

@ -389,6 +389,16 @@ class Exchange(object):
logger.info("downloaded %s with length %s.", pair, len(data)) logger.info("downloaded %s with length %s.", pair, len(data))
return 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], async def async_get_candles_history(self, pairs: List[str],
tick_interval: str) -> List[Tuple[str, List]]: tick_interval: str) -> List[Tuple[str, List]]:
"""Download ohlcv history for pair-list asyncronously """ """Download ohlcv history for pair-list asyncronously """

View File

@ -2,7 +2,6 @@
Freqtrade is the main module of this bot. It contains the class Freqtrade() Freqtrade is the main module of this bot. It contains the class Freqtrade()
""" """
import asyncio
import copy import copy
import logging import logging
import time import time
@ -162,20 +161,6 @@ class FreqtradeBot(object):
time.sleep(duration) time.sleep(duration)
return result 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: def _process(self, nb_assets: Optional[int] = 0) -> bool:
""" """
Queries the persistence layer for open trades and handles them, Queries the persistence layer for open trades and handles them,
@ -197,7 +182,7 @@ class FreqtradeBot(object):
self.config['exchange']['pair_whitelist'] = final_list self.config['exchange']['pair_whitelist'] = final_list
# Refreshing candles # Refreshing candles
self.refresh_tickers(final_list) self.exchange.refresh_tickers(final_list, self.strategy.ticker_interval)
# Query trades from persistence layer # Query trades from persistence layer
trades = Trade.query.filter(Trade.is_open.is_(True)).all() trades = Trade.query.filter(Trade.is_open.is_(True)).all()

View File

@ -599,6 +599,33 @@ def test_get_history(default_conf, mocker, caplog):
assert len(ret) == 2 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 @pytest.mark.asyncio
async def test__async_get_candle_history(default_conf, mocker, caplog): async def test__async_get_candle_history(default_conf, mocker, caplog):
tick = [ tick = [

View File

@ -44,7 +44,7 @@ def patch_get_signal(freqtrade: FreqtradeBot, value=(True, False)) -> None:
""" """
freqtrade.strategy.get_signal = lambda e, s, t: value freqtrade.strategy.get_signal = lambda e, s, t: value
freqtrade.exchange.get_candle_history = lambda p, i: None 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: def patch_RPCManager(mocker) -> MagicMock:
@ -137,36 +137,6 @@ def test_throttle_with_assets(mocker, default_conf) -> None:
assert result == -1 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: def test_gen_pair_whitelist(mocker, default_conf, tickers) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)
mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers) mocker.patch('freqtrade.exchange.Exchange.get_tickers', tickers)