# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement # flake8: noqa: F401 # isort: skip_file # --- Do not remove these imports --- import numpy as np import pandas as pd from datetime import datetime, timedelta, timezone from pandas import DataFrame from typing import Optional, Union from freqtrade.strategy import ( IStrategy, Trade, Order, PairLocks, informative, # @informative decorator # Hyperopt Parameters BooleanParameter, CategoricalParameter, DecimalParameter, IntParameter, RealParameter, # timeframe helpers timeframe_to_minutes, timeframe_to_next_date, timeframe_to_prev_date, # Strategy helper functions merge_informative_pair, stoploss_from_absolute, stoploss_from_open, ) import logging logger = logging.getLogger(__name__) # -------------------------------- # Add your lib to import here import talib.abstract as ta from technical import qtpylib class CCIMultiTimeframeSpotStrategy(IStrategy): # 策略参数 INTERFACE_VERSION = 3 minimal_roi = {"0": 100} stoploss = -1 trailing_stop = False timeframe = '4h' use_exit_signal = True exit_profit_only = False ignore_roi_if_entry_signal = False def TD(self, dataframe:DataFrame): close = dataframe['close'].to_list() td = [0,0,0,0] up = 0 down = 0 for i in range(4, len(close)): if close[i] > close[i-4]: up += 1 down = 0 td.append(up) else: down -= 1 up = 0 td.append(down) return td @informative('1d') def populate_indicators_1d(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe['cci'] = ta.CCI(dataframe, timeperiod=26) dataframe["adx"] = ta.ADX(dataframe) macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9,) dataframe["macd"] = macd["macd"] dataframe["macdsignal"] = macd["macdsignal"] dataframe["macdhist"] = macd["macdhist"] logger.info(dataframe.tail()) return dataframe @informative('1w') def populate_indicators_1w(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe['cci'] = ta.CCI(dataframe, timeperiod=26) logger.info(dataframe.tail()) return dataframe def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # 计算4h CCI dataframe['cci_4h'] = ta.CCI(dataframe, timeperiod=26) dataframe['sma12_4h'] = ta.SMA(dataframe, timeperiod=12) dataframe['sma26_4h'] = ta.SMA(dataframe, timeperiod=26) dataframe["adx_4h"] = ta.ADX(dataframe) macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9,) dataframe["macd_4h"] = macd["macd"] dataframe["macdsignal_4h"] = macd["macdsignal"] dataframe["macdhist_4h"] = macd["macdhist"] dataframe['cci_hist_4h'] = ta.CCI(dataframe['macdhist_1d'], dataframe['macdhist_1d'], dataframe['macdhist_1d'], timeperiod=26) # dataframe['adx_hist_4h'] = ta.ADX(dataframe['macdhist_4h']) dataframe['TD'] = self.TD(dataframe) logger.info(dataframe.tail()) return dataframe def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # 入场:1d与4h CCI < -100,且4h CCI上升(当前 > 前一根) dataframe.loc[ ( (dataframe['cci_4h'] < -100) & (dataframe['cci_1d'] < -100) & (dataframe['cci_4h'] > dataframe['cci_4h'].shift(1)) & # (dataframe['macdhist_1d'] < dataframe['macdhist_1d'].shift(1)) & (dataframe['volume'] > 0) ), 'enter_long', ] = 1 return dataframe def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # 离场:1d与4h CCI > 100,且4h CCI下降(当前 < 前一根) dataframe.loc[ ( (dataframe['cci_4h'] > 100) & (dataframe['cci_1d'] > 100) & (dataframe['cci_4h'] < dataframe['cci_4h'].shift(1)) & # (dataframe['macdhist_1d'] > dataframe['macdhist_1d'].shift(1)) & (dataframe['volume'] > 0) ), 'exit_long', ] = 1 return dataframe