[SQUASH] Use HyperStrategyMixin as part of IStrategy interface.

This commit is contained in:
Rokas Kupstys 2021-03-24 16:03:38 +02:00
parent 2d13e5fd50
commit e9f0babe8a
6 changed files with 6 additions and 24 deletions

View File

@ -70,10 +70,6 @@ class Hyperopt:
self.backtesting = Backtesting(self.config) self.backtesting = Backtesting(self.config)
if not self.config.get('hyperopt'): if not self.config.get('hyperopt'):
if not getattr(self.backtesting.strategy, 'HYPER_STRATEGY', False):
raise OperationalException('Strategy is not auto-hyperoptable. Specify --hyperopt '
'parameter or add HyperStrategyMixin mixin to your '
'strategy class.')
self.custom_hyperopt = HyperOptAuto(self.config) self.custom_hyperopt = HyperOptAuto(self.config)
else: else:
self.custom_hyperopt = HyperOptResolver.load_hyperopt(self.config) self.custom_hyperopt = HyperOptResolver.load_hyperopt(self.config)

View File

@ -7,7 +7,6 @@ from typing import Any, Callable, Dict, List
from pandas import DataFrame from pandas import DataFrame
from skopt.space import Categorical, Dimension, Integer, Real # noqa from skopt.space import Categorical, Dimension, Integer, Real # noqa
from freqtrade.exceptions import OperationalException
from freqtrade.optimize.hyperopt_interface import IHyperOpt from freqtrade.optimize.hyperopt_interface import IHyperOpt
@ -19,9 +18,6 @@ class HyperOptAuto(IHyperOpt):
""" """
def buy_strategy_generator(self, params: Dict[str, Any]) -> Callable: def buy_strategy_generator(self, params: Dict[str, Any]) -> Callable:
if not getattr(self.strategy, 'HYPER_STRATEGY', False):
raise OperationalException('Strategy must inherit from IHyperStrategy.')
def populate_buy_trend(dataframe: DataFrame, metadata: dict): def populate_buy_trend(dataframe: DataFrame, metadata: dict):
for attr_name, attr in self.strategy.enumerate_parameters('buy'): for attr_name, attr in self.strategy.enumerate_parameters('buy'):
attr.value = params[attr_name] attr.value = params[attr_name]
@ -30,9 +26,6 @@ class HyperOptAuto(IHyperOpt):
return populate_buy_trend return populate_buy_trend
def sell_strategy_generator(self, params: Dict[str, Any]) -> Callable: def sell_strategy_generator(self, params: Dict[str, Any]) -> Callable:
if not getattr(self.strategy, 'HYPER_STRATEGY', False):
raise OperationalException('Strategy must inherit from IHyperStrategy.')
def populate_buy_trend(dataframe: DataFrame, metadata: dict): def populate_buy_trend(dataframe: DataFrame, metadata: dict):
for attr_name, attr in self.strategy.enumerate_parameters('sell'): for attr_name, attr in self.strategy.enumerate_parameters('sell'):
attr.value = params[attr_name] attr.value = params[attr_name]
@ -54,9 +47,6 @@ class HyperOptAuto(IHyperOpt):
return default_func return default_func
def _generate_indicator_space(self, category): def _generate_indicator_space(self, category):
if not getattr(self.strategy, 'HYPER_STRATEGY', False):
raise OperationalException('Strategy must inherit from IHyperStrategy.')
for attr_name, attr in self.strategy.enumerate_parameters(category): for attr_name, attr in self.strategy.enumerate_parameters(category):
yield attr.get_space(attr_name) yield attr.get_space(attr_name)

View File

@ -5,14 +5,14 @@ This module defines the interface to apply for hyperopt
import logging import logging
import math import math
from abc import ABC from abc import ABC
from typing import Any, Callable, Dict, List, Union from typing import Any, Callable, Dict, List
from skopt.space import Categorical, Dimension, Integer, Real from skopt.space import Categorical, Dimension, Integer, Real
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.exchange import timeframe_to_minutes from freqtrade.exchange import timeframe_to_minutes
from freqtrade.misc import round_dict from freqtrade.misc import round_dict
from freqtrade.strategy import IStrategy, HyperStrategyMixin from freqtrade.strategy import IStrategy
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -34,7 +34,7 @@ class IHyperOpt(ABC):
""" """
ticker_interval: str # DEPRECATED ticker_interval: str # DEPRECATED
timeframe: str timeframe: str
strategy: Union[IStrategy, HyperStrategyMixin] strategy: IStrategy
def __init__(self, config: dict) -> None: def __init__(self, config: dict) -> None:
self.config = config self.config = config

View File

@ -2,6 +2,5 @@
from freqtrade.exchange import (timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date, from freqtrade.exchange import (timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date,
timeframe_to_prev_date, timeframe_to_seconds) timeframe_to_prev_date, timeframe_to_seconds)
from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.interface import IStrategy
from freqtrade.strategy.hyper import HyperStrategyMixin, IntParameter, FloatParameter,\ from freqtrade.strategy.hyper import IntParameter, FloatParameter, CategoricalParameter
CategoricalParameter
from freqtrade.strategy.strategy_helper import merge_informative_pair, stoploss_from_open from freqtrade.strategy.strategy_helper import merge_informative_pair, stoploss_from_open

View File

@ -135,13 +135,9 @@ class HyperStrategyMixin(object):
strategy logic. strategy logic.
""" """
# Hint that class can be used with HyperOptAuto.
HYPER_STRATEGY = 1
def __init__(self): def __init__(self):
""" """
Initialize hyperoptable strategy mixin. Initialize hyperoptable strategy mixin.
:param config:
""" """
self._load_params(getattr(self, 'buy_params', None)) self._load_params(getattr(self, 'buy_params', None))
self._load_params(getattr(self, 'sell_params', None)) self._load_params(getattr(self, 'sell_params', None))

View File

@ -18,6 +18,7 @@ from freqtrade.exceptions import OperationalException, StrategyError
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds
from freqtrade.exchange.exchange import timeframe_to_next_date from freqtrade.exchange.exchange import timeframe_to_next_date
from freqtrade.persistence import PairLocks, Trade from freqtrade.persistence import PairLocks, Trade
from freqtrade.strategy.hyper import HyperStrategyMixin
from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper
from freqtrade.wallets import Wallets from freqtrade.wallets import Wallets
@ -59,7 +60,7 @@ class SellCheckTuple(NamedTuple):
sell_type: SellType sell_type: SellType
class IStrategy(ABC): class IStrategy(ABC, HyperStrategyMixin):
""" """
Interface for freqtrade strategies Interface for freqtrade strategies
Defines the mandatory structure must follow any custom strategies Defines the mandatory structure must follow any custom strategies