freqtrade_origin/freqtrade/resolvers/hyperopt_resolver.py

50 lines
1.7 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 HYPEROPT_LOSS_BUILTIN, USERPATH_HYPEROPTS
from freqtrade.exceptions import OperationalException
2019-07-16 04:27:23 +00:00
from freqtrade.optimize.hyperopt_loss_interface import IHyperOptLoss
from freqtrade.resolvers import IResolver
2020-09-28 17:39:41 +00:00
logger = logging.getLogger(__name__)
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
"""
hyperoptloss_name = config.get('hyperopt_loss')
if not hyperoptloss_name:
raise OperationalException(
"No Hyperopt loss set. Please use `--hyperopt-loss` to "
"specify the Hyperopt-Loss class to use.\n"
f"Built-in Hyperopt-loss-functions are: {', '.join(HYPEROPT_LOSS_BUILTIN)}"
)
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__.timeframe = str(config['timeframe'])
2019-07-16 04:27:23 +00:00
return hyperoptloss