stable/freqtrade/resolvers/hyperopt_resolver.py

89 lines
3.4 KiB
Python
Raw Normal View History

# pragma pylint: disable=attribute-defined-outside-init
"""
2019-11-13 08:38:06 +00:00
This module load custom hyperopt
"""
import logging
2018-11-24 19:39:16 +00:00
from pathlib import Path
2019-12-24 12:54:46 +00:00
from typing import Dict
from freqtrade.constants import DEFAULT_HYPEROPT_LOSS, USERPATH_HYPEROPTS
from freqtrade.exceptions import OperationalException
from freqtrade.optimize.hyperopt_interface import IHyperOpt
2019-07-16 04:27:23 +00:00
from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss
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-24 12:34:37 +00:00
object_type = IHyperOpt
2019-12-24 12:54:46 +00:00
object_type_str = "Hyperopt"
user_subdir = USERPATH_HYPEROPTS
initial_search_path = None
@staticmethod
def load_hyperopt(config: Dict) -> IHyperOpt:
"""
Load the custom hyperopt class from config parameter
2019-07-21 17:21:50 +00:00
:param config: configuration dictionary
"""
if not config.get('hyperopt'):
2019-10-14 17:42:28 +00:00
raise OperationalException("No Hyperopt set. Please use `--hyperopt` to specify "
"the Hyperopt class to use.")
hyperopt_name = config['hyperopt']
2019-12-24 12:54:46 +00:00
hyperopt = HyperOptResolver.load_object(hyperopt_name, config,
kwargs={'config': config},
extra_dir=config.get('hyperopt_path'))
if not hasattr(hyperopt, 'populate_indicators'):
logger.info("Hyperopt class does not provide populate_indicators() method. "
"Using populate_indicators from the strategy.")
if not hasattr(hyperopt, 'populate_buy_trend'):
logger.info("Hyperopt class does not provide populate_buy_trend() method. "
"Using populate_buy_trend from the strategy.")
if not hasattr(hyperopt, 'populate_sell_trend'):
logger.info("Hyperopt class does not provide populate_sell_trend() method. "
"Using populate_sell_trend from the strategy.")
return hyperopt
2019-07-16 04:27:23 +00:00
class HyperOptLossResolver(IResolver):
"""
This class contains all the logic to load custom hyperopt loss class
"""
2019-12-24 12:34:37 +00:00
object_type = IHyperOptLoss
2019-12-24 12:54:46 +00:00
object_type_str = "HyperoptLoss"
user_subdir = USERPATH_HYPEROPTS
initial_search_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
2019-07-16 04:27:23 +00:00
@staticmethod
def load_hyperoptloss(config: Dict) -> IHyperOptLoss:
2019-07-16 04:27:23 +00:00
"""
Load the custom class from config parameter
:param config: configuration dictionary
2019-07-16 04:27:23 +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-12-24 12:54:46 +00:00
hyperoptloss = HyperOptLossResolver.load_object(hyperoptloss_name,
config, kwargs={},
extra_dir=config.get('hyperopt_path'))
2019-07-16 04:27:23 +00:00
# Assign timeframe to be used in hyperopt
hyperoptloss.__class__.ticker_interval = str(config['timeframe'])
hyperoptloss.__class__.timeframe = str(config['timeframe'])
2019-07-16 04:27:23 +00:00
if not hasattr(hyperoptloss, 'hyperopt_loss_function'):
2019-07-16 04:27:23 +00:00
raise OperationalException(
f"Found HyperoptLoss class {hyperoptloss_name} does not "
"implement `hyperopt_loss_function`.")
return hyperoptloss