fix开仓价卖回价与真盈亏一次冻结,复盘同步不再反复改写。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-13 10:56:53 +08:00
parent 1aa279f191
commit aa8b16ce27
5 changed files with 106 additions and 64 deletions
+33 -52
View File
@@ -556,6 +556,10 @@ def sync_options_from_local_trades(
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
@@ -576,43 +580,35 @@ def sync_options_from_local_trades(
close_fee=cfee,
premium_ccy=ccy,
)
# 初始权利金可用开仓价,缺则兜底指数;真盈亏必须用冻结的开仓价+卖回价,禁止实时价冒充
# 权利金币量/币盈亏固定;开仓价与卖回价各自冻一次后真盈亏只读
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
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"],
)
# 能算新公式则写回;卖回未完成→待卖回;算不出→缺价未核
status_label = "已平"
if pnl_src == "pending_sell":
status_label = "待卖回"
pnl_u = None
# 清掉曾用指数提前冻上的假真盈亏
try:
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = NULL, pnl_settled = 0
WHERE id = ? AND COALESCE(pnl_settled, 0) = 1
""",
(trade_id,),
)
except Exception:
pass
elif pnl_u is not None and pnl_src not in ("none", "pending_sell"):
if frozen_usdt is None or abs(float(frozen_usdt) - float(pnl_u)) > 0.05:
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
@@ -626,33 +622,18 @@ def sync_options_from_local_trades(
"""
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),
pnl_settled = 1
WHERE id = ?
WHERE id = ? AND COALESCE(pnl_settled, 0)=0
""",
(pnl_u, opx, cpx, trade_id),
)
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
try:
conn.execute(
"""
UPDATE options_trades
SET realized_pnl_usdt = NULL, pnl_settled = 0
WHERE id = ?
AND (
COALESCE(pnl_settled, 0) = 1
OR realized_pnl_usdt IS NOT NULL
)
""",
(trade_id,),
)
except Exception:
pass
else:
status_label = "缺价未核"
pnl_u = None
# 初始权利金(U)=开仓指数×权利金币量;不含多买
stored_initial = _safe_float(r["initial_premium_usdt"])
prem_u = stored_initial