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:
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user