检查 MACD 列是否存在
Some checks are pending
Update Docker Hub Description / dockerHubDescription (push) Waiting to run
Some checks are pending
Update Docker Hub Description / dockerHubDescription (push) Waiting to run
This commit is contained in:
parent
ff7aff8ee7
commit
8f4c374e6b
@ -70,7 +70,6 @@
|
||||
"learning_rate": 0.05,
|
||||
"max_depth": 5
|
||||
},
|
||||
"train_period_days": 15,
|
||||
"train_period_days": 180,
|
||||
"backtest_period_days": 60,
|
||||
"live_retrain_hours": 0,
|
||||
|
||||
@ -76,9 +76,14 @@ class FreqaiExampleStrategy(IStrategy):
|
||||
dataframe["rsi"] = ta.RSI(dataframe, timeperiod=14)
|
||||
|
||||
# 确保 MACD 列被正确计算并保留
|
||||
try:
|
||||
macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9)
|
||||
dataframe["macd"] = macd["macd"]
|
||||
dataframe["macdsignal"] = macd["macdsignal"]
|
||||
except Exception as e:
|
||||
logger.error(f"计算 MACD 列时出错:{str(e)}")
|
||||
dataframe["macd"] = np.nan
|
||||
dataframe["macdsignal"] = np.nan
|
||||
|
||||
# 检查 MACD 列是否存在
|
||||
if "macd" not in dataframe.columns or "macdsignal" not in dataframe.columns:
|
||||
@ -290,21 +295,29 @@ class FreqaiExampleStrategy(IStrategy):
|
||||
# 改进买入信号条件
|
||||
# 检查 MACD 列是否存在
|
||||
if "macd" not in df.columns or "macdsignal" not in df.columns:
|
||||
logger.error("MACD 或 MACD 信号列缺失,无法生成买入信号")
|
||||
raise ValueError("DataFrame 缺少必要的 MACD 列")
|
||||
logger.error("MACD 或 MACD 信号列缺失,无法生成买入信号。尝试重新计算 MACD 列。")
|
||||
|
||||
# 检查 MACD 列是否存在
|
||||
if "macd" not in df.columns or "macdsignal" not in df.columns:
|
||||
logger.error("MACD 或 MACD 信号列缺失,无法生成买入信号")
|
||||
raise ValueError("DataFrame 缺少必要的 MACD 列")
|
||||
try:
|
||||
macd = ta.MACD(df, fastperiod=12, slowperiod=26, signalperiod=9)
|
||||
df["macd"] = macd["macd"]
|
||||
df["macdsignal"] = macd["macdsignal"]
|
||||
logger.info("MACD 列已成功重新计算。")
|
||||
except Exception as e:
|
||||
logger.error(f"重新计算 MACD 列时出错:{str(e)}")
|
||||
raise ValueError("DataFrame 缺少必要的 MACD 列且无法重新计算。")
|
||||
|
||||
enter_long_conditions = [
|
||||
(df["rsi"] < df["buy_rsi_pred"]), # RSI 低于买入阈值
|
||||
(df["volume"] > df["volume"].rolling(window=10).mean() * 1.2), # 成交量高于近期均值20%
|
||||
(df["close"] > df["bb_middleband"]), # 价格高于布林带中轨
|
||||
(df["macd"] > df["macdsignal"]), # MACD 金叉
|
||||
(df["do_predict"] == 1) # 确保模型预测为买入
|
||||
(df["close"] > df["bb_middleband"]) # 价格高于布林带中轨
|
||||
]
|
||||
|
||||
# 如果 MACD 列存在,则添加 MACD 金叉条件
|
||||
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))
|
||||
if enter_long_conditions:
|
||||
df.loc[
|
||||
reduce(lambda x, y: x & y, enter_long_conditions),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user