修复审计P1:基数纠偏用开仓止损并清空币数量、Gate保证金/手动平仓成交价/全仓实盘空仓校验、OKX·Binance占位函数、快照超时。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -134,6 +134,12 @@ def resolve_trade_record_margin_usdt(
|
||||
"""写入 trade_records.基数:计划保证金优先于异常交易所快照;禁止币×价÷杠杆虚增."""
|
||||
plan = _pos_float(plan_margin_capital)
|
||||
ex = _pos_float(exchange_margin_usdt)
|
||||
from_risk = margin_from_risk_amount(
|
||||
risk_amount,
|
||||
trigger_price=trigger_price,
|
||||
stop_loss=stop_loss,
|
||||
leverage=leverage,
|
||||
)
|
||||
|
||||
if plan is not None and looks_like_coin_amount_as_margin(
|
||||
plan,
|
||||
@@ -143,6 +149,8 @@ def resolve_trade_record_margin_usdt(
|
||||
notional_value=notional_value,
|
||||
):
|
||||
plan = None
|
||||
if plan is not None and from_risk is not None and plan > from_risk * 2.5:
|
||||
plan = None
|
||||
|
||||
if ex is not None and looks_like_coin_amount_as_margin(
|
||||
ex,
|
||||
@@ -155,18 +163,13 @@ def resolve_trade_record_margin_usdt(
|
||||
ex = None
|
||||
if ex is not None and plan is not None and ex > plan * 2.5:
|
||||
ex = None
|
||||
if ex is not None and from_risk is not None and ex > from_risk * 2.5:
|
||||
ex = None
|
||||
|
||||
if plan is not None:
|
||||
return round(plan, 2)
|
||||
if ex is not None:
|
||||
return round(ex, 2)
|
||||
|
||||
from_risk = margin_from_risk_amount(
|
||||
risk_amount,
|
||||
trigger_price=trigger_price,
|
||||
stop_loss=stop_loss,
|
||||
leverage=leverage,
|
||||
)
|
||||
if from_risk is not None:
|
||||
return from_risk
|
||||
|
||||
@@ -187,10 +190,16 @@ def repair_stored_margin_capital(
|
||||
risk_amount: Any = None,
|
||||
initial_stop_loss: Any = None,
|
||||
) -> Optional[float]:
|
||||
"""展示/列表:修复已入库的异常基数;禁止把币数量换算成虚高保证金."""
|
||||
"""展示/列表:修复已入库的异常基数.
|
||||
|
||||
返回值语义:
|
||||
- 正数:应用该保证金
|
||||
- None:若原值像币数量,调用方应清空显示;否则保留原值(见 apply_repaired_margin_capital)
|
||||
"""
|
||||
del symbol # 预留按币种阈值
|
||||
m = _pos_float(margin_capital)
|
||||
sl = stop_loss if stop_loss not in (None, "") else initial_stop_loss
|
||||
# 与写入路径一致:优先开仓止损,避免保本后距离变小导致反推虚高
|
||||
sl = initial_stop_loss if initial_stop_loss not in (None, "") else stop_loss
|
||||
from_risk = margin_from_risk_amount(
|
||||
risk_amount,
|
||||
trigger_price=trigger_price,
|
||||
@@ -207,7 +216,38 @@ def repair_stored_margin_capital(
|
||||
return from_risk
|
||||
|
||||
if from_risk is not None and m > from_risk * 2.5:
|
||||
# 已入库虚高(如曾用币×价÷杠杆「纠偏」成 384)
|
||||
return from_risk
|
||||
|
||||
return round(m, 2)
|
||||
|
||||
|
||||
def apply_repaired_margin_capital(
|
||||
item: dict[str, Any],
|
||||
*,
|
||||
trigger_price: Any = None,
|
||||
leverage: Any = None,
|
||||
stop_loss: Any = None,
|
||||
initial_stop_loss: Any = None,
|
||||
risk_amount: Any = None,
|
||||
symbol: Any = None,
|
||||
) -> None:
|
||||
"""就地更新 item['margin_capital'];币数量无法修复时清空为 None."""
|
||||
if not isinstance(item, dict):
|
||||
return
|
||||
raw = item.get("margin_capital")
|
||||
fixed = repair_stored_margin_capital(
|
||||
raw,
|
||||
trigger_price=trigger_price,
|
||||
leverage=leverage,
|
||||
symbol=symbol,
|
||||
stop_loss=stop_loss,
|
||||
initial_stop_loss=initial_stop_loss,
|
||||
risk_amount=risk_amount,
|
||||
)
|
||||
if fixed is not None:
|
||||
item["margin_capital"] = fixed
|
||||
return
|
||||
if looks_like_coin_amount_as_margin(
|
||||
raw, trigger_price=trigger_price, leverage=leverage
|
||||
):
|
||||
item["margin_capital"] = None
|
||||
|
||||
Reference in New Issue
Block a user