完全取消EMA限制
This commit is contained in:
parent
21fdcd66ae
commit
1687e2ab37
@ -659,30 +659,19 @@ class FreqaiPrimer(IStrategy):
|
||||
# 辅助条件: 3m 和 15m 趋势确认(允许部分时间框架不一致)
|
||||
trend_confirmation = (dataframe['trend_3m'] == 1) | (dataframe['trend_15m'] == 1)
|
||||
|
||||
# 新增:EMA趋势过滤条件(核心入场限制,进一步放宽)
|
||||
# 方案2:最近N根K线内发生过EMA5向上穿越,或者当前EMA5在EMA20之上
|
||||
# 这样既能捕捉趋势启动,又能在趋势延续时继续入场
|
||||
# 添加防御性检查,防止列不存在
|
||||
if 'ema_5_1h' in dataframe.columns and 'ema_20_1h' in dataframe.columns:
|
||||
# 条件1:EMA5保持在EMA20之上(趋势延续)
|
||||
ema5_above_ema20 = dataframe['ema_5_1h'] > dataframe['ema_20_1h']
|
||||
|
||||
# 条件2:最近20根1h K线内发生过向上穿越(捕捉趋势启动后的窗口期)
|
||||
if 'ema5_cross_above_ema20' in dataframe.columns:
|
||||
# 使用rolling.max检查最近20根K线内是否有True值
|
||||
recent_cross = dataframe['ema5_cross_above_ema20'].rolling(window=20, min_periods=1).max() == 1
|
||||
# 两个条件满足其一即可:要么当前在上方,要么最近穿越过
|
||||
ema_trend_filter = ema5_above_ema20 | recent_cross
|
||||
else:
|
||||
# 如果没有交叉列,只用保持在上方的条件
|
||||
ema_trend_filter = ema5_above_ema20
|
||||
else:
|
||||
# 如果列不存在,创建一个全False的Series(不允许入场)
|
||||
self.strategy_log(f"[{metadata['pair']}] 警告:ema_5_1h或ema_20_1h列不存在,过滤条件设为False")
|
||||
ema_trend_filter = pd.Series(False, index=dataframe.index)
|
||||
# EMA趋势过滤条件(已取消,太严格导致入场太少)
|
||||
# 如果需要重新启用,取消下面注释并在final_condition中加上 & ema_trend_filter
|
||||
# if 'ema_5_1h' in dataframe.columns and 'ema_20_1h' in dataframe.columns:
|
||||
# ema5_above_ema20 = dataframe['ema_5_1h'] > dataframe['ema_20_1h']
|
||||
# if 'ema5_cross_above_ema20' in dataframe.columns:
|
||||
# recent_cross = dataframe['ema5_cross_above_ema20'].rolling(window=20, min_periods=1).max() == 1
|
||||
# ema_trend_filter = ema5_above_ema20 | recent_cross
|
||||
# else:
|
||||
# ema_trend_filter = ema5_above_ema20
|
||||
# else:
|
||||
# ema_trend_filter = pd.Series(False, index=dataframe.index)
|
||||
|
||||
# 合并所有条件(减少强制性条件)
|
||||
# 至少满足多个条件中的一定数量,并且必须满足EMA5向上穿越EMA20
|
||||
# 合并所有条件(已移除EMA过滤限制)
|
||||
condition_count = (
|
||||
close_to_bb_lower_1h.astype(int) +
|
||||
rsi_condition_1h.astype(int) +
|
||||
@ -691,9 +680,8 @@ class FreqaiPrimer(IStrategy):
|
||||
(volume_spike | bb_width_condition).astype(int) + # 成交量或布林带宽度满足其一即可
|
||||
trend_confirmation.astype(int)
|
||||
)
|
||||
# 修改最终条件:必须同时满足最小条件数量和EMA趋势过滤
|
||||
basic_condition = condition_count >= self.min_condition_count.value
|
||||
final_condition = basic_condition & ema_trend_filter
|
||||
# 最终条件:至少满足最小条件数量(已移除EMA过滤限制)
|
||||
final_condition = condition_count >= self.min_condition_count.value
|
||||
|
||||
# 设置入场信号
|
||||
dataframe.loc[final_condition, 'enter_long'] = 1
|
||||
@ -767,13 +755,6 @@ class FreqaiPrimer(IStrategy):
|
||||
dataframe.loc[final_condition_updated, 'enter_price'] = dataframe.loc[final_condition_updated, 'close'] * 0.9833
|
||||
|
||||
# 增强调试信息
|
||||
# 确保ema_trend_filter是Series类型才能调用sum()
|
||||
if isinstance(ema_trend_filter, pd.Series):
|
||||
ema_trend_count = ema_trend_filter.sum()
|
||||
else:
|
||||
ema_trend_count = 0
|
||||
|
||||
basic_condition_count = basic_condition.sum()
|
||||
final_condition_count = final_condition.sum()
|
||||
|
||||
self.strategy_log(f"[{metadata['pair']}] 入场条件检查:")
|
||||
@ -783,13 +764,7 @@ class FreqaiPrimer(IStrategy):
|
||||
self.strategy_log(f" - MACD 上升趋势: {macd_condition_1h.sum()} 次")
|
||||
self.strategy_log(f" - 成交量或布林带宽度: {(volume_spike | bb_width_condition).sum()} 次")
|
||||
self.strategy_log(f" - 趋势确认: {trend_confirmation.sum()} 次")
|
||||
self.strategy_log(f" - EMA趋势过滤(在上方或20根K线内穿越): {ema_trend_count} 次")
|
||||
self.strategy_log(f" - 基本条件满足: {basic_condition_count} 次")
|
||||
self.strategy_log(f" - 最终条件(基本+EMA过滤): {final_condition_count} 次")
|
||||
|
||||
# 如果EMA条件满足但最终条件未满足,输出详细信息
|
||||
if ema_trend_count > 0 and final_condition_count == 0:
|
||||
self.strategy_log(f"[{metadata['pair']}] 注意:检测到 {ema_trend_count} 次EMA趋势过滤满足,但由于其他条件不足未能生成入场信号")
|
||||
self.strategy_log(f" - 最终入场信号: {final_condition_count} 次")
|
||||
# 在populate_entry_trend方法末尾添加
|
||||
# 计算条件间的相关性
|
||||
conditions = DataFrame({
|
||||
@ -798,8 +773,7 @@ class FreqaiPrimer(IStrategy):
|
||||
'stochrsi': stochrsi_condition_1h,
|
||||
'macd': macd_condition_1h,
|
||||
'vol_bb': (volume_spike | bb_width_condition),
|
||||
'trend': trend_confirmation,
|
||||
'ema_trend': ema_trend_filter
|
||||
'trend': trend_confirmation
|
||||
})
|
||||
correlation = conditions.corr().mean().mean()
|
||||
#self.strategy_log(f"[{metadata['pair']}] 条件平均相关性: {correlation:.2f}")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user