Convert MACDStrategy to hyperoptable strategy

This commit is contained in:
Matthias
2021-08-03 20:22:58 +02:00
parent f2de8f7833
commit 314ad6c1ec
2 changed files with 26 additions and 106 deletions
@@ -1,100 +0,0 @@
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
import talib.abstract as ta
from pandas import DataFrame
from typing import Dict, Any, Callable, List
# import numpy as np
from skopt.space import Categorical, Dimension, Integer, Real
# import freqtrade.vendor.qtpylib.indicators as qtpylib
from freqtrade.optimize.hyperopt_interface import IHyperOpt
class_name = 'MACDStrategy_hyperopt'
# This class is a sample. Feel free to customize it.
class MACDStrategy_hyperopt(IHyperOpt):
"""
This is an Example hyperopt to inspire you. - corresponding to MACDStrategy in this repository.
To run this, best use the following command (adjust to your environment if needed):
```
freqtrade hyperopt --strategy MACDStrategy --hyperopt MACDStrategy_hyperopt --spaces buy sell
```
The idea is to optimize only the CCI value.
- Buy side: CCI between -700 and 0
- Sell side: CCI between 0 and 700
More information in https://github.com/freqtrade/freqtrade/blob/develop/docs/hyperopt.md
"""
@staticmethod
def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame:
macd = ta.MACD(dataframe)
dataframe['macd'] = macd['macd']
dataframe['macdsignal'] = macd['macdsignal']
dataframe['macdhist'] = macd['macdhist']
dataframe['cci'] = ta.CCI(dataframe)
return dataframe
@staticmethod
def buy_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the buy strategy parameters to be used by hyperopt
"""
def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Buy strategy Hyperopt will build and use
"""
dataframe.loc[
(
(dataframe['macd'] > dataframe['macdsignal']) &
(dataframe['cci'] <= params['buy-cci-value']) &
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'buy'] = 1
return dataframe
return populate_buy_trend
@staticmethod
def indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching strategy parameters
"""
return [
Integer(-700, 0, name='buy-cci-value'),
]
@staticmethod
def sell_strategy_generator(params: Dict[str, Any]) -> Callable:
"""
Define the sell strategy parameters to be used by hyperopt
"""
def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
Sell strategy Hyperopt will build and use
"""
dataframe.loc[
(
(dataframe['macd'] < dataframe['macdsignal']) &
(dataframe['cci'] >= params['sell-cci-value'])
),
'sell'] = 1
return dataframe
return populate_sell_trend
@staticmethod
def sell_indicator_space() -> List[Dimension]:
"""
Define your Hyperopt space for searching sell strategy parameters
"""
return [
Integer(0, 700, name='sell-cci-value'),
]
@@ -1,8 +1,7 @@
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from typing import Dict, List
from functools import reduce
from freqtrade.strategy import IStrategy
from freqtrade.strategy import CategoricalParameter, DecimalParameter, IntParameter
from pandas import DataFrame
# --------------------------------
@@ -11,7 +10,6 @@ import talib.abstract as ta
class MACDStrategy(IStrategy):
"""
author@: Gert Wohlgemuth
idea:
@@ -24,7 +22,14 @@ class MACDStrategy(IStrategy):
MACD below MACD signal
and CCI > 100
freqtrade hyperopt --strategy MACDStrategy --hyperopt-loss <someLossFunction> --spaces buy sell
The idea is to optimize only the CCI value.
- Buy side: CCI between -700 and 0
- Sell side: CCI between 0 and 700
"""
INTERFACE_VERSION = 2
# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
@@ -42,6 +47,19 @@ class MACDStrategy(IStrategy):
# Optimal timeframe for the strategy
timeframe = '5m'
buy_cci = IntParameter(low=-700, high=0, default=-50, space='buy', optimize=True)
sell_cci = IntParameter(low=0, high=700, default=100, space='sell', optimize=True)
# Buy hyperspace params:
buy_params = {
"buy_cci": -48,
}
# Sell hyperspace params:
sell_params = {
"sell_cci": 687,
}
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
macd = ta.MACD(dataframe)
@@ -61,7 +79,8 @@ class MACDStrategy(IStrategy):
dataframe.loc[
(
(dataframe['macd'] > dataframe['macdsignal']) &
(dataframe['cci'] <= -50.0)
(dataframe['cci'] <= self.buy_cci.value) &
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'buy'] = 1
@@ -76,7 +95,8 @@ class MACDStrategy(IStrategy):
dataframe.loc[
(
(dataframe['macd'] < dataframe['macdsignal']) &
(dataframe['cci'] >= 100.0)
(dataframe['cci'] >= self.sell_cci.value) &
(dataframe['volume'] > 0) # Make sure Volume is not 0
),
'sell'] = 1