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

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
+14 -3
View File
@@ -378,7 +378,8 @@ def close_option_by_bid1(
open_rows = conn.execute(
"""
SELECT id, premium_paid, open_fee, premium_ccy, underlying,
exchange_ord_id
exchange_ord_id, budget_usdt, open_index_px, fee_ccy,
eth_amount, open_quote
FROM options_trades
WHERE inst_id = ? AND status = 'open'
ORDER BY id ASC
@@ -492,6 +493,11 @@ def close_option_by_bid1(
if stored_ofee <= 0:
allocated_ofee += row_ofee
row_budget = _safe_float(row["budget_usdt"])
row_open_idx = _safe_float(row["open_index_px"])
if (row_open_idx is None or row_open_idx <= 0) and row_budget and paid > 1e-12:
# 无开仓指数时用预算/已付反推
row_open_idx = float(row_budget) / paid
settled = settle_closed_pnl(
premium_paid=paid,
premium_received=recv,
@@ -499,12 +505,15 @@ def close_option_by_bid1(
close_fee=row_cfee,
premium_ccy=str(row["premium_ccy"] or row_ccy or premium_ccy),
index_px=close_idx,
open_index_px=row_open_idx,
budget_usdt=row_budget,
fee_ccy=str(row["fee_ccy"] or row["premium_ccy"] or row_ccy or ""),
)
pnl = settled.get("realized_pnl")
pnl_usdt = settled.get("realized_pnl_usdt")
note_sql = ""
# 占位: close_quote/recv/pnl/ord/closed_at/open_fee/close_fee/fee_ccy/idx/pnl_u/[note]/id
# 占位: close_quote/recv/pnl/ord/closed_at/open_fee/close_fee/fee_ccy/close_idx/open_idx/pnl_u/[note]/id
params: list[Any] = [
close_px,
recv,
@@ -515,6 +524,7 @@ def close_option_by_bid1(
row_cfee,
str(row["premium_ccy"] or row_ccy or premium_ccy or ""),
settled.get("close_index_px"),
settled.get("open_index_px") or row_open_idx,
pnl_usdt,
]
if signal_note and i == len(open_rows) - 1:
@@ -531,7 +541,8 @@ def close_option_by_bid1(
SET status = 'closed', close_quote = ?, premium_received = ?,
realized_pnl = ?, close_ord_id = ?, closed_at = ?,
open_fee = ?, close_fee = ?, fee_ccy = ?,
close_index_px = ?, realized_pnl_usdt = ?,
close_index_px = ?, open_index_px = COALESCE(open_index_px, ?),
realized_pnl_usdt = ?,
close_recv_accum = 0, close_sheets_accum = 0,
close_notional_accum = 0
{note_sql}