fix复盘交易记录改回真盈亏A,期权历史仅记期权腿不含现货。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 09:55:34 +08:00
parent 78f18e731a
commit 09989d2709
5 changed files with 41 additions and 32 deletions
+17 -12
View File
@@ -555,9 +555,16 @@ def sync_options_from_local_trades(
ccy = str(r["premium_ccy"] or "").strip().upper()
if not ccy:
ccy = premium_ccy_for_mode(margin_mode_from_inst_id(inst), underly)
frozen_usdt = _safe_float(r["realized_pnl_usdt"])
try:
settled = int(r["pnl_settled"] or 0) == 1
except Exception:
settled = False
# 有账户快照未结算:交易记录显示待结算(真盈亏=卖回后 after−before)
acct_pending = _safe_float(r["trading_usdt_before"]) is not None and not settled
close_idx = _safe_float(r["close_index_px"])
open_idx = _safe_float(r["open_index_px"])
# 折算指数:平仓卖回价 > 开仓 > 实时(仅无冻结时兜底)
# 折算指数:仅用于初始权利金/期权手续费展示,不用于真盈亏
idx = None
if close_idx is not None and close_idx > 0:
idx = close_idx
@@ -565,23 +572,21 @@ def sync_options_from_local_trades(
idx = open_idx
else:
idx = idx_cache.get(underly)
pnl = _safe_float(r["realized_pnl"])
if pnl is None:
paid0 = _safe_float(r["premium_paid"])
recv0 = _safe_float(r["premium_received"])
if paid0 is not None and recv0 is not None:
pnl = recv0 - paid0
prem = _safe_float(r["premium_paid"])
# 期权交易记录盈亏与数据统计同口径 B:币盈亏 × 平仓卖回价(不用账户差分 A)
pnl_u = _to_usdt(pnl, ccy=ccy, idx=idx)
# 交易记录真盈亏=口径 A(卖光币后账户差分);未结算不写假 U
if acct_pending:
pnl_u = None
elif frozen_usdt is not None and (settled or _safe_float(r["trading_usdt_before"]) is None):
pnl_u = round(float(frozen_usdt), 4)
else:
# 旧单无账户快照:不回退币×价冒充真盈亏
pnl_u = None
# 初始权利金(U)=开仓指数×权利金币量;不含多买
stored_initial = _safe_float(r["initial_premium_usdt"])
open_idx_for_prem = open_idx if (open_idx is not None and open_idx > 0) else idx
prem_u = stored_initial
if prem_u is None:
prem_u = _to_usdt(prem, ccy=ccy, idx=open_idx_for_prem)
if ccy in ("ETH", "BTC") and pnl_u is None:
pnl_u = None
if ccy in ("ETH", "BTC") and prem_u is None:
prem_u = prem
# 仅期权开/平手续费折 U(不含现货)
@@ -635,7 +640,7 @@ def sync_options_from_local_trades(
"option_fee_usdt": fee_u,
"created_at": opened_at,
"closed_at": closed_at,
"status_label": "已平",
"status_label": "待结算" if acct_pending else "已平",
},
)
if action == "inserted":