From 043cefd60af23841a08e73c4d6cee5c875c104b9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 16 Dec 2018 10:17:11 +0100 Subject: [PATCH] allow reloading single pair --- freqtrade/data/history.py | 36 ++++++++++++++++++---------- freqtrade/tests/data/test_history.py | 28 ++++++++++++++-------- scripts/download_backtest_data.py | 2 +- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/freqtrade/data/history.py b/freqtrade/data/history.py index a5a3f52da..e8c5e7816 100644 --- a/freqtrade/data/history.py +++ b/freqtrade/data/history.py @@ -99,12 +99,28 @@ def load_tickerdata_file( def load_pair_history(pair: str, ticker_interval: str, datadir: Optional[Path], - timerange: TimeRange = TimeRange(None, None, 0, 0)) -> DataFrame: + timerange: TimeRange = TimeRange(None, None, 0, 0), + refresh_pairs: bool = False, + exchange: Optional[Exchange] = None, + ) -> DataFrame: """ Loads cached ticker history for the given pair. """ pairdata = load_tickerdata_file(datadir, pair, ticker_interval, timerange=timerange) + # If the user force the refresh of pairs + if refresh_pairs: + if not exchange: + raise OperationalException("Exchange needs to be initialized when " + "calling load_data with refresh_pairs=True") + + logger.info('Download data for all pairs and store them in %s', datadir) + download_backtesting_testdata(datadir=datadir, + exchange=exchange, + pair=pair, + tick_interval=ticker_interval, + timerange=timerange) + if pairdata: if timerange.starttype == 'date' and pairdata[0][0] > timerange.startts * 1000: logger.warning('Missing data at start for pair %s, data starts at %s', @@ -124,26 +140,20 @@ def load_pair_history(pair: str, def load_data(datadir: Optional[Path], ticker_interval: str, pairs: List[str], - refresh_pairs: Optional[bool] = False, + refresh_pairs: bool = False, exchange: Optional[Exchange] = None, timerange: TimeRange = TimeRange(None, None, 0, 0)) -> Dict[str, DataFrame]: """ - Loads ticker history data for the given parameters + Loads ticker history data for a list of pairs the given parameters :return: dict """ result = {} - # If the user force the refresh of pairs - if refresh_pairs: - logger.info('Download data for all pairs and store them in %s', datadir) - if not exchange: - raise OperationalException("Exchange needs to be initialized when " - "calling load_data with refresh_pairs=True") - download_pairs(datadir, exchange, pairs, ticker_interval, timerange=timerange) - for pair in pairs: hist = load_pair_history(pair=pair, ticker_interval=ticker_interval, - datadir=datadir, timerange=timerange) + datadir=datadir, timerange=timerange, + refresh_pairs=refresh_pairs, + exchange=exchange) if hist is not None: result[pair] = hist return result @@ -160,7 +170,7 @@ def download_pairs(datadir, exchange: Exchange, pairs: List[str], """For each pairs passed in parameters, download the ticker intervals""" for pair in pairs: try: - download_backtesting_testdata(datadir, + download_backtesting_testdata(datadir=datadir, exchange=exchange, pair=pair, tick_interval=ticker_interval, diff --git a/freqtrade/tests/data/test_history.py b/freqtrade/tests/data/test_history.py index 8e7185b33..908f17df7 100644 --- a/freqtrade/tests/data/test_history.py +++ b/freqtrade/tests/data/test_history.py @@ -8,7 +8,9 @@ from shutil import copyfile import arrow from pandas import DataFrame +import pytest +from freqtrade import OperationalException from freqtrade.arguments import TimeRange from freqtrade.data import history from freqtrade.data.history import (download_backtesting_testdata, @@ -82,7 +84,7 @@ def test_load_data_1min_ticker(ticker_history, mocker, caplog) -> None: def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, default_conf) -> None: """ - Test load_data() with 1 min ticker + Test load_pair_history() with 1 min ticker """ mocker.patch('freqtrade.exchange.Exchange.get_history', return_value=ticker_history_list) exchange = get_patched_exchange(mocker, default_conf) @@ -90,23 +92,29 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog, defau _backup_file(file) # do not download a new pair if refresh_pairs isn't set - history.load_data(datadir=None, - ticker_interval='1m', - refresh_pairs=False, - pairs=['MEME/BTC']) + history.load_pair_history(datadir=None, + ticker_interval='1m', + refresh_pairs=False, + pair='MEME/BTC') assert os.path.isfile(file) is False assert log_has('No data for pair: "MEME/BTC", Interval: 1m. ' 'Use --refresh-pairs-cached to download the data', caplog.record_tuples) # download a new pair if refresh_pairs is set - history.load_data(datadir=None, - ticker_interval='1m', - refresh_pairs=True, - exchange=exchange, - pairs=['MEME/BTC']) + history.load_pair_history(datadir=None, + ticker_interval='1m', + refresh_pairs=True, + exchange=exchange, + pair='MEME/BTC') assert os.path.isfile(file) is True assert log_has('Download the pair: "MEME/BTC", Interval: 1m', caplog.record_tuples) + with pytest.raises(OperationalException, match=r'Exchange needs to be initialized when.*'): + history.load_pair_history(datadir=None, + ticker_interval='1m', + refresh_pairs=True, + exchange=None, + pair='MEME/BTC') _clean_test_file(file) diff --git a/scripts/download_backtest_data.py b/scripts/download_backtest_data.py index 6235fe667..c32ac1018 100755 --- a/scripts/download_backtest_data.py +++ b/scripts/download_backtest_data.py @@ -82,7 +82,7 @@ for pair in PAIRS: dl_file.unlink() print(f'downloading pair {pair}, interval {tick_interval}') - download_backtesting_testdata(dl_path, exchange=exchange, + download_backtesting_testdata(datadir=dl_path, exchange=exchange, pair=pair, tick_interval=tick_interval, timerange=timerange)