fix真盈亏只认历史币盈亏不再另扣费,刷新按固定量价重算;修模拟到期币盈亏。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 11:30:07 +08:00
parent aa8b16ce27
commit dd979d9dd0
7 changed files with 278 additions and 128 deletions
+51 -48
View File
@@ -555,11 +555,6 @@ 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
close_idx = _safe_float(r["close_index_px"])
open_idx = _safe_float(r["open_index_px"])
idx = None
@@ -580,60 +575,67 @@ def sync_options_from_local_trades(
close_fee=cfee,
premium_ccy=ccy,
)
# 权利金币量/币盈亏固定;开仓价与卖回价各自冻一次后真盈亏只读
# 权利金币量/币盈亏/已冻均价不变;刷新时按公式重算真盈亏U
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 resolve_true_pnl_usdt_from_trade
from lib.options.options_pnl_settle_lib import (
resolve_spot_vwap_prices,
resolve_true_pnl_usdt_from_trade,
)
status_label = "已平"
pnl_u = None
if settled and frozen_usdt is not None:
# 已冻结:只读展示,禁止同步反复改写
pnl_u = round(float(frozen_usdt), 4)
else:
pnl_u, pnl_src = resolve_true_pnl_usdt_from_trade(
premium_paid=prem,
premium_received=r["premium_received"],
realized_pnl=r["realized_pnl"],
open_index_px=open_idx if (open_idx is not None and open_idx > 0) else None,
close_index_px=sell_idx,
open_fee=ofee,
close_fee=cfee,
premium_ccy=ccy,
fee_ccy=str(r["premium_ccy"] or ccy or ""),
conn=conn,
bridge_id=r["bridge_id"],
)
if pnl_src == "pending_sell":
status_label = "待卖回"
pnl_u = None
elif pnl_u is not None and pnl_src not in ("none", "pending_sell"):
# 未冻结且可算:只写一次
try:
from lib.options.options_pnl_settle_lib import resolve_spot_vwap_prices
# 价已冻则只用库内价;缺价才问桥 VWAP(避免刷新改均价)
prices_frozen = (
open_idx is not None
and open_idx > 0
and sell_idx is not None
and sell_idx > 0
)
use_bridge = None if prices_frozen else r["bridge_id"]
pnl_u, pnl_src = resolve_true_pnl_usdt_from_trade(
premium_paid=prem,
premium_received=r["premium_received"],
realized_pnl=r["realized_pnl"],
open_index_px=open_idx if (open_idx is not None and open_idx > 0) else None,
close_index_px=sell_idx,
open_fee=ofee,
close_fee=cfee,
premium_ccy=ccy,
fee_ccy=str(r["premium_ccy"] or ccy or ""),
conn=conn if use_bridge is not None else None,
bridge_id=use_bridge,
)
if pnl_src == "pending_sell":
status_label = "待卖回"
pnl_u = None
elif pnl_u is not None and pnl_src not in ("none", "pending_sell"):
try:
opx, cpx = open_idx, sell_idx
if not prices_frozen:
opx, cpx, _g = resolve_spot_vwap_prices(
conn,
bridge_id=r["bridge_id"],
open_index_px=open_idx,
close_index_px=sell_idx,
)
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = ?,
open_index_px = COALESCE(open_index_px, ?),
close_index_px = COALESCE(?, close_index_px),
pnl_settled = 1
WHERE id = ? AND COALESCE(pnl_settled, 0)=0
""",
(pnl_u, opx, cpx, trade_id),
)
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
# 真盈亏U每次刷新按固定量+价重算;均价只补缺不覆盖
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = ?,
open_index_px = COALESCE(open_index_px, ?),
close_index_px = COALESCE(close_index_px, ?),
pnl_settled = 1
WHERE id = ?
""",
(pnl_u, opx, cpx, trade_id),
)
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
# 初始权利金(U)=开仓指数×权利金币量;不含多买
stored_initial = _safe_float(r["initial_premium_usdt"])
prem_u = stored_initial
@@ -641,6 +643,7 @@ def sync_options_from_local_trades(
prem_u = _to_usdt(prem, ccy=ccy, idx=open_idx_for_prem)
if ccy in ("ETH", "BTC") and prem_u is None:
prem_u = prem
# 手续费列仅展示(已含在历史币盈亏内,真盈亏不再扣)
fee_u = None
if ccy in ("ETH", "BTC"):
fee_parts = 0.0