Split "enabled" to "load" and "optimize" parameters.

This commit is contained in:
Rokas Kupstys 2021-03-31 12:31:28 +03:00
parent 6954a1e029
commit 5e5b11d4d6
2 changed files with 27 additions and 17 deletions

View File

@ -25,6 +25,7 @@ class HyperOptAuto(IHyperOpt):
def buy_strategy_generator(self, params: Dict[str, Any]) -> Callable: def buy_strategy_generator(self, params: Dict[str, Any]) -> Callable:
def populate_buy_trend(dataframe: DataFrame, metadata: dict): def populate_buy_trend(dataframe: DataFrame, metadata: dict):
for attr_name, attr in self.strategy.enumerate_parameters('buy'): for attr_name, attr in self.strategy.enumerate_parameters('buy'):
if attr.optimize:
attr.value = params[attr_name] attr.value = params[attr_name]
return self.strategy.populate_buy_trend(dataframe, metadata) return self.strategy.populate_buy_trend(dataframe, metadata)
@ -33,6 +34,7 @@ class HyperOptAuto(IHyperOpt):
def sell_strategy_generator(self, params: Dict[str, Any]) -> Callable: def sell_strategy_generator(self, params: Dict[str, Any]) -> Callable:
def populate_buy_trend(dataframe: DataFrame, metadata: dict): def populate_buy_trend(dataframe: DataFrame, metadata: dict):
for attr_name, attr in self.strategy.enumerate_parameters('sell'): for attr_name, attr in self.strategy.enumerate_parameters('sell'):
if attr.optimize:
attr.value = params[attr_name] attr.value = params[attr_name]
return self.strategy.populate_sell_trend(dataframe, metadata) return self.strategy.populate_sell_trend(dataframe, metadata)
@ -53,7 +55,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: if attr.optimize:
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

@ -27,12 +27,14 @@ class BaseParameter(ABC):
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,
enabled: bool = True, **kwargs): optimize: bool = True, load: 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
parameter field parameter field
name is prefixed with 'buy_' or 'sell_'. name is prefixed with 'buy_' or 'sell_'.
:param optimize: Include parameter in hyperopt optimizations.
:param load: Load parameter value from {space}_params.
:param kwargs: Extra parameters to skopt.space.(Integer|Real|Categorical). :param kwargs: Extra parameters to skopt.space.(Integer|Real|Categorical).
""" """
if 'name' in kwargs: if 'name' in kwargs:
@ -42,7 +44,8 @@ class BaseParameter(ABC):
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 self.optimize = optimize
self.load = load
def __repr__(self): def __repr__(self):
return f'{self.__class__.__name__}({self.value})' return f'{self.__class__.__name__}({self.value})'
@ -60,7 +63,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, enabled: bool = True, **kwargs): space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param low: Lower end (inclusive) of optimization space or [low, high]. :param low: Lower end (inclusive) of optimization space or [low, high].
@ -69,6 +72,8 @@ class IntParameter(BaseParameter):
:param default: A default value. :param default: A default value.
: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
parameter fieldname is prefixed with 'buy_' or 'sell_'. parameter fieldname is prefixed with 'buy_' or 'sell_'.
:param optimize: Include parameter in hyperopt optimizations.
:param load: Load parameter value from {space}_params.
:param kwargs: Extra parameters to skopt.space.Integer. :param kwargs: Extra parameters to skopt.space.Integer.
""" """
if high is not None and isinstance(low, Sequence): if high is not None and isinstance(low, Sequence):
@ -79,8 +84,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, enabled=enabled, super().__init__(opt_range=opt_range, default=default, space=space, optimize=optimize,
**kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'Integer': def get_space(self, name: str) -> 'Integer':
""" """
@ -96,7 +101,7 @@ class FloatParameter(BaseParameter):
opt_range: Sequence[float] opt_range: Sequence[float]
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *, def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
default: float, space: Optional[str] = None, enabled: bool = True, **kwargs): default: float, space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param low: Lower end (inclusive) of optimization space or [low, high]. :param low: Lower end (inclusive) of optimization space or [low, high].
@ -105,6 +110,8 @@ class FloatParameter(BaseParameter):
:param default: A default value. :param default: A default value.
: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
parameter fieldname is prefixed with 'buy_' or 'sell_'. parameter fieldname is prefixed with 'buy_' or 'sell_'.
:param optimize: Include parameter in hyperopt optimizations.
:param load: Load parameter value from {space}_params.
:param kwargs: Extra parameters to skopt.space.Real. :param kwargs: Extra parameters to skopt.space.Real.
""" """
if high is not None and isinstance(low, Sequence): if high is not None and isinstance(low, Sequence):
@ -115,8 +122,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, enabled=enabled, super().__init__(opt_range=opt_range, default=default, space=space, optimize=optimize,
**kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'Real': def get_space(self, name: str) -> 'Real':
""" """
@ -132,7 +139,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, enabled: bool = True, **kwargs): space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
:param categories: Optimization space, [a, b, ...]. :param categories: Optimization space, [a, b, ...].
@ -141,13 +148,15 @@ class CategoricalParameter(BaseParameter):
: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
parameter field parameter field
name is prefixed with 'buy_' or 'sell_'. name is prefixed with 'buy_' or 'sell_'.
:param optimize: Include parameter in hyperopt optimizations.
:param load: Load parameter value from {space}_params.
:param kwargs: Extra parameters to skopt.space.Categorical. :param kwargs: Extra parameters to skopt.space.Categorical.
""" """
if len(categories) < 2: if len(categories) < 2:
raise OperationalException( raise OperationalException(
'CategoricalParameter 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, super().__init__(opt_range=categories, default=default, space=space, optimize=optimize,
**kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'Categorical': def get_space(self, name: str) -> 'Categorical':
""" """
@ -184,7 +193,6 @@ 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:
@ -196,7 +204,7 @@ 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: if attr.load:
attr.value = params[attr_name] attr.value = params[attr_name]
logger.info(f'attr_name = {attr.value}') logger.info(f'attr_name = {attr.value}')
else: else: