最小化去除iloc[-1], 重构detect_h1_rapid_rise

This commit is contained in:
zhangkun9038@dingtalk.com 2025-09-07 22:45:27 +08:00
parent 0b256e31d9
commit 6286124810

View File

@ -345,19 +345,24 @@ class FreqaiPrimer(IStrategy):
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 使用前一行的市场状态,避免未来数据泄露
current_state = dataframe['prev_market_state'] if 'prev_market_state' in dataframe.columns else 'neutral'
# 确保prev_market_state列存在
if 'prev_market_state' not in dataframe.columns:
dataframe['prev_market_state'] = 'neutral'
# 条件1: 价格接近布林带下轨(允许一定偏差)
close_to_bb_lower_1h = (dataframe['close'] <= dataframe['bb_lower_1h'] * 1.03) # 放宽到3%偏差
# 条件2: RSI 不高于阈值(根据市场状态动态调整)
rsi_threshold = 50 if current_state in ['strong_bull', 'weak_bull'] else 45
rsi_condition_1h = dataframe['rsi_1h'] < rsi_threshold
# 为每一行创建动态阈值
rsi_condition_1h = dataframe.apply(lambda row:
row['rsi_1h'] < 50 if row['prev_market_state'] in ['strong_bull', 'weak_bull'] else row['rsi_1h'] < 45,
axis=1)
# 条件3: StochRSI 处于超卖区域(根据市场状态动态调整)
stochrsi_threshold = 35 if current_state in ['strong_bull', 'weak_bull'] else 25
stochrsi_condition_1h = (dataframe['stochrsi_k_1h'] < stochrsi_threshold) & (dataframe['stochrsi_d_1h'] < stochrsi_threshold)
stochrsi_condition_1h = dataframe.apply(lambda row:
(row['stochrsi_k_1h'] < 35 and row['stochrsi_d_1h'] < 35) if row['prev_market_state'] in ['strong_bull', 'weak_bull']
else (row['stochrsi_k_1h'] < 25 and row['stochrsi_d_1h'] < 25),
axis=1)
# 条件4: MACD 上升趋势
macd_condition_1h = dataframe['macd_1h'] > dataframe['macd_signal_1h']