2022-08-13 08:48:57 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
from datetime import datetime, timezone
|
2022-08-13 07:10:03 +00:00
|
|
|
from pathlib import Path
|
2022-08-13 09:07:58 +00:00
|
|
|
from unittest.mock import PropertyMock
|
2022-08-13 07:10:03 +00:00
|
|
|
|
2022-09-26 22:01:24 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-09-24 11:21:01 +00:00
|
|
|
from freqtrade.commands.optimize_commands import setup_optimize_configuration
|
|
|
|
from freqtrade.enums import RunMode
|
2022-09-26 22:01:24 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2022-08-13 08:48:57 +00:00
|
|
|
from freqtrade.optimize.backtesting import Backtesting
|
|
|
|
from tests.conftest import (CURRENT_TEST_STRATEGY, get_args, log_has_re, patch_exchange,
|
2022-08-13 07:10:03 +00:00
|
|
|
patched_configuration_load_config_file)
|
|
|
|
|
|
|
|
|
2022-09-24 11:21:01 +00:00
|
|
|
def test_freqai_backtest_start_backtest_list(freqai_conf, mocker, testdatadir, caplog):
|
2022-08-13 07:10:03 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
|
2022-09-24 11:21:01 +00:00
|
|
|
now = datetime.now(timezone.utc)
|
2022-08-13 07:10:03 +00:00
|
|
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
|
|
|
PropertyMock(return_value=['HULUMULU/USDT', 'XRP/USDT']))
|
2022-09-24 11:21:01 +00:00
|
|
|
mocker.patch('freqtrade.optimize.backtesting.history.load_data')
|
|
|
|
mocker.patch('freqtrade.optimize.backtesting.history.get_timerange', return_value=(now, now))
|
2022-08-13 07:10:03 +00:00
|
|
|
|
|
|
|
patched_configuration_load_config_file(mocker, freqai_conf)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'backtesting',
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--datadir', str(testdatadir),
|
|
|
|
'--strategy-path', str(Path(__file__).parents[1] / 'strategy/strats'),
|
2022-10-09 12:36:12 +00:00
|
|
|
'--timeframe', '1m',
|
2022-08-13 07:10:03 +00:00
|
|
|
'--strategy-list', CURRENT_TEST_STRATEGY
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
2022-09-24 11:21:01 +00:00
|
|
|
bt_config = setup_optimize_configuration(args, RunMode.BACKTEST)
|
|
|
|
Backtesting(bt_config)
|
2023-02-21 13:22:40 +00:00
|
|
|
assert log_has_re('Using --strategy-list with FreqAI REQUIRES all strategies to have identical',
|
|
|
|
caplog)
|
2022-09-24 11:21:01 +00:00
|
|
|
Backtesting.cleanup()
|
2022-08-13 08:48:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_freqai_backtest_load_data(freqai_conf, mocker, caplog):
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
|
|
|
PropertyMock(return_value=['HULUMULU/USDT', 'XRP/USDT']))
|
|
|
|
mocker.patch('freqtrade.optimize.backtesting.history.load_data')
|
|
|
|
mocker.patch('freqtrade.optimize.backtesting.history.get_timerange', return_value=(now, now))
|
|
|
|
backtesting = Backtesting(deepcopy(freqai_conf))
|
|
|
|
backtesting.load_bt_data()
|
|
|
|
|
|
|
|
assert log_has_re('Increasing startup_candle_count for freqai to.*', caplog)
|
|
|
|
|
|
|
|
Backtesting.cleanup()
|
2022-09-26 22:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_freqai_backtest_live_models_model_not_found(freqai_conf, mocker, testdatadir, caplog):
|
|
|
|
patch_exchange(mocker)
|
|
|
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
|
|
|
PropertyMock(return_value=['HULUMULU/USDT', 'XRP/USDT']))
|
|
|
|
mocker.patch('freqtrade.optimize.backtesting.history.load_data')
|
|
|
|
mocker.patch('freqtrade.optimize.backtesting.history.get_timerange', return_value=(now, now))
|
|
|
|
freqai_conf["timerange"] = ""
|
2022-11-20 01:27:58 +00:00
|
|
|
freqai_conf.get("freqai", {}).update({"backtest_using_historic_predictions": False})
|
|
|
|
|
2022-09-26 22:01:24 +00:00
|
|
|
patched_configuration_load_config_file(mocker, freqai_conf)
|
|
|
|
|
|
|
|
args = [
|
|
|
|
'backtesting',
|
|
|
|
'--config', 'config.json',
|
|
|
|
'--datadir', str(testdatadir),
|
|
|
|
'--strategy-path', str(Path(__file__).parents[1] / 'strategy/strats'),
|
2022-10-10 18:15:43 +00:00
|
|
|
'--timeframe', '5m',
|
2022-09-26 22:01:24 +00:00
|
|
|
'--freqai-backtest-live-models'
|
|
|
|
]
|
|
|
|
args = get_args(args)
|
|
|
|
bt_config = setup_optimize_configuration(args, RunMode.BACKTEST)
|
|
|
|
|
|
|
|
with pytest.raises(OperationalException,
|
2022-11-22 16:09:09 +00:00
|
|
|
match=r".* Historic predictions data is required to run backtest .*"):
|
2022-09-26 22:01:24 +00:00
|
|
|
Backtesting(bt_config)
|
|
|
|
|
|
|
|
Backtesting.cleanup()
|