change for SKDecimal and othercomments
This commit is contained in:
@@ -14,12 +14,12 @@ from freqtrade.constants import Config
|
|||||||
from freqtrade.exchange import timeframe_to_minutes
|
from freqtrade.exchange import timeframe_to_minutes
|
||||||
from freqtrade.misc import round_dict
|
from freqtrade.misc import round_dict
|
||||||
from freqtrade.optimize.space import SKDecimal
|
from freqtrade.optimize.space import SKDecimal
|
||||||
from freqtrade.strategy import IStrategy
|
from freqtrade.optimize.space.optunaspaces import (
|
||||||
from freqtrade.strategy.parameters import (
|
|
||||||
DimensionProtocol,
|
DimensionProtocol,
|
||||||
ft_CategoricalDistribution,
|
ft_CategoricalDistribution,
|
||||||
ft_IntDistribution,
|
ft_IntDistribution,
|
||||||
)
|
)
|
||||||
|
from freqtrade.strategy import IStrategy
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -137,9 +137,9 @@ class IHyperOpt(ABC):
|
|||||||
logger.info(f"Max roi table: {round_dict(self.generate_roi_table(p), 3)}")
|
logger.info(f"Max roi table: {round_dict(self.generate_roi_table(p), 3)}")
|
||||||
|
|
||||||
return [
|
return [
|
||||||
ft_IntDistribution("roi_t1", roi_limits["roi_t1_min"], roi_limits["roi_t1_max"]),
|
ft_IntDistribution(roi_limits["roi_t1_min"], roi_limits["roi_t1_max"], "roi_t1"),
|
||||||
ft_IntDistribution("roi_t2", roi_limits["roi_t2_min"], roi_limits["roi_t2_max"]),
|
ft_IntDistribution(roi_limits["roi_t2_min"], roi_limits["roi_t2_max"], "roi_t2"),
|
||||||
ft_IntDistribution("roi_t3", roi_limits["roi_t3_min"], roi_limits["roi_t3_max"]),
|
ft_IntDistribution(roi_limits["roi_t3_min"], roi_limits["roi_t3_max"], "roi_t3"),
|
||||||
SKDecimal(
|
SKDecimal(
|
||||||
roi_limits["roi_p1_min"], roi_limits["roi_p1_max"], decimals=3, name="roi_p1"
|
roi_limits["roi_p1_min"], roi_limits["roi_p1_max"], decimals=3, name="roi_p1"
|
||||||
),
|
),
|
||||||
@@ -188,7 +188,7 @@ class IHyperOpt(ABC):
|
|||||||
# This parameter is included into the hyperspace dimensions rather than assigning
|
# This parameter is included into the hyperspace dimensions rather than assigning
|
||||||
# it explicitly in the code in order to have it printed in the results along with
|
# it explicitly in the code in order to have it printed in the results along with
|
||||||
# other 'trailing' hyperspace parameters.
|
# other 'trailing' hyperspace parameters.
|
||||||
ft_CategoricalDistribution("trailing_stop", [True]),
|
ft_CategoricalDistribution([True], "trailing_stop"),
|
||||||
SKDecimal(0.01, 0.35, decimals=3, name="trailing_stop_positive"),
|
SKDecimal(0.01, 0.35, decimals=3, name="trailing_stop_positive"),
|
||||||
# 'trailing_stop_positive_offset' should be greater than 'trailing_stop_positive',
|
# 'trailing_stop_positive_offset' should be greater than 'trailing_stop_positive',
|
||||||
# so this intermediate parameter is used as the value of the difference between
|
# so this intermediate parameter is used as the value of the difference between
|
||||||
@@ -196,7 +196,7 @@ class IHyperOpt(ABC):
|
|||||||
# generate_trailing_params() method.
|
# generate_trailing_params() method.
|
||||||
# This is similar to the hyperspace dimensions used for constructing the ROI tables.
|
# This is similar to the hyperspace dimensions used for constructing the ROI tables.
|
||||||
SKDecimal(0.001, 0.1, decimals=3, name="trailing_stop_positive_offset_p1"),
|
SKDecimal(0.001, 0.1, decimals=3, name="trailing_stop_positive_offset_p1"),
|
||||||
ft_CategoricalDistribution("trailing_only_offset_is_reached", [True, False]),
|
ft_CategoricalDistribution([True, False], "trailing_only_offset_is_reached"),
|
||||||
]
|
]
|
||||||
|
|
||||||
def max_open_trades_space(self) -> list[DimensionProtocol]:
|
def max_open_trades_space(self) -> list[DimensionProtocol]:
|
||||||
@@ -205,7 +205,7 @@ class IHyperOpt(ABC):
|
|||||||
|
|
||||||
You may override it in your custom Hyperopt class.
|
You may override it in your custom Hyperopt class.
|
||||||
"""
|
"""
|
||||||
return [ft_IntDistribution("max_open_trades", -1, 10)]
|
return [ft_IntDistribution(-1, 10, "max_open_trades")]
|
||||||
|
|
||||||
# This is needed for proper unpickling the class attribute timeframe
|
# This is needed for proper unpickling the class attribute timeframe
|
||||||
# which is set to the actual value by the resolver.
|
# which is set to the actual value by the resolver.
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ from freqtrade.optimize.hyperopt_loss.hyperopt_loss_interface import IHyperOptLo
|
|||||||
from freqtrade.optimize.hyperopt_tools import HyperoptStateContainer, HyperoptTools
|
from freqtrade.optimize.hyperopt_tools import HyperoptStateContainer, HyperoptTools
|
||||||
from freqtrade.optimize.optimize_reports import generate_strategy_stats
|
from freqtrade.optimize.optimize_reports import generate_strategy_stats
|
||||||
from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver
|
from freqtrade.resolvers.hyperopt_resolver import HyperOptLossResolver
|
||||||
from freqtrade.strategy.parameters import DimensionProtocol
|
|
||||||
from freqtrade.util.dry_run_wallet import get_dry_run_wallet
|
from freqtrade.util.dry_run_wallet import get_dry_run_wallet
|
||||||
|
|
||||||
|
|
||||||
@@ -43,7 +42,8 @@ with warnings.catch_warnings():
|
|||||||
import optuna
|
import optuna
|
||||||
|
|
||||||
from freqtrade.optimize.space.decimalspace import SKDecimal
|
from freqtrade.optimize.space.decimalspace import SKDecimal
|
||||||
from freqtrade.strategy.parameters import (
|
from freqtrade.optimize.space.optunaspaces import (
|
||||||
|
DimensionProtocol,
|
||||||
ft_CategoricalDistribution,
|
ft_CategoricalDistribution,
|
||||||
ft_FloatDistribution,
|
ft_FloatDistribution,
|
||||||
ft_IntDistribution,
|
ft_IntDistribution,
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
from skopt.space import Categorical, Dimension, Integer, Real # noqa: F401
|
# from skopt.space import Categorical, Dimension, Integer, Real # noqa: F401
|
||||||
|
|
||||||
from .decimalspace import SKDecimal # noqa: F401
|
from .decimalspace import SKDecimal # noqa: F401
|
||||||
|
from .optunaspaces import DimensionProtocol # noqa: F401
|
||||||
|
from .optunaspaces import ft_CategoricalDistribution as Categorical # noqa: F401
|
||||||
|
from .optunaspaces import ft_FloatDistribution as Real # noqa: F401
|
||||||
|
from .optunaspaces import ft_IntDistribution as Integer # noqa: F401
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from optuna.distributions import FloatDistribution
|
from optuna.distributions import FloatDistribution
|
||||||
|
|
||||||
|
|
||||||
@@ -7,7 +9,7 @@ class SKDecimal(FloatDistribution):
|
|||||||
low: float,
|
low: float,
|
||||||
high: float,
|
high: float,
|
||||||
step: float | None = None,
|
step: float | None = None,
|
||||||
decimals: int | None = 3,
|
decimals: int = 4,
|
||||||
name=None,
|
name=None,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
@@ -19,6 +21,7 @@ class SKDecimal(FloatDistribution):
|
|||||||
self.step = step or 1 / 10**decimals
|
self.step = step or 1 / 10**decimals
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
# with localcontext(prec=max(decimals, 4)) as ctx:
|
||||||
super().__init__(
|
super().__init__(
|
||||||
low=low,
|
low=low,
|
||||||
high=high,
|
high=high,
|
||||||
|
|||||||
@@ -7,16 +7,19 @@ import logging
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from typing import Any, Protocol, Union
|
from typing import Any, Union
|
||||||
|
|
||||||
from freqtrade.enums import HyperoptState
|
from freqtrade.enums import HyperoptState
|
||||||
from freqtrade.optimize.hyperopt_tools import HyperoptStateContainer
|
from freqtrade.optimize.hyperopt_tools import HyperoptStateContainer
|
||||||
|
|
||||||
|
|
||||||
with suppress(ImportError):
|
with suppress(ImportError):
|
||||||
from optuna.distributions import CategoricalDistribution, FloatDistribution, IntDistribution
|
|
||||||
|
|
||||||
from freqtrade.optimize.space import SKDecimal
|
from freqtrade.optimize.space import SKDecimal
|
||||||
|
from freqtrade.optimize.space.optunaspaces import (
|
||||||
|
ft_CategoricalDistribution,
|
||||||
|
ft_FloatDistribution,
|
||||||
|
ft_IntDistribution,
|
||||||
|
)
|
||||||
|
|
||||||
from freqtrade.exceptions import OperationalException
|
from freqtrade.exceptions import OperationalException
|
||||||
|
|
||||||
@@ -24,43 +27,43 @@ from freqtrade.exceptions import OperationalException
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class DimensionProtocol(Protocol):
|
# class DimensionProtocol(Protocol):
|
||||||
name: str
|
# name: str
|
||||||
|
|
||||||
|
|
||||||
class ft_CategoricalDistribution(CategoricalDistribution):
|
# class ft_CategoricalDistribution(CategoricalDistribution):
|
||||||
def __init__(
|
# def __init__(
|
||||||
self,
|
# self,
|
||||||
name: str,
|
# name: str,
|
||||||
categories: Sequence[Any],
|
# categories: Sequence[Any],
|
||||||
**kwargs,
|
# **kwargs,
|
||||||
):
|
# ):
|
||||||
self.name = name
|
# self.name = name
|
||||||
return super().__init__(categories)
|
# return super().__init__(categories)
|
||||||
|
|
||||||
|
|
||||||
class ft_IntDistribution(IntDistribution):
|
# class ft_IntDistribution(IntDistribution):
|
||||||
def __init__(
|
# def __init__(
|
||||||
self,
|
# self,
|
||||||
name: str,
|
# name: str,
|
||||||
low: int | float,
|
# low: int | float,
|
||||||
high: int | float,
|
# high: int | float,
|
||||||
**kwargs,
|
# **kwargs,
|
||||||
):
|
# ):
|
||||||
self.name = name
|
# self.name = name
|
||||||
return super().__init__(int(low), int(high), **kwargs)
|
# return super().__init__(int(low), int(high), **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class ft_FloatDistribution(FloatDistribution):
|
# class ft_FloatDistribution(FloatDistribution):
|
||||||
def __init__(
|
# def __init__(
|
||||||
self,
|
# self,
|
||||||
name: str,
|
# name: str,
|
||||||
low: float,
|
# low: float,
|
||||||
high: float,
|
# high: float,
|
||||||
**kwargs,
|
# **kwargs,
|
||||||
):
|
# ):
|
||||||
self.name = name
|
# self.name = name
|
||||||
return super().__init__(low, high, **kwargs)
|
# return super().__init__(low, high, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class BaseParameter(ABC):
|
class BaseParameter(ABC):
|
||||||
@@ -207,7 +210,7 @@ class IntParameter(NumericParameter):
|
|||||||
Create optuna distribution space.
|
Create optuna distribution space.
|
||||||
:param name: A name of parameter field.
|
:param name: A name of parameter field.
|
||||||
"""
|
"""
|
||||||
return ft_IntDistribution(name, self.low, self.high, **self._space_params)
|
return ft_IntDistribution(self.low, self.high, name, **self._space_params)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def range(self):
|
def range(self):
|
||||||
@@ -260,7 +263,7 @@ class RealParameter(NumericParameter):
|
|||||||
Create optimization space.
|
Create optimization space.
|
||||||
:param name: A name of parameter field.
|
:param name: A name of parameter field.
|
||||||
"""
|
"""
|
||||||
return ft_FloatDistribution(name, self.low, self.high, **self._space_params)
|
return ft_FloatDistribution(self.low, self.high, name, **self._space_params)
|
||||||
|
|
||||||
|
|
||||||
class DecimalParameter(NumericParameter):
|
class DecimalParameter(NumericParameter):
|
||||||
@@ -363,7 +366,7 @@ class CategoricalParameter(BaseParameter):
|
|||||||
Create optuna distribution space.
|
Create optuna distribution space.
|
||||||
:param name: A name of parameter field.
|
:param name: A name of parameter field.
|
||||||
"""
|
"""
|
||||||
return ft_CategoricalDistribution(name, self.opt_range)
|
return ft_CategoricalDistribution(self.opt_range, name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def range(self):
|
def range(self):
|
||||||
|
|||||||
@@ -17,10 +17,8 @@ from freqtrade.optimize.hyperopt.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.optimize.space.optunaspaces import ft_IntDistribution
|
||||||
from freqtrade.strategy import IntParameter
|
from freqtrade.strategy import IntParameter
|
||||||
|
|
||||||
# from skopt.space import Integer
|
|
||||||
from freqtrade.strategy.parameters import ft_IntDistribution
|
|
||||||
from freqtrade.util import dt_utc
|
from freqtrade.util import dt_utc
|
||||||
from tests.conftest import (
|
from tests.conftest import (
|
||||||
CURRENT_TEST_STRATEGY,
|
CURRENT_TEST_STRATEGY,
|
||||||
@@ -1305,7 +1303,7 @@ def test_max_open_trades_consistency(mocker, hyperopt_conf, tmp_path, fee) -> No
|
|||||||
assert isinstance(hyperopt.hyperopter.custom_hyperopt, HyperOptAuto)
|
assert isinstance(hyperopt.hyperopter.custom_hyperopt, HyperOptAuto)
|
||||||
|
|
||||||
hyperopt.hyperopter.custom_hyperopt.max_open_trades_space = lambda: [
|
hyperopt.hyperopter.custom_hyperopt.max_open_trades_space = lambda: [
|
||||||
ft_IntDistribution("max_open_trades", 1, 10)
|
ft_IntDistribution(1, 10, "max_open_trades")
|
||||||
]
|
]
|
||||||
|
|
||||||
first_time_evaluated = False
|
first_time_evaluated = False
|
||||||
|
|||||||
Reference in New Issue
Block a user