From 421ccb23d34a160f3858926e29183ae1cd44c816 Mon Sep 17 00:00:00 2001 From: kryofly Date: Fri, 5 Jan 2018 10:20:48 +0100 Subject: [PATCH] split load tickerdata function --- freqtrade/optimize/__init__.py | 39 ++++++++++++++++------- freqtrade/tests/optimize/test_optimize.py | 12 ++++++- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/freqtrade/optimize/__init__.py b/freqtrade/optimize/__init__.py index 1be7ce536..f36c28fbc 100644 --- a/freqtrade/optimize/__init__.py +++ b/freqtrade/optimize/__init__.py @@ -12,6 +12,27 @@ from freqtrade.analyze import populate_indicators, parse_ticker_dataframe logger = logging.getLogger(__name__) +def load_tickerdata_file(pair, ticker_interval): + """ + Load a pair from file, + :return dict OR empty if unsuccesful + """ + path = testdata_path() + file = '{abspath}/{pair}-{ticker_interval}.json'.format( + abspath=path, + pair=pair, + ticker_interval=ticker_interval, + ) + # The file does not exist we download it + if not os.path.isfile(file): + return None + + # Read the file, load the json + with open(file) as tickerdata: + pairdata = json.load(tickerdata) + return pairdata + + def load_data(ticker_interval: int = 5, pairs: Optional[List[str]] = None, refresh_pairs: Optional[bool] = False) -> Dict[str, List]: """ @@ -20,7 +41,6 @@ def load_data(ticker_interval: int = 5, pairs: Optional[List[str]] = None, :param pairs: list of pairs :return: dict """ - path = testdata_path() result = {} _pairs = pairs or hyperopt_optimize_conf()['exchange']['pair_whitelist'] @@ -31,18 +51,13 @@ def load_data(ticker_interval: int = 5, pairs: Optional[List[str]] = None, download_pairs(_pairs) for pair in _pairs: - file = '{abspath}/{pair}-{ticker_interval}.json'.format( - abspath=path, - pair=pair, - ticker_interval=ticker_interval, - ) - # The file does not exist we download it - if not os.path.isfile(file): + pairdata = load_tickerdata_file(pair, ticker_interval) + if not pairdata: + # download the tickerdata from exchange download_backtesting_testdata(pair=pair, interval=ticker_interval) - - # Read the file, load the json - with open(file) as tickerdata: - result[pair] = json.load(tickerdata) + # and retry reading the pair + pairdata = load_tickerdata_file(pair, ticker_interval) + result[pair] = pairdata return result diff --git a/freqtrade/tests/optimize/test_optimize.py b/freqtrade/tests/optimize/test_optimize.py index aad20567e..62b798c2c 100644 --- a/freqtrade/tests/optimize/test_optimize.py +++ b/freqtrade/tests/optimize/test_optimize.py @@ -5,7 +5,11 @@ import logging from shutil import copyfile from freqtrade import exchange, optimize from freqtrade.exchange import Bittrex -from freqtrade.optimize.__init__ import testdata_path, download_pairs, download_backtesting_testdata +from freqtrade.optimize.__init__ import testdata_path, download_pairs,\ + download_backtesting_testdata, load_tickerdata_file + +# Change this if modifying BTC_UNITEST testdatafile +_btc_unittest_length = 13681 def _backup_file(file: str, copy_file: bool = False) -> None: @@ -164,3 +168,9 @@ def test_download_backtesting_testdata(default_conf, ticker_history, mocker): download_backtesting_testdata(pair="BTC-STORJ", interval=5) assert os.path.isfile(file2) is True _clean_test_file(file2) + + +def test_load_tickerdata_file(): + assert not load_tickerdata_file('BTC_UNITEST', 7) + tickerdata = load_tickerdata_file('BTC_UNITEST', 1) + assert _btc_unittest_length == len(tickerdata)