fix真盈亏收口为币本位×卖回价减初始权利金U,禁指数冒充平仓价。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 12:29:33 +08:00
parent d42e1c7edf
commit 839baea375
10 changed files with 171 additions and 83 deletions
+15 -8
View File
@@ -575,7 +575,8 @@ def sync_options_from_local_trades(
close_fee=cfee,
premium_ccy=ccy,
)
# 权利金币量/币盈亏/已冻均价不变;刷新时按公式重算真盈亏U
# 币本位/初始权利金U 冻结不变;刷新只按卖回价重算真盈亏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
sell_idx = close_idx if (close_idx is not None and close_idx > 0) else None
from lib.options.options_pnl_settle_lib import (
@@ -585,7 +586,7 @@ def sync_options_from_local_trades(
status_label = "已平"
pnl_u = None
# 始终问桥:买/卖回 VWAP 优先于库内指数占位价(防预算反推开仓价、平仓指数冒充卖回价)
# 有桥时卖回价只认 close_sell VWAP;开仓价不覆盖已冻值
opx, cpx, _gate = resolve_spot_vwap_prices(
conn,
bridge_id=r["bridge_id"],
@@ -598,6 +599,7 @@ def sync_options_from_local_trades(
realized_pnl=r["realized_pnl"],
open_index_px=opx,
close_index_px=cpx,
initial_premium_usdt=stored_initial,
open_fee=ofee,
close_fee=cfee,
premium_ccy=ccy,
@@ -610,30 +612,35 @@ def sync_options_from_local_trades(
pnl_u = None
elif pnl_u is not None and pnl_src not in ("none", "pending_sell"):
try:
# VWAP 非空时覆盖指数占位;币量字段不改
# 卖回 VWAP 写入 close;开仓价/初始权利金仅缺省时补
heal_initial = stored_initial
if heal_initial is None and prem is not None and opx is not None and opx > 0:
heal_initial = round(float(prem) * float(opx), 4)
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = ?,
open_index_px = COALESCE(?, open_index_px),
open_index_px = COALESCE(open_index_px, ?),
close_index_px = COALESCE(?, close_index_px),
initial_premium_usdt = COALESCE(initial_premium_usdt, ?),
pnl_settled = 1
WHERE id = ?
""",
(pnl_u, opx, cpx, trade_id),
(pnl_u, opx, cpx, heal_initial, trade_id),
)
if opx is not None and opx > 0:
if (open_idx is None or open_idx <= 0) and opx is not None and opx > 0:
open_idx = opx
open_idx_for_prem = opx
if cpx is not None and cpx > 0:
sell_idx = cpx
if stored_initial is None and heal_initial is not None:
stored_initial = heal_initial
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
# 初始权利金(U)=开仓指数×权利金币量;不含多买
stored_initial = _safe_float(r["initial_premium_usdt"])
# 初始权利金U:优先开仓冻结值
prem_u = stored_initial
if prem_u is None:
prem_u = _to_usdt(prem, ccy=ccy, idx=open_idx_for_prem)