Convert HyperoptResolver to static loader
This commit is contained in:
parent
560acb7cea
commit
248ef5a0ea
@ -64,7 +64,7 @@ class Hyperopt:
|
|||||||
|
|
||||||
self.backtesting = Backtesting(self.config)
|
self.backtesting = Backtesting(self.config)
|
||||||
|
|
||||||
self.custom_hyperopt = HyperOptResolver(self.config).hyperopt
|
self.custom_hyperopt = HyperOptResolver.load_hyperopt(self.config)
|
||||||
|
|
||||||
self.custom_hyperoptloss = HyperOptLossResolver(self.config).hyperoptloss
|
self.custom_hyperoptloss = HyperOptLossResolver(self.config).hyperoptloss
|
||||||
self.calculate_loss = self.custom_hyperoptloss.hyperopt_loss_function
|
self.calculate_loss = self.custom_hyperoptloss.hyperopt_loss_function
|
||||||
|
@ -20,11 +20,11 @@ class HyperOptResolver(IResolver):
|
|||||||
"""
|
"""
|
||||||
This class contains all the logic to load custom hyperopt class
|
This class contains all the logic to load custom hyperopt class
|
||||||
"""
|
"""
|
||||||
__slots__ = ['hyperopt']
|
|
||||||
|
|
||||||
def __init__(self, config: Dict) -> None:
|
@staticmethod
|
||||||
|
def load_hyperopt(config: Dict) -> IHyperOpt:
|
||||||
"""
|
"""
|
||||||
Load the custom class from config parameter
|
Load the custom hyperopt class from config parameter
|
||||||
:param config: configuration dictionary
|
:param config: configuration dictionary
|
||||||
"""
|
"""
|
||||||
if not config.get('hyperopt'):
|
if not config.get('hyperopt'):
|
||||||
@ -33,21 +33,23 @@ class HyperOptResolver(IResolver):
|
|||||||
|
|
||||||
hyperopt_name = config['hyperopt']
|
hyperopt_name = config['hyperopt']
|
||||||
|
|
||||||
self.hyperopt = self._load_hyperopt(hyperopt_name, config,
|
hyperopt = HyperOptResolver._load_hyperopt(hyperopt_name, config,
|
||||||
extra_dir=config.get('hyperopt_path'))
|
extra_dir=config.get('hyperopt_path'))
|
||||||
|
|
||||||
if not hasattr(self.hyperopt, 'populate_indicators'):
|
if not hasattr(hyperopt, 'populate_indicators'):
|
||||||
logger.warning("Hyperopt class does not provide populate_indicators() method. "
|
logger.warning("Hyperopt class does not provide populate_indicators() method. "
|
||||||
"Using populate_indicators from the strategy.")
|
"Using populate_indicators from the strategy.")
|
||||||
if not hasattr(self.hyperopt, 'populate_buy_trend'):
|
if not hasattr(hyperopt, 'populate_buy_trend'):
|
||||||
logger.warning("Hyperopt class does not provide populate_buy_trend() method. "
|
logger.warning("Hyperopt class does not provide populate_buy_trend() method. "
|
||||||
"Using populate_buy_trend from the strategy.")
|
"Using populate_buy_trend from the strategy.")
|
||||||
if not hasattr(self.hyperopt, 'populate_sell_trend'):
|
if not hasattr(hyperopt, 'populate_sell_trend'):
|
||||||
logger.warning("Hyperopt class does not provide populate_sell_trend() method. "
|
logger.warning("Hyperopt class does not provide populate_sell_trend() method. "
|
||||||
"Using populate_sell_trend from the strategy.")
|
"Using populate_sell_trend from the strategy.")
|
||||||
|
return hyperopt
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def _load_hyperopt(
|
def _load_hyperopt(
|
||||||
self, hyperopt_name: str, config: Dict, extra_dir: Optional[str] = None) -> IHyperOpt:
|
hyperopt_name: str, config: Dict, extra_dir: Optional[str] = None) -> IHyperOpt:
|
||||||
"""
|
"""
|
||||||
Search and loads the specified hyperopt.
|
Search and loads the specified hyperopt.
|
||||||
:param hyperopt_name: name of the module to import
|
:param hyperopt_name: name of the module to import
|
||||||
@ -57,11 +59,12 @@ class HyperOptResolver(IResolver):
|
|||||||
"""
|
"""
|
||||||
current_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
|
current_path = Path(__file__).parent.parent.joinpath('optimize').resolve()
|
||||||
|
|
||||||
abs_paths = self.build_search_paths(config, current_path=current_path,
|
abs_paths = IResolver.build_search_paths(config, current_path=current_path,
|
||||||
user_subdir=USERPATH_HYPEROPTS, extra_dir=extra_dir)
|
user_subdir=USERPATH_HYPEROPTS,
|
||||||
|
extra_dir=extra_dir)
|
||||||
|
|
||||||
hyperopt = self._load_object(paths=abs_paths, object_type=IHyperOpt,
|
hyperopt = IResolver._load_object(paths=abs_paths, object_type=IHyperOpt,
|
||||||
object_name=hyperopt_name, kwargs={'config': config})
|
object_name=hyperopt_name, kwargs={'config': config})
|
||||||
if hyperopt:
|
if hyperopt:
|
||||||
return hyperopt
|
return hyperopt
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
|
@ -163,7 +163,7 @@ def test_hyperoptresolver(mocker, default_conf, caplog) -> None:
|
|||||||
MagicMock(return_value=hyperopt(default_conf))
|
MagicMock(return_value=hyperopt(default_conf))
|
||||||
)
|
)
|
||||||
default_conf.update({'hyperopt': 'DefaultHyperOpt'})
|
default_conf.update({'hyperopt': 'DefaultHyperOpt'})
|
||||||
x = HyperOptResolver(default_conf).hyperopt
|
x = HyperOptResolver.load_hyperopt(default_conf)
|
||||||
assert not hasattr(x, 'populate_indicators')
|
assert not hasattr(x, 'populate_indicators')
|
||||||
assert not hasattr(x, 'populate_buy_trend')
|
assert not hasattr(x, 'populate_buy_trend')
|
||||||
assert not hasattr(x, 'populate_sell_trend')
|
assert not hasattr(x, 'populate_sell_trend')
|
||||||
@ -180,7 +180,7 @@ def test_hyperoptresolver_wrongname(mocker, default_conf, caplog) -> None:
|
|||||||
default_conf.update({'hyperopt': "NonExistingHyperoptClass"})
|
default_conf.update({'hyperopt': "NonExistingHyperoptClass"})
|
||||||
|
|
||||||
with pytest.raises(OperationalException, match=r'Impossible to load Hyperopt.*'):
|
with pytest.raises(OperationalException, match=r'Impossible to load Hyperopt.*'):
|
||||||
HyperOptResolver(default_conf).hyperopt
|
HyperOptResolver.load_hyperopt(default_conf)
|
||||||
|
|
||||||
|
|
||||||
def test_hyperoptresolver_noname(default_conf):
|
def test_hyperoptresolver_noname(default_conf):
|
||||||
@ -188,7 +188,7 @@ def test_hyperoptresolver_noname(default_conf):
|
|||||||
with pytest.raises(OperationalException,
|
with pytest.raises(OperationalException,
|
||||||
match="No Hyperopt set. Please use `--hyperopt` to specify "
|
match="No Hyperopt set. Please use `--hyperopt` to specify "
|
||||||
"the Hyperopt class to use."):
|
"the Hyperopt class to use."):
|
||||||
HyperOptResolver(default_conf)
|
HyperOptResolver.load_hyperopt(default_conf)
|
||||||
|
|
||||||
|
|
||||||
def test_hyperoptlossresolver(mocker, default_conf, caplog) -> None:
|
def test_hyperoptlossresolver(mocker, default_conf, caplog) -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user