From 555262b6e1f2c28ff03b27dc03452efb17029044 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 1 May 2021 16:36:53 +0200 Subject: [PATCH] Only calculate additional indicators if the space is selected --- freqtrade/strategy/hyper.py | 9 ++++++--- tests/optimize/test_hyperopt.py | 2 +- tests/strategy/test_interface.py | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 1848730dd..2714ffb43 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -7,6 +7,8 @@ from abc import ABC, abstractmethod from contextlib import suppress from typing import Any, Dict, Iterator, Optional, Sequence, Tuple, Union +from freqtrade.optimize.hyperopt_tools import HyperoptTools + with suppress(ImportError): from skopt.space import Integer, Real, Categorical @@ -26,7 +28,7 @@ class BaseParameter(ABC): category: Optional[str] default: Any value: Any - hyperopt: bool = False + in_space: bool = False def __init__(self, *, default: Any, space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs): @@ -131,7 +133,7 @@ class IntParameter(NumericParameter): Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid calculating 100ds of indicators. """ - if self.hyperopt: + if self.in_space and self.optimize: # Scikit-optimize ranges are "inclusive", while python's "range" is exclusive return range(self.low, self.high + 1) else: @@ -247,6 +249,7 @@ class HyperStrategyMixin(object): """ Initialize hyperoptable strategy mixin. """ + self.config = config self._load_hyper_params(config.get('runmode') == RunMode.HYPEROPT) def enumerate_parameters(self, category: str = None) -> Iterator[Tuple[str, BaseParameter]]: @@ -285,7 +288,7 @@ class HyperStrategyMixin(object): logger.info(f"No params for {space} found, using default values.") for attr_name, attr in self.enumerate_parameters(space): - attr.hyperopt = hyperopt + attr.in_space = hyperopt and HyperoptTools.has_space(self.config, space) if params and attr_name in params: if attr.load: attr.value = params[attr_name] diff --git a/tests/optimize/test_hyperopt.py b/tests/optimize/test_hyperopt.py index d125cfca3..5a7f0f32e 100644 --- a/tests/optimize/test_hyperopt.py +++ b/tests/optimize/test_hyperopt.py @@ -1108,7 +1108,7 @@ def test_in_strategy_auto_hyperopt(mocker, hyperopt_conf, tmpdir, fee) -> None: assert isinstance(hyperopt.custom_hyperopt, HyperOptAuto) assert isinstance(hyperopt.backtesting.strategy.buy_rsi, IntParameter) - assert hyperopt.backtesting.strategy.buy_rsi.hyperopt is True + assert hyperopt.backtesting.strategy.buy_rsi.in_space is True assert hyperopt.backtesting.strategy.buy_rsi.value == 35 buy_rsi_range = hyperopt.backtesting.strategy.buy_rsi.range assert isinstance(buy_rsi_range, range) diff --git a/tests/strategy/test_interface.py b/tests/strategy/test_interface.py index bd81bc80c..4ca514349 100644 --- a/tests/strategy/test_interface.py +++ b/tests/strategy/test_interface.py @@ -636,7 +636,7 @@ def test_hyperopt_parameters(): assert len(list(intpar.range)) == 1 # Range contains ONLY the default / value. assert list(intpar.range) == [intpar.value] - intpar.hyperopt = True + intpar.in_space = True assert len(list(intpar.range)) == 6 assert list(intpar.range) == [0, 1, 2, 3, 4, 5]