Only calculate additional indicators if the space is selected

This commit is contained in:
Matthias 2021-05-01 16:36:53 +02:00
parent e381df9098
commit 555262b6e1
3 changed files with 8 additions and 5 deletions

View File

@ -7,6 +7,8 @@ from abc import ABC, abstractmethod
from contextlib import suppress from contextlib import suppress
from typing import Any, Dict, Iterator, Optional, Sequence, Tuple, Union from typing import Any, Dict, Iterator, Optional, Sequence, Tuple, Union
from freqtrade.optimize.hyperopt_tools import HyperoptTools
with suppress(ImportError): with suppress(ImportError):
from skopt.space import Integer, Real, Categorical from skopt.space import Integer, Real, Categorical
@ -26,7 +28,7 @@ class BaseParameter(ABC):
category: Optional[str] category: Optional[str]
default: Any default: Any
value: Any value: Any
hyperopt: bool = False in_space: bool = False
def __init__(self, *, default: Any, space: Optional[str] = None, def __init__(self, *, default: Any, space: Optional[str] = None,
optimize: bool = True, load: bool = True, **kwargs): 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 Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
calculating 100ds of indicators. 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 # Scikit-optimize ranges are "inclusive", while python's "range" is exclusive
return range(self.low, self.high + 1) return range(self.low, self.high + 1)
else: else:
@ -247,6 +249,7 @@ class HyperStrategyMixin(object):
""" """
Initialize hyperoptable strategy mixin. Initialize hyperoptable strategy mixin.
""" """
self.config = config
self._load_hyper_params(config.get('runmode') == RunMode.HYPEROPT) self._load_hyper_params(config.get('runmode') == RunMode.HYPEROPT)
def enumerate_parameters(self, category: str = None) -> Iterator[Tuple[str, BaseParameter]]: 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.") logger.info(f"No params for {space} found, using default values.")
for attr_name, attr in self.enumerate_parameters(space): 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 params and attr_name in params:
if attr.load: if attr.load:
attr.value = params[attr_name] attr.value = params[attr_name]

View File

@ -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.custom_hyperopt, HyperOptAuto)
assert isinstance(hyperopt.backtesting.strategy.buy_rsi, IntParameter) 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 assert hyperopt.backtesting.strategy.buy_rsi.value == 35
buy_rsi_range = hyperopt.backtesting.strategy.buy_rsi.range buy_rsi_range = hyperopt.backtesting.strategy.buy_rsi.range
assert isinstance(buy_rsi_range, range) assert isinstance(buy_rsi_range, range)

View File

@ -636,7 +636,7 @@ def test_hyperopt_parameters():
assert len(list(intpar.range)) == 1 assert len(list(intpar.range)) == 1
# Range contains ONLY the default / value. # Range contains ONLY the default / value.
assert list(intpar.range) == [intpar.value] assert list(intpar.range) == [intpar.value]
intpar.hyperopt = True intpar.in_space = True
assert len(list(intpar.range)) == 6 assert len(list(intpar.range)) == 6
assert list(intpar.range) == [0, 1, 2, 3, 4, 5] assert list(intpar.range) == [0, 1, 2, 3, 4, 5]