间隔时间改为根据市场状态动态调整

This commit is contained in:
zhangkun9038@dingtalk.com 2026-01-09 23:03:19 +08:00
parent 7e52719054
commit 744c814f94

View File

@ -821,6 +821,9 @@ class FreqaiPrimer(IStrategy):
f"时间: {current_time.strftime('%H:%M')}"
)
# 更新最后入场时间,用于入场间隔控制
self._last_entry_time[trade.pair] = current_time
except Exception as e:
self.logger.debug(f"order_filled 日志失败 [{trade.pair}]: {e}")
@ -904,7 +907,21 @@ class FreqaiPrimer(IStrategy):
default_threshold = 0.50
return thresholds_map.get(market_state, {}).get(threshold_type, default_threshold)
def get_entry_interval_by_market_state(self, market_state: str) -> int:
"""
根据市场状态返回入场间隔时间分钟
强牛市20分钟强熊市150分钟其他状态按比例插值
"""
interval_map = {
'strong_bull': 20, # 强牛市间隔20分钟
'weak_bull': 55, # 弱牛市间隔55分钟 (20 + (150-20) * 1/4)
'neutral': 85, # 中性市场间隔85分钟 (20 + (150-20) * 1/2)
'weak_bear': 115, # 弱熊市间隔115分钟 (20 + (150-20) * 3/4)
'strong_bear': 150 # 强熊市间隔150分钟
}
return interval_map.get(market_state, 85) # 默认为中性市场间隔时间
def confirm_trade_entry(
self,
pair: str,
@ -927,12 +944,25 @@ class FreqaiPrimer(IStrategy):
# 仅对多头交易进行检查
if side == 'long':
# 检查1入场间隔控制使用hyperopt参数
# 检查1入场间隔控制根据市场状态动态调整
if pair in self._last_entry_time:
# 获取当前市场状态
market_state = 'neutral' # 默认值
try:
df, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
if len(df) > 0:
last_row = df.iloc[-1]
market_state = str(last_row.get('market_state', 'neutral'))
except Exception as e:
self.strategy_log(f"[{pair}] 获取市场状态失败: {e}")
# 根据市场状态计算动态间隔时间
dynamic_interval = self.get_entry_interval_by_market_state(market_state)
last_entry = self._last_entry_time[pair]
time_diff = (current_time - last_entry).total_seconds() * 0.0166666667 # 转换为分钟(使用乘法避免除法)
if time_diff < self.entry_interval_minutes.value:
self.strategy_log(f"[{pair}] 入场间隔不足: 距离上次入场 {time_diff:.1f}分钟 < {self.entry_interval_minutes.value}分钟,取消本次入场")
if time_diff < dynamic_interval:
self.strategy_log(f"[{pair}] 入场间隔不足: 距离上次入场 {time_diff:.1f}分钟 < {dynamic_interval}分钟(市场: {market_state},取消本次入场")
allow_trade = False
# 检查2检查是否处于剧烈拉升的不稳固区域