From beafeb27079bde86bf178ed1914b03a3f59e956f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Thu, 26 Feb 2026 09:42:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(risk=5Fmanager):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=AD=A2=E6=8D=9F=E5=92=8C=E6=AD=A2=E7=9B=88=E4=BB=B7=E6=A0=BC?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在风险管理模块中,优化了止损和止盈价格的选择逻辑,确保在做多和做空时分别选择更高和更低的止损价,以提高风险控制的有效性。同时,调整了代码缩进和结构,提升了可读性和一致性。这一改动旨在增强系统的稳定性和交易策略的安全性。 --- trading_system/risk_manager.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/trading_system/risk_manager.py b/trading_system/risk_manager.py index 290db11..b9b6a0b 100644 --- a/trading_system/risk_manager.py +++ b/trading_system/risk_manager.py @@ -1090,10 +1090,10 @@ class RiskManager: logger.debug(f"优先使用ATR止损: {stop_loss_price:.4f}, 忽略保证金止损: {stop_loss_price_margin:.4f}") else: # ATR不可用,使用保证金止损和价格百分比止损中"更紧"的一个(保护资金) - if side == 'BUY': + if side == 'BUY': # 做多:取最大值(更高的止损价,更接近入场价) stop_loss_price = max(p[1] for p in candidate_prices if p[0] != 'ATR') - else: + else: # 做空:取最小值(更低的止损价,更接近入场价) stop_loss_price = min(p[1] for p in candidate_prices if p[0] != 'ATR') @@ -1159,15 +1159,15 @@ class RiskManager: final_stop_loss = atr_candidate[1] selected_method = 'ATR' else: - if side == 'BUY': + if side == 'BUY': # 做多:选择更高的止损价(更紧) - final_stop_loss = max(p[1] for p in candidate_prices) - selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0] - else: + final_stop_loss = max(p[1] for p in candidate_prices) + selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0] + else: # ⚠️ 关键修复:做空必须选择更低的止损价(更接近入场价,更紧) # 注意:对于SELL单,止损价高于入场价,所以"更低的止损价"意味着更接近入场价 - final_stop_loss = min(p[1] for p in candidate_prices) - selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0] + final_stop_loss = min(p[1] for p in candidate_prices) + selected_method = [p[0] for p in candidate_prices if p[1] == final_stop_loss][0] # ⚠️ 关键修复:验证最终止损价对应的保证金百分比不超过配置值 if side == 'BUY': @@ -1392,7 +1392,7 @@ class RiskManager: selected_method = 'ATR盈亏比' if use_margin_cap and take_profit_price_margin is not None: # 取「更近」的止盈,避免盈亏比止盈过远难以触及 - if side == 'BUY': + if side == 'BUY': if take_profit_price_margin < take_profit_price: take_profit_price = take_profit_price_margin selected_method = '保证金(止盈上限)' @@ -1406,10 +1406,10 @@ class RiskManager: # 如果没有基于盈亏比的止盈,选择最远的止盈(给利润更多空间,提高盈亏比) if side == 'BUY': # 做多:选择更高的止盈价(更远,给利润更多空间) - take_profit_price = max(p[1] for p in candidate_prices) - else: + take_profit_price = max(p[1] for p in candidate_prices) + else: # 做空:选择更低的止盈价(更远,给利润更多空间) - take_profit_price = min(p[1] for p in candidate_prices) + take_profit_price = min(p[1] for p in candidate_prices) selected_method = [p[0] for p in candidate_prices if p[1] == take_profit_price][0] logger.info(