From 03a54a5b0ae8efd49daf13c408a37a2d3c7816bf Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Mon, 28 Apr 2025 13:13:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=8C=87=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- freqtrade/templates/FreqaiExampleStrategy.py | 71 +++++++++++++------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/freqtrade/templates/FreqaiExampleStrategy.py b/freqtrade/templates/FreqaiExampleStrategy.py index 2adbf252..688e6446 100644 --- a/freqtrade/templates/FreqaiExampleStrategy.py +++ b/freqtrade/templates/FreqaiExampleStrategy.py @@ -72,33 +72,54 @@ class FreqaiExampleStrategy(IStrategy): } def feature_engineering_expand_all(self, dataframe: DataFrame, period: int, metadata: dict, **kwargs) -> DataFrame: - # 添加更多稳定的特征 - dataframe["%-rsi-period"] = ta.RSI(dataframe, timeperiod=period) - dataframe["%-mfi-period"] = ta.MFI(dataframe, timeperiod=period) - dataframe["%-sma-period"] = ta.SMA(dataframe, timeperiod=period) - dataframe["%-ema-period"] = ta.EMA(dataframe, timeperiod=period) - dataframe["%-adx-period"] = ta.ADX(dataframe, timeperiod=period) + # 添加更多技术指标 + dataframe["%-rsi"] = ta.RSI(dataframe, timeperiod=14) + dataframe["%-mfi"] = ta.MFI(dataframe, timeperiod=14) + dataframe["%-sma"] = ta.SMA(dataframe, timeperiod=20) + dataframe["%-ema"] = ta.EMA(dataframe, timeperiod=20) + dataframe["%-adx"] = ta.ADX(dataframe, timeperiod=14) + dataframe["%-atr"] = ta.ATR(dataframe, timeperiod=14) + dataframe["%-obv"] = ta.OBV(dataframe) + dataframe["%-cci"] = ta.CCI(dataframe, timeperiod=20) + dataframe["%-stoch"] = ta.STOCH(dataframe)['slowk'] + dataframe["%-macd"] = ta.MACD(dataframe)['macd'] + dataframe["%-macdsignal"] = ta.MACD(dataframe)['macdsignal'] + dataframe["%-macdhist"] = ta.MACD(dataframe)['macdhist'] + dataframe["%-willr"] = ta.WILLR(dataframe, timeperiod=14) + dataframe["%-ultosc"] = ta.ULTOSC(dataframe) + dataframe["%-trix"] = ta.TRIX(dataframe, timeperiod=14) + dataframe["%-ad"] = ta.ADOSC(dataframe) + dataframe["%-mom"] = ta.MOM(dataframe, timeperiod=10) + dataframe["%-roc"] = ta.ROC(dataframe, timeperiod=10) - # 改进布林带计算 - bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=period, stds=2.0) - dataframe["bb_lowerband-period"] = bollinger["lower"] - dataframe["bb_middleband-period"] = bollinger["mid"] - dataframe["bb_upperband-period"] = bollinger["upper"] + # 添加布林带相关特征 + bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2) + dataframe["bb_lowerband"] = bollinger["lower"] + dataframe["bb_middleband"] = bollinger["mid"] + dataframe["bb_upperband"] = bollinger["upper"] + dataframe["bb_width"] = (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) / dataframe["bb_middleband"] + dataframe["bb_pct"] = (dataframe["close"] - dataframe["bb_lowerband"]) / (dataframe["bb_upperband"] - dataframe["bb_lowerband"]) - # 添加更多相对特征 - dataframe["%-bb_width-period"] = ( - dataframe["bb_upperband-period"] - dataframe["bb_lowerband-period"] - ) / dataframe["bb_middleband-period"] - dataframe["%-close-bb_lower-period"] = dataframe["close"] / dataframe["bb_lowerband-period"] - dataframe["%-roc-period"] = ta.ROC(dataframe, timeperiod=period) - dataframe["%-relative_volume-period"] = ( - dataframe["volume"] / dataframe["volume"].rolling(period).mean() - ) + # 添加成交量相关特征 + dataframe["volume_ma"] = dataframe["volume"].rolling(window=20).mean() + dataframe["volume_roc"] = dataframe["volume"].pct_change(periods=10) + dataframe["volume_obv"] = ta.OBV(dataframe) - # 添加更多稳定特征 - dataframe["%-atr-period"] = ta.ATR(dataframe, timeperiod=period) - dataframe["%-obv-period"] = ta.OBV(dataframe) - dataframe["%-cci-period"] = ta.CCI(dataframe, timeperiod=period) + # 添加价格相关特征 + dataframe["close_ma"] = dataframe["close"].rolling(window=20).mean() + dataframe["close_roc"] = dataframe["close"].pct_change(periods=10) + dataframe["close_log_ret"] = np.log(dataframe["close"]).diff() + dataframe["close_zscore"] = (dataframe["close"] - dataframe["close"].rolling(window=20).mean()) / dataframe["close"].rolling(window=20).std() + + # 添加时间相关特征 + dataframe["hour"] = dataframe["date"].dt.hour + dataframe["day_of_week"] = dataframe["date"].dt.dayofweek + dataframe["is_weekend"] = dataframe["day_of_week"].isin([5, 6]).astype(int) + + # 添加波动率相关特征 + dataframe["volatility"] = dataframe["close"].pct_change().rolling(window=20).std() + dataframe["volatility_ma"] = dataframe["volatility"].rolling(window=20).mean() + dataframe["volatility_roc"] = dataframe["volatility"].pct_change(periods=10) # 改进数据清理 for col in dataframe.columns: @@ -262,7 +283,7 @@ class FreqaiExampleStrategy(IStrategy): f"stoploss={self.stoploss}, trailing_stop_positive={self.trailing_stop_positive}") dataframe.replace([np.inf, -np.inf], 0, inplace=True) - dataframe.fillna(method='ffill', inplace=True) + dataframe.ffill(inplace=True) dataframe.fillna(0, inplace=True) logger.info(f"up_or_down 值统计:\n{dataframe['up_or_down'].value_counts().to_string()}")