mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-12-02 18:13:04 +00:00
ruff format: freqtrade/strategies
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
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
|
||||
@@ -26,14 +27,22 @@ 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):
|
||||
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
|
||||
@@ -43,9 +52,10 @@ class BaseParameter(ABC):
|
||||
:param load: Load parameter value from {space}_params.
|
||||
:param kwargs: Extra parameters to skopt.space.(Integer|Real|Categorical).
|
||||
"""
|
||||
if 'name' in kwargs:
|
||||
if "name" in kwargs:
|
||||
raise OperationalException(
|
||||
'Name is determined by parameter field name and can not be specified manually.')
|
||||
"Name is determined by parameter field name and can not be specified manually."
|
||||
)
|
||||
self.category = space
|
||||
self._space_params = kwargs
|
||||
self.value = default
|
||||
@@ -53,10 +63,10 @@ class BaseParameter(ABC):
|
||||
self.load = load
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.value})'
|
||||
return f"{self.__class__.__name__}({self.value})"
|
||||
|
||||
@abstractmethod
|
||||
def get_space(self, name: str) -> Union['Integer', 'Real', 'SKDecimal', 'Categorical']:
|
||||
def get_space(self, name: str) -> Union["Integer", "Real", "SKDecimal", "Categorical"]:
|
||||
"""
|
||||
Get-space - will be used by Hyperopt to get the hyperopt Space
|
||||
"""
|
||||
@@ -70,14 +80,23 @@ class BaseParameter(ABC):
|
||||
|
||||
|
||||
class NumericParameter(BaseParameter):
|
||||
""" Internal parameter used for Numeric purposes """
|
||||
"""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):
|
||||
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
|
||||
@@ -92,17 +111,16 @@ class NumericParameter(BaseParameter):
|
||||
:param kwargs: Extra parameters to skopt.space.*.
|
||||
"""
|
||||
if high is not None and isinstance(low, Sequence):
|
||||
raise OperationalException(f'{self.__class__.__name__} space invalid.')
|
||||
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]')
|
||||
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)
|
||||
super().__init__(default=default, space=space, optimize=optimize, load=load, **kwargs)
|
||||
|
||||
|
||||
class IntParameter(NumericParameter):
|
||||
@@ -111,8 +129,17 @@ class IntParameter(NumericParameter):
|
||||
low: int
|
||||
high: 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):
|
||||
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].
|
||||
@@ -126,10 +153,11 @@ class IntParameter(NumericParameter):
|
||||
:param kwargs: Extra parameters to skopt.space.Integer.
|
||||
"""
|
||||
|
||||
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
||||
load=load, **kwargs)
|
||||
super().__init__(
|
||||
low=low, high=high, default=default, space=space, optimize=optimize, load=load, **kwargs
|
||||
)
|
||||
|
||||
def get_space(self, name: str) -> 'Integer':
|
||||
def get_space(self, name: str) -> "Integer":
|
||||
"""
|
||||
Create skopt optimization space.
|
||||
:param name: A name of parameter field.
|
||||
@@ -155,9 +183,17 @@ 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):
|
||||
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].
|
||||
@@ -170,10 +206,11 @@ class RealParameter(NumericParameter):
|
||||
: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)
|
||||
super().__init__(
|
||||
low=low, high=high, default=default, space=space, optimize=optimize, load=load, **kwargs
|
||||
)
|
||||
|
||||
def get_space(self, name: str) -> 'Real':
|
||||
def get_space(self, name: str) -> "Real":
|
||||
"""
|
||||
Create skopt optimization space.
|
||||
:param name: A name of parameter field.
|
||||
@@ -185,9 +222,18 @@ 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):
|
||||
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].
|
||||
@@ -204,16 +250,18 @@ class DecimalParameter(NumericParameter):
|
||||
self._decimals = decimals
|
||||
default = round(default, self._decimals)
|
||||
|
||||
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
|
||||
load=load, **kwargs)
|
||||
super().__init__(
|
||||
low=low, high=high, default=default, space=space, optimize=optimize, load=load, **kwargs
|
||||
)
|
||||
|
||||
def get_space(self, name: str) -> 'SKDecimal':
|
||||
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)
|
||||
return SKDecimal(
|
||||
low=self.low, high=self.high, decimals=self._decimals, name=name, **self._space_params
|
||||
)
|
||||
|
||||
@property
|
||||
def range(self):
|
||||
@@ -236,8 +284,16 @@ class CategoricalParameter(BaseParameter):
|
||||
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):
|
||||
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, ...].
|
||||
@@ -252,12 +308,12 @@ class CategoricalParameter(BaseParameter):
|
||||
"""
|
||||
if len(categories) < 2:
|
||||
raise OperationalException(
|
||||
'CategoricalParameter space must be [a, b, ...] (at least two parameters)')
|
||||
"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)
|
||||
super().__init__(default=default, space=space, optimize=optimize, load=load, **kwargs)
|
||||
|
||||
def get_space(self, name: str) -> 'Categorical':
|
||||
def get_space(self, name: str) -> "Categorical":
|
||||
"""
|
||||
Create skopt optimization space.
|
||||
:param name: A name of parameter field.
|
||||
@@ -279,9 +335,15 @@ class CategoricalParameter(BaseParameter):
|
||||
|
||||
|
||||
class BooleanParameter(CategoricalParameter):
|
||||
|
||||
def __init__(self, *, default: Optional[Any] = None,
|
||||
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
|
||||
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])`.
|
||||
@@ -296,5 +358,11 @@ class BooleanParameter(CategoricalParameter):
|
||||
"""
|
||||
|
||||
categories = [True, False]
|
||||
super().__init__(categories=categories, default=default, space=space, optimize=optimize,
|
||||
load=load, **kwargs)
|
||||
super().__init__(
|
||||
categories=categories,
|
||||
default=default,
|
||||
space=space,
|
||||
optimize=optimize,
|
||||
load=load,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user