修复已实现盈亏重复扣手续费,并自动纠正偏亏冻结值。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -308,13 +308,16 @@ _CLOSED_STATS_SELECT = """
|
||||
|
||||
|
||||
def freeze_missing_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
"""旧已平仓缺冻结 U 时按 close_index/当前指数写一次,之后不再跟盘变."""
|
||||
"""旧已平仓缺冻结 U 时按 close_index/当前指数写一次,之后不再跟盘变.
|
||||
|
||||
注意:已有 realized_pnl 视为净额直接冻结,不再二次扣 open_fee/close_fee
|
||||
(避免对 OKX realizedPnl 或已扣费本地净额重复减费).
|
||||
"""
|
||||
init_options_tables(conn)
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, realized_pnl, realized_pnl_usdt, close_index_px,
|
||||
inst_id, underlying, premium_ccy, premium_paid, premium_received,
|
||||
open_fee, close_fee
|
||||
inst_id, underlying, premium_ccy
|
||||
FROM options_trades
|
||||
WHERE status = 'closed'
|
||||
AND realized_pnl_usdt IS NULL
|
||||
@@ -325,7 +328,10 @@ def freeze_missing_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
).fetchall()
|
||||
if not rows:
|
||||
return 0
|
||||
from lib.options.options_pnl_settle_lib import settle_closed_pnl, resolve_close_index_px
|
||||
from lib.options.options_pnl_settle_lib import (
|
||||
freeze_pnl_to_usdt,
|
||||
resolve_close_index_px,
|
||||
)
|
||||
|
||||
updated = 0
|
||||
for row in rows:
|
||||
@@ -337,48 +343,106 @@ def freeze_missing_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
inst_id=str(_row_get(row, "inst_id") or ""),
|
||||
underlying=str(_row_get(row, "underlying") or ""),
|
||||
)
|
||||
# 有手续费列时按净额重算;否则用已有 realized_pnl 冻结
|
||||
ofee = _safe_float(_row_get(row, "open_fee")) or 0.0
|
||||
cfee = _safe_float(_row_get(row, "close_fee")) or 0.0
|
||||
paid = _row_get(row, "premium_paid")
|
||||
recv = _row_get(row, "premium_received")
|
||||
if paid is not None and recv is not None and (ofee > 0 or cfee > 0):
|
||||
settled = settle_closed_pnl(
|
||||
premium_paid=paid,
|
||||
premium_received=recv,
|
||||
open_fee=ofee,
|
||||
close_fee=cfee,
|
||||
premium_ccy=ccy or "USDC",
|
||||
index_px=idx,
|
||||
)
|
||||
pnl = settled.get("realized_pnl")
|
||||
usdt = settled.get("realized_pnl_usdt")
|
||||
close_idx = settled.get("close_index_px")
|
||||
else:
|
||||
pnl = _safe_float(_row_get(row, "realized_pnl"))
|
||||
if pnl is None:
|
||||
continue
|
||||
settled = settle_closed_pnl(
|
||||
premium_paid=0,
|
||||
premium_received=pnl,
|
||||
open_fee=0,
|
||||
close_fee=0,
|
||||
premium_ccy=ccy or "USDC",
|
||||
index_px=idx,
|
||||
)
|
||||
usdt = settled.get("realized_pnl_usdt")
|
||||
close_idx = settled.get("close_index_px")
|
||||
pnl = _safe_float(_row_get(row, "realized_pnl"))
|
||||
if pnl is None:
|
||||
continue
|
||||
settled = freeze_pnl_to_usdt(pnl, premium_ccy=ccy or "USDC", index_px=idx)
|
||||
usdt = settled.get("realized_pnl_usdt")
|
||||
close_idx = settled.get("close_index_px")
|
||||
if usdt is None:
|
||||
continue
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE options_trades
|
||||
SET realized_pnl_usdt = ?,
|
||||
close_index_px = COALESCE(close_index_px, ?),
|
||||
realized_pnl = COALESCE(?, realized_pnl)
|
||||
close_index_px = COALESCE(close_index_px, ?)
|
||||
WHERE id = ? AND realized_pnl_usdt IS NULL
|
||||
""",
|
||||
(float(usdt), close_idx, pnl, int(row["id"])),
|
||||
(float(usdt), close_idx, int(row["id"])),
|
||||
)
|
||||
updated += 1
|
||||
return updated
|
||||
|
||||
|
||||
def repair_double_counted_fee_pnl(conn, ex: Any = None) -> int:
|
||||
"""修复「交易所净盈亏上又减了一次手续费」导致的偏亏冻结值.
|
||||
|
||||
判定:存在开/平手续费,且 realized_pnl ≈ (recv-paid) - 2×fees,则改回只扣一次.
|
||||
若无可靠 recv/paid,但 usdt ≈ (realized + fees)×index 的反向不符则跳过.
|
||||
"""
|
||||
init_options_tables(conn)
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT id, realized_pnl, realized_pnl_usdt, close_index_px,
|
||||
inst_id, underlying, premium_ccy, premium_paid, premium_received,
|
||||
open_fee, close_fee
|
||||
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
|
||||
"""
|
||||
).fetchall()
|
||||
if not rows:
|
||||
return 0
|
||||
from lib.options.options_pnl_settle_lib import resolve_close_index_px, settle_closed_pnl
|
||||
|
||||
updated = 0
|
||||
for row in rows:
|
||||
paid = _safe_float(_row_get(row, "premium_paid"))
|
||||
recv = _safe_float(_row_get(row, "premium_received"))
|
||||
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(
|
||||
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
|
||||
settled = settle_closed_pnl(
|
||||
premium_paid=paid,
|
||||
premium_received=recv,
|
||||
open_fee=ofee,
|
||||
close_fee=cfee,
|
||||
premium_ccy=ccy,
|
||||
index_px=idx,
|
||||
)
|
||||
if settled.get("realized_pnl_usdt") is None and settled.get("realized_pnl") is None:
|
||||
continue
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE options_trades
|
||||
SET realized_pnl = ?,
|
||||
close_index_px = COALESCE(?, close_index_px),
|
||||
realized_pnl_usdt = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(
|
||||
settled.get("realized_pnl"),
|
||||
settled.get("close_index_px"),
|
||||
settled.get("realized_pnl_usdt"),
|
||||
int(row["id"]),
|
||||
),
|
||||
)
|
||||
updated += 1
|
||||
return updated
|
||||
@@ -387,6 +451,10 @@ def freeze_missing_realized_pnl_usdt(conn, ex: Any = None) -> int:
|
||||
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)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
freeze_missing_realized_pnl_usdt(conn, ex)
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user