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)
|
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:
|
def _enrich_group(g: dict, fills: list) -> dict:
|
||||||
summary = summarize_fills_pnl(fills)
|
summary = summarize_fills_pnl(fills)
|
||||||
# LIVE:优先 groups.realized_pnl(已按交易所回写,含资金费)
|
# LIVE:优先 groups.realized_pnl(已按交易所回写,含资金费)
|
||||||
@@ -31,6 +76,11 @@ def _enrich_group(g: dict, fills: list) -> dict:
|
|||||||
elif g.get("realized_pnl") is not None:
|
elif g.get("realized_pnl") is not None:
|
||||||
g["net_pnl"] = float(g["realized_pnl"])
|
g["net_pnl"] = float(g["realized_pnl"])
|
||||||
g.update(hold_timing(g, fills))
|
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
|
return g
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ CREATE TABLE IF NOT EXISTS groups (
|
|||||||
slip_cost REAL DEFAULT 0,
|
slip_cost REAL DEFAULT 0,
|
||||||
note TEXT,
|
note TEXT,
|
||||||
exec_mode TEXT,
|
exec_mode TEXT,
|
||||||
funding_usdt REAL
|
funding_usdt REAL,
|
||||||
|
settle_index_px REAL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS fills (
|
CREATE TABLE IF NOT EXISTS fills (
|
||||||
@@ -152,6 +153,7 @@ class Database:
|
|||||||
for table, col, decl in (
|
for table, col, decl in (
|
||||||
("groups", "exec_mode", "TEXT"),
|
("groups", "exec_mode", "TEXT"),
|
||||||
("groups", "funding_usdt", "REAL"),
|
("groups", "funding_usdt", "REAL"),
|
||||||
|
("groups", "settle_index_px", "REAL"),
|
||||||
("fills", "exec_mode", "TEXT"),
|
("fills", "exec_mode", "TEXT"),
|
||||||
("fills", "fee_ccy", "TEXT"),
|
("fills", "fee_ccy", "TEXT"),
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -566,10 +566,20 @@ class Matcher:
|
|||||||
).fetchone()
|
).fetchone()
|
||||||
fees = float(g["fees"] or 0) + pf.fee + of.fee
|
fees = float(g["fees"] or 0) + pf.fee + of.fee
|
||||||
slip = float(g["slip_cost"] or 0) + pf.slip + of.slip
|
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(
|
self.db._conn.execute(
|
||||||
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?,
|
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?,
|
||||||
fees=?, slip_cost=?, note=NULL WHERE group_id=?""",
|
fees=?, slip_cost=?, note=NULL, settle_index_px=? WHERE group_id=?""",
|
||||||
("closed", now, reason, net_after_all_fees, fees, slip, group_id),
|
(
|
||||||
|
"closed",
|
||||||
|
now,
|
||||||
|
reason,
|
||||||
|
net_after_all_fees,
|
||||||
|
fees,
|
||||||
|
slip,
|
||||||
|
settle_index,
|
||||||
|
group_id,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
self.db._conn.execute(
|
self.db._conn.execute(
|
||||||
"""UPDATE positions SET
|
"""UPDATE positions SET
|
||||||
|
|||||||
@@ -5,6 +5,16 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## 2026-07-29 — 到期结算展示:指数 / 行权价 / 内在价值
|
||||||
|
|
||||||
|
### 变更
|
||||||
|
|
||||||
|
1. 到期平仓期权价本就是 **内在价值**(`max(指数−K,0)` / Put 对称),不是盘口;详情页补 **结算指数、行权价、内在价值**。
|
||||||
|
2. 成交行标注「期权到期结算」;组表落库 `settle_index_px`。
|
||||||
|
3. 说明:永续到期时仍按市价平(与策略文档一致)。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 2026-07-29 — 持仓卡显示开仓时间 / 持仓时长
|
## 2026-07-29 — 持仓卡显示开仓时间 / 持仓时长
|
||||||
|
|
||||||
### 变更
|
### 变更
|
||||||
|
|||||||
@@ -52,10 +52,18 @@ export function positionSidesZh(
|
|||||||
return `${sideZh(perp)}/${sideZh(option)}`;
|
return `${sideZh(perp)}/${sideZh(option)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fillDescZh(leg: string, action: string, side: string): string {
|
export function fillDescZh(
|
||||||
|
leg: string,
|
||||||
|
action: string,
|
||||||
|
side: string,
|
||||||
|
closeReason?: string | null,
|
||||||
|
): string {
|
||||||
const l = LEG_ZH[leg] || leg;
|
const l = LEG_ZH[leg] || leg;
|
||||||
const a = ACTION_ZH[action] || action;
|
const a = ACTION_ZH[action] || action;
|
||||||
const s = SIDE_ZH[side] || side;
|
const s = SIDE_ZH[side] || side;
|
||||||
|
if (leg === "option" && action === "close" && closeReason === "expiry") {
|
||||||
|
return "期权到期结算";
|
||||||
|
}
|
||||||
// 期权买入开仓:「期权开多」;永续:「永续开多/开空」
|
// 期权买入开仓:「期权开多」;永续:「永续开多/开空」
|
||||||
if (leg === "option" && action === "open") return "期权开多";
|
if (leg === "option" && action === "open") return "期权开多";
|
||||||
if (leg === "option" && action === "close") return "期权平多";
|
if (leg === "option" && action === "close") return "期权平多";
|
||||||
|
|||||||
@@ -15,6 +15,14 @@ type PnlSummary = {
|
|||||||
net_pnl: number | null;
|
net_pnl: number | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type ExpirySettle = {
|
||||||
|
settle_index_px: number | null;
|
||||||
|
strike: number | null;
|
||||||
|
intrinsic: number | null;
|
||||||
|
formula: string;
|
||||||
|
perp_note: string;
|
||||||
|
};
|
||||||
|
|
||||||
type Group = {
|
type Group = {
|
||||||
group_id: string;
|
group_id: string;
|
||||||
status: string;
|
status: string;
|
||||||
@@ -31,6 +39,9 @@ type Group = {
|
|||||||
hold_close_at_ms?: number | null;
|
hold_close_at_ms?: number | null;
|
||||||
hold_ms?: number | null;
|
hold_ms?: number | null;
|
||||||
hold_basis?: string | null;
|
hold_basis?: string | null;
|
||||||
|
strike?: number | null;
|
||||||
|
settle_index_px?: number | null;
|
||||||
|
expiry_settle?: ExpirySettle | null;
|
||||||
pnl_summary?: PnlSummary;
|
pnl_summary?: PnlSummary;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -276,14 +287,46 @@ export default function TradesPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p className="trade-detail-hint">
|
<p className="trade-detail-hint">
|
||||||
成交价为成交均价(未预先扣费);手续费单独列出。净盈亏 = 期权盈亏 +
|
{selectedGroup.close_reason === "expiry"
|
||||||
永续盈亏 − 全部手续费。
|
? "到期结算:期权按「指数 vs 行权价」的内在价值入账(非盘口);永续仍按市价平。净盈亏 = 期权盈亏 + 永续盈亏 − 全部手续费。"
|
||||||
|
: "成交价为成交均价(未预先扣费);手续费单独列出。净盈亏 = 期权盈亏 + 永续盈亏 − 全部手续费。"}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
{selectedGroup.expiry_settle ? (
|
||||||
|
<div className="trade-hold-summary" style={{ marginBottom: 10 }}>
|
||||||
|
<div className="kv">
|
||||||
|
<span>结算指数</span>
|
||||||
|
<span className="mono">
|
||||||
|
{fmt(selectedGroup.expiry_settle.settle_index_px)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="kv">
|
||||||
|
<span>行权价</span>
|
||||||
|
<span className="mono">
|
||||||
|
{fmt(selectedGroup.expiry_settle.strike, 0)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="kv">
|
||||||
|
<span>内在价值</span>
|
||||||
|
<span className="mono">
|
||||||
|
{fmt(selectedGroup.expiry_settle.intrinsic)}
|
||||||
|
{selectedGroup.expiry_settle.formula
|
||||||
|
? ` · ${selectedGroup.expiry_settle.formula}`
|
||||||
|
: ""}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
{fills.map((f) => (
|
{fills.map((f) => (
|
||||||
<div key={f.id} className="trade-fill kv">
|
<div key={f.id} className="trade-fill kv">
|
||||||
<span className="mono">
|
<span className="mono">
|
||||||
{fillDescZh(f.leg, f.action, f.side)}
|
{fillDescZh(
|
||||||
|
f.leg,
|
||||||
|
f.action,
|
||||||
|
f.side,
|
||||||
|
selectedGroup.close_reason,
|
||||||
|
)}
|
||||||
</span>
|
</span>
|
||||||
<span className="mono">
|
<span className="mono">
|
||||||
价 {f.fill_px.toFixed(4)} · 数量 {f.qty_eth} · 手续费{" "}
|
价 {f.fill_px.toFixed(4)} · 数量 {f.qty_eth} · 手续费{" "}
|
||||||
|
|||||||
Reference in New Issue
Block a user