2021-03-23 08:02:32 +00:00
|
|
|
"""
|
|
|
|
IHyperStrategy interface, hyperoptable Parameter class.
|
|
|
|
This module defines a base class for auto-hyperoptable strategies.
|
|
|
|
"""
|
2021-03-24 09:17:17 +00:00
|
|
|
from typing import Iterator, Tuple, Any, Optional, Sequence, Union
|
2021-03-23 08:02:32 +00:00
|
|
|
|
|
|
|
from skopt.space import Integer, Real, Categorical
|
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2021-03-23 08:02:32 +00:00
|
|
|
|
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
class BaseParameter(object):
|
2021-03-23 08:02:32 +00:00
|
|
|
"""
|
|
|
|
Defines a parameter that can be optimized by hyperopt.
|
|
|
|
"""
|
2021-03-24 08:32:34 +00:00
|
|
|
category: Optional[str]
|
|
|
|
default: Any
|
|
|
|
value: Any
|
|
|
|
space: Sequence[Any]
|
2021-03-23 08:02:32 +00:00
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
def __init__(self, *, space: Sequence[Any], default: Any, category: Optional[str] = None,
|
|
|
|
**kwargs):
|
2021-03-23 08:02:32 +00:00
|
|
|
"""
|
|
|
|
Initialize hyperopt-optimizable parameter.
|
2021-03-24 09:17:17 +00:00
|
|
|
:param category: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
|
|
parameter field
|
2021-03-23 08:02:32 +00:00
|
|
|
name is prefixed with 'buy_' or 'sell_'.
|
|
|
|
:param kwargs: Extra parameters to skopt.space.(Integer|Real|Categorical).
|
|
|
|
"""
|
2021-03-24 08:32:34 +00:00
|
|
|
if 'name' in kwargs:
|
|
|
|
raise OperationalException(
|
|
|
|
'Name is determined by parameter field name and can not be specified manually.')
|
2021-03-23 08:02:32 +00:00
|
|
|
self.category = category
|
|
|
|
self._space_params = kwargs
|
2021-03-24 08:32:34 +00:00
|
|
|
self.value = default
|
|
|
|
self.space = space
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'{self.__class__.__name__}({self.value})'
|
|
|
|
|
2021-03-24 09:17:17 +00:00
|
|
|
def get_space(self, name: str) -> Union[Integer, Real, Categorical]:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
|
|
|
|
class IntParameter(BaseParameter):
|
|
|
|
default: int
|
|
|
|
value: int
|
|
|
|
space: Sequence[int]
|
|
|
|
|
|
|
|
def __init__(self, *, space: Sequence[int], default: int, category: Optional[str] = None,
|
|
|
|
**kwargs):
|
|
|
|
"""
|
|
|
|
Initialize hyperopt-optimizable parameter.
|
|
|
|
:param space: Optimization space, [min, max].
|
|
|
|
:param default: A default value.
|
2021-03-24 09:17:17 +00:00
|
|
|
:param category: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
|
|
parameter field
|
2021-03-24 08:32:34 +00:00
|
|
|
name is prefixed with 'buy_' or 'sell_'.
|
|
|
|
:param kwargs: Extra parameters to skopt.space.Integer.
|
|
|
|
"""
|
|
|
|
if len(space) != 2:
|
|
|
|
raise OperationalException('IntParameter space must be [min, max]')
|
|
|
|
super().__init__(space=space, default=default, category=category, **kwargs)
|
|
|
|
|
|
|
|
def get_space(self, name: str) -> Integer:
|
|
|
|
"""
|
|
|
|
Create skopt optimization space.
|
|
|
|
:param name: A name of parameter field.
|
|
|
|
"""
|
|
|
|
return Integer(*self.space, name=name, **self._space_params)
|
|
|
|
|
|
|
|
|
|
|
|
class FloatParameter(BaseParameter):
|
|
|
|
default: float
|
|
|
|
value: float
|
|
|
|
space: Sequence[float]
|
|
|
|
|
|
|
|
def __init__(self, *, space: Sequence[float], default: float, category: Optional[str] = None,
|
|
|
|
**kwargs):
|
|
|
|
"""
|
|
|
|
Initialize hyperopt-optimizable parameter.
|
|
|
|
:param space: Optimization space, [min, max].
|
|
|
|
:param default: A default value.
|
2021-03-24 09:17:17 +00:00
|
|
|
:param category: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
|
|
parameter field
|
2021-03-24 08:32:34 +00:00
|
|
|
name is prefixed with 'buy_' or 'sell_'.
|
|
|
|
:param kwargs: Extra parameters to skopt.space.Real.
|
|
|
|
"""
|
|
|
|
if len(space) != 2:
|
|
|
|
raise OperationalException('IntParameter space must be [min, max]')
|
|
|
|
super().__init__(space=space, default=default, category=category, **kwargs)
|
2021-03-23 08:02:32 +00:00
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
def get_space(self, name: str) -> Real:
|
2021-03-23 08:02:32 +00:00
|
|
|
"""
|
|
|
|
Create skopt optimization space.
|
|
|
|
:param name: A name of parameter field.
|
2021-03-24 08:32:34 +00:00
|
|
|
"""
|
|
|
|
return Real(*self.space, name=name, **self._space_params)
|
|
|
|
|
|
|
|
|
|
|
|
class CategoricalParameter(BaseParameter):
|
|
|
|
default: Any
|
|
|
|
value: Any
|
|
|
|
space: Sequence[Any]
|
|
|
|
|
|
|
|
def __init__(self, *, space: Sequence[Any], default: Optional[Any] = None,
|
|
|
|
category: Optional[str] = None,
|
|
|
|
**kwargs):
|
|
|
|
"""
|
|
|
|
Initialize hyperopt-optimizable parameter.
|
|
|
|
:param space: Optimization space, [a, b, ...].
|
|
|
|
:param default: A default value. If not specified, first item from specified space will be
|
|
|
|
used.
|
|
|
|
:param category: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
|
|
parameter field
|
|
|
|
name is prefixed with 'buy_' or 'sell_'.
|
|
|
|
:param kwargs: Extra parameters to skopt.space.Categorical.
|
|
|
|
"""
|
|
|
|
if len(space) < 2:
|
|
|
|
raise OperationalException(
|
|
|
|
'IntParameter space must be [a, b, ...] (at least two parameters)')
|
|
|
|
super().__init__(space=space, default=default, category=category, **kwargs)
|
|
|
|
|
|
|
|
def get_space(self, name: str) -> Categorical:
|
|
|
|
"""
|
|
|
|
Create skopt optimization space.
|
|
|
|
:param name: A name of parameter field.
|
|
|
|
"""
|
2021-03-23 08:02:32 +00:00
|
|
|
return Categorical(self.space, name=name, **self._space_params)
|
|
|
|
|
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
class HyperStrategyMixin(object):
|
2021-03-23 08:02:32 +00:00
|
|
|
"""
|
2021-03-24 08:32:34 +00:00
|
|
|
A helper base class which allows HyperOptAuto class to reuse implementations of of buy/sell
|
|
|
|
strategy logic.
|
2021-03-23 08:02:32 +00:00
|
|
|
"""
|
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
# Hint that class can be used with HyperOptAuto.
|
|
|
|
HYPER_STRATEGY = 1
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Initialize hyperoptable strategy mixin.
|
|
|
|
:param config:
|
|
|
|
"""
|
2021-03-23 08:02:32 +00:00
|
|
|
self._load_params(getattr(self, 'buy_params', None))
|
|
|
|
self._load_params(getattr(self, 'sell_params', None))
|
|
|
|
|
2021-03-24 08:32:34 +00:00
|
|
|
def enumerate_parameters(self, category: str = None) -> Iterator[Tuple[str, BaseParameter]]:
|
2021-03-23 08:02:32 +00:00
|
|
|
"""
|
|
|
|
Find all optimizeable parameters and return (name, attr) iterator.
|
|
|
|
:param category:
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-24 08:32:34 +00:00
|
|
|
if category not in ('buy', 'sell', None):
|
|
|
|
raise OperationalException('Category must be one of: "buy", "sell", None.')
|
2021-03-23 08:02:32 +00:00
|
|
|
for attr_name in dir(self):
|
|
|
|
if not attr_name.startswith('__'): # Ignore internals, not strictly necessary.
|
|
|
|
attr = getattr(self, attr_name)
|
2021-03-24 08:32:34 +00:00
|
|
|
if issubclass(attr.__class__, BaseParameter):
|
|
|
|
if category is None or category == attr.category or \
|
|
|
|
attr_name.startswith(category + '_'):
|
2021-03-23 08:02:32 +00:00
|
|
|
yield attr_name, attr
|
|
|
|
|
|
|
|
def _load_params(self, params: dict) -> None:
|
|
|
|
"""
|
|
|
|
Set optimizeable parameter values.
|
|
|
|
:param params: Dictionary with new parameter values.
|
|
|
|
"""
|
|
|
|
if not params:
|
|
|
|
return
|
|
|
|
for attr_name, attr in self.enumerate_parameters():
|
|
|
|
if attr_name in params:
|
|
|
|
attr.value = params[attr_name]
|