fix(gate-bot): allow profit-side stop loss on TP/SL entrust
Skip min planned RR when stop is on the winning side of entry; validate entrust against open price and fall back to plan take-profit when omitted.
This commit is contained in:
@@ -46,6 +46,39 @@ def tpsl_slot_trigger_price(slot: Any) -> Optional[float]:
|
||||
return None
|
||||
|
||||
|
||||
def stop_is_profit_protecting(direction: str, entry_price: Any, stop_loss: Any) -> bool:
|
||||
"""
|
||||
止损是否已在盈利侧(保本/锁盈),不再适用「开仓盈亏比」风控。
|
||||
做空:止损 < 成交价;做多:止损 > 成交价。
|
||||
"""
|
||||
entry = _positive_float(entry_price)
|
||||
sl = _positive_float(stop_loss)
|
||||
if entry is None or sl is None:
|
||||
return False
|
||||
d = (direction or "long").strip().lower()
|
||||
if d == "short":
|
||||
return sl < entry
|
||||
return sl > entry
|
||||
|
||||
|
||||
def tpsl_update_passes_rr_gate(
|
||||
direction: str,
|
||||
entry_price: Any,
|
||||
stop_loss: Any,
|
||||
take_profit: Any,
|
||||
min_rr: float,
|
||||
calc_rr_ratio_fn: Callable[..., Optional[float]],
|
||||
) -> tuple[bool, Optional[str]]:
|
||||
"""持仓委托改价:盈利侧止损跳过最低盈亏比;否则按开仓价几何校验。"""
|
||||
if stop_is_profit_protecting(direction, entry_price, stop_loss):
|
||||
return True, None
|
||||
rr = calc_rr_ratio_fn(direction or "long", entry_price, stop_loss, take_profit)
|
||||
if rr is not None and rr >= float(min_rr):
|
||||
return True, None
|
||||
rr_txt = f"{rr:.4f}" if rr is not None else "无法计算"
|
||||
return False, f"计划盈亏比 {rr_txt}:1 低于最低要求 {min_rr}:1(盈利侧保本止损不受此限)"
|
||||
|
||||
|
||||
def is_sl_breakeven_secured(direction: str, entry_price: Any, exchange_sl_price: Any) -> bool:
|
||||
"""
|
||||
交易所当前止损相对开仓成交价是否已保本。
|
||||
|
||||
Reference in New Issue
Block a user