This commit is contained in:
薇薇安 2026-02-06 08:21:29 +08:00
parent 79fb20bf41
commit 7e62247217

View File

@ -959,6 +959,39 @@ class RiskManager:
f"({final_stop_loss_pct_margin:.2f}% > {stop_loss_percent*100:.1f}%)"
f"但予以保留(风险已通过仓位控制)"
)
# ⚠️ 最终强制检查:最小止损距离(防止 -2021 Order would immediately trigger
# 此检查必须在最后执行,覆盖之前的逻辑,因为这是交易所/系统的硬性约束
if stop_loss_price_price is not None:
adjusted = False
if side == 'BUY':
# 做多:止损价必须足够低 (SL <= Entry * (1 - MinDist))
if final_stop_loss > stop_loss_price_price:
logger.warning(
f"⚠️ 止损价({final_stop_loss:.4f})离入场价太近,"
f"强制调整为最小距离止损价({stop_loss_price_price:.4f})"
)
final_stop_loss = stop_loss_price_price
selected_method = '最小距离(强制)'
adjusted = True
else:
# 做空:止损价必须足够高 (SL >= Entry * (1 + MinDist))
if final_stop_loss < stop_loss_price_price:
logger.warning(
f"⚠️ 止损价({final_stop_loss:.4f})离入场价太近,"
f"强制调整为最小距离止损价({stop_loss_price_price:.4f})"
)
final_stop_loss = stop_loss_price_price
selected_method = '最小距离(强制)'
adjusted = True
if adjusted:
# 重新计算保证金百分比用于日志
if side == 'BUY':
final_stop_loss_amount = (entry_price - final_stop_loss) * quantity
else:
final_stop_loss_amount = (final_stop_loss - entry_price) * quantity
final_stop_loss_pct_margin = (final_stop_loss_amount / margin * 100) if margin > 0 else 0
logger.info(
f"最终止损 ({side}): {final_stop_loss:.4f} (使用{selected_method}), "