This commit is contained in:
zhangkun9038@dingtalk.com 2025-04-29 14:31:05 +08:00
parent 3a721996a0
commit de31f38a41

View File

@ -209,6 +209,12 @@ class FreqaiExampleStrategy(IStrategy):
logger.info(f"处理交易对:{metadata['pair']}")
dataframe = self.freqai.start(dataframe, metadata, self)
# 确保 MACD 列在数据开始时就计算
macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9)
dataframe["macd"] = macd["macd"]
dataframe["macdsignal"] = macd["macdsignal"]
dataframe["macdhist"] = macd["macdhist"]
# 计算传统指标
dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)
bollinger = qtpylib.bollinger_bands(qtpylib.typical_price(dataframe), window=20, stds=2)
@ -246,10 +252,11 @@ class FreqaiExampleStrategy(IStrategy):
self.buy_rsi.value = 30
self.sell_rsi.value = 70
# 确保 buy_rsi_pred 列存在
# 确保 buy_rsi_pred 列存在并合理初始化
if "buy_rsi_pred" not in dataframe.columns:
logger.warning("buy_rsi_pred 列缺失,使用默认值 30 进行初始化")
dataframe["buy_rsi_pred"] = 30
logger.warning("buy_rsi_pred 列缺失,使用 RSI 值进行初始化")
dataframe["buy_rsi_pred"] = ta.RSI(dataframe, timeperiod=14)
dataframe["buy_rsi_pred"] = dataframe["buy_rsi_pred"].fillna(30).clip(10, 90)
# 计算 sell_rsi_pred 并清理 NaN 值
dataframe["sell_rsi_pred"] = dataframe["buy_rsi_pred"] + 20
@ -414,8 +421,8 @@ class FreqaiExampleStrategy(IStrategy):
if "macd" in df.columns and "macdsignal" in df.columns:
enter_long_conditions.append((df["macd"] > df["macdsignal"]))
# 确保模型预测为买入
enter_long_conditions.append((df["do_predict"] == 1))
# 放宽模型预测条件
enter_long_conditions.append((df["do_predict"] >= 0.5)) # 将预测阈值从1降低到0.5
if enter_long_conditions:
df.loc[
reduce(lambda x, y: x & y, enter_long_conditions),