From 929f3296079b4536bfb163b544dcf4ffda46f6ce Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 28 Mar 2021 19:49:20 +0200 Subject: [PATCH] more tests --- freqtrade/strategy/hyper.py | 2 +- .../strategy/strats/hyperoptable_strategy.py | 6 ++++-- tests/strategy/test_interface.py | 21 +++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 461e6314f..64e457d75 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -145,7 +145,7 @@ class CategoricalParameter(BaseParameter): """ if len(categories) < 2: raise OperationalException( - 'IntParameter space must be [a, b, ...] (at least two parameters)') + 'CategoricalParameter space must be [a, b, ...] (at least two parameters)') super().__init__(opt_range=categories, default=default, space=space, enabled=enabled, **kwargs) diff --git a/tests/strategy/strats/hyperoptable_strategy.py b/tests/strategy/strats/hyperoptable_strategy.py index 8cde28321..97d2092d4 100644 --- a/tests/strategy/strats/hyperoptable_strategy.py +++ b/tests/strategy/strats/hyperoptable_strategy.py @@ -55,12 +55,14 @@ class HyperoptableStrategy(IStrategy): } sell_params = { - 'sell_rsi': 74 + 'sell_rsi': 74, + 'sell_minusdi': 0.4 } buy_rsi = IntParameter([0, 50], default=30, space='buy') buy_plusdi = FloatParameter(low=0, high=1, default=0.5, space='buy') sell_rsi = IntParameter(low=50, high=100, default=70, space='sell') + sell_minusdi = FloatParameter(low=0, high=1, default=0.5, space='sell', enabled=False) def informative_pairs(self): """ @@ -164,7 +166,7 @@ class HyperoptableStrategy(IStrategy): ) | ( (dataframe['adx'] > 70) & - (dataframe['minus_di'] > 0.5) + (dataframe['minus_di'] > self.sell_minusdi.value) ), 'sell'] = 1 return dataframe diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index e9d57dd17..72b78338d 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -13,7 +13,8 @@ from freqtrade.data.history import load_data from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.persistence import PairLocks, Trade from freqtrade.resolvers import StrategyResolver -from freqtrade.strategy.hyper import BaseParameter, FloatParameter, IntParameter +from freqtrade.strategy.hyper import (BaseParameter, CategoricalParameter, FloatParameter, + IntParameter) from freqtrade.strategy.interface import SellCheckTuple, SellType from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from tests.conftest import log_has, log_has_re @@ -556,6 +557,7 @@ def test_strategy_safe_wrapper(value): def test_hyperopt_parameters(): + from skopt.space import Categorical, Integer, Real with pytest.raises(OperationalException, match=r"Name is determined.*"): IntParameter(low=0, high=5, default=1, name='hello') @@ -571,12 +573,24 @@ def test_hyperopt_parameters(): with pytest.raises(OperationalException, match=r"FloatParameter space invalid\."): FloatParameter([0, 10], high=7, default=5, space='buy') + with pytest.raises(OperationalException, match=r"CategoricalParameter space must.*"): + CategoricalParameter(['aa'], default='aa', space='buy') + with pytest.raises(TypeError): BaseParameter(opt_range=[0, 1], default=1, space='buy') - fltpar = IntParameter(low=0, high=5, default=1, space='buy') + intpar = IntParameter(low=0, high=5, default=1, space='buy') + assert intpar.value == 1 + assert isinstance(intpar.get_space(''), Integer) + + fltpar = FloatParameter(low=0.0, high=5.5, default=1.0, space='buy') + assert isinstance(fltpar.get_space(''), Real) assert fltpar.value == 1 + catpar = CategoricalParameter(['buy_rsi', 'buy_macd', 'buy_none'], default='buy_macd', space='buy') + assert isinstance(catpar.get_space(''), Categorical) + assert catpar.value == 'buy_macd' + def test_auto_hyperopt_interface(default_conf): default_conf.update({'strategy': 'HyperoptableStrategy'}) @@ -587,3 +601,6 @@ def test_auto_hyperopt_interface(default_conf): # PlusDI is NOT in the buy-params, so default should be used assert strategy.buy_plusdi.value == 0.5 assert strategy.sell_rsi.value == strategy.sell_params['sell_rsi'] + + # Parameter is disabled - so value from sell_param dict will NOT be used. + assert strategy.sell_minusdi.value == 0.5