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}")