修复币本位统计:开仓成本按预算/开仓指数计,不再用平仓价重估。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-08 23:11:45 +08:00
parent 2369aae362
commit d05629c362
7 changed files with 258 additions and 120 deletions
+20 -8
View File
@@ -315,7 +315,8 @@ def sync_open_options_trades(
rows = conn.execute(
"""
SELECT id, inst_id, premium_paid, exp_time, created_at,
open_fee, premium_ccy, underlying, exchange_ord_id
open_fee, premium_ccy, underlying, exchange_ord_id,
budget_usdt, open_index_px, fee_ccy
FROM options_trades
WHERE status = 'open'
"""
@@ -326,7 +327,6 @@ def sync_open_options_trades(
from lib.options.options_pnl_settle_lib import (
resolve_close_index_px,
settle_closed_pnl,
settle_exchange_realized_pnl,
)
for row in rows:
@@ -371,17 +371,24 @@ def sync_open_options_trades(
prem_ccy = str(row["premium_ccy"] or "").strip() or "ETH"
underly = str(row["underlying"] or "").strip()
close_idx = resolve_close_index_px(None, inst_id=inst_id, underlying=underly)
budget = _safe_float(row["budget_usdt"])
open_idx = _safe_float(row["open_index_px"])
fee_ccy = str(row["fee_ccy"] or prem_ccy or "")
if close_reason == "exchange" and realized_pnl is not None:
# OKX realizedPnl 已含费,禁止再减 open_fee
settled = settle_exchange_realized_pnl(
realized_pnl=realized_pnl,
# OKX realizedPnl 已含费 → 反推毛收回,再按开仓成本折 U(不再二次扣费)
prem_recv = round(paid + float(realized_pnl) + open_fee, 4)
settled = settle_closed_pnl(
premium_paid=paid,
premium_received=prem_recv,
open_fee=open_fee,
close_fee=0,
premium_ccy=prem_ccy,
index_px=close_idx,
fee_hint=open_fee,
open_index_px=open_idx,
budget_usdt=budget,
fee_ccy=fee_ccy,
)
prem_recv = round(paid + float(realized_pnl) + open_fee, 4)
else:
# 到期归零:本地权利金口径,只扣一次开仓费
if prem_recv is None:
prem_recv = 0.0
settled = settle_closed_pnl(
@@ -391,6 +398,9 @@ def sync_open_options_trades(
close_fee=0,
premium_ccy=prem_ccy,
index_px=close_idx,
open_index_px=open_idx,
budget_usdt=budget,
fee_ccy=fee_ccy,
)
realized_pnl = settled.get("realized_pnl")
pnl_usdt = settled.get("realized_pnl_usdt")
@@ -405,6 +415,7 @@ def sync_open_options_trades(
close_ord_id = COALESCE(?, close_ord_id),
closed_at = COALESCE(?, closed_at),
close_index_px = COALESCE(?, close_index_px),
open_index_px = COALESCE(open_index_px, ?),
realized_pnl_usdt = COALESCE(?, realized_pnl_usdt),
fee_ccy = COALESCE(fee_ccy, ?),
signal_note = CASE
@@ -421,6 +432,7 @@ def sync_open_options_trades(
close_ord_id,
closed_at,
settled.get("close_index_px"),
settled.get("open_index_px"),
pnl_usdt,
prem_ccy,
close_reason,