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

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
+50 -33
View File
@@ -364,23 +364,22 @@ def freeze_missing_realized_pnl_usdt(conn, ex: Any = None) -> int:
return updated
def repair_double_counted_fee_pnl(conn, ex: Any = None) -> int:
"""修复「交易所净盈亏上又减了一次手续费」导致的偏亏冻结值.
def recompute_closed_realized_pnl_usdt(conn, ex: Any = None) -> int:
"""强制按「平仓收回×平仓指数 − 开仓成本U − 手续费」重算已平冻结 U.
判定:存在开/平手续费,且 realized_pnl ≈ (recv-paid) - 2×fees,则改回只扣一次.
若无可靠 recv/paid,但 usdt ≈ (realized + fees)×index 的反向不符则跳过.
旧逻辑用 (收回−已付)×平仓指数,等于把开仓成本也按平仓价重估,ETH 上涨时会偏亏,
导致合计盈亏与余额对不上.有 budget_usdt/open_index_px 时优先用真实开仓成本.
"""
init_options_tables(conn)
rows = conn.execute(
"""
SELECT id, realized_pnl, realized_pnl_usdt, close_index_px,
SELECT id, realized_pnl, realized_pnl_usdt, close_index_px, open_index_px,
inst_id, underlying, premium_ccy, premium_paid, premium_received,
open_fee, close_fee
open_fee, close_fee, fee_ccy, budget_usdt, close_quote, eth_amount,
open_quote
FROM options_trades
WHERE status = 'closed'
AND (COALESCE(open_fee, 0) > 0 OR COALESCE(close_fee, 0) > 0)
AND premium_paid IS NOT NULL
AND premium_received IS NOT NULL
ORDER BY id DESC
LIMIT 300
"""
@@ -388,59 +387,72 @@ def repair_double_counted_fee_pnl(conn, ex: Any = None) -> int:
if not rows:
return 0
from lib.options.options_pnl_settle_lib import resolve_close_index_px, settle_closed_pnl
from lib.options.options_pricing_lib import total_premium
updated = 0
for row in rows:
paid = _safe_float(_row_get(row, "premium_paid"))
recv = _safe_float(_row_get(row, "premium_received"))
if recv is None:
cq = _safe_float(_row_get(row, "close_quote"))
eth = _safe_float(_row_get(row, "eth_amount"))
if cq is not None and eth is not None and eth > 0:
recv = float(total_premium(cq, eth))
if paid is None or recv is None:
continue
ofee = abs(_safe_float(_row_get(row, "open_fee")) or 0.0)
cfee = abs(_safe_float(_row_get(row, "close_fee")) or 0.0)
fees = ofee + cfee
if paid is None or recv is None or fees <= 0:
continue
gross = float(recv) - float(paid)
single = round(gross - fees, 8)
double = round(gross - 2.0 * fees, 8)
cur = _safe_float(_row_get(row, "realized_pnl"))
usdt = _safe_float(_row_get(row, "realized_pnl_usdt"))
ccy = str(_row_get(row, "premium_ccy") or "").strip().upper() or "ETH"
idx = _safe_float(_row_get(row, "close_index_px"))
if idx is None or idx <= 0:
idx = resolve_close_index_px(
close_idx = _safe_float(_row_get(row, "close_index_px"))
open_idx = _safe_float(_row_get(row, "open_index_px"))
budget = _safe_float(_row_get(row, "budget_usdt"))
if close_idx is None or close_idx <= 0:
close_idx = resolve_close_index_px(
ex,
inst_id=str(_row_get(row, "inst_id") or ""),
underlying=str(_row_get(row, "underlying") or ""),
)
# 币数量层面像双重扣费,或 U 层面像双重扣费
coin_double = cur is not None and abs(cur - double) <= max(1e-8, abs(fees) * 1e-6)
usdt_double = False
if usdt is not None and idx is not None and idx > 0 and ccy in ("ETH", "BTC"):
usdt_double = abs(usdt - double * float(idx)) <= max(0.02, abs(fees * float(idx)) * 0.05)
if not coin_double and not usdt_double:
# 若当前已是单次扣费则不动
continue
if (open_idx is None or open_idx <= 0) and budget and paid > 1e-12:
open_idx = float(budget) / float(paid)
settled = settle_closed_pnl(
premium_paid=paid,
premium_received=recv,
open_fee=ofee,
close_fee=cfee,
premium_ccy=ccy,
index_px=idx,
index_px=close_idx,
open_index_px=open_idx,
budget_usdt=budget,
fee_ccy=str(_row_get(row, "fee_ccy") or ccy),
)
if settled.get("realized_pnl_usdt") is None and settled.get("realized_pnl") is None:
new_usdt = settled.get("realized_pnl_usdt")
if new_usdt is None:
continue
old_usdt = _safe_float(_row_get(row, "realized_pnl_usdt"))
new_pnl = settled.get("realized_pnl")
# 有变化才写,避免无意义刷库
if (
old_usdt is not None
and abs(float(old_usdt) - float(new_usdt)) < 1e-6
and _safe_float(_row_get(row, "realized_pnl")) is not None
and new_pnl is not None
and abs(float(_row_get(row, "realized_pnl")) - float(new_pnl)) < 1e-10
):
continue
conn.execute(
"""
UPDATE options_trades
SET realized_pnl = ?,
SET realized_pnl = COALESCE(?, realized_pnl),
close_index_px = COALESCE(?, close_index_px),
open_index_px = COALESCE(open_index_px, ?),
realized_pnl_usdt = ?
WHERE id = ?
""",
(
settled.get("realized_pnl"),
new_pnl,
settled.get("close_index_px"),
settled.get("realized_pnl_usdt"),
settled.get("open_index_px"),
float(new_usdt),
int(row["id"]),
),
)
@@ -448,11 +460,16 @@ def repair_double_counted_fee_pnl(conn, ex: Any = None) -> int:
return updated
def repair_double_counted_fee_pnl(conn, ex: Any = None) -> int:
"""兼容旧名:改为强制按开仓成本重算冻结 U."""
return recompute_closed_realized_pnl_usdt(conn, ex)
def load_local_options_stats_history(conn, ex: Any = None) -> list[dict[str, Any]]:
"""本地已平(含冻结 U/手续费净额) + 当前 open,供统计/中控窗口复用."""
init_options_tables(conn)
try:
repair_double_counted_fee_pnl(conn, ex)
recompute_closed_realized_pnl_usdt(conn, ex)
except Exception:
pass
try: