stable/freqtrade/resolvers/hyperopt_resolver.py

130 lines
4.9 KiB
Python
Raw Normal View History

# pragma pylint: disable=attribute-defined-outside-init
"""
This module load custom hyperopts
"""
import logging
2018-11-24 19:39:16 +00:00
from pathlib import Path
from typing import Optional, Dict
2019-07-12 20:45:49 +00:00
from freqtrade import OperationalException
2019-07-16 04:27:23 +00:00
from freqtrade.constants import DEFAULT_HYPEROPT, DEFAULT_HYPEROPT_LOSS
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
"""
__slots__ = ['hyperopt']
def __init__(self, config: Dict) -> None:
"""
Load the custom class from config parameter
2019-07-21 17:21:50 +00:00
:param config: configuration dictionary
"""
# Verify the hyperopt is in the configuration, otherwise fallback to the default hyperopt
hyperopt_name = config.get('hyperopt') or DEFAULT_HYPEROPT
2019-07-28 13:02:11 +00:00
self.hyperopt = self._load_hyperopt(hyperopt_name, config,
extra_dir=config.get('hyperopt_path'))
if not hasattr(self.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.")
if not hasattr(self.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.")
def _load_hyperopt(
2019-07-28 13:02:11 +00:00
self, hyperopt_name: str, config: Dict, extra_dir: Optional[str] = None) -> IHyperOpt:
"""
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
: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()
abs_paths = [
2019-07-28 13:02:11 +00:00
config['user_data_dir'].joinpath('hyperopts'),
current_path,
]
if extra_dir:
# Add extra hyperopt directory on top of search paths
2019-07-22 16:37:34 +00:00
abs_paths.insert(0, Path(extra_dir).resolve())
hyperopt = self._load_object(paths=abs_paths, object_type=IHyperOpt,
object_name=hyperopt_name, kwargs={'config': config})
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."
)
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-07-28 13:02:11 +00:00
def __init__(self, config: Dict = None) -> None:
2019-07-16 04:27:23 +00:00
"""
Load the custom class from config parameter
:param config: configuration dictionary or None
"""
config = config or {}
# Verify the hyperopt is in the configuration, otherwise fallback to the default hyperopt
hyperopt_name = config.get('hyperopt_loss') or DEFAULT_HYPEROPT_LOSS
self.hyperoptloss = self._load_hyperoptloss(
2019-07-28 13:02:11 +00:00
hyperopt_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(
f"Found hyperopt {hyperopt_name} does not implement `hyperopt_loss_function`.")
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()
abs_paths = [
2019-07-28 13:02:11 +00:00
config['user_data_dir'].joinpath('hyperopts'),
2019-07-16 04:27:23 +00:00
current_path,
]
if extra_dir:
# Add extra hyperopt directory on top of search paths
2019-07-22 16:37:34 +00:00
abs_paths.insert(0, Path(extra_dir).resolve())
2019-07-16 04:27:23 +00:00
hyperoptloss = self._load_object(paths=abs_paths, object_type=IHyperOptLoss,
object_name=hyper_loss_name)
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."
)