Simplify "no-space-configured" error handling by moving it to hyperopt_auto

This commit is contained in:
Matthias 2021-10-13 19:54:35 +02:00
parent df45f467c6
commit aed919a05f
5 changed files with 33 additions and 57 deletions

View File

@ -32,7 +32,7 @@ ARGS_HYPEROPT = ARGS_COMMON_OPTIMIZE + ["hyperopt", "hyperopt_path",
"print_colorized", "print_json", "hyperopt_jobs", "print_colorized", "print_json", "hyperopt_jobs",
"hyperopt_random_state", "hyperopt_min_trades", "hyperopt_random_state", "hyperopt_min_trades",
"hyperopt_loss", "disableparamexport", "hyperopt_loss", "disableparamexport",
"hyperopt_ignore_unparam_space"] "hyperopt_ignore_missing_space"]
ARGS_EDGE = ARGS_COMMON_OPTIMIZE + ["stoploss_range"] ARGS_EDGE = ARGS_COMMON_OPTIMIZE + ["stoploss_range"]

View File

@ -552,8 +552,8 @@ AVAILABLE_CLI_OPTIONS = {
help='Do not print epoch details header.', help='Do not print epoch details header.',
action='store_true', action='store_true',
), ),
"hyperopt_ignore_unparam_space": Arg( "hyperopt_ignore_missing_space": Arg(
"-u", "--ignore-unparameterized-spaces", "--ignore-missing-spaces", "--ignore-unparameterized-spaces",
help="Suppress errors for any requested Hyperopt spaces that do not contain any parameters", help="Suppress errors for any requested Hyperopt spaces that do not contain any parameters",
action="store_true", action="store_true",
), ),

View File

@ -369,8 +369,8 @@ class Configuration:
self._args_to_config(config, argname='hyperopt_show_no_header', self._args_to_config(config, argname='hyperopt_show_no_header',
logstring='Parameter --no-header detected: {}') logstring='Parameter --no-header detected: {}')
self._args_to_config(config, argname="hyperopt_ignore_unparam_space", self._args_to_config(config, argname="hyperopt_ignore_missing_space",
logstring="Paramter --ignore-unparameterized-spaces detected: {}") logstring="Paramter --ignore-missing-space detected: {}")
def _process_plot_options(self, config: Dict[str, Any]) -> None: def _process_plot_options(self, config: Dict[str, Any]) -> None:

View File

@ -237,63 +237,28 @@ class Hyperopt:
logger.debug("Hyperopt has 'protection' space") logger.debug("Hyperopt has 'protection' space")
# Enable Protections if protection space is selected. # Enable Protections if protection space is selected.
self.config['enable_protections'] = True self.config['enable_protections'] = True
try: self.protection_space = self.custom_hyperopt.protection_space()
self.protection_space = self.custom_hyperopt.protection_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
if HyperoptTools.has_space(self.config, 'buy'): if HyperoptTools.has_space(self.config, 'buy'):
logger.debug("Hyperopt has 'buy' space") logger.debug("Hyperopt has 'buy' space")
try: self.buy_space = self.custom_hyperopt.buy_indicator_space()
self.buy_space = self.custom_hyperopt.buy_indicator_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
if HyperoptTools.has_space(self.config, 'sell'): if HyperoptTools.has_space(self.config, 'sell'):
logger.debug("Hyperopt has 'sell' space") logger.debug("Hyperopt has 'sell' space")
try: self.sell_space = self.custom_hyperopt.sell_indicator_space()
self.sell_space = self.custom_hyperopt.sell_indicator_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
if HyperoptTools.has_space(self.config, 'roi'): if HyperoptTools.has_space(self.config, 'roi'):
logger.debug("Hyperopt has 'roi' space") logger.debug("Hyperopt has 'roi' space")
try: self.roi_space = self.custom_hyperopt.roi_space()
self.roi_space = self.custom_hyperopt.roi_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
if HyperoptTools.has_space(self.config, 'stoploss'): if HyperoptTools.has_space(self.config, 'stoploss'):
logger.debug("Hyperopt has 'stoploss' space") logger.debug("Hyperopt has 'stoploss' space")
try: self.stoploss_space = self.custom_hyperopt.stoploss_space()
self.stoploss_space = self.custom_hyperopt.stoploss_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
if HyperoptTools.has_space(self.config, 'trailing'): if HyperoptTools.has_space(self.config, 'trailing'):
logger.debug("Hyperopt has 'trailing' space") logger.debug("Hyperopt has 'trailing' space")
try: self.trailing_space = self.custom_hyperopt.trailing_space()
self.trailing_space = self.custom_hyperopt.trailing_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
self.dimensions = (self.buy_space + self.sell_space + self.protection_space self.dimensions = (self.buy_space + self.sell_space + self.protection_space
+ self.roi_space + self.stoploss_space + self.trailing_space) + self.roi_space + self.stoploss_space + self.trailing_space)

View File

@ -3,6 +3,7 @@ HyperOptAuto class.
This module implements a convenience auto-hyperopt class, which can be used together with strategies This module implements a convenience auto-hyperopt class, which can be used together with strategies
that implement IHyperStrategy interface. that implement IHyperStrategy interface.
""" """
import logging
from contextlib import suppress from contextlib import suppress
from typing import Callable, Dict, List from typing import Callable, Dict, List
@ -15,12 +16,19 @@ with suppress(ImportError):
from freqtrade.optimize.hyperopt_interface import EstimatorType, IHyperOpt from freqtrade.optimize.hyperopt_interface import EstimatorType, IHyperOpt
def _format_exception_message(space: str) -> str: logger = logging.getLogger(__name__)
raise OperationalException(
f"The '{space}' space is included into the hyperoptimization "
f"but no parameter for this space was not found in your Strategy. " def _format_exception_message(space: str, ignore_missing_space: bool) -> None:
f"Please make sure to have parameters for this space enabled for optimization " msg = (f"The '{space}' space is included into the hyperoptimization "
f"or remove the '{space}' space from hyperoptimization.") f"but no parameter for this space was not found in your Strategy. "
)
if ignore_missing_space:
logger.warning(msg + "This space will be ignored.")
else:
raise OperationalException(
msg + f"Please make sure to have parameters for this space enabled for optimization "
f"or remove the '{space}' space from hyperoptimization.")
class HyperOptAuto(IHyperOpt): class HyperOptAuto(IHyperOpt):
@ -48,13 +56,16 @@ class HyperOptAuto(IHyperOpt):
if attr.optimize: if attr.optimize:
yield attr.get_space(attr_name) yield attr.get_space(attr_name)
def _get_indicator_space(self, category): def _get_indicator_space(self, category) -> List:
# TODO: is this necessary, or can we call "generate_space" directly? # TODO: is this necessary, or can we call "generate_space" directly?
indicator_space = list(self._generate_indicator_space(category)) indicator_space = list(self._generate_indicator_space(category))
if len(indicator_space) > 0: if len(indicator_space) > 0:
return indicator_space return indicator_space
else: else:
_format_exception_message(category) _format_exception_message(
category,
self.config.get("hyperopt_ignore_missing_space", False))
return []
def buy_indicator_space(self) -> List['Dimension']: def buy_indicator_space(self) -> List['Dimension']:
return self._get_indicator_space('buy') return self._get_indicator_space('buy')