添加更多技术指标
Some checks are pending
Update Docker Hub Description / dockerHubDescription (push) Waiting to run

This commit is contained in:
zhangkun9038@dingtalk.com 2025-04-28 13:13:19 +08:00
parent e3ffdd92e0
commit 03a54a5b0a

View File

@ -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()}")