revert detect_h1_rapid_rise

This commit is contained in:
Ubuntu 2025-11-23 19:22:08 +08:00
parent a4a9d6dfd9
commit 0124b8cd21

View File

@ -488,38 +488,58 @@ class FreqaiPrimer(IStrategy):
def detect_h1_rapid_rise(self, pair: str) -> bool:
"""
修复版剧烈拉升检测 同时兼容 backtest / dry-run / live
关键改进
- 强制排除最后一根未闭合的 1h K线解决 dry-run 误杀
- 只看最近 N 根完整K线N hyperopt安全范围 16-50
- 历史污点终身制彻底废除
检测1小时K线图上的剧烈拉升情况轻量级版本用于confirm_trade_entry
参数:
- pair: 交易对
返回:
- bool: 是否处于不稳固区域
"""
try:
# 获取1小时K线数据
df_1h = self.dp.get_pair_dataframe(pair=pair, timeframe='1h')
lookback = self.h1_max_candles.value # ← 这里用你的超参数!安全范围 16-50
# 必须有足够数据
if len(df_1h) < lookback + 5: # 留点余量
# 获取当前优化参数值
max_candles = self.h1_max_candles.value
rapid_rise_threshold = self.h1_rapid_rise_threshold.value
max_consecutive_candles = self.h1_max_consecutive_candles.value
# 确保有足够的K线数据
if len(df_1h) < max_candles:
logger.warning(f"[{pair}] 1h K线数据不足 {max_candles} 根,当前只有 {len(df_1h)} 根,无法完整检测剧烈拉升")
return False
# 关键只看已闭合的K线干掉最后一根正在形成的蜡烛
closed_candles = df_1h.iloc[-lookback-1:-1] # 取最近 lookback 根完整K线
# 计算这段时间内从最低点到最高点的最大涨幅
low_price = closed_candles['low'].min()
high_price = closed_candles['high'].max()
if low_price <= 0:
return False
rise_ratio = (high_price - low_price) / low_price
# 是否触发剧烈拉升阈值
return rise_ratio >= self.h1_rapid_rise_threshold.value
# 获取最近的K线
recent_data = df_1h.iloc[-max_candles:].copy()
# 检查连续最多几根K线内的最大涨幅
rapid_rise_detected = False
max_rise = 0
for i in range(len(recent_data) - max_consecutive_candles + 1):
window_data = recent_data.iloc[i:i + max_consecutive_candles]
window_low = window_data['low'].min()
window_high = window_data['high'].max()
# 计算区间内的最大涨幅
if window_low > 0:
rise_percentage = (window_high - window_low) / window_low
if rise_percentage > max_rise:
max_rise = rise_percentage
# 检查是否超过阈值
if rise_percentage >= rapid_rise_threshold:
rapid_rise_detected = True
#logger.info(f"[{pair}] 检测到剧烈拉升: 从 {window_low:.2f} 到 {window_high:.2f} ({rise_percentage:.2%}) 在 {max_consecutive_candles} 根K线内")
break
current_price = recent_data['close'].iloc[-1]
#logger.info(f"[{pair}] 剧烈拉升检测结果: {'不稳固' if rapid_rise_detected else '稳固'}")
#logger.info(f"[{pair}] 最近最大涨幅: {max_rise:.2%}")
return rapid_rise_detected
except Exception as e:
logger.debug(f"[{pair}] 剧烈拉升检测异常: {e}")
logger.error(f"[{pair}] 剧烈拉升检测过程中发生错误: {str(e)}")
return False
def confirm_trade_entry(