From 2374cda8d0a987d0ca60e87a44d38db6b12b35fb Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 8 Oct 2019 20:45:35 +0200 Subject: [PATCH] Cleanup and tests for refresh_backtest_trades --- freqtrade/data/history.py | 11 ++++------- tests/data/test_history.py | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/freqtrade/data/history.py b/freqtrade/data/history.py index 0049f0db5..a32a1b9aa 100644 --- a/freqtrade/data/history.py +++ b/freqtrade/data/history.py @@ -82,7 +82,7 @@ def store_tickerdata_file(datadir: Path, pair: str, misc.file_dump_json(filename, data, is_zip=is_zip) -def load_trades_file(datadir: Optional[Path], pair: str, +def load_trades_file(datadir: Path, pair: str, timerange: Optional[TimeRange] = None) -> List[Dict]: """ Load a pair from file, either .json.gz or .json @@ -97,7 +97,7 @@ def load_trades_file(datadir: Optional[Path], pair: str, return tradesdata -def store_trades_file(datadir: Optional[Path], pair: str, +def store_trades_file(datadir: Path, pair: str, data: list, is_zip: bool = True): """ Stores tickerdata to file @@ -329,17 +329,14 @@ def refresh_backtest_ohlcv_data(exchange: Exchange, pairs: List[str], timeframes return pairs_not_available -def download_trades_history(datadir: Optional[Path], - exchange: Optional[Exchange], +def download_trades_history(datadir: Path, + exchange: Exchange, pair: str, timerange: Optional[TimeRange] = None) -> bool: """ Download trade history from the exchange. Appends to previously downloaded trades data. """ - if not exchange: - raise OperationalException( - "Exchange needs to be initialized to download data") try: since = timerange.startts * 1000 if timerange and timerange.starttype == 'date' else None diff --git a/tests/data/test_history.py b/tests/data/test_history.py index 032c1b306..52e37e735 100644 --- a/tests/data/test_history.py +++ b/tests/data/test_history.py @@ -18,7 +18,7 @@ from freqtrade.data.history import (download_pair_history, refresh_backtest_ohlcv_data, load_tickerdata_file, pair_data_filename, pair_trades_filename, - trim_tickerlist) + trim_tickerlist, refresh_backtest_trades_data) from freqtrade.exchange import timeframe_to_minutes from freqtrade.misc import file_dump_json from freqtrade.strategy.default_strategy import DefaultStrategy @@ -583,3 +583,27 @@ def test_download_data_no_markets(mocker, default_conf, caplog, testdatadir): assert "ETH/BTC" in unav_pairs assert "XRP/BTC" in unav_pairs assert log_has("Skipping pair ETH/BTC...", caplog) + + +def test_refresh_backtest_trades_data(mocker, default_conf, markets, caplog, testdatadir): + dl_mock = mocker.patch('freqtrade.data.history.download_trades_history', MagicMock()) + mocker.patch( + 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) + ) + mocker.patch.object(Path, "exists", MagicMock(return_value=True)) + mocker.patch.object(Path, "unlink", MagicMock()) + + ex = get_patched_exchange(mocker, default_conf) + timerange = TimeRange.parse_timerange("20190101-20190102") + unavailable_pairs = refresh_backtest_trades_data(exchange=ex, + pairs=["ETH/BTC", "XRP/BTC", "XRP/ETH"], + datadir=testdatadir, + timerange=timerange, erase=True + ) + + assert dl_mock.call_count == 2 + assert dl_mock.call_args[1]['timerange'].starttype == 'date' + + assert log_has("Downloading trades for pair ETH/BTC.", caplog) + assert unavailable_pairs == ["XRP/ETH"] + assert log_has("Skipping pair XRP/ETH...", caplog)