From e9f0babe8a7276cdb1fa3f22cb2b21368e209c31 Mon Sep 17 00:00:00 2001 From: Rokas Kupstys Date: Wed, 24 Mar 2021 16:03:38 +0200 Subject: [PATCH] [SQUASH] Use HyperStrategyMixin as part of IStrategy interface. --- freqtrade/optimize/hyperopt.py | 4 ---- freqtrade/optimize/hyperopt_auto.py | 10 ---------- freqtrade/optimize/hyperopt_interface.py | 6 +++--- freqtrade/strategy/__init__.py | 3 +-- freqtrade/strategy/hyper.py | 4 ---- freqtrade/strategy/interface.py | 3 ++- 6 files changed, 6 insertions(+), 24 deletions(-) diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index b5ee1da1a..4926bf1b3 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -70,10 +70,6 @@ class Hyperopt: self.backtesting = Backtesting(self.config) 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) else: self.custom_hyperopt = HyperOptResolver.load_hyperopt(self.config) diff --git a/freqtrade/optimize/hyperopt_auto.py b/freqtrade/optimize/hyperopt_auto.py index 41f086ad2..753e8175e 100644 --- a/freqtrade/optimize/hyperopt_auto.py +++ b/freqtrade/optimize/hyperopt_auto.py @@ -7,7 +7,6 @@ from typing import Any, Callable, Dict, List from pandas import DataFrame from skopt.space import Categorical, Dimension, Integer, Real # noqa -from freqtrade.exceptions import OperationalException from freqtrade.optimize.hyperopt_interface import IHyperOpt @@ -19,9 +18,6 @@ class HyperOptAuto(IHyperOpt): """ 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): for attr_name, attr in self.strategy.enumerate_parameters('buy'): attr.value = params[attr_name] @@ -30,9 +26,6 @@ class HyperOptAuto(IHyperOpt): return populate_buy_trend 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): for attr_name, attr in self.strategy.enumerate_parameters('sell'): attr.value = params[attr_name] @@ -54,9 +47,6 @@ class HyperOptAuto(IHyperOpt): return default_func 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): yield attr.get_space(attr_name) diff --git a/freqtrade/optimize/hyperopt_interface.py b/freqtrade/optimize/hyperopt_interface.py index 2dd8500a6..46adf55b8 100644 --- a/freqtrade/optimize/hyperopt_interface.py +++ b/freqtrade/optimize/hyperopt_interface.py @@ -5,14 +5,14 @@ This module defines the interface to apply for hyperopt import logging import math 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 freqtrade.exceptions import OperationalException from freqtrade.exchange import timeframe_to_minutes from freqtrade.misc import round_dict -from freqtrade.strategy import IStrategy, HyperStrategyMixin +from freqtrade.strategy import IStrategy logger = logging.getLogger(__name__) @@ -34,7 +34,7 @@ class IHyperOpt(ABC): """ ticker_interval: str # DEPRECATED timeframe: str - strategy: Union[IStrategy, HyperStrategyMixin] + strategy: IStrategy def __init__(self, config: dict) -> None: self.config = config diff --git a/freqtrade/strategy/__init__.py b/freqtrade/strategy/__init__.py index e395be106..a300c601b 100644 --- a/freqtrade/strategy/__init__.py +++ b/freqtrade/strategy/__init__.py @@ -2,6 +2,5 @@ from freqtrade.exchange import (timeframe_to_minutes, timeframe_to_msecs, timeframe_to_next_date, timeframe_to_prev_date, timeframe_to_seconds) from freqtrade.strategy.interface import IStrategy -from freqtrade.strategy.hyper import HyperStrategyMixin, IntParameter, FloatParameter,\ - CategoricalParameter +from freqtrade.strategy.hyper import IntParameter, FloatParameter, CategoricalParameter from freqtrade.strategy.strategy_helper import merge_informative_pair, stoploss_from_open diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index 154dcead6..53a0c6462 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -135,13 +135,9 @@ class HyperStrategyMixin(object): strategy logic. """ - # Hint that class can be used with HyperOptAuto. - HYPER_STRATEGY = 1 - def __init__(self): """ Initialize hyperoptable strategy mixin. - :param config: """ self._load_params(getattr(self, 'buy_params', None)) self._load_params(getattr(self, 'sell_params', None)) diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py index 6d40e56cc..b00e0ccb8 100644 --- a/freqtrade/strategy/interface.py +++ b/freqtrade/strategy/interface.py @@ -18,6 +18,7 @@ from freqtrade.exceptions import OperationalException, StrategyError from freqtrade.exchange import timeframe_to_minutes, timeframe_to_seconds from freqtrade.exchange.exchange import timeframe_to_next_date from freqtrade.persistence import PairLocks, Trade +from freqtrade.strategy.hyper import HyperStrategyMixin from freqtrade.strategy.strategy_wrapper import strategy_safe_wrapper from freqtrade.wallets import Wallets @@ -59,7 +60,7 @@ class SellCheckTuple(NamedTuple): sell_type: SellType -class IStrategy(ABC): +class IStrategy(ABC, HyperStrategyMixin): """ Interface for freqtrade strategies Defines the mandatory structure must follow any custom strategies