From 3e6a2bf9b04d0c140350b0f78baa274dc7bae11d Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 20 May 2023 20:12:04 +0200 Subject: [PATCH] Add parameters for analysis tests ... --- tests/optimize/test_lookahead_analysis.py | 23 +++++++++++++++++-- .../strategy_test_v3_with_lookahead_bias.py | 17 ++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/tests/optimize/test_lookahead_analysis.py b/tests/optimize/test_lookahead_analysis.py index ef4f8809c..0706750ec 100644 --- a/tests/optimize/test_lookahead_analysis.py +++ b/tests/optimize/test_lookahead_analysis.py @@ -113,7 +113,10 @@ def test_initialize_single_lookahead_analysis(): pytest.skip("TODO") -def test_biased_strategy(lookahead_conf, mocker, caplog) -> None: +@pytest.mark.parametrize('scenario', [ + 'no_bias', 'bias1' +]) +def test_biased_strategy(lookahead_conf, mocker, caplog, scenario) -> None: mocker.patch('freqtrade.data.history.get_timerange', get_timerange) mocker.patch(f'{EXMS}.get_fee', return_value=0.0) @@ -128,10 +131,26 @@ def test_biased_strategy(lookahead_conf, mocker, caplog) -> None: lookahead_conf['timerange'] = '20180119-20180122' lookahead_conf['strategy'] = 'strategy_test_v3_with_lookahead_bias' + # Patch scenario Parameter to allow for easy selection + mocker.patch('freqtrade.strategy.hyper.HyperStrategyMixin.load_params_from_file', + return_value={ + 'params': { + "buy": { + "scenario": scenario + } + } + }) + strategy_obj = {} strategy_obj['name'] = "strategy_test_v3_with_lookahead_bias" instance = LookaheadAnalysis(lookahead_conf, strategy_obj) instance.start() + # Assert init correct + assert log_has_re(f"Strategy Parameter: scenario = {scenario}", caplog) + # Assert bias detected assert log_has_re(r".*bias detected.*", caplog) - # TODO: assert something ... most likely output (?) or instance state? + + # Assert False to see full logs in output + # assert False + # Run with `pytest tests/optimize/test_lookahead_analysis.py -k test_biased_strategy` diff --git a/tests/strategy/strats/lookahead_bias/strategy_test_v3_with_lookahead_bias.py b/tests/strategy/strats/lookahead_bias/strategy_test_v3_with_lookahead_bias.py index 6cf894586..d35b85b2d 100644 --- a/tests/strategy/strats/lookahead_bias/strategy_test_v3_with_lookahead_bias.py +++ b/tests/strategy/strats/lookahead_bias/strategy_test_v3_with_lookahead_bias.py @@ -3,6 +3,7 @@ from pandas import DataFrame from technical.indicators import ichimoku from freqtrade.strategy import IStrategy +from freqtrade.strategy.parameters import CategoricalParameter class strategy_test_v3_with_lookahead_bias(IStrategy): @@ -21,6 +22,7 @@ class strategy_test_v3_with_lookahead_bias(IStrategy): # Optimal timeframe for the strategy timeframe = '5m' + scenario = CategoricalParameter(['no_bias', 'bias1'], default='bias1', space="buy") # Number of candles the strategy requires before producing valid signals startup_candle_count: int = 20 @@ -37,14 +39,19 @@ class strategy_test_v3_with_lookahead_bias(IStrategy): return dataframe def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - dataframe.loc[ - dataframe['close'].shift(-10) > dataframe['close'], - 'enter_long'] = 1 + if self.scenario.value == 'no_bias': + dataframe.loc[dataframe['close'].shift(10) < dataframe['close'], 'enter_long'] = 1 + else: + dataframe.loc[dataframe['close'].shift(-10) > dataframe['close'], 'enter_long'] = 1 return dataframe def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: - dataframe.loc[ - dataframe['close'].shift(-10) > dataframe['close'], 'exit'] = 1 + if self.scenario.value == 'no_bias': + dataframe.loc[ + dataframe['close'].shift(10) < dataframe['close'], 'exit'] = 1 + else: + dataframe.loc[ + dataframe['close'].shift(-10) > dataframe['close'], 'exit'] = 1 return dataframe