# 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'), ] def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators. Should be a copy of from strategy must align to populate_indicators in this file Only used when --spaces does not include buy """ dataframe.loc[ ( (dataframe['macd'] > dataframe['macdsignal']) & (dataframe['cci'] <= -50.0) ), 'buy'] = 1 return dataframe def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: """ Based on TA indicators. Should be a copy of from strategy must align to populate_indicators in this file Only used when --spaces does not include sell """ dataframe.loc[ ( (dataframe['macd'] < dataframe['macdsignal']) & (dataframe['cci'] >= 100.0) ), 'sell'] = 1 return dataframe