2018-11-24 19:14:08 +00:00
|
|
|
# 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
|
2018-11-24 19:14:08 +00:00
|
|
|
from typing import Optional, Dict
|
|
|
|
|
|
|
|
from freqtrade.constants import DEFAULT_HYPEROPT
|
|
|
|
from freqtrade.optimize.hyperopt_interface import IHyperOpt
|
|
|
|
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: Optional[Dict] = None) -> None:
|
|
|
|
"""
|
|
|
|
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') or DEFAULT_HYPEROPT
|
|
|
|
self.hyperopt = self._load_hyperopt(hyperopt_name, extra_dir=config.get('hyperopt_path'))
|
|
|
|
|
2019-06-21 05:06:54 +00:00
|
|
|
# Assign ticker_interval to be used in hyperopt
|
2019-06-21 05:10:30 +00:00
|
|
|
self.hyperopt.__class__.ticker_interval = str(config['ticker_interval'])
|
2019-06-21 05:06:54 +00:00
|
|
|
|
2019-01-06 13:12:55 +00:00
|
|
|
if not hasattr(self.hyperopt, 'populate_buy_trend'):
|
|
|
|
logger.warning("Custom Hyperopt does not provide populate_buy_trend. "
|
|
|
|
"Using populate_buy_trend from DefaultStrategy.")
|
|
|
|
if not hasattr(self.hyperopt, 'populate_sell_trend'):
|
|
|
|
logger.warning("Custom Hyperopt does not provide populate_sell_trend. "
|
|
|
|
"Using populate_sell_trend from DefaultStrategy.")
|
|
|
|
|
2018-11-24 19:14:08 +00:00
|
|
|
def _load_hyperopt(
|
|
|
|
self, hyperopt_name: str, extra_dir: Optional[str] = None) -> IHyperOpt:
|
|
|
|
"""
|
|
|
|
Search and loads the specified hyperopt.
|
|
|
|
:param hyperopt_name: name of the module to import
|
|
|
|
: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
|
|
|
|
|
|
|
abs_paths = [
|
2018-11-24 19:39:16 +00:00
|
|
|
current_path.parent.parent.joinpath('user_data/hyperopts'),
|
2018-11-24 19:14:08 +00:00
|
|
|
current_path,
|
|
|
|
]
|
|
|
|
|
|
|
|
if extra_dir:
|
|
|
|
# Add extra hyperopt directory on top of search paths
|
2018-11-24 19:39:16 +00:00
|
|
|
abs_paths.insert(0, Path(extra_dir))
|
2018-11-24 19:14:08 +00:00
|
|
|
|
|
|
|
for _path in abs_paths:
|
2018-12-05 19:44:41 +00:00
|
|
|
try:
|
|
|
|
hyperopt = self._search_object(directory=_path, object_type=IHyperOpt,
|
|
|
|
object_name=hyperopt_name)
|
|
|
|
if hyperopt:
|
2019-02-19 21:27:20 +00:00
|
|
|
logger.info("Using resolved hyperopt %s from '%s'", hyperopt_name, _path)
|
2018-12-05 19:44:41 +00:00
|
|
|
return hyperopt
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.warning('Path "%s" does not exist', _path.relative_to(Path.cwd()))
|
2018-11-24 19:14:08 +00:00
|
|
|
|
|
|
|
raise ImportError(
|
|
|
|
"Impossible to load Hyperopt '{}'. This class does not exist"
|
|
|
|
" or contains Python code errors".format(hyperopt_name)
|
|
|
|
)
|