fix(position_manager): 优化止损价格验证逻辑以增强风险控制

在持仓管理中,更新了止损价格的验证逻辑,确保多单止损价格不低于入场价,空单止损价格不高于入场价。同时,增加了对止损价格过近的警告和修正机制,以确保止损策略的有效性和安全性。这一改动旨在提升风险控制能力,确保交易策略的稳健性。
This commit is contained in:
薇薇安 2026-02-25 20:51:42 +08:00
parent e0dfb4c31e
commit 1c33096917

View File

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