优化退出条件, hyperopt sell space

This commit is contained in:
zhangkun9038@dingtalk.com 2025-09-07 11:44:45 +08:00
parent 1ef2463747
commit 0bfb35c841

View File

@ -102,6 +102,14 @@ class FreqaiPrimer(IStrategy):
volume_spike_multiplier = DecimalParameter(1.0, 3.0, default=1.5, decimals=1, space="buy", optimize=True, load=True)
bb_width_min = DecimalParameter(0.01, 0.05, default=0.02, decimals=3, space="buy", optimize=True, load=True)
# 退出逻辑相关hyperopt参数sell空间
exit_volume_multiplier = DecimalParameter(1.5, 3.0, default=2.0, decimals=1, space="sell", optimize=True, load=True) # 成交量放大倍数
exit_rsi_threshold = IntParameter(60, 80, default=65, space="sell", optimize=True, load=True) # 出场RSI阈值
exit_max_hold_hours = IntParameter(8, 12, default=12, space="sell", optimize=True, load=True) # 最大持仓时间
exit_recent_high_threshold = DecimalParameter(0.95, 0.99, default=0.98, decimals=3, space="sell", optimize=True, load=True) # 接近近期高点的阈值
exit_volume_spike_multiplier = DecimalParameter(2.0, 3.0, default=2.5, decimals=1, space="sell", optimize=True, load=True) # 8-12小时区间的成交量放大倍数
exit_profit_threshold = DecimalParameter(0.01, 0.05, default=0.02, decimals=3, space="sell", optimize=True, load=True) # 8-12小时区间的利润阈值
def informative_pairs(self):
pairs = self.dp.current_whitelist()
@ -486,14 +494,14 @@ class FreqaiPrimer(IStrategy):
# 条件1: 价格突破布林带上轨
breakout_condition = dataframe['close'] >= dataframe['bb_upper_1h']
# 条件2: 成交量显著放大
volume_spike = dataframe['volume'] > dataframe['volume_ma'] * 2
# 条件2: 成交量显著放大使用sell空间参数
volume_spike = dataframe['volume'] > dataframe['volume_ma'] * self.exit_volume_multiplier.value
# 条件3: MACD 下降趋势
macd_downward = dataframe['macd_1h'] < dataframe['macd_signal_1h']
# 条件4: RSI 进入超买区域
rsi_overbought = dataframe['rsi_1h'] > self.rsi_overbought.value
# 条件4: RSI 进入超买区域使用sell空间参数
rsi_overbought = dataframe['rsi_1h'] > self.exit_rsi_threshold.value
# 合并所有条件
final_condition = breakout_condition | volume_spike | macd_downward | rsi_overbought
@ -713,22 +721,22 @@ class FreqaiPrimer(IStrategy):
# 如果current_time没有时区信息直接计算
holding_time = (current_time - trade.open_date).total_seconds() / 3600
# 如果持仓时间超过12小时立即强制平仓
if holding_time >= 12:
logger.info(f"[{pair}] 持仓时间超过12小时,立即强制平仓")
# 如果持仓时间超过最大允许时间立即强制平仓使用sell空间参数
if holding_time >= self.exit_max_hold_hours.value:
logger.info(f"[{pair}] 持仓时间超过{self.exit_max_hold_hours.value}小时,立即强制平仓")
return 1.0 # 全部退出
# 在8-12小时之间,寻找最佳退出时机
if 8 <= holding_time < 12:
# 在最小和最大持仓时间之间,寻找最佳退出时机
if 8 <= holding_time < self.exit_max_hold_hours.value:
# 检查是否满足最佳退出时机条件
# 1. 当前价格接近近期高点过去24根K线
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
recent_high = dataframe['high'].rolling(window=24).max().iloc[-1]
price_near_high = (current_rate >= recent_high * 0.98) # 价格在近期高点的2%以内
price_near_high = (current_rate >= recent_high * self.exit_recent_high_threshold.value) # 价格接近近期高点
# 2. RSI处于超买区域基于小时级RSI
if 'rsi_1h' in dataframe.columns:
rsi_overbought = dataframe['rsi_1h'].iloc[-1] > 65
rsi_overbought = dataframe['rsi_1h'].iloc[-1] > self.exit_rsi_threshold.value
else:
rsi_overbought = False
@ -740,17 +748,17 @@ class FreqaiPrimer(IStrategy):
# 4. 成交量明显放大,可能是见顶信号
volume_spike = False
if 'volume' in dataframe.columns and 'volume_ma' in dataframe.columns:
volume_spike = dataframe['volume'].iloc[-1] > dataframe['volume_ma'].iloc[-1] * 2.5
volume_spike = dataframe['volume'].iloc[-1] > dataframe['volume_ma'].iloc[-1] * self.exit_volume_spike_multiplier.value
# 如果满足2个或更多条件认为是较好的退出时机
exit_conditions_count = sum([price_near_high, rsi_overbought, macd_bearish, volume_spike])
if exit_conditions_count >= 2:
logger.info(f"[{pair}] 持仓时间在8-12小时区间,满足{exit_conditions_count}个最佳退出条件,执行平仓")
logger.info(f"[{pair}] 持仓时间在8-{self.exit_max_hold_hours.value}小时区间,满足{exit_conditions_count}个最佳退出条件,执行平仓")
return 1.0 # 全部退出
# 如果有利润保护,也可以考虑在这段时间内逐步止盈
if current_profit > 0.02: # 利润超过2%
logger.info(f"[{pair}] 持仓时间在8-12小时区间,利润{current_profit:.2%},执行止盈")
if current_profit > self.exit_profit_threshold.value: # 利润超过阈值
logger.info(f"[{pair}] 持仓时间在8-{self.exit_max_hold_hours.value}小时区间,利润{current_profit:.2%}超过阈值{self.exit_profit_threshold.value:.2%},执行止盈")
return 1.0 # 全部退出
"""渐进式止盈逻辑"""