用custom_exit来替代custom_roi, 防止结果被hyperopt冲掉
This commit is contained in:
parent
84b625013d
commit
a060f3a496
@ -571,52 +571,11 @@ class FreqaiPrimer(IStrategy):
|
||||
|
||||
def custom_exit(self, pair: str, trade: 'Trade', current_time, current_rate: float,
|
||||
current_profit: float, **kwargs) -> float:
|
||||
"""渐进式止盈逻辑"""
|
||||
|
||||
# 获取当前市场状态
|
||||
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
|
||||
current_state = dataframe['market_state'].iloc[-1] if 'market_state' in dataframe.columns else 'unknown'
|
||||
|
||||
# 定义更激进的渐进式止盈水平,提高收益上限
|
||||
profit_levels = {
|
||||
# 状态: [(止盈触发利润, 止盈比例)]
|
||||
'strong_bull': [(0.04, 0.2), (0.08, 0.4), (0.12, 0.6), (0.16, 0.8), (0.20, 1.0)], # 强劲牛市的渐进止盈,提高目标
|
||||
'weak_bull': [(0.03, 0.3), (0.06, 0.5), (0.09, 0.7), (0.12, 0.9)], # 弱牛市的渐进止盈
|
||||
'neutral': [(0.02, 0.4), (0.04, 0.6), (0.06, 0.8), (0.08, 1.0)], # 中性市场的渐进止盈
|
||||
'bear': [(0.01, 0.6), (0.02, 0.8), (0.03, 1.0)] # 熊市的渐进止盈(更保守)
|
||||
}
|
||||
|
||||
# 默认使用中性市场的止盈设置
|
||||
levels = profit_levels.get(current_state, profit_levels['neutral'])
|
||||
|
||||
# 在强劲牛市中,进一步放宽止盈目标
|
||||
if current_state == 'strong_bull':
|
||||
levels = [(p + 0.01, r) for p, r in levels] # 将止盈触发利润提高1%
|
||||
|
||||
# 确定当前应该止盈的比例
|
||||
exit_ratio = 0.0
|
||||
for profit_target, ratio in levels:
|
||||
if current_profit >= profit_target:
|
||||
exit_ratio = ratio
|
||||
else:
|
||||
break
|
||||
|
||||
# 记录渐进式止盈决策
|
||||
if exit_ratio > 0:
|
||||
logger.info(f"[{pair}] 渐进式止盈: 当前利润 {current_profit:.2%}, 市场状态 {current_state}, 止盈比例 {exit_ratio:.0%}")
|
||||
|
||||
# 返回应退出的比例(0.0表示不退出,1.0表示全部退出)
|
||||
return exit_ratio
|
||||
|
||||
def custom_roi(self, pair: str, trade: 'Trade', current_time, **kwargs) -> float:
|
||||
"""
|
||||
自定义动态ROI函数,使用指数衰减公式:ROI(t) = a·e^(-k·t) + c
|
||||
- t: 交易已持仓时间(分钟)
|
||||
- a: 初始ROI水平参数
|
||||
- k: 衰减速率参数
|
||||
- c: 最低ROI水平参数
|
||||
|
||||
该实现支持快进快出和趋势奔跑策略,并仅用于多头交易。
|
||||
结合指数衰减ROI逻辑的动态止盈函数
|
||||
- 根据交易持仓时间使用指数衰减公式计算动态止盈阈值
|
||||
- 考虑当前市场状态调整止盈策略
|
||||
- 仅支持多头交易
|
||||
"""
|
||||
# 只支持多头
|
||||
if trade.is_short:
|
||||
@ -629,21 +588,56 @@ class FreqaiPrimer(IStrategy):
|
||||
if trade_age_minutes < 0:
|
||||
trade_age_minutes = 0
|
||||
|
||||
# 获取Hyperopt优化的参数值
|
||||
# 获取Hyperopt优化的指数衰减参数值
|
||||
a = self.roi_param_a.value
|
||||
k = self.roi_param_k.value
|
||||
c = self.roi_param_c.value
|
||||
|
||||
# 应用指数衰减公式计算ROI阈值
|
||||
roi_threshold = a * math.exp(-k * trade_age_minutes) + c
|
||||
# 应用指数衰减公式计算动态ROI阈值
|
||||
dynamic_roi_threshold = a * math.exp(-k * trade_age_minutes) + c
|
||||
|
||||
# 确保ROI阈值为正数
|
||||
roi_threshold = max(roi_threshold, 0.0)
|
||||
dynamic_roi_threshold = max(dynamic_roi_threshold, 0.0)
|
||||
|
||||
# 记录ROI计算日志
|
||||
logger.info(f"[{pair}] 动态ROI计算: 持仓时间={trade_age_minutes:.1f}分钟, a={a:.3f}, k={k:.4f}, c={c:.4f}, ROI阈值={roi_threshold:.4f}")
|
||||
# 获取当前市场状态以调整止盈策略
|
||||
dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)
|
||||
current_state = dataframe['market_state'].iloc[-1] if 'market_state' in dataframe.columns else 'unknown'
|
||||
|
||||
return roi_threshold
|
||||
# 根据市场状态调整退出比例计算
|
||||
exit_ratio = 0.0
|
||||
|
||||
# 计算当前利润与动态ROI阈值的比值
|
||||
if dynamic_roi_threshold > 0:
|
||||
profit_ratio = current_profit / dynamic_roi_threshold
|
||||
else:
|
||||
profit_ratio = 0
|
||||
|
||||
# 根据市场状态和利润比值确定退出比例
|
||||
if profit_ratio >= 1.0:
|
||||
# 利润达到或超过动态ROI阈值
|
||||
if current_state == 'strong_bull':
|
||||
# 强劲牛市中,允许部分利润奔跑
|
||||
if profit_ratio < 1.5:
|
||||
exit_ratio = 0.5 # 达到ROI阈值时只退出50%
|
||||
else:
|
||||
exit_ratio = 0.8 # 利润显著高于阈值时退出80%
|
||||
elif current_state == 'weak_bull':
|
||||
# 弱牛市中,平衡利润和风险
|
||||
if profit_ratio < 1.2:
|
||||
exit_ratio = 0.6 # 达到ROI阈值时退出60%
|
||||
else:
|
||||
exit_ratio = 0.9 # 利润高于阈值时退出90%
|
||||
else:
|
||||
# 其他市场状态下,更保守的策略
|
||||
exit_ratio = 1.0 # 达到ROI阈值时全部退出
|
||||
|
||||
# 记录动态止盈决策
|
||||
logger.info(f"[{pair}] 动态止盈: 持仓时间={trade_age_minutes:.1f}分钟, 当前利润={current_profit:.2%}, \
|
||||
动态ROI阈值={dynamic_roi_threshold:.4f}, 利润比值={profit_ratio:.2f}, \
|
||||
市场状态={current_state}, 退出比例={exit_ratio:.0%}")
|
||||
|
||||
# 返回应退出的比例(0.0表示不退出,1.0表示全部退出)
|
||||
return exit_ratio
|
||||
|
||||
def adjust_trade_position(self, trade: 'Trade', current_time, current_rate: float,
|
||||
current_profit: float, min_stake: float, max_stake: float, **kwargs) -> float:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user