[SQUASH] Add a way to temporarily disable a parameter (excludes from parameter loading/hyperopt) and print parameter values when executed.

This commit is contained in:
Rokas Kupstys 2021-03-26 14:25:49 +02:00
parent bbe6ece38d
commit 40f5c7853e
2 changed files with 25 additions and 10 deletions

View File

@ -51,6 +51,7 @@ class HyperOptAuto(IHyperOpt):
def _generate_indicator_space(self, category): def _generate_indicator_space(self, category):
for attr_name, attr in self.strategy.enumerate_parameters(category): for attr_name, attr in self.strategy.enumerate_parameters(category):
if attr.enabled:
yield attr.get_space(attr_name) yield attr.get_space(attr_name)
def _get_indicator_space(self, category, fallback_method_name): def _get_indicator_space(self, category, fallback_method_name):

View File

@ -2,6 +2,7 @@
IHyperStrategy interface, hyperoptable Parameter class. IHyperStrategy interface, hyperoptable Parameter class.
This module defines a base class for auto-hyperoptable strategies. This module defines a base class for auto-hyperoptable strategies.
""" """
import logging
from contextlib import suppress from contextlib import suppress
from typing import Iterator, Tuple, Any, Optional, Sequence, Union from typing import Iterator, Tuple, Any, Optional, Sequence, Union
@ -11,6 +12,9 @@ with suppress(ImportError):
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
logger = logging.getLogger(__name__)
class BaseParameter(object): class BaseParameter(object):
""" """
Defines a parameter that can be optimized by hyperopt. Defines a parameter that can be optimized by hyperopt.
@ -21,7 +25,7 @@ class BaseParameter(object):
opt_range: Sequence[Any] opt_range: Sequence[Any]
def __init__(self, *, opt_range: Sequence[Any], default: Any, space: Optional[str] = None, def __init__(self, *, opt_range: Sequence[Any], default: Any, space: Optional[str] = None,
**kwargs): enabled: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if :param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
@ -36,6 +40,7 @@ class BaseParameter(object):
self._space_params = kwargs self._space_params = kwargs
self.value = default self.value = default
self.opt_range = opt_range self.opt_range = opt_range
self.enabled = enabled
def __repr__(self): def __repr__(self):
return f'{self.__class__.__name__}({self.value})' return f'{self.__class__.__name__}({self.value})'
@ -50,7 +55,7 @@ class IntParameter(BaseParameter):
opt_range: Sequence[int] opt_range: Sequence[int]
def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int, def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int,
space: Optional[str] = None, **kwargs): space: Optional[str] = None, enabled: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param low: lower end of optimization space or [low, high]. :param low: lower end of optimization space or [low, high].
@ -67,7 +72,8 @@ class IntParameter(BaseParameter):
opt_range = low opt_range = low
else: else:
opt_range = [low, high] opt_range = [low, high]
super().__init__(opt_range=opt_range, default=default, space=space, **kwargs) super().__init__(opt_range=opt_range, default=default, space=space, enabled=enabled,
**kwargs)
def get_space(self, name: str) -> 'Integer': def get_space(self, name: str) -> 'Integer':
""" """
@ -83,7 +89,7 @@ class FloatParameter(BaseParameter):
opt_range: Sequence[float] opt_range: Sequence[float]
def __init__(self, low: Union[float, Sequence[float]], high: Optional[int] = None, *, def __init__(self, low: Union[float, Sequence[float]], high: Optional[int] = None, *,
default: float, space: Optional[str] = None, **kwargs): default: float, space: Optional[str] = None, enabled: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param low: lower end of optimization space or [low, high]. :param low: lower end of optimization space or [low, high].
@ -100,7 +106,8 @@ class FloatParameter(BaseParameter):
opt_range = low opt_range = low
else: else:
opt_range = [low, high] opt_range = [low, high]
super().__init__(opt_range=opt_range, default=default, space=space, **kwargs) super().__init__(opt_range=opt_range, default=default, space=space, enabled=enabled,
**kwargs)
def get_space(self, name: str) -> 'Real': def get_space(self, name: str) -> 'Real':
""" """
@ -116,7 +123,7 @@ class CategoricalParameter(BaseParameter):
opt_range: Sequence[Any] opt_range: Sequence[Any]
def __init__(self, categories: Sequence[Any], *, default: Optional[Any] = None, def __init__(self, categories: Sequence[Any], *, default: Optional[Any] = None,
space: Optional[str] = None, **kwargs): space: Optional[str] = None, enabled: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param categories: Optimization space, [a, b, ...]. :param categories: Optimization space, [a, b, ...].
@ -130,7 +137,8 @@ class CategoricalParameter(BaseParameter):
if len(categories) < 2: if len(categories) < 2:
raise OperationalException( raise OperationalException(
'IntParameter space must be [a, b, ...] (at least two parameters)') 'IntParameter space must be [a, b, ...] (at least two parameters)')
super().__init__(opt_range=categories, default=default, space=space, **kwargs) super().__init__(opt_range=categories, default=default, space=space, enabled=enabled,
**kwargs)
def get_space(self, name: str) -> 'Categorical': def get_space(self, name: str) -> 'Categorical':
""" """
@ -167,6 +175,7 @@ class HyperStrategyMixin(object):
if issubclass(attr.__class__, BaseParameter): if issubclass(attr.__class__, BaseParameter):
if category is None or category == attr.category or \ if category is None or category == attr.category or \
attr_name.startswith(category + '_'): attr_name.startswith(category + '_'):
if attr.enabled:
yield attr_name, attr yield attr_name, attr
def _load_params(self, params: dict) -> None: def _load_params(self, params: dict) -> None:
@ -178,4 +187,9 @@ class HyperStrategyMixin(object):
return return
for attr_name, attr in self.enumerate_parameters(): for attr_name, attr in self.enumerate_parameters():
if attr_name in params: if attr_name in params:
if attr.enabled:
attr.value = params[attr_name] attr.value = params[attr_name]
logger.info(f'attr_name = {attr.value}')
else:
logger.warning(f'Parameter "{attr_name}" exists, but is disabled. '
f'Default value "{attr.value}" used.')