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",
"hyperopt_random_state", "hyperopt_min_trades",
"hyperopt_loss", "disableparamexport",
"hyperopt_ignore_unparam_space"]
"hyperopt_ignore_missing_space"]
ARGS_EDGE = ARGS_COMMON_OPTIMIZE + ["stoploss_range"]

View File

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

View File

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

View File

@ -237,63 +237,28 @@ class Hyperopt:
logger.debug("Hyperopt has 'protection' space")
# Enable Protections if protection space is selected.
self.config['enable_protections'] = True
try:
self.protection_space = self.custom_hyperopt.protection_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
self.protection_space = self.custom_hyperopt.protection_space()
if HyperoptTools.has_space(self.config, 'buy'):
logger.debug("Hyperopt has 'buy' space")
try:
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
self.buy_space = self.custom_hyperopt.buy_indicator_space()
if HyperoptTools.has_space(self.config, 'sell'):
logger.debug("Hyperopt has 'sell' space")
try:
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
self.sell_space = self.custom_hyperopt.sell_indicator_space()
if HyperoptTools.has_space(self.config, 'roi'):
logger.debug("Hyperopt has 'roi' space")
try:
self.roi_space = self.custom_hyperopt.roi_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
self.roi_space = self.custom_hyperopt.roi_space()
if HyperoptTools.has_space(self.config, 'stoploss'):
logger.debug("Hyperopt has 'stoploss' space")
try:
self.stoploss_space = self.custom_hyperopt.stoploss_space()
except OperationalException as e:
if self.config["hyperopt_ignore_unparam_space"]:
logger.warning(e)
else:
raise
self.stoploss_space = self.custom_hyperopt.stoploss_space()
if HyperoptTools.has_space(self.config, 'trailing'):
logger.debug("Hyperopt has 'trailing' space")
try:
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.trailing_space = self.custom_hyperopt.trailing_space()
self.dimensions = (self.buy_space + self.sell_space + self.protection_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
that implement IHyperStrategy interface.
"""
import logging
from contextlib import suppress
from typing import Callable, Dict, List
@ -15,12 +16,19 @@ with suppress(ImportError):
from freqtrade.optimize.hyperopt_interface import EstimatorType, IHyperOpt
def _format_exception_message(space: str) -> str:
raise OperationalException(
f"The '{space}' space is included into the hyperoptimization "
f"but no parameter for this space was not found in your Strategy. "
f"Please make sure to have parameters for this space enabled for optimization "
f"or remove the '{space}' space from hyperoptimization.")
logger = logging.getLogger(__name__)
def _format_exception_message(space: str, ignore_missing_space: bool) -> None:
msg = (f"The '{space}' space is included into the 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):
@ -48,13 +56,16 @@ class HyperOptAuto(IHyperOpt):
if attr.optimize:
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?
indicator_space = list(self._generate_indicator_space(category))
if len(indicator_space) > 0:
return indicator_space
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']:
return self._get_indicator_space('buy')