refactor: further refactor of hyperopt spaces

This commit is contained in:
Matthias
2025-11-01 15:38:50 +01:00
parent 4cac68d774
commit e34e84c5c6

View File

@@ -71,11 +71,6 @@ class HyperOptimizer:
def __init__(self, config: Config, data_pickle_file: Path) -> None: def __init__(self, config: Config, data_pickle_file: Path) -> None:
self.spaces: dict[str, list[DimensionProtocol]] = {} self.spaces: dict[str, list[DimensionProtocol]] = {}
self.protection_space: list[DimensionProtocol] = []
self.roi_space: list[DimensionProtocol] = []
self.stoploss_space: list[DimensionProtocol] = []
self.trailing_space: list[DimensionProtocol] = []
self.max_open_trades_space: list[DimensionProtocol] = []
self.dimensions: list[DimensionProtocol] = [] self.dimensions: list[DimensionProtocol] = []
self.o_dimensions: dict = {} self.o_dimensions: dict = {}
@@ -166,37 +161,39 @@ class HyperOptimizer:
""" """
result: dict = {} result: dict = {}
for indicator in self.spaces.keys(): for space in self.spaces.keys():
result[indicator] = round_dict( if space == "protection":
{p.name: params.get(p.name) for p in self.spaces.get(indicator, [])}, 13 result["protection"] = round_dict(
) {p.name: params.get(p.name) for p in self.spaces[space]}, 13
if HyperoptTools.has_space(self.config, "protection"): )
result["protection"] = round_dict( elif space == "roi":
{p.name: params.get(p.name) for p in self.protection_space}, 13 result["roi"] = round_dict(
) {str(k): v for k, v in self.custom_hyperopt.generate_roi_table(params).items()},
if HyperoptTools.has_space(self.config, "roi"): 13,
result["roi"] = round_dict( )
{str(k): v for k, v in self.custom_hyperopt.generate_roi_table(params).items()}, 13 elif space == "stoploss":
) result["stoploss"] = round_dict(
if HyperoptTools.has_space(self.config, "stoploss"): {p.name: params.get(p.name) for p in self.spaces[space]}, 13
result["stoploss"] = round_dict( )
{p.name: params.get(p.name) for p in self.stoploss_space}, 13 elif space == "trailing":
) result["trailing"] = round_dict(
if HyperoptTools.has_space(self.config, "trailing"): self.custom_hyperopt.generate_trailing_params(params), 13
result["trailing"] = round_dict( )
self.custom_hyperopt.generate_trailing_params(params), 13 elif space == "trades":
) result["max_open_trades"] = round_dict(
if HyperoptTools.has_space(self.config, "trades"): {
result["max_open_trades"] = round_dict( "max_open_trades": (
{ self.backtesting.strategy.max_open_trades
"max_open_trades": ( if self.backtesting.strategy.max_open_trades != float("inf")
self.backtesting.strategy.max_open_trades else -1
if self.backtesting.strategy.max_open_trades != float("inf") )
else -1 },
) 13,
}, )
13, else:
) result[space] = round_dict(
{p.name: params.get(p.name) for p in self.spaces[space]}, 13
)
return result return result
@@ -225,43 +222,28 @@ class HyperOptimizer:
""" """
Assign the dimensions in the hyperoptimization space. Assign the dimensions in the hyperoptimization space.
""" """
if HyperoptTools.has_space(self.config, "protection"): for space in ["buy", "sell", "protection", "roi", "stoploss", "trailing", "trades"]:
# Protections can only be optimized when using the Parameter interface if not HyperoptTools.has_space(self.config, space):
logger.debug("Hyperopt has 'protection' space") continue
# Enable Protections if protection space is selected. logger.debug(f"Hyperopt has '{space}' space")
self.config["enable_protections"] = True if space == "protection":
self.backtesting.enable_protections = True # Protections can only be optimized when using the Parameter interface
self.protection_space = self.custom_hyperopt.protection_space() # Enable Protections if protection space is selected.
self.config["enable_protections"] = True
self.backtesting.enable_protections = True
self.spaces[space] = self.custom_hyperopt.protection_space()
elif space == "roi":
self.spaces[space] = self.custom_hyperopt.roi_space()
elif space == "stoploss":
self.spaces[space] = self.custom_hyperopt.stoploss_space()
elif space == "trailing":
self.spaces[space] = self.custom_hyperopt.trailing_space()
elif space == "trades":
self.spaces[space] = self.custom_hyperopt.max_open_trades_space()
else:
self.spaces[space] = self.custom_hyperopt.get_indicator_space(space)
for indicator in ["buy", "sell"]: self.dimensions = [s for space in self.spaces.values() for s in space]
if HyperoptTools.has_space(self.config, indicator):
logger.debug(f"Hyperopt has '{indicator}' space")
self.spaces[indicator] = self.custom_hyperopt.get_indicator_space(indicator)
if HyperoptTools.has_space(self.config, "roi"):
logger.debug("Hyperopt has 'roi' space")
self.roi_space = self.custom_hyperopt.roi_space()
if HyperoptTools.has_space(self.config, "stoploss"):
logger.debug("Hyperopt has 'stoploss' space")
self.stoploss_space = self.custom_hyperopt.stoploss_space()
if HyperoptTools.has_space(self.config, "trailing"):
logger.debug("Hyperopt has 'trailing' space")
self.trailing_space = self.custom_hyperopt.trailing_space()
if HyperoptTools.has_space(self.config, "trades"):
logger.debug("Hyperopt has 'trades' space")
self.max_open_trades_space = self.custom_hyperopt.max_open_trades_space()
self.dimensions = (
[s for space in self.spaces.values() for s in space]
+ self.protection_space
+ self.roi_space
+ self.stoploss_space
+ self.trailing_space
+ self.max_open_trades_space
)
def assign_params(self, params_dict: dict[str, Any], category: str) -> None: def assign_params(self, params_dict: dict[str, Any], category: str) -> None:
""" """