From b8feefc5412e82cd11ade99e994b645377149d36 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 29 Sep 2024 09:43:50 +0200 Subject: [PATCH] tests: update protection tests --- docs/hyperopt.md | 1 - tests/freqtradebot/test_freqtradebot.py | 2 +- tests/optimize/test_backtesting.py | 4 ++-- tests/plugins/test_protections.py | 16 ++++++++-------- tests/rpc/test_rpc_manager.py | 2 +- tests/strategy/strats/strategy_test_v3.py | 16 +++++++--------- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/docs/hyperopt.md b/docs/hyperopt.md index f88928344..43085029c 100644 --- a/docs/hyperopt.md +++ b/docs/hyperopt.md @@ -445,7 +445,6 @@ While this strategy is most likely too simple to provide consistent profit, it s Whether you are using `.range` functionality or the alternatives above, you should try to use space ranges as small as possible since this will improve CPU/RAM usage. - ## Optimizing protections Freqtrade can also optimize protections. How you optimize protections is up to you, and the following should be considered as example only. diff --git a/tests/freqtradebot/test_freqtradebot.py b/tests/freqtradebot/test_freqtradebot.py index 8587e7f9d..08fb3db00 100644 --- a/tests/freqtradebot/test_freqtradebot.py +++ b/tests/freqtradebot/test_freqtradebot.py @@ -553,7 +553,7 @@ def test_enter_positions_global_pairlock( @pytest.mark.parametrize("is_short", [False, True]) def test_handle_protections(mocker, default_conf_usdt, fee, is_short): - default_conf_usdt["protections"] = [ + default_conf_usdt["_strategy_protections"] = [ {"method": "CooldownPeriod", "stop_duration": 60}, { "method": "StoplossGuard", diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py index b25230791..f064247c6 100644 --- a/tests/optimize/test_backtesting.py +++ b/tests/optimize/test_backtesting.py @@ -1299,7 +1299,7 @@ def test_backtest_pricecontours_protections(default_conf, fee, mocker, testdatad # While this test IS a copy of test_backtest_pricecontours, it's needed to ensure # results do not carry-over to the next run, which is not given by using parametrize. patch_exchange(mocker) - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ { "method": "CooldownPeriod", "stop_duration": 3, @@ -1358,7 +1358,7 @@ def test_backtest_pricecontours( default_conf, mocker, testdatadir, protections, contour, expected ) -> None: if protections: - default_conf["protections"] = protections + default_conf["_strategy_protections"] = protections default_conf["enable_protections"] = True patch_exchange(mocker) diff --git a/tests/plugins/test_protections.py b/tests/plugins/test_protections.py index 3fb27ce3d..45a523c92 100644 --- a/tests/plugins/test_protections.py +++ b/tests/plugins/test_protections.py @@ -88,7 +88,7 @@ def generate_mock_trade( def test_protectionmanager(mocker, default_conf): - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ {"method": protection} for protection in constants.AVAILABLE_PROTECTIONS ] freqtrade = get_patched_freqtradebot(mocker, default_conf) @@ -196,7 +196,7 @@ def test_protections_init(default_conf, timeframe, expected_lookback, expected_s @pytest.mark.usefixtures("init_persistence") def test_stoploss_guard(mocker, default_conf, fee, caplog, is_short): # Active for both sides (long and short) - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ {"method": "StoplossGuard", "lookback_period": 60, "stop_duration": 40, "trade_limit": 3} ] freqtrade = get_patched_freqtradebot(mocker, default_conf) @@ -268,7 +268,7 @@ def test_stoploss_guard(mocker, default_conf, fee, caplog, is_short): @pytest.mark.parametrize("only_per_side", [False, True]) @pytest.mark.usefixtures("init_persistence") def test_stoploss_guard_perpair(mocker, default_conf, fee, caplog, only_per_pair, only_per_side): - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ { "method": "StoplossGuard", "lookback_period": 60, @@ -379,7 +379,7 @@ def test_stoploss_guard_perpair(mocker, default_conf, fee, caplog, only_per_pair @pytest.mark.usefixtures("init_persistence") def test_CooldownPeriod(mocker, default_conf, fee, caplog): - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ { "method": "CooldownPeriod", "stop_duration": 60, @@ -425,7 +425,7 @@ def test_CooldownPeriod(mocker, default_conf, fee, caplog): @pytest.mark.usefixtures("init_persistence") def test_CooldownPeriod_unlock_at(mocker, default_conf, fee, caplog, time_machine): - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ { "method": "CooldownPeriod", "unlock_at": "05:00", @@ -509,7 +509,7 @@ def test_CooldownPeriod_unlock_at(mocker, default_conf, fee, caplog, time_machin @pytest.mark.parametrize("only_per_side", [False, True]) @pytest.mark.usefixtures("init_persistence") def test_LowProfitPairs(mocker, default_conf, fee, caplog, only_per_side): - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ { "method": "LowProfitPairs", "lookback_period": 400, @@ -599,7 +599,7 @@ def test_LowProfitPairs(mocker, default_conf, fee, caplog, only_per_side): @pytest.mark.usefixtures("init_persistence") def test_MaxDrawdown(mocker, default_conf, fee, caplog): - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ { "method": "MaxDrawdown", "lookback_period": 1000, @@ -812,7 +812,7 @@ def test_MaxDrawdown(mocker, default_conf, fee, caplog): def test_protection_manager_desc( mocker, default_conf, protectionconf, desc_expected, exception_expected ): - default_conf["protections"] = [protectionconf] + default_conf["_strategy_protections"] = [protectionconf] freqtrade = get_patched_freqtradebot(mocker, default_conf) short_desc = str(freqtrade.protections.short_desc()) diff --git a/tests/rpc/test_rpc_manager.py b/tests/rpc/test_rpc_manager.py index 2792fd082..67755255f 100644 --- a/tests/rpc/test_rpc_manager.py +++ b/tests/rpc/test_rpc_manager.py @@ -173,7 +173,7 @@ def test_startupmessages_telegram_enabled(mocker, default_conf) -> None: telegram_mock.reset_mock() default_conf["dry_run"] = True default_conf["whitelist"] = {"method": "VolumePairList", "config": {"number_assets": 20}} - default_conf["protections"] = [ + default_conf["_strategy_protections"] = [ {"method": "StoplossGuard", "lookback_period": 60, "trade_limit": 2, "stop_duration": 60} ] freqtradebot = get_patched_freqtradebot(mocker, default_conf) diff --git a/tests/strategy/strats/strategy_test_v3.py b/tests/strategy/strats/strategy_test_v3.py index 71404242a..007c7655e 100644 --- a/tests/strategy/strats/strategy_test_v3.py +++ b/tests/strategy/strats/strategy_test_v3.py @@ -75,15 +75,13 @@ class StrategyTestV3(IStrategy): protection_cooldown_lookback = IntParameter([0, 50], default=30) # TODO: Can this work with protection tests? (replace HyperoptableStrategy implicitly ... ) - # @property - # def protections(self): - # prot = [] - # if self.protection_enabled.value: - # prot.append({ - # "method": "CooldownPeriod", - # "stop_duration_candles": self.protection_cooldown_lookback.value - # }) - # return prot + @property + def protections(self): + prot = [] + if self.protection_enabled.value: + # Workaround to simplify tests. This will not work in real scenarios. + prot = self.config.get("_strategy_protections", {}) + return prot bot_started = False