From 6e09d552ac19377acbabe17846f2d60ce3a59b2c Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 5 Nov 2022 13:14:35 +0100 Subject: [PATCH] Properly handle and test ohlcv min_max with empty files --- freqtrade/data/history/idatahandler.py | 5 +++++ tests/data/test_datahandler.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/freqtrade/data/history/idatahandler.py b/freqtrade/data/history/idatahandler.py index cbc3f1a34..b82d2055b 100644 --- a/freqtrade/data/history/idatahandler.py +++ b/freqtrade/data/history/idatahandler.py @@ -102,6 +102,11 @@ class IDataHandler(ABC): :return: (min, max) """ data = self._ohlcv_load(pair, timeframe, None, candle_type) + if data.empty: + return ( + datetime.fromtimestamp(0, tz=timezone.utc), + datetime.fromtimestamp(0, tz=timezone.utc) + ) return data.iloc[0]['date'].to_pydatetime(), data.iloc[-1]['date'].to_pydatetime() @abstractmethod diff --git a/tests/data/test_datahandler.py b/tests/data/test_datahandler.py index 67eeda7d0..c067d0339 100644 --- a/tests/data/test_datahandler.py +++ b/tests/data/test_datahandler.py @@ -1,6 +1,7 @@ # pragma pylint: disable=missing-docstring, protected-access, C0103 import re +from datetime import datetime, timezone from pathlib import Path from unittest.mock import MagicMock @@ -154,6 +155,23 @@ def test_jsondatahandler_ohlcv_load(testdatadir, caplog): assert df.columns.equals(df1.columns) +def test_datahandler_ohlcv_data_min_max(testdatadir): + dh = JsonDataHandler(testdatadir) + min_max = dh.ohlcv_data_min_max('UNITTEST/BTC', '5m', 'spot') + assert len(min_max) == 2 + + # Empty pair + min_max = dh.ohlcv_data_min_max('UNITTEST/BTC', '8m', 'spot') + assert len(min_max) == 2 + assert min_max[0] == datetime.fromtimestamp(0, tz=timezone.utc) + assert min_max[0] == min_max[1] + # Empty pair2 + min_max = dh.ohlcv_data_min_max('NOPAIR/XXX', '4m', 'spot') + assert len(min_max) == 2 + assert min_max[0] == datetime.fromtimestamp(0, tz=timezone.utc) + assert min_max[0] == min_max[1] + + def test_datahandler__check_empty_df(testdatadir, caplog): dh = JsonDataHandler(testdatadir) expected_text = r"Price jump in UNITTEST/USDT, 1h, spot between"