change SKDecimal low/high to be rounded by decimals

This commit is contained in:
viotemp1
2025-05-07 21:57:56 +03:00
parent 2d2dc7f14a
commit 5d2f5ec12f
4 changed files with 6 additions and 48 deletions

View File

@@ -330,7 +330,6 @@ class Hyperopt:
self.config,
self.hyperopter.get_strategy_name(),
self.current_best_epoch,
self.hyperopter.o_dimensions,
)
HyperoptTools.show_epoch_details(

View File

@@ -7,7 +7,6 @@ from typing import Any
import numpy as np
import rapidjson
from optuna.distributions import BaseDistribution
from pandas import isna, json_normalize
from freqtrade.constants import FTHYPT_FILEVERSION, Config
@@ -15,7 +14,6 @@ from freqtrade.enums import HyperoptState
from freqtrade.exceptions import OperationalException
from freqtrade.misc import deep_merge_dicts, round_dict, safe_value_fallback2
from freqtrade.optimize.hyperopt_epoch_filters import hyperopt_filter_epochs
from freqtrade.optimize.space import SKDecimal, _adjust_discrete_uniform
logger = logging.getLogger(__name__)
@@ -67,22 +65,11 @@ class HyperoptTools:
params,
strategy_name: str,
filename: Path,
o_dimensions: dict[str, BaseDistribution] | None = None,
):
"""
Generate files
"""
final_params = deepcopy(params["params_not_optimized"])
if o_dimensions:
for key, val in params["params_details"].items():
if isinstance(val, dict):
for key1, val1 in val.items():
if isinstance(o_dimensions.get(key1), SKDecimal):
step = getattr(o_dimensions.get(key1), "step", None)
if step:
params["params_details"][key][key1] = _adjust_discrete_uniform(
val1, step
)
final_params = deep_merge_dicts(params["params_details"], final_params)
final_params = {
"strategy_name": strategy_name,
@@ -115,15 +102,12 @@ class HyperoptTools:
config: Config,
strategy_name: str,
params: dict,
o_dimensions: dict[str, BaseDistribution] | None = None,
):
if params.get(FTHYPT_FILEVERSION, 1) >= 2 and not config.get("disableparamexport", False):
# Export parameters ...
fn = HyperoptTools.get_strategy_filename(config, strategy_name)
if fn:
HyperoptTools.export_params(
params, strategy_name, fn.with_suffix(".json"), o_dimensions
)
HyperoptTools.export_params(params, strategy_name, fn.with_suffix(".json"))
else:
logger.warning("Strategy not found, not exporting parameter file.")

View File

@@ -1,4 +1,4 @@
from .decimalspace import SKDecimal, _adjust_discrete_uniform
from .decimalspace import SKDecimal
from .optunaspaces import (
DimensionProtocol,
ft_CategoricalDistribution,
@@ -13,4 +13,4 @@ Categorical = ft_CategoricalDistribution
Integer = ft_IntDistribution
Real = ft_FloatDistribution
__all__ = ["Categorical", "Dimension", "Integer", "Real", "SKDecimal", "_adjust_discrete_uniform"]
__all__ = ["Categorical", "Dimension", "Integer", "Real", "SKDecimal"]

View File

@@ -1,4 +1,4 @@
import decimal
from math import log10
from optuna.distributions import FloatDistribution
@@ -22,32 +22,7 @@ class SKDecimal(FloatDistribution):
self.name = name
super().__init__(
low=_adjust_discrete_uniform(low, self.step),
high=_adjust_discrete_uniform_high(low, high, self.step),
low=round(low, int(log10(1 / self.step))) if self.step < 1 else low,
high=round(high, int(log10(1 / self.step))) if self.step < 1 else high,
step=self.step,
)
def _adjust_discrete_uniform_high(low: float, high: float, step: float | None) -> float:
if step:
d_high = decimal.Decimal(str(high))
d_low = decimal.Decimal(str(low))
d_step = decimal.Decimal(str(step))
d_r = d_high - d_low
if d_r % d_step != decimal.Decimal("0"):
high = float((d_r // d_step) * d_step + d_low)
return high
def _adjust_discrete_uniform(val: float, step: float | None) -> float:
if step:
d_val = decimal.Decimal(str(val))
d_step = decimal.Decimal(str(step))
if d_val % d_step != decimal.Decimal("0"):
val = float((d_val // d_step) * d_step)
return val