From 55ebbeec1820dfef17eb7cecca4a5b2fd412a3ae Mon Sep 17 00:00:00 2001 From: Wagner Costa Santos Date: Wed, 28 Sep 2022 08:48:32 -0300 Subject: [PATCH] backtest_live models tests refactoring --- freqtrade/configuration/config_validation.py | 27 +++++++++++-------- freqtrade/freqai/data_kitchen.py | 5 ---- tests/freqai/test_freqai_backtesting.py | 28 -------------------- tests/test_configuration.py | 12 ++++++++- 4 files changed, 27 insertions(+), 45 deletions(-) diff --git a/freqtrade/configuration/config_validation.py b/freqtrade/configuration/config_validation.py index 6e27fc748..eafba2a28 100644 --- a/freqtrade/configuration/config_validation.py +++ b/freqtrade/configuration/config_validation.py @@ -336,18 +336,23 @@ def _validate_freqai_hyperopt(conf: Dict[str, Any]) -> None: def _validate_freqai_backtest(conf: Dict[str, Any]) -> None: - freqai_enabled = conf.get('freqai', {}).get('enabled', False) - timerange = conf.get('timerange') - freqai_backtest_live_models = conf.get('freqai_backtest_live_models', False) - if freqai_backtest_live_models and freqai_enabled and timerange: - raise OperationalException( - 'Using timerange parameter is not supported with ' - '--freqai-backtest-live-models parameter.') + if conf.get('runmode', RunMode.OTHER) == RunMode.BACKTEST: + freqai_enabled = conf.get('freqai', {}).get('enabled', False) + timerange = conf.get('timerange') + freqai_backtest_live_models = conf.get('freqai_backtest_live_models', False) + if freqai_backtest_live_models and freqai_enabled and timerange: + raise OperationalException( + 'Using timerange parameter is not supported with ' + '--freqai-backtest-live-models parameter.') - if freqai_backtest_live_models and not freqai_enabled: - raise OperationalException( - 'Using --freqai-backtest-live-models parameter is only ' - 'supported with a FreqAI strategy.') + if freqai_backtest_live_models and not freqai_enabled: + raise OperationalException( + 'Using --freqai-backtest-live-models parameter is only ' + 'supported with a FreqAI strategy.') + + if freqai_enabled and not freqai_backtest_live_models and not timerange: + raise OperationalException( + 'Please pass --timerange if you intend to use FreqAI for backtesting.') def _validate_consumers(conf: Dict[str, Any]) -> None: diff --git a/freqtrade/freqai/data_kitchen.py b/freqtrade/freqai/data_kitchen.py index 7f32c942d..667250c4c 100644 --- a/freqtrade/freqai/data_kitchen.py +++ b/freqtrade/freqai/data_kitchen.py @@ -84,11 +84,6 @@ class FreqaiDataKitchen: self.backtest_live_models = config.get("freqai_backtest_live_models", False) if not self.live: - if (not self.config.get("timerange") and - not self.backtest_live_models): - raise OperationalException( - 'Please pass --timerange if you intend to use FreqAI for backtesting.') - self.full_path = freqai_util.get_full_model_path(self.config) self.full_timerange = self.create_fulltimerange( self.config["timerange"], self.freqai_config.get("train_period_days", 0) diff --git a/tests/freqai/test_freqai_backtesting.py b/tests/freqai/test_freqai_backtesting.py index d30383283..446e8295a 100644 --- a/tests/freqai/test_freqai_backtesting.py +++ b/tests/freqai/test_freqai_backtesting.py @@ -56,34 +56,6 @@ def test_freqai_backtest_load_data(freqai_conf, mocker, caplog): Backtesting.cleanup() -def test_freqai_backtest_live_models_validations(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)) - - 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'), - '--timeframe', '1h', - '--timerange', '20220108-20220115', - '--freqai-backtest-live-models' - ] - args = get_args(args) - with pytest.raises(OperationalException, - match=r".* timerange parameter is not supported .*"): - setup_optimize_configuration(args, RunMode.BACKTEST) - - Backtesting.cleanup() - - def test_freqai_backtest_live_models_model_not_found(freqai_conf, mocker, testdatadir, caplog): patch_exchange(mocker) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 94d4b4c78..9905be88c 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -1609,7 +1609,7 @@ def test_setup_hyperopt_freqai(mocker, default_conf, caplog) -> None: validate_config_consistency(config) -def test_setup_freqai_backtest_live_models(mocker, default_conf, caplog) -> None: +def test_setup_freqai_backtesting(mocker, default_conf, caplog) -> None: patched_configuration_load_config_file(mocker, default_conf) mocker.patch( 'freqtrade.configuration.configuration.create_datadir', @@ -1633,6 +1633,8 @@ def test_setup_freqai_backtest_live_models(mocker, default_conf, caplog) -> None configuration = Configuration(args) config = configuration.get_config() + config['runmode'] = RunMode.BACKTEST + with pytest.raises( OperationalException, match=r".*--freqai-backtest-live-models parameter is only.*" ): @@ -1646,3 +1648,11 @@ def test_setup_freqai_backtest_live_models(mocker, default_conf, caplog) -> None OperationalException, match=r".* timerange parameter is not supported with .*" ): validate_config_consistency(conf) + + conf['timerange'] = None + conf['freqai_backtest_live_models'] = False + + with pytest.raises( + OperationalException, match=r".* pass --timerange if you intend to use FreqAI .*" + ): + validate_config_consistency(conf)