Show expiry settlement as index vs strike intrinsic.

Persist settle_index_px and surface formula in trade detail so expiry closes are not mistaken for book fills.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-29 16:15:22 +08:00
parent 3df64aeb2d
commit 8518a207a7
6 changed files with 130 additions and 7 deletions
+50
View File
@@ -16,6 +16,51 @@ def _row(r: Any) -> dict:
return dict(r)
def _expiry_settle_info(g: dict, fills: list) -> dict | None:
"""到期结算口径:期权价 = 内在价值(指数 vs 行权价),非盘口。"""
if str(g.get("close_reason") or "") != "expiry":
return None
strike = g.get("strike")
side = str(g.get("option_side") or "").lower()
settle_index = g.get("settle_index_px")
if settle_index is None and strike is not None:
for raw in fills:
f = dict(raw) if not isinstance(raw, dict) else raw
if str(f.get("leg")) != "option" or str(f.get("action")) != "close":
continue
if abs(float(f.get("slip") or 0)) > 1e-12:
continue
px = float(f.get("fill_px") or 0)
k = float(strike)
if side in ("call", "c"):
settle_index = k + px
elif side in ("put", "p"):
settle_index = k - px
break
intrinsic = None
if settle_index is not None and strike is not None:
s = float(settle_index)
k = float(strike)
if side in ("call", "c"):
intrinsic = max(s - k, 0.0)
elif side in ("put", "p"):
intrinsic = max(k - s, 0.0)
formula = (
"Call: max(指数−行权价, 0)"
if side in ("call", "c")
else "Put: max(行权价−指数, 0)"
if side in ("put", "p")
else ""
)
return {
"settle_index_px": float(settle_index) if settle_index is not None else None,
"strike": float(strike) if strike is not None else None,
"intrinsic": intrinsic,
"formula": formula,
"perp_note": "永续仍按市价平仓(非指数交割)",
}
def _enrich_group(g: dict, fills: list) -> dict:
summary = summarize_fills_pnl(fills)
# LIVE:优先 groups.realized_pnl(已按交易所回写,含资金费)
@@ -31,6 +76,11 @@ def _enrich_group(g: dict, fills: list) -> dict:
elif g.get("realized_pnl") is not None:
g["net_pnl"] = float(g["realized_pnl"])
g.update(hold_timing(g, fills))
info = _expiry_settle_info(g, fills)
if info:
g["expiry_settle"] = info
if g.get("settle_index_px") is None and info.get("settle_index_px") is not None:
g["settle_index_px"] = info["settle_index_px"]
return g
+3 -1
View File
@@ -42,7 +42,8 @@ CREATE TABLE IF NOT EXISTS groups (
slip_cost REAL DEFAULT 0,
note TEXT,
exec_mode TEXT,
funding_usdt REAL
funding_usdt REAL,
settle_index_px REAL
);
CREATE TABLE IF NOT EXISTS fills (
@@ -152,6 +153,7 @@ class Database:
for table, col, decl in (
("groups", "exec_mode", "TEXT"),
("groups", "funding_usdt", "REAL"),
("groups", "settle_index_px", "REAL"),
("fills", "exec_mode", "TEXT"),
("fills", "fee_ccy", "TEXT"),
):
+12 -2
View File
@@ -566,10 +566,20 @@ class Matcher:
).fetchone()
fees = float(g["fees"] or 0) + pf.fee + of.fee
slip = float(g["slip_cost"] or 0) + pf.slip + of.slip
settle_index = float(spot) if is_expiry and spot is not None else None
self.db._conn.execute(
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?,
fees=?, slip_cost=?, note=NULL WHERE group_id=?""",
("closed", now, reason, net_after_all_fees, fees, slip, group_id),
fees=?, slip_cost=?, note=NULL, settle_index_px=? WHERE group_id=?""",
(
"closed",
now,
reason,
net_after_all_fees,
fees,
slip,
settle_index,
group_id,
),
)
self.db._conn.execute(
"""UPDATE positions SET