From 5efe759c93b58e2f35c51e12cc471f53d3ef618c Mon Sep 17 00:00:00 2001 From: "zhangkun9038@dingtalk.com" Date: Sun, 7 Sep 2025 18:29:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=87=BA=E6=9D=A1=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=B9=B6=E4=BD=BF=E7=94=A8hyperopt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- freqtrade/templates/freqaiprimer.py | 53 ++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/freqtrade/templates/freqaiprimer.py b/freqtrade/templates/freqaiprimer.py index 67e4ef0d..1b71fbf0 100644 --- a/freqtrade/templates/freqaiprimer.py +++ b/freqtrade/templates/freqaiprimer.py @@ -93,6 +93,15 @@ class FreqaiPrimer(IStrategy): H1_MAX_CANDLES = IntParameter(100, 300, default=240, space="buy", optimize=True, load=True) # 检查最近200根1h K线 H1_RAPID_RISE_THRESHOLD = DecimalParameter(0.05, 0.20, default=0.12, decimals=2, space="buy", optimize=True, load=True) # 5%的抬升阈值 H1_MAX_CONSECUTIVE_CANDLES = IntParameter(1, 5, default=1, space="buy", optimize=True, load=True) # 最多连续3根K线内检查 + + # 退出逻辑相关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=9, space="sell", optimize=True, load=True) # 最大持仓时间 + exit_profit_strong_bull_start = DecimalParameter(0.02, 0.04, default=0.03, decimals=3, space="sell", optimize=True, load=True) # 强劲牛市起始利润阈值 + exit_profit_weak_bull_start = DecimalParameter(0.015, 0.025, default=0.02, decimals=3, space="sell", optimize=True, load=True) # 弱牛市起始利润阈值 + exit_profit_neutral_start = DecimalParameter(0.01, 0.02, default=0.015, decimals=3, space="sell", optimize=True, load=True) # 中性市场起始利润阈值 + exit_profit_bear_start = DecimalParameter(0.005, 0.015, default=0.01, decimals=3, space="sell", optimize=True, load=True) # 熊市起始利润阈值 @@ -479,17 +488,23 @@ 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 + # 计算满足条件的数量 + conditions_met = (breakout_condition.astype(int) + + volume_spike.astype(int) + + macd_downward.astype(int) + + rsi_overbought.astype(int)) + + # 设置出场信号:至少满足两个条件 + final_condition = conditions_met >= 2 # 设置出场信号 dataframe.loc[final_condition, 'exit_long'] = 1 @@ -711,9 +726,9 @@ class FreqaiPrimer(IStrategy): # 如果current_time没有时区信息,直接计算 holding_time = (current_time - trade.open_date).total_seconds() / 3600 - # 如果持仓时间超过9小时,强制平仓 - if holding_time >= 9: - logger.info(f"[{pair}] 持仓时间超过9小时,强制平仓") + # 如果持仓时间超过最大允许时间,立即强制平仓(使用sell空间参数) + if holding_time >= self.exit_max_hold_hours.value: + logger.info(f"[{pair}] 持仓时间超过{self.exit_max_hold_hours.value}小时,强制平仓") return 1.0 # 全部退出 """渐进式止盈逻辑""" @@ -724,10 +739,22 @@ class FreqaiPrimer(IStrategy): # 定义更激进的渐进式止盈水平,提高收益上限 profit_levels = { # 状态: [(止盈触发利润, 止盈比例)] - 'strong_bull': [(0.03, 0.2), (0.06, 0.4), (0.09, 0.6), (0.12, 0.8), (0.15, 1.0)], # 强劲牛市的渐进止盈,降低目标 - 'weak_bull': [(0.02, 0.3), (0.04, 0.5), (0.06, 0.7), (0.08, 0.9)], # 弱牛市的渐进止盈 - 'neutral': [(0.015, 0.4), (0.03, 0.6), (0.045, 0.8), (0.06, 1.0)], # 中性市场的渐进止盈 - 'bear': [(0.01, 0.6), (0.015, 0.8), (0.02, 1.0)] # 熊市的渐进止盈(更保守) + 'strong_bull': [(self.exit_profit_strong_bull_start.value, 0.2), + (self.exit_profit_strong_bull_start.value * 2, 0.4), + (self.exit_profit_strong_bull_start.value * 3, 0.6), + (self.exit_profit_strong_bull_start.value * 4, 0.8), + (self.exit_profit_strong_bull_start.value * 5, 1.0)], # 强劲牛市的渐进止盈 + 'weak_bull': [(self.exit_profit_weak_bull_start.value, 0.3), + (self.exit_profit_weak_bull_start.value * 2, 0.5), + (self.exit_profit_weak_bull_start.value * 3, 0.7), + (self.exit_profit_weak_bull_start.value * 4, 0.9)], # 弱牛市的渐进止盈 + 'neutral': [(self.exit_profit_neutral_start.value, 0.4), + (self.exit_profit_neutral_start.value * 2, 0.6), + (self.exit_profit_neutral_start.value * 3, 0.8), + (self.exit_profit_neutral_start.value * 4, 1.0)], # 中性市场的渐进止盈 + 'bear': [(self.exit_profit_bear_start.value, 0.6), + (self.exit_profit_bear_start.value * 1.5, 0.8), + (self.exit_profit_bear_start.value * 2, 1.0)] # 熊市的渐进止盈(更保守) } # 默认使用中性市场的止盈设置