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
+14 -5
View File
@@ -243,9 +243,9 @@ def settle_closed_trades_from_trading_account(
bridge_id: int | None = None,
trade_id: int | None = None,
) -> int:
"""卖回后:拍 after 快照,并冻结复盘真盈亏(权利金 USDT 波动口径).
"""卖回后:拍 after 快照,并冻结复盘真盈亏.
realized_pnl_usdt = 历史币盈亏×卖回价 − (权利金×开仓价 − 权利金×卖回价);
realized_pnl_usdt = (权利金+币盈亏)×卖回VWAP − 初始权利金U;
不含现货多买.账户 after 仅审计.已 pnl_settled=1 的行永不改写.
"""
from lib.options.options_pnl_settle_lib import resolve_true_pnl_usdt_from_trade
@@ -269,7 +269,7 @@ def settle_closed_trades_from_trading_account(
f"""
SELECT id, trading_usdt_before, premium_paid, premium_received, realized_pnl,
open_index_px, close_index_px, open_fee, close_fee, premium_ccy, fee_ccy,
bridge_id
bridge_id, initial_premium_usdt
FROM options_trades
WHERE {" AND ".join(clauses)}
""",
@@ -287,6 +287,9 @@ def settle_closed_trades_from_trading_account(
row_bid = row["bridge_id"] if hasattr(row, "keys") else row[11]
if row_bid is None and bridge_id is not None:
row_bid = bridge_id
initial_u = (
row["initial_premium_usdt"] if hasattr(row, "keys") else row[12]
)
open_px, close_px, _gate = resolve_spot_vwap_prices(
conn,
bridge_id=row_bid,
@@ -299,6 +302,7 @@ def settle_closed_trades_from_trading_account(
realized_pnl=row["realized_pnl"] if hasattr(row, "keys") else row[4],
open_index_px=open_px,
close_index_px=close_px,
initial_premium_usdt=initial_u,
open_fee=row["open_fee"] if hasattr(row, "keys") else row[7],
close_fee=row["close_fee"] if hasattr(row, "keys") else row[8],
premium_ccy=row["premium_ccy"] if hasattr(row, "keys") else row[9],
@@ -308,7 +312,11 @@ def settle_closed_trades_from_trading_account(
)
if pnl_u is None or src in ("none", "pending_sell"):
continue
# 卖回价一次冻结为卖回 VWAP;开仓价已在开仓时冻住,仅缺省时补
# 卖回价冻为卖回 VWAP;开仓价/初始权利金已在开仓时冻住,仅缺省时补
paid = _safe_float(row["premium_paid"] if hasattr(row, "keys") else row[2])
heal_initial = _safe_float(initial_u)
if heal_initial is None and paid is not None and open_px is not None and open_px > 0:
heal_initial = round(float(paid) * float(open_px), 4)
conn.execute(
"""
UPDATE options_trades
@@ -316,10 +324,11 @@ def settle_closed_trades_from_trading_account(
realized_pnl_usdt = ?,
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 = ? AND COALESCE(pnl_settled, 0)=0
""",
(after_use, pnl_u, open_px, close_px, tid),
(after_use, pnl_u, open_px, close_px, heal_initial, tid),
)
updated += 1
return updated