Refactor hyperopt parameters to separate file
This commit is contained in:
parent
063fc5174d
commit
42ae8ba6fb
@ -1,9 +1,9 @@
|
|||||||
# flake8: noqa: F401
|
# flake8: noqa: F401
|
||||||
from freqtrade.exchange import (timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date,
|
from freqtrade.exchange import (timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date,
|
||||||
timeframe_to_prev_date, timeframe_to_seconds)
|
timeframe_to_prev_date, timeframe_to_seconds)
|
||||||
from freqtrade.strategy.hyper import (BooleanParameter, CategoricalParameter, DecimalParameter,
|
|
||||||
IntParameter, RealParameter)
|
|
||||||
from freqtrade.strategy.informative_decorator import informative
|
from freqtrade.strategy.informative_decorator import informative
|
||||||
from freqtrade.strategy.interface import IStrategy
|
from freqtrade.strategy.interface import IStrategy
|
||||||
|
from freqtrade.strategy.parameters import (BooleanParameter, CategoricalParameter, DecimalParameter,
|
||||||
|
IntParameter, RealParameter)
|
||||||
from freqtrade.strategy.strategy_helper import (merge_informative_pair, stoploss_from_absolute,
|
from freqtrade.strategy.strategy_helper import (merge_informative_pair, stoploss_from_absolute,
|
||||||
stoploss_from_open)
|
stoploss_from_open)
|
||||||
|
@ -3,295 +3,19 @@ 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
|
import logging
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
from contextlib import suppress
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple, Union
|
from typing import Any, Dict, Iterator, List, Tuple
|
||||||
|
|
||||||
from freqtrade.misc import deep_merge_dicts, json_load
|
|
||||||
from freqtrade.optimize.hyperopt_tools import HyperoptTools
|
|
||||||
|
|
||||||
|
|
||||||
with suppress(ImportError):
|
|
||||||
from skopt.space import Integer, Real, Categorical
|
|
||||||
from freqtrade.optimize.space import SKDecimal
|
|
||||||
|
|
||||||
from freqtrade.enums import RunMode
|
from freqtrade.enums import RunMode
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
|
from freqtrade.misc import deep_merge_dicts, json_load
|
||||||
|
from freqtrade.optimize.hyperopt_tools import HyperoptTools
|
||||||
|
from freqtrade.strategy.parameters import BaseParameter
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class BaseParameter(ABC):
|
|
||||||
"""
|
|
||||||
Defines a parameter that can be optimized by hyperopt.
|
|
||||||
"""
|
|
||||||
category: Optional[str]
|
|
||||||
default: Any
|
|
||||||
value: Any
|
|
||||||
in_space: bool = False
|
|
||||||
name: str
|
|
||||||
|
|
||||||
def __init__(self, *, default: Any, space: Optional[str] = None,
|
|
||||||
optimize: bool = True, load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable parameter.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
parameter field
|
|
||||||
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).
|
|
||||||
"""
|
|
||||||
if 'name' in kwargs:
|
|
||||||
raise OperationalException(
|
|
||||||
'Name is determined by parameter field name and can not be specified manually.')
|
|
||||||
self.category = space
|
|
||||||
self._space_params = kwargs
|
|
||||||
self.value = default
|
|
||||||
self.optimize = optimize
|
|
||||||
self.load = load
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return f'{self.__class__.__name__}({self.value})'
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def get_space(self, name: str) -> Union['Integer', 'Real', 'SKDecimal', 'Categorical']:
|
|
||||||
"""
|
|
||||||
Get-space - will be used by Hyperopt to get the hyperopt Space
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class NumericParameter(BaseParameter):
|
|
||||||
""" Internal parameter used for Numeric purposes """
|
|
||||||
float_or_int = Union[int, float]
|
|
||||||
default: float_or_int
|
|
||||||
value: float_or_int
|
|
||||||
|
|
||||||
def __init__(self, low: Union[float_or_int, Sequence[float_or_int]],
|
|
||||||
high: Optional[float_or_int] = None, *, default: float_or_int,
|
|
||||||
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable numeric parameter.
|
|
||||||
Cannot be instantiated, but provides the validation for other numeric parameters
|
|
||||||
:param low: Lower end (inclusive) of optimization space or [low, high].
|
|
||||||
:param high: Upper end (inclusive) of optimization space.
|
|
||||||
Must be none of entire range is passed first parameter.
|
|
||||||
:param default: A default value.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
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.*.
|
|
||||||
"""
|
|
||||||
if high is not None and isinstance(low, Sequence):
|
|
||||||
raise OperationalException(f'{self.__class__.__name__} space invalid.')
|
|
||||||
if high is None or isinstance(low, Sequence):
|
|
||||||
if not isinstance(low, Sequence) or len(low) != 2:
|
|
||||||
raise OperationalException(f'{self.__class__.__name__} space must be [low, high]')
|
|
||||||
self.low, self.high = low
|
|
||||||
else:
|
|
||||||
self.low = low
|
|
||||||
self.high = high
|
|
||||||
|
|
||||||
super().__init__(default=default, space=space, optimize=optimize,
|
|
||||||
load=load, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class IntParameter(NumericParameter):
|
|
||||||
default: int
|
|
||||||
value: int
|
|
||||||
|
|
||||||
def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int,
|
|
||||||
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable integer parameter.
|
|
||||||
:param low: Lower end (inclusive) of optimization space or [low, high].
|
|
||||||
:param high: Upper end (inclusive) of optimization space.
|
|
||||||
Must be none of entire range is passed first parameter.
|
|
||||||
:param default: A default value.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
|
|
||||||
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
|
||||||
load=load, **kwargs)
|
|
||||||
|
|
||||||
def get_space(self, name: str) -> 'Integer':
|
|
||||||
"""
|
|
||||||
Create skopt optimization space.
|
|
||||||
:param name: A name of parameter field.
|
|
||||||
"""
|
|
||||||
return Integer(low=self.low, high=self.high, name=name, **self._space_params)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def range(self):
|
|
||||||
"""
|
|
||||||
Get each value in this space as list.
|
|
||||||
Returns a List from low to high (inclusive) in Hyperopt mode.
|
|
||||||
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
|
|
||||||
calculating 100ds of indicators.
|
|
||||||
"""
|
|
||||||
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:
|
|
||||||
return range(self.value, self.value + 1)
|
|
||||||
|
|
||||||
|
|
||||||
class RealParameter(NumericParameter):
|
|
||||||
default: float
|
|
||||||
value: float
|
|
||||||
|
|
||||||
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
|
|
||||||
default: float, space: Optional[str] = None, optimize: bool = True,
|
|
||||||
load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable floating point parameter with unlimited precision.
|
|
||||||
:param low: Lower end (inclusive) of optimization space or [low, high].
|
|
||||||
:param high: Upper end (inclusive) of optimization space.
|
|
||||||
Must be none if entire range is passed first parameter.
|
|
||||||
:param default: A default value.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
|
||||||
load=load, **kwargs)
|
|
||||||
|
|
||||||
def get_space(self, name: str) -> 'Real':
|
|
||||||
"""
|
|
||||||
Create skopt optimization space.
|
|
||||||
:param name: A name of parameter field.
|
|
||||||
"""
|
|
||||||
return Real(low=self.low, high=self.high, name=name, **self._space_params)
|
|
||||||
|
|
||||||
|
|
||||||
class DecimalParameter(NumericParameter):
|
|
||||||
default: float
|
|
||||||
value: float
|
|
||||||
|
|
||||||
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
|
|
||||||
default: float, decimals: int = 3, space: Optional[str] = None,
|
|
||||||
optimize: bool = True, load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable decimal parameter with a limited precision.
|
|
||||||
:param low: Lower end (inclusive) of optimization space or [low, high].
|
|
||||||
:param high: Upper end (inclusive) of optimization space.
|
|
||||||
Must be none if entire range is passed first parameter.
|
|
||||||
:param default: A default value.
|
|
||||||
:param decimals: A number of decimals after floating point to be included in testing.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
self._decimals = decimals
|
|
||||||
default = round(default, self._decimals)
|
|
||||||
|
|
||||||
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
|
||||||
load=load, **kwargs)
|
|
||||||
|
|
||||||
def get_space(self, name: str) -> 'SKDecimal':
|
|
||||||
"""
|
|
||||||
Create skopt optimization space.
|
|
||||||
:param name: A name of parameter field.
|
|
||||||
"""
|
|
||||||
return SKDecimal(low=self.low, high=self.high, decimals=self._decimals, name=name,
|
|
||||||
**self._space_params)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def range(self):
|
|
||||||
"""
|
|
||||||
Get each value in this space as list.
|
|
||||||
Returns a List from low to high (inclusive) in Hyperopt mode.
|
|
||||||
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
|
|
||||||
calculating 100ds of indicators.
|
|
||||||
"""
|
|
||||||
if self.in_space and self.optimize:
|
|
||||||
low = int(self.low * pow(10, self._decimals))
|
|
||||||
high = int(self.high * pow(10, self._decimals)) + 1
|
|
||||||
return [round(n * pow(0.1, self._decimals), self._decimals) for n in range(low, high)]
|
|
||||||
else:
|
|
||||||
return [self.value]
|
|
||||||
|
|
||||||
|
|
||||||
class CategoricalParameter(BaseParameter):
|
|
||||||
default: Any
|
|
||||||
value: Any
|
|
||||||
opt_range: Sequence[Any]
|
|
||||||
|
|
||||||
def __init__(self, categories: Sequence[Any], *, default: Optional[Any] = None,
|
|
||||||
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable parameter.
|
|
||||||
:param categories: Optimization space, [a, b, ...].
|
|
||||||
:param default: A default value. If not specified, first item from specified space will be
|
|
||||||
used.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
parameter field
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
if len(categories) < 2:
|
|
||||||
raise OperationalException(
|
|
||||||
'CategoricalParameter space must be [a, b, ...] (at least two parameters)')
|
|
||||||
self.opt_range = categories
|
|
||||||
super().__init__(default=default, space=space, optimize=optimize,
|
|
||||||
load=load, **kwargs)
|
|
||||||
|
|
||||||
def get_space(self, name: str) -> 'Categorical':
|
|
||||||
"""
|
|
||||||
Create skopt optimization space.
|
|
||||||
:param name: A name of parameter field.
|
|
||||||
"""
|
|
||||||
return Categorical(self.opt_range, name=name, **self._space_params)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def range(self):
|
|
||||||
"""
|
|
||||||
Get each value in this space as list.
|
|
||||||
Returns a List of categories in Hyperopt mode.
|
|
||||||
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
|
|
||||||
calculating 100ds of indicators.
|
|
||||||
"""
|
|
||||||
if self.in_space and self.optimize:
|
|
||||||
return self.opt_range
|
|
||||||
else:
|
|
||||||
return [self.value]
|
|
||||||
|
|
||||||
|
|
||||||
class BooleanParameter(CategoricalParameter):
|
|
||||||
|
|
||||||
def __init__(self, *, default: Optional[Any] = None,
|
|
||||||
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
|
||||||
"""
|
|
||||||
Initialize hyperopt-optimizable Boolean Parameter.
|
|
||||||
It's a shortcut to `CategoricalParameter([True, False])`.
|
|
||||||
:param default: A default value. If not specified, first item from specified space will be
|
|
||||||
used.
|
|
||||||
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
|
||||||
parameter field
|
|
||||||
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.
|
|
||||||
"""
|
|
||||||
|
|
||||||
categories = [True, False]
|
|
||||||
super().__init__(categories=categories, default=default, space=space, optimize=optimize,
|
|
||||||
load=load, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class HyperStrategyMixin:
|
class HyperStrategyMixin:
|
||||||
"""
|
"""
|
||||||
A helper base class which allows HyperOptAuto class to reuse implementations of buy/sell
|
A helper base class which allows HyperOptAuto class to reuse implementations of buy/sell
|
||||||
|
287
freqtrade/strategy/parameters.py
Normal file
287
freqtrade/strategy/parameters.py
Normal file
@ -0,0 +1,287 @@
|
|||||||
|
"""
|
||||||
|
IHyperStrategy interface, hyperoptable Parameter class.
|
||||||
|
This module defines a base class for auto-hyperoptable strategies.
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from contextlib import suppress
|
||||||
|
from typing import Any, Optional, Sequence, Union
|
||||||
|
|
||||||
|
|
||||||
|
with suppress(ImportError):
|
||||||
|
from skopt.space import Integer, Real, Categorical
|
||||||
|
from freqtrade.optimize.space import SKDecimal
|
||||||
|
|
||||||
|
from freqtrade.exceptions import OperationalException
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseParameter(ABC):
|
||||||
|
"""
|
||||||
|
Defines a parameter that can be optimized by hyperopt.
|
||||||
|
"""
|
||||||
|
category: Optional[str]
|
||||||
|
default: Any
|
||||||
|
value: Any
|
||||||
|
in_space: bool = False
|
||||||
|
name: str
|
||||||
|
|
||||||
|
def __init__(self, *, default: Any, space: Optional[str] = None,
|
||||||
|
optimize: bool = True, load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable parameter.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
parameter field
|
||||||
|
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).
|
||||||
|
"""
|
||||||
|
if 'name' in kwargs:
|
||||||
|
raise OperationalException(
|
||||||
|
'Name is determined by parameter field name and can not be specified manually.')
|
||||||
|
self.category = space
|
||||||
|
self._space_params = kwargs
|
||||||
|
self.value = default
|
||||||
|
self.optimize = optimize
|
||||||
|
self.load = load
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'{self.__class__.__name__}({self.value})'
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_space(self, name: str) -> Union['Integer', 'Real', 'SKDecimal', 'Categorical']:
|
||||||
|
"""
|
||||||
|
Get-space - will be used by Hyperopt to get the hyperopt Space
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class NumericParameter(BaseParameter):
|
||||||
|
""" Internal parameter used for Numeric purposes """
|
||||||
|
float_or_int = Union[int, float]
|
||||||
|
default: float_or_int
|
||||||
|
value: float_or_int
|
||||||
|
|
||||||
|
def __init__(self, low: Union[float_or_int, Sequence[float_or_int]],
|
||||||
|
high: Optional[float_or_int] = None, *, default: float_or_int,
|
||||||
|
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable numeric parameter.
|
||||||
|
Cannot be instantiated, but provides the validation for other numeric parameters
|
||||||
|
:param low: Lower end (inclusive) of optimization space or [low, high].
|
||||||
|
:param high: Upper end (inclusive) of optimization space.
|
||||||
|
Must be none of entire range is passed first parameter.
|
||||||
|
:param default: A default value.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
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.*.
|
||||||
|
"""
|
||||||
|
if high is not None and isinstance(low, Sequence):
|
||||||
|
raise OperationalException(f'{self.__class__.__name__} space invalid.')
|
||||||
|
if high is None or isinstance(low, Sequence):
|
||||||
|
if not isinstance(low, Sequence) or len(low) != 2:
|
||||||
|
raise OperationalException(f'{self.__class__.__name__} space must be [low, high]')
|
||||||
|
self.low, self.high = low
|
||||||
|
else:
|
||||||
|
self.low = low
|
||||||
|
self.high = high
|
||||||
|
|
||||||
|
super().__init__(default=default, space=space, optimize=optimize,
|
||||||
|
load=load, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class IntParameter(NumericParameter):
|
||||||
|
default: int
|
||||||
|
value: int
|
||||||
|
|
||||||
|
def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int,
|
||||||
|
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable integer parameter.
|
||||||
|
:param low: Lower end (inclusive) of optimization space or [low, high].
|
||||||
|
:param high: Upper end (inclusive) of optimization space.
|
||||||
|
Must be none of entire range is passed first parameter.
|
||||||
|
:param default: A default value.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
||||||
|
load=load, **kwargs)
|
||||||
|
|
||||||
|
def get_space(self, name: str) -> 'Integer':
|
||||||
|
"""
|
||||||
|
Create skopt optimization space.
|
||||||
|
:param name: A name of parameter field.
|
||||||
|
"""
|
||||||
|
return Integer(low=self.low, high=self.high, name=name, **self._space_params)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def range(self):
|
||||||
|
"""
|
||||||
|
Get each value in this space as list.
|
||||||
|
Returns a List from low to high (inclusive) in Hyperopt mode.
|
||||||
|
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
|
||||||
|
calculating 100ds of indicators.
|
||||||
|
"""
|
||||||
|
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:
|
||||||
|
return range(self.value, self.value + 1)
|
||||||
|
|
||||||
|
|
||||||
|
class RealParameter(NumericParameter):
|
||||||
|
default: float
|
||||||
|
value: float
|
||||||
|
|
||||||
|
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
|
||||||
|
default: float, space: Optional[str] = None, optimize: bool = True,
|
||||||
|
load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable floating point parameter with unlimited precision.
|
||||||
|
:param low: Lower end (inclusive) of optimization space or [low, high].
|
||||||
|
:param high: Upper end (inclusive) of optimization space.
|
||||||
|
Must be none if entire range is passed first parameter.
|
||||||
|
:param default: A default value.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
||||||
|
load=load, **kwargs)
|
||||||
|
|
||||||
|
def get_space(self, name: str) -> 'Real':
|
||||||
|
"""
|
||||||
|
Create skopt optimization space.
|
||||||
|
:param name: A name of parameter field.
|
||||||
|
"""
|
||||||
|
return Real(low=self.low, high=self.high, name=name, **self._space_params)
|
||||||
|
|
||||||
|
|
||||||
|
class DecimalParameter(NumericParameter):
|
||||||
|
default: float
|
||||||
|
value: float
|
||||||
|
|
||||||
|
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
|
||||||
|
default: float, decimals: int = 3, space: Optional[str] = None,
|
||||||
|
optimize: bool = True, load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable decimal parameter with a limited precision.
|
||||||
|
:param low: Lower end (inclusive) of optimization space or [low, high].
|
||||||
|
:param high: Upper end (inclusive) of optimization space.
|
||||||
|
Must be none if entire range is passed first parameter.
|
||||||
|
:param default: A default value.
|
||||||
|
:param decimals: A number of decimals after floating point to be included in testing.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
self._decimals = decimals
|
||||||
|
default = round(default, self._decimals)
|
||||||
|
|
||||||
|
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
||||||
|
load=load, **kwargs)
|
||||||
|
|
||||||
|
def get_space(self, name: str) -> 'SKDecimal':
|
||||||
|
"""
|
||||||
|
Create skopt optimization space.
|
||||||
|
:param name: A name of parameter field.
|
||||||
|
"""
|
||||||
|
return SKDecimal(low=self.low, high=self.high, decimals=self._decimals, name=name,
|
||||||
|
**self._space_params)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def range(self):
|
||||||
|
"""
|
||||||
|
Get each value in this space as list.
|
||||||
|
Returns a List from low to high (inclusive) in Hyperopt mode.
|
||||||
|
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
|
||||||
|
calculating 100ds of indicators.
|
||||||
|
"""
|
||||||
|
if self.in_space and self.optimize:
|
||||||
|
low = int(self.low * pow(10, self._decimals))
|
||||||
|
high = int(self.high * pow(10, self._decimals)) + 1
|
||||||
|
return [round(n * pow(0.1, self._decimals), self._decimals) for n in range(low, high)]
|
||||||
|
else:
|
||||||
|
return [self.value]
|
||||||
|
|
||||||
|
|
||||||
|
class CategoricalParameter(BaseParameter):
|
||||||
|
default: Any
|
||||||
|
value: Any
|
||||||
|
opt_range: Sequence[Any]
|
||||||
|
|
||||||
|
def __init__(self, categories: Sequence[Any], *, default: Optional[Any] = None,
|
||||||
|
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable parameter.
|
||||||
|
:param categories: Optimization space, [a, b, ...].
|
||||||
|
:param default: A default value. If not specified, first item from specified space will be
|
||||||
|
used.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
parameter field
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
if len(categories) < 2:
|
||||||
|
raise OperationalException(
|
||||||
|
'CategoricalParameter space must be [a, b, ...] (at least two parameters)')
|
||||||
|
self.opt_range = categories
|
||||||
|
super().__init__(default=default, space=space, optimize=optimize,
|
||||||
|
load=load, **kwargs)
|
||||||
|
|
||||||
|
def get_space(self, name: str) -> 'Categorical':
|
||||||
|
"""
|
||||||
|
Create skopt optimization space.
|
||||||
|
:param name: A name of parameter field.
|
||||||
|
"""
|
||||||
|
return Categorical(self.opt_range, name=name, **self._space_params)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def range(self):
|
||||||
|
"""
|
||||||
|
Get each value in this space as list.
|
||||||
|
Returns a List of categories in Hyperopt mode.
|
||||||
|
Returns a List with 1 item (`value`) in "non-hyperopt" mode, to avoid
|
||||||
|
calculating 100ds of indicators.
|
||||||
|
"""
|
||||||
|
if self.in_space and self.optimize:
|
||||||
|
return self.opt_range
|
||||||
|
else:
|
||||||
|
return [self.value]
|
||||||
|
|
||||||
|
|
||||||
|
class BooleanParameter(CategoricalParameter):
|
||||||
|
|
||||||
|
def __init__(self, *, default: Optional[Any] = None,
|
||||||
|
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
||||||
|
"""
|
||||||
|
Initialize hyperopt-optimizable Boolean Parameter.
|
||||||
|
It's a shortcut to `CategoricalParameter([True, False])`.
|
||||||
|
:param default: A default value. If not specified, first item from specified space will be
|
||||||
|
used.
|
||||||
|
:param space: A parameter category. Can be 'buy' or 'sell'. This parameter is optional if
|
||||||
|
parameter field
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
categories = [True, False]
|
||||||
|
super().__init__(categories=categories, default=default, space=space, optimize=optimize,
|
||||||
|
load=load, **kwargs)
|
@ -17,7 +17,7 @@ from freqtrade.optimize.hyperopt_auto import HyperOptAuto
|
|||||||
from freqtrade.optimize.hyperopt_tools import HyperoptTools
|
from freqtrade.optimize.hyperopt_tools import HyperoptTools
|
||||||
from freqtrade.optimize.optimize_reports import generate_strategy_stats
|
from freqtrade.optimize.optimize_reports import generate_strategy_stats
|
||||||
from freqtrade.optimize.space import SKDecimal
|
from freqtrade.optimize.space import SKDecimal
|
||||||
from freqtrade.strategy.hyper import IntParameter
|
from freqtrade.strategy import IntParameter
|
||||||
from tests.conftest import (CURRENT_TEST_STRATEGY, get_args, log_has, log_has_re, patch_exchange,
|
from tests.conftest import (CURRENT_TEST_STRATEGY, get_args, log_has, log_has_re, patch_exchange,
|
||||||
patched_configuration_load_config_file)
|
patched_configuration_load_config_file)
|
||||||
|
|
||||||
|
@ -16,8 +16,8 @@ from freqtrade.exceptions import OperationalException, StrategyError
|
|||||||
from freqtrade.optimize.space import SKDecimal
|
from freqtrade.optimize.space import SKDecimal
|
||||||
from freqtrade.persistence import PairLocks, Trade
|
from freqtrade.persistence import PairLocks, Trade
|
||||||
from freqtrade.resolvers import StrategyResolver
|
from freqtrade.resolvers import StrategyResolver
|
||||||
from freqtrade.strategy.hyper import (BaseParameter, BooleanParameter, CategoricalParameter,
|
from freqtrade.strategy.parameters import (BaseParameter, BooleanParameter, CategoricalParameter,
|
||||||
DecimalParameter, IntParameter, RealParameter)
|
DecimalParameter, IntParameter, RealParameter)
|
||||||
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
|
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
|
||||||
from tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re
|
from tests.conftest import CURRENT_TEST_STRATEGY, TRADE_SIDES, log_has, log_has_re
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user