2018-11-24 19:14:08 +00:00
|
|
|
# pragma pylint: disable=attribute-defined-outside-init
|
|
|
|
|
|
|
|
"""
|
2019-11-13 08:38:06 +00:00
|
|
|
This module load custom hyperopt
|
2018-11-24 19:14:08 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2018-11-24 19:39:16 +00:00
|
|
|
from pathlib import Path
|
2018-11-24 19:14:08 +00:00
|
|
|
from typing import Optional, Dict
|
|
|
|
|
2019-07-12 20:45:49 +00:00
|
|
|
from freqtrade import OperationalException
|
2019-11-16 21:00:50 +00:00
|
|
|
from freqtrade.constants import DEFAULT_HYPEROPT_LOSS, USERPATH_HYPEROPTS
|
2018-11-24 19:14:08 +00:00
|
|
|
from freqtrade.optimize.hyperopt_interface import IHyperOpt
|
2019-07-16 04:27:23 +00:00
|
|
|
from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss
|
2018-11-24 19:14:08 +00:00
|
|
|
from freqtrade.resolvers import IResolver
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class HyperOptResolver(IResolver):
|
|
|
|
"""
|
|
|
|
This class contains all the logic to load custom hyperopt class
|
|
|
|
"""
|
|
|
|
|
2019-12-23 09:06:19 +00:00
|
|
|
@staticmethod
|
|
|
|
def load_hyperopt(config: Dict) -> IHyperOpt:
|
2018-11-24 19:14:08 +00:00
|
|
|
"""
|
2019-12-23 09:06:19 +00:00
|
|
|
Load the custom hyperopt class from config parameter
|
2019-07-21 17:21:50 +00:00
|
|
|
:param config: configuration dictionary
|
2018-11-24 19:14:08 +00:00
|
|
|
"""
|
2019-10-10 01:37:32 +00:00
|
|
|
if not config.get('hyperopt'):
|
2019-10-14 17:42:28 +00:00
|
|
|
raise OperationalException("No Hyperopt set. Please use `--hyperopt` to specify "
|
2019-10-10 01:37:32 +00:00
|
|
|
"the Hyperopt class to use.")
|
|
|
|
|
|
|
|
hyperopt_name = config['hyperopt']
|
2018-11-24 19:14:08 +00:00
|
|
|
|
2019-12-23 09:06:19 +00:00
|
|
|
hyperopt = HyperOptResolver._load_hyperopt(hyperopt_name, config,
|
|
|
|
extra_dir=config.get('hyperopt_path'))
|
2018-11-24 19:14:08 +00:00
|
|
|
|
2019-12-23 09:06:19 +00:00
|
|
|
if not hasattr(hyperopt, 'populate_indicators'):
|
2019-11-02 10:10:33 +00:00
|
|
|
logger.warning("Hyperopt class does not provide populate_indicators() method. "
|
|
|
|
"Using populate_indicators from the strategy.")
|
2019-12-23 09:06:19 +00:00
|
|
|
if not hasattr(hyperopt, 'populate_buy_trend'):
|
2019-09-18 19:57:17 +00:00
|
|
|
logger.warning("Hyperopt class does not provide populate_buy_trend() method. "
|
|
|
|
"Using populate_buy_trend from the strategy.")
|
2019-12-23 09:06:19 +00:00
|
|
|
if not hasattr(hyperopt, 'populate_sell_trend'):
|
2019-09-18 19:57:17 +00:00
|
|
|
logger.warning("Hyperopt class does not provide populate_sell_trend() method. "
|
|
|
|
"Using populate_sell_trend from the strategy.")
|
2019-12-23 09:06:19 +00:00
|
|
|
return hyperopt
|
2019-01-06 13:12:55 +00:00
|
|
|
|
2019-12-23 09:06:19 +00:00
|
|
|
@staticmethod
|
2018-11-24 19:14:08 +00:00
|
|
|
def _load_hyperopt(
|
2019-12-23 09:06:19 +00:00
|
|
|
hyperopt_name: str, config: Dict, extra_dir: Optional[str] = None) -> IHyperOpt:
|
2018-11-24 19:14:08 +00:00
|
|
|
"""
|
|
|
|
Search and loads the specified hyperopt.
|
|
|
|
:param hyperopt_name: name of the module to import
|
2019-07-28 13:02:11 +00:00
|
|
|
:param config: configuration dictionary
|
2018-11-24 19:14:08 +00:00
|
|
|
:param extra_dir: additional directory to search for the given hyperopt
|
|
|
|
:return: HyperOpt instance or None
|
|
|
|
"""
|
2018-11-24 19:39:16 +00:00
|
|
|
current_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
|
2018-11-24 19:14:08 +00:00
|
|
|
|
2019-12-23 09:06:19 +00:00
|
|
|
abs_paths = IResolver.build_search_paths(config, current_path=current_path,
|
|
|
|
user_subdir=USERPATH_HYPEROPTS,
|
|
|
|
extra_dir=extra_dir)
|
2018-11-24 19:14:08 +00:00
|
|
|
|
2019-12-23 09:06:19 +00:00
|
|
|
hyperopt = IResolver._load_object(paths=abs_paths, object_type=IHyperOpt,
|
|
|
|
object_name=hyperopt_name, kwargs={'config': config})
|
2019-07-21 13:03:12 +00:00
|
|
|
if hyperopt:
|
|
|
|
return hyperopt
|
2019-07-12 20:45:49 +00:00
|
|
|
raise OperationalException(
|
|
|
|
f"Impossible to load Hyperopt '{hyperopt_name}'. This class does not exist "
|
|
|
|
"or contains Python code errors."
|
2018-11-24 19:14:08 +00:00
|
|
|
)
|
2019-07-16 04:27:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HyperOptLossResolver(IResolver):
|
|
|
|
"""
|
|
|
|
This class contains all the logic to load custom hyperopt loss class
|
|
|
|
"""
|
|
|
|
__slots__ = ['hyperoptloss']
|
|
|
|
|
2019-10-13 00:41:25 +00:00
|
|
|
def __init__(self, config: Dict) -> None:
|
2019-07-16 04:27:23 +00:00
|
|
|
"""
|
|
|
|
Load the custom class from config parameter
|
2019-10-13 00:41:25 +00:00
|
|
|
:param config: configuration dictionary
|
2019-07-16 04:27:23 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-11 20:33:22 +00:00
|
|
|
# Verify the hyperopt_loss is in the configuration, otherwise fallback to the
|
|
|
|
# default hyperopt loss
|
|
|
|
hyperoptloss_name = config.get('hyperopt_loss') or DEFAULT_HYPEROPT_LOSS
|
2019-10-10 01:37:32 +00:00
|
|
|
|
2019-07-16 04:27:23 +00:00
|
|
|
self.hyperoptloss = self._load_hyperoptloss(
|
2019-10-10 01:37:32 +00:00
|
|
|
hyperoptloss_name, config, extra_dir=config.get('hyperopt_path'))
|
2019-07-16 04:27:23 +00:00
|
|
|
|
|
|
|
# Assign ticker_interval to be used in hyperopt
|
|
|
|
self.hyperoptloss.__class__.ticker_interval = str(config['ticker_interval'])
|
|
|
|
|
|
|
|
if not hasattr(self.hyperoptloss, 'hyperopt_loss_function'):
|
|
|
|
raise OperationalException(
|
2019-10-13 00:41:25 +00:00
|
|
|
f"Found HyperoptLoss class {hyperoptloss_name} does not "
|
|
|
|
"implement `hyperopt_loss_function`.")
|
2019-07-16 04:27:23 +00:00
|
|
|
|
|
|
|
def _load_hyperoptloss(
|
2019-07-28 13:02:11 +00:00
|
|
|
self, hyper_loss_name: str, config: Dict,
|
|
|
|
extra_dir: Optional[str] = None) -> IHyperOptLoss:
|
2019-07-16 04:27:23 +00:00
|
|
|
"""
|
|
|
|
Search and loads the specified hyperopt loss class.
|
|
|
|
:param hyper_loss_name: name of the module to import
|
2019-07-28 13:02:11 +00:00
|
|
|
:param config: configuration dictionary
|
2019-07-16 04:27:23 +00:00
|
|
|
:param extra_dir: additional directory to search for the given hyperopt
|
|
|
|
:return: HyperOptLoss instance or None
|
|
|
|
"""
|
|
|
|
current_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
|
|
|
|
|
2019-10-15 10:11:14 +00:00
|
|
|
abs_paths = self.build_search_paths(config, current_path=current_path,
|
2019-11-16 21:00:50 +00:00
|
|
|
user_subdir=USERPATH_HYPEROPTS, extra_dir=extra_dir)
|
2019-07-16 04:27:23 +00:00
|
|
|
|
2019-07-21 13:25:48 +00:00
|
|
|
hyperoptloss = self._load_object(paths=abs_paths, object_type=IHyperOptLoss,
|
|
|
|
object_name=hyper_loss_name)
|
2019-07-21 13:03:12 +00:00
|
|
|
if hyperoptloss:
|
|
|
|
return hyperoptloss
|
2019-07-16 04:27:23 +00:00
|
|
|
|
|
|
|
raise OperationalException(
|
|
|
|
f"Impossible to load HyperoptLoss '{hyper_loss_name}'. This class does not exist "
|
|
|
|
"or contains Python code errors."
|
|
|
|
)
|