From 1c330969171824fc5165cec601a52d73e0c978db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Wed, 25 Feb 2026 20:51:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(position=5Fmanager):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=AD=A2=E6=8D=9F=E4=BB=B7=E6=A0=BC=E9=AA=8C=E8=AF=81=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BB=A5=E5=A2=9E=E5=BC=BA=E9=A3=8E=E9=99=A9=E6=8E=A7?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在持仓管理中,更新了止损价格的验证逻辑,确保多单止损价格不低于入场价,空单止损价格不高于入场价。同时,增加了对止损价格过近的警告和修正机制,以确保止损策略的有效性和安全性。这一改动旨在提升风险控制能力,确保交易策略的稳健性。 --- trading_system/position_manager.py | 37 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index 28a7056..a030258 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -1412,30 +1412,35 @@ class PositionManager: logger.warning(f"{symbol} 止损或止盈价格为空,跳过挂保护单: stop_loss={stop_loss}, take_profit={take_profit}") return - # 验证止损价格是否合理;若无效则自动修正为最小安全距离,避免跳过挂单导致无保护 + # 验证止损价格是否合理。保本/移动止损时:多单止损可≥入场价、空单止损可≤入场价,不得被改回亏损价 entry_price = position_info.get("entryPrice") if entry_price: try: entry_price_val = float(entry_price) stop_loss_val = float(stop_loss) - # 验证止损价格方向:BUY时止损价应低于入场价,SELL时止损价应高于入场价 min_gap_pct = 0.005 # 最小 0.5% 距离,与 risk_manager 一致 + # 多单止损≥入场价 = 保本/移动止损,有效;空单止损≤入场价 = 保本/移动止损,有效 if side == "BUY" and stop_loss_val >= entry_price_val: - safe_sl = entry_price_val * (1 - min_gap_pct) - logger.warning( - f"{symbol} 止损价({stop_loss_val:.8f})>=入场价({entry_price_val:.8f}) 无效(BUY)," - f"已修正为 {safe_sl:.8f} 并继续挂单" - ) - stop_loss = safe_sl - position_info["stopLoss"] = stop_loss + # 保本或移动止损,保留不修正 + pass elif side == "SELL" and stop_loss_val <= entry_price_val: - safe_sl = entry_price_val * (1 + min_gap_pct) - logger.warning( - f"{symbol} 止损价({stop_loss_val:.8f})<=入场价({entry_price_val:.8f}) 无效(SELL)," - f"已修正为 {safe_sl:.8f} 并继续挂单" - ) - stop_loss = safe_sl - position_info["stopLoss"] = stop_loss + pass + elif side == "BUY" and stop_loss_val < entry_price_val: + if stop_loss_val > entry_price_val * (1 - min_gap_pct): + safe_sl = entry_price_val * (1 - min_gap_pct) + logger.warning( + f"{symbol} 止损价({stop_loss_val:.8f})距入场过近(BUY),已修正为 {safe_sl:.8f}" + ) + stop_loss = safe_sl + position_info["stopLoss"] = stop_loss + elif side == "SELL" and stop_loss_val > entry_price_val: + if stop_loss_val < entry_price_val * (1 + min_gap_pct): + safe_sl = entry_price_val * (1 + min_gap_pct) + logger.warning( + f"{symbol} 止损价({stop_loss_val:.8f})距入场过近(SELL),已修正为 {safe_sl:.8f}" + ) + stop_loss = safe_sl + position_info["stopLoss"] = stop_loss except Exception as e: logger.warning(f"{symbol} 验证止损价格时出错: {e}")