Merge pull request #2675 from freqtrade/align_test_history
Align usage of history import in test
This commit is contained in:
commit
69f8738d00
@ -12,16 +12,17 @@ from pandas import DataFrame
|
|||||||
|
|
||||||
from freqtrade import OperationalException
|
from freqtrade import OperationalException
|
||||||
from freqtrade.configuration import TimeRange
|
from freqtrade.configuration import TimeRange
|
||||||
from freqtrade.data import history
|
from freqtrade.data.history import (_download_pair_history,
|
||||||
from freqtrade.data.history import (_load_cached_data_for_updating,
|
|
||||||
_download_pair_history,
|
|
||||||
_download_trades_history,
|
_download_trades_history,
|
||||||
convert_trades_to_ohlcv,
|
_load_cached_data_for_updating,
|
||||||
|
convert_trades_to_ohlcv, get_timeframe,
|
||||||
|
load_data, load_pair_history,
|
||||||
load_tickerdata_file, pair_data_filename,
|
load_tickerdata_file, pair_data_filename,
|
||||||
pair_trades_filename,
|
pair_trades_filename,
|
||||||
refresh_backtest_ohlcv_data,
|
refresh_backtest_ohlcv_data,
|
||||||
refresh_backtest_trades_data,
|
refresh_backtest_trades_data,
|
||||||
trim_tickerlist)
|
trim_dataframe, trim_tickerlist,
|
||||||
|
validate_backtest_data)
|
||||||
from freqtrade.exchange import timeframe_to_minutes
|
from freqtrade.exchange import timeframe_to_minutes
|
||||||
from freqtrade.misc import file_dump_json
|
from freqtrade.misc import file_dump_json
|
||||||
from freqtrade.strategy.default_strategy import DefaultStrategy
|
from freqtrade.strategy.default_strategy import DefaultStrategy
|
||||||
@ -64,7 +65,7 @@ def _clean_test_file(file: Path) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_load_data_30min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
|
def test_load_data_30min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
|
||||||
ld = history.load_pair_history(pair='UNITTEST/BTC', timeframe='30m', datadir=testdatadir)
|
ld = load_pair_history(pair='UNITTEST/BTC', timeframe='30m', datadir=testdatadir)
|
||||||
assert isinstance(ld, DataFrame)
|
assert isinstance(ld, DataFrame)
|
||||||
assert not log_has(
|
assert not log_has(
|
||||||
'Download history data for pair: "UNITTEST/BTC", timeframe: 30m '
|
'Download history data for pair: "UNITTEST/BTC", timeframe: 30m '
|
||||||
@ -73,7 +74,7 @@ def test_load_data_30min_ticker(mocker, caplog, default_conf, testdatadir) -> No
|
|||||||
|
|
||||||
|
|
||||||
def test_load_data_7min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
|
def test_load_data_7min_ticker(mocker, caplog, default_conf, testdatadir) -> None:
|
||||||
ld = history.load_pair_history(pair='UNITTEST/BTC', timeframe='7m', datadir=testdatadir)
|
ld = load_pair_history(pair='UNITTEST/BTC', timeframe='7m', datadir=testdatadir)
|
||||||
assert isinstance(ld, DataFrame)
|
assert isinstance(ld, DataFrame)
|
||||||
assert ld.empty
|
assert ld.empty
|
||||||
assert log_has(
|
assert log_has(
|
||||||
@ -86,7 +87,7 @@ def test_load_data_1min_ticker(ticker_history, mocker, caplog, testdatadir) -> N
|
|||||||
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history)
|
mocker.patch('freqtrade.exchange.Exchange.get_historic_ohlcv', return_value=ticker_history)
|
||||||
file = testdatadir / 'UNITTEST_BTC-1m.json'
|
file = testdatadir / 'UNITTEST_BTC-1m.json'
|
||||||
_backup_file(file, copy_file=True)
|
_backup_file(file, copy_file=True)
|
||||||
history.load_data(datadir=testdatadir, timeframe='1m', pairs=['UNITTEST/BTC'])
|
load_data(datadir=testdatadir, timeframe='1m', pairs=['UNITTEST/BTC'])
|
||||||
assert file.is_file()
|
assert file.is_file()
|
||||||
assert not log_has(
|
assert not log_has(
|
||||||
'Download history data for pair: "UNITTEST/BTC", interval: 1m '
|
'Download history data for pair: "UNITTEST/BTC", interval: 1m '
|
||||||
@ -99,10 +100,9 @@ def test_load_data_startup_candles(mocker, caplog, default_conf, testdatadir) ->
|
|||||||
ltfmock = mocker.patch('freqtrade.data.history.load_tickerdata_file',
|
ltfmock = mocker.patch('freqtrade.data.history.load_tickerdata_file',
|
||||||
MagicMock(return_value=None))
|
MagicMock(return_value=None))
|
||||||
timerange = TimeRange('date', None, 1510639620, 0)
|
timerange = TimeRange('date', None, 1510639620, 0)
|
||||||
history.load_pair_history(pair='UNITTEST/BTC', timeframe='1m',
|
load_pair_history(pair='UNITTEST/BTC', timeframe='1m',
|
||||||
datadir=testdatadir, timerange=timerange,
|
datadir=testdatadir, timerange=timerange,
|
||||||
startup_candles=20,
|
startup_candles=20,)
|
||||||
)
|
|
||||||
|
|
||||||
assert ltfmock.call_count == 1
|
assert ltfmock.call_count == 1
|
||||||
assert ltfmock.call_args_list[0][1]['timerange'] != timerange
|
assert ltfmock.call_args_list[0][1]['timerange'] != timerange
|
||||||
@ -121,9 +121,7 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog,
|
|||||||
|
|
||||||
_backup_file(file)
|
_backup_file(file)
|
||||||
# do not download a new pair if refresh_pairs isn't set
|
# do not download a new pair if refresh_pairs isn't set
|
||||||
history.load_pair_history(datadir=testdatadir,
|
load_pair_history(datadir=testdatadir, timeframe='1m', pair='MEME/BTC')
|
||||||
timeframe='1m',
|
|
||||||
pair='MEME/BTC')
|
|
||||||
assert not file.is_file()
|
assert not file.is_file()
|
||||||
assert log_has(
|
assert log_has(
|
||||||
'No history data for pair: "MEME/BTC", timeframe: 1m. '
|
'No history data for pair: "MEME/BTC", timeframe: 1m. '
|
||||||
@ -131,22 +129,16 @@ def test_load_data_with_new_pair_1min(ticker_history_list, mocker, caplog,
|
|||||||
)
|
)
|
||||||
|
|
||||||
# download a new pair if refresh_pairs is set
|
# download a new pair if refresh_pairs is set
|
||||||
history.load_pair_history(datadir=testdatadir,
|
load_pair_history(datadir=testdatadir, timeframe='1m',
|
||||||
timeframe='1m',
|
refresh_pairs=True, exchange=exchange, pair='MEME/BTC')
|
||||||
refresh_pairs=True,
|
|
||||||
exchange=exchange,
|
|
||||||
pair='MEME/BTC')
|
|
||||||
assert file.is_file()
|
assert file.is_file()
|
||||||
assert log_has_re(
|
assert log_has_re(
|
||||||
'Download history data for pair: "MEME/BTC", timeframe: 1m '
|
'Download history data for pair: "MEME/BTC", timeframe: 1m '
|
||||||
'and store in .*', caplog
|
'and store in .*', caplog
|
||||||
)
|
)
|
||||||
with pytest.raises(OperationalException, match=r'Exchange needs to be initialized when.*'):
|
with pytest.raises(OperationalException, match=r'Exchange needs to be initialized when.*'):
|
||||||
history.load_pair_history(datadir=testdatadir,
|
load_pair_history(datadir=testdatadir, timeframe='1m',
|
||||||
timeframe='1m',
|
refresh_pairs=True, exchange=None, pair='MEME/BTC')
|
||||||
refresh_pairs=True,
|
|
||||||
exchange=None,
|
|
||||||
pair='MEME/BTC')
|
|
||||||
_clean_test_file(file)
|
_clean_test_file(file)
|
||||||
|
|
||||||
|
|
||||||
@ -351,10 +343,8 @@ def test_load_partial_missing(testdatadir, caplog) -> None:
|
|||||||
# Make sure we start fresh - test missing data at start
|
# Make sure we start fresh - test missing data at start
|
||||||
start = arrow.get('2018-01-01T00:00:00')
|
start = arrow.get('2018-01-01T00:00:00')
|
||||||
end = arrow.get('2018-01-11T00:00:00')
|
end = arrow.get('2018-01-11T00:00:00')
|
||||||
tickerdata = history.load_data(testdatadir, '5m', ['UNITTEST/BTC'],
|
tickerdata = load_data(testdatadir, '5m', ['UNITTEST/BTC'], startup_candles=20,
|
||||||
startup_candles=20,
|
timerange=TimeRange('date', 'date', start.timestamp, end.timestamp))
|
||||||
timerange=TimeRange('date', 'date',
|
|
||||||
start.timestamp, end.timestamp))
|
|
||||||
assert log_has(
|
assert log_has(
|
||||||
'Using indicator startup period: 20 ...', caplog
|
'Using indicator startup period: 20 ...', caplog
|
||||||
)
|
)
|
||||||
@ -369,10 +359,8 @@ def test_load_partial_missing(testdatadir, caplog) -> None:
|
|||||||
caplog.clear()
|
caplog.clear()
|
||||||
start = arrow.get('2018-01-10T00:00:00')
|
start = arrow.get('2018-01-10T00:00:00')
|
||||||
end = arrow.get('2018-02-20T00:00:00')
|
end = arrow.get('2018-02-20T00:00:00')
|
||||||
tickerdata = history.load_data(datadir=testdatadir, timeframe='5m',
|
tickerdata = load_data(datadir=testdatadir, timeframe='5m', pairs=['UNITTEST/BTC'],
|
||||||
pairs=['UNITTEST/BTC'],
|
timerange=TimeRange('date', 'date', start.timestamp, end.timestamp))
|
||||||
timerange=TimeRange('date', 'date',
|
|
||||||
start.timestamp, end.timestamp))
|
|
||||||
# timedifference in 5 minutes
|
# timedifference in 5 minutes
|
||||||
td = ((end - start).total_seconds() // 60 // 5) + 1
|
td = ((end - start).total_seconds() // 60 // 5) + 1
|
||||||
assert td != len(tickerdata['UNITTEST/BTC'])
|
assert td != len(tickerdata['UNITTEST/BTC'])
|
||||||
@ -385,7 +373,7 @@ def test_load_partial_missing(testdatadir, caplog) -> None:
|
|||||||
|
|
||||||
def test_init(default_conf, mocker) -> None:
|
def test_init(default_conf, mocker) -> None:
|
||||||
exchange = get_patched_exchange(mocker, default_conf)
|
exchange = get_patched_exchange(mocker, default_conf)
|
||||||
assert {} == history.load_data(
|
assert {} == load_data(
|
||||||
datadir='',
|
datadir='',
|
||||||
exchange=exchange,
|
exchange=exchange,
|
||||||
pairs=[],
|
pairs=[],
|
||||||
@ -447,7 +435,7 @@ def test_trim_tickerlist(testdatadir) -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_trim_dataframe(testdatadir) -> None:
|
def test_trim_dataframe(testdatadir) -> None:
|
||||||
data = history.load_data(
|
data = load_data(
|
||||||
datadir=testdatadir,
|
datadir=testdatadir,
|
||||||
timeframe='1m',
|
timeframe='1m',
|
||||||
pairs=['UNITTEST/BTC']
|
pairs=['UNITTEST/BTC']
|
||||||
@ -458,7 +446,7 @@ def test_trim_dataframe(testdatadir) -> None:
|
|||||||
|
|
||||||
# Remove first 30 minutes (1800 s)
|
# Remove first 30 minutes (1800 s)
|
||||||
tr = TimeRange('date', None, min_date + 1800, 0)
|
tr = TimeRange('date', None, min_date + 1800, 0)
|
||||||
data_modify = history.trim_dataframe(data_modify, tr)
|
data_modify = trim_dataframe(data_modify, tr)
|
||||||
assert not data_modify.equals(data)
|
assert not data_modify.equals(data)
|
||||||
assert len(data_modify) < len(data)
|
assert len(data_modify) < len(data)
|
||||||
assert len(data_modify) == len(data) - 30
|
assert len(data_modify) == len(data) - 30
|
||||||
@ -468,7 +456,7 @@ def test_trim_dataframe(testdatadir) -> None:
|
|||||||
data_modify = data.copy()
|
data_modify = data.copy()
|
||||||
# Remove last 30 minutes (1800 s)
|
# Remove last 30 minutes (1800 s)
|
||||||
tr = TimeRange(None, 'date', 0, max_date - 1800)
|
tr = TimeRange(None, 'date', 0, max_date - 1800)
|
||||||
data_modify = history.trim_dataframe(data_modify, tr)
|
data_modify = trim_dataframe(data_modify, tr)
|
||||||
assert not data_modify.equals(data)
|
assert not data_modify.equals(data)
|
||||||
assert len(data_modify) < len(data)
|
assert len(data_modify) < len(data)
|
||||||
assert len(data_modify) == len(data) - 30
|
assert len(data_modify) == len(data) - 30
|
||||||
@ -478,7 +466,7 @@ def test_trim_dataframe(testdatadir) -> None:
|
|||||||
data_modify = data.copy()
|
data_modify = data.copy()
|
||||||
# Remove first 25 and last 30 minutes (1800 s)
|
# Remove first 25 and last 30 minutes (1800 s)
|
||||||
tr = TimeRange('date', 'date', min_date + 1500, max_date - 1800)
|
tr = TimeRange('date', 'date', min_date + 1500, max_date - 1800)
|
||||||
data_modify = history.trim_dataframe(data_modify, tr)
|
data_modify = trim_dataframe(data_modify, tr)
|
||||||
assert not data_modify.equals(data)
|
assert not data_modify.equals(data)
|
||||||
assert len(data_modify) < len(data)
|
assert len(data_modify) < len(data)
|
||||||
assert len(data_modify) == len(data) - 55
|
assert len(data_modify) == len(data) - 55
|
||||||
@ -515,13 +503,13 @@ def test_get_timeframe(default_conf, mocker, testdatadir) -> None:
|
|||||||
strategy = DefaultStrategy(default_conf)
|
strategy = DefaultStrategy(default_conf)
|
||||||
|
|
||||||
data = strategy.tickerdata_to_dataframe(
|
data = strategy.tickerdata_to_dataframe(
|
||||||
history.load_data(
|
load_data(
|
||||||
datadir=testdatadir,
|
datadir=testdatadir,
|
||||||
timeframe='1m',
|
timeframe='1m',
|
||||||
pairs=['UNITTEST/BTC']
|
pairs=['UNITTEST/BTC']
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
min_date, max_date = history.get_timeframe(data)
|
min_date, max_date = get_timeframe(data)
|
||||||
assert min_date.isoformat() == '2017-11-04T23:02:00+00:00'
|
assert min_date.isoformat() == '2017-11-04T23:02:00+00:00'
|
||||||
assert max_date.isoformat() == '2017-11-14T22:58:00+00:00'
|
assert max_date.isoformat() == '2017-11-14T22:58:00+00:00'
|
||||||
|
|
||||||
@ -531,17 +519,17 @@ def test_validate_backtest_data_warn(default_conf, mocker, caplog, testdatadir)
|
|||||||
strategy = DefaultStrategy(default_conf)
|
strategy = DefaultStrategy(default_conf)
|
||||||
|
|
||||||
data = strategy.tickerdata_to_dataframe(
|
data = strategy.tickerdata_to_dataframe(
|
||||||
history.load_data(
|
load_data(
|
||||||
datadir=testdatadir,
|
datadir=testdatadir,
|
||||||
timeframe='1m',
|
timeframe='1m',
|
||||||
pairs=['UNITTEST/BTC'],
|
pairs=['UNITTEST/BTC'],
|
||||||
fill_up_missing=False
|
fill_up_missing=False
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
min_date, max_date = history.get_timeframe(data)
|
min_date, max_date = get_timeframe(data)
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
assert history.validate_backtest_data(data['UNITTEST/BTC'], 'UNITTEST/BTC',
|
assert validate_backtest_data(data['UNITTEST/BTC'], 'UNITTEST/BTC',
|
||||||
min_date, max_date, timeframe_to_minutes('1m'))
|
min_date, max_date, timeframe_to_minutes('1m'))
|
||||||
assert len(caplog.record_tuples) == 1
|
assert len(caplog.record_tuples) == 1
|
||||||
assert log_has(
|
assert log_has(
|
||||||
"UNITTEST/BTC has missing frames: expected 14396, got 13680, that's 716 missing values",
|
"UNITTEST/BTC has missing frames: expected 14396, got 13680, that's 716 missing values",
|
||||||
@ -554,7 +542,7 @@ def test_validate_backtest_data(default_conf, mocker, caplog, testdatadir) -> No
|
|||||||
|
|
||||||
timerange = TimeRange('index', 'index', 200, 250)
|
timerange = TimeRange('index', 'index', 200, 250)
|
||||||
data = strategy.tickerdata_to_dataframe(
|
data = strategy.tickerdata_to_dataframe(
|
||||||
history.load_data(
|
load_data(
|
||||||
datadir=testdatadir,
|
datadir=testdatadir,
|
||||||
timeframe='5m',
|
timeframe='5m',
|
||||||
pairs=['UNITTEST/BTC'],
|
pairs=['UNITTEST/BTC'],
|
||||||
@ -562,10 +550,10 @@ def test_validate_backtest_data(default_conf, mocker, caplog, testdatadir) -> No
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
min_date, max_date = history.get_timeframe(data)
|
min_date, max_date = get_timeframe(data)
|
||||||
caplog.clear()
|
caplog.clear()
|
||||||
assert not history.validate_backtest_data(data['UNITTEST/BTC'], 'UNITTEST/BTC',
|
assert not validate_backtest_data(data['UNITTEST/BTC'], 'UNITTEST/BTC',
|
||||||
min_date, max_date, timeframe_to_minutes('5m'))
|
min_date, max_date, timeframe_to_minutes('5m'))
|
||||||
assert len(caplog.record_tuples) == 0
|
assert len(caplog.record_tuples) == 0
|
||||||
|
|
||||||
|
|
||||||
@ -668,12 +656,8 @@ def test_convert_trades_to_ohlcv(mocker, default_conf, testdatadir, caplog):
|
|||||||
file1 = testdatadir / 'XRP_ETH-1m.json'
|
file1 = testdatadir / 'XRP_ETH-1m.json'
|
||||||
file5 = testdatadir / 'XRP_ETH-5m.json'
|
file5 = testdatadir / 'XRP_ETH-5m.json'
|
||||||
# Compare downloaded dataset with converted dataset
|
# Compare downloaded dataset with converted dataset
|
||||||
dfbak_1m = history.load_pair_history(datadir=testdatadir,
|
dfbak_1m = load_pair_history(datadir=testdatadir, timeframe="1m", pair=pair)
|
||||||
timeframe="1m",
|
dfbak_5m = load_pair_history(datadir=testdatadir, timeframe="5m", pair=pair)
|
||||||
pair=pair)
|
|
||||||
dfbak_5m = history.load_pair_history(datadir=testdatadir,
|
|
||||||
timeframe="5m",
|
|
||||||
pair=pair)
|
|
||||||
|
|
||||||
_backup_file(file1, copy_file=True)
|
_backup_file(file1, copy_file=True)
|
||||||
_backup_file(file5)
|
_backup_file(file5)
|
||||||
@ -685,12 +669,8 @@ def test_convert_trades_to_ohlcv(mocker, default_conf, testdatadir, caplog):
|
|||||||
|
|
||||||
assert log_has("Deleting existing data for pair XRP/ETH, interval 1m.", caplog)
|
assert log_has("Deleting existing data for pair XRP/ETH, interval 1m.", caplog)
|
||||||
# Load new data
|
# Load new data
|
||||||
df_1m = history.load_pair_history(datadir=testdatadir,
|
df_1m = load_pair_history(datadir=testdatadir, timeframe="1m", pair=pair)
|
||||||
timeframe="1m",
|
df_5m = load_pair_history(datadir=testdatadir, timeframe="5m", pair=pair)
|
||||||
pair=pair)
|
|
||||||
df_5m = history.load_pair_history(datadir=testdatadir,
|
|
||||||
timeframe="5m",
|
|
||||||
pair=pair)
|
|
||||||
|
|
||||||
assert df_1m.equals(dfbak_1m)
|
assert df_1m.equals(dfbak_1m)
|
||||||
assert df_5m.equals(dfbak_5m)
|
assert df_5m.equals(dfbak_5m)
|
||||||
|
Loading…
Reference in New Issue
Block a user