Fix OO trade records: include Put PnL and show dual-leg detail clearly.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+99
-28
@@ -16,35 +16,62 @@ def _row(r: Any) -> dict:
|
||||
return dict(r)
|
||||
|
||||
|
||||
def _is_oo_group(g: dict) -> bool:
|
||||
return (
|
||||
str(g.get("hedge_mode") or "") == "option_option"
|
||||
or bool(g.get("option2_inst_id"))
|
||||
or str(g.get("bias") or "") == "option_option"
|
||||
)
|
||||
|
||||
|
||||
def _infer_settle_index(g: dict, fills: list) -> float | None:
|
||||
settle_index = g.get("settle_index_px")
|
||||
if settle_index is not None:
|
||||
try:
|
||||
v = float(settle_index)
|
||||
if v > 0:
|
||||
return v
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
strike = g.get("strike")
|
||||
side = str(g.get("option_side") or "").lower()
|
||||
if strike is None:
|
||||
return 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"):
|
||||
return k + px
|
||||
if side in ("put", "p"):
|
||||
return k - px
|
||||
break
|
||||
return None
|
||||
|
||||
|
||||
def _intrinsic(side: str, settle_index: float, strike: float) -> float:
|
||||
s = str(side or "").lower()
|
||||
if s in ("call", "c"):
|
||||
return max(settle_index - strike, 0.0)
|
||||
if s in ("put", "p"):
|
||||
return max(strike - settle_index, 0.0)
|
||||
return 0.0
|
||||
|
||||
|
||||
def _expiry_settle_info(g: dict, fills: list) -> dict | None:
|
||||
"""到期结算口径:期权价 = 内在价值(指数 vs 行权价),非盘口。"""
|
||||
if str(g.get("close_reason") or "") != "expiry":
|
||||
return None
|
||||
settle_index = _infer_settle_index(g, fills)
|
||||
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)
|
||||
intrinsic = _intrinsic(side, float(settle_index), float(strike))
|
||||
formula = (
|
||||
"Call: max(指数−行权价, 0)"
|
||||
if side in ("call", "c")
|
||||
@@ -52,13 +79,36 @@ def _expiry_settle_info(g: dict, fills: list) -> dict | None:
|
||||
if side in ("put", "p")
|
||||
else ""
|
||||
)
|
||||
return {
|
||||
is_oo = _is_oo_group(g)
|
||||
out: dict[str, Any] = {
|
||||
"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": "永续仍按市价平仓(非指数交割)",
|
||||
"perp_note": (
|
||||
"期期无永续腿;两腿均按内在价值结算"
|
||||
if is_oo
|
||||
else "永续仍按市价平仓(非指数交割)"
|
||||
),
|
||||
"is_oo": is_oo,
|
||||
}
|
||||
if is_oo:
|
||||
strike2 = g.get("strike2")
|
||||
side2 = str(g.get("option2_side") or "put").lower()
|
||||
intrinsic2 = None
|
||||
if settle_index is not None and strike2 is not None:
|
||||
intrinsic2 = _intrinsic(side2, float(settle_index), float(strike2))
|
||||
out["strike2"] = float(strike2) if strike2 is not None else None
|
||||
out["intrinsic2"] = intrinsic2
|
||||
out["formula2"] = (
|
||||
"Put: max(行权价−指数, 0)"
|
||||
if side2 in ("put", "p")
|
||||
else "Call: max(指数−行权价, 0)"
|
||||
if side2 in ("call", "c")
|
||||
else ""
|
||||
)
|
||||
out["option2_side"] = side2
|
||||
return out
|
||||
|
||||
|
||||
def _close_index_px(g: dict, fills: list) -> float | None:
|
||||
@@ -101,10 +151,10 @@ def _move_points(g: dict, fills: list) -> float | None:
|
||||
return round(float(close_px) - e, 2)
|
||||
|
||||
|
||||
def _option_entry_px(fills: list) -> float | None:
|
||||
def _option_entry_px(fills: list, *, leg: str = "option") -> float | None:
|
||||
for row in fills:
|
||||
f = dict(row) if not isinstance(row, dict) else row
|
||||
if str(f.get("leg") or "") != "option" or str(f.get("action") or "") != "open":
|
||||
if str(f.get("leg") or "") != leg or str(f.get("action") or "") != "open":
|
||||
continue
|
||||
try:
|
||||
v = float(f.get("fill_px") or 0)
|
||||
@@ -116,7 +166,9 @@ def _option_entry_px(fills: list) -> float | None:
|
||||
return None
|
||||
|
||||
|
||||
def _option_leverage(g: dict, fills: list) -> float | None:
|
||||
def _option_leverage_for_leg(
|
||||
g: dict, fills: list, *, leg: str = "option"
|
||||
) -> float | None:
|
||||
"""开仓期权杠杆 = 开仓指数 ÷ 期权开仓均价(与选约门限口径一致)。"""
|
||||
from ..strategy.selection import option_leverage
|
||||
|
||||
@@ -124,7 +176,7 @@ def _option_leverage(g: dict, fills: list) -> float | None:
|
||||
entry = float(g.get("entry_index_px") or 0)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
opt_px = _option_entry_px(fills)
|
||||
opt_px = _option_entry_px(fills, leg=leg)
|
||||
if entry <= 0 or opt_px is None:
|
||||
return None
|
||||
lev = option_leverage(entry, opt_px)
|
||||
@@ -132,6 +184,8 @@ def _option_leverage(g: dict, fills: list) -> float | None:
|
||||
|
||||
|
||||
def _enrich_group(g: dict, fills: list) -> dict:
|
||||
is_oo = _is_oo_group(g)
|
||||
g["is_oo"] = is_oo
|
||||
summary = summarize_fills_pnl(fills)
|
||||
# LIVE:优先 groups.realized_pnl(已按交易所回写,含资金费)
|
||||
if str(g.get("exec_mode") or "").upper() == "LIVE" and g.get("realized_pnl") is not None:
|
||||
@@ -140,11 +194,26 @@ def _enrich_group(g: dict, fills: list) -> dict:
|
||||
if g.get("funding_usdt") is not None:
|
||||
summary["funding_usdt"] = float(g["funding_usdt"])
|
||||
summary["pnl_source"] = "live_exchange"
|
||||
# 期期 SIM:若成交汇总缺腿但组上已有 realized_pnl,用组值兜底
|
||||
elif (
|
||||
is_oo
|
||||
and g.get("realized_pnl") is not None
|
||||
and (
|
||||
summary.get("option_pnl") is None
|
||||
or summary.get("option2_pnl") is None
|
||||
)
|
||||
):
|
||||
summary = dict(summary)
|
||||
summary["net_pnl"] = float(g["realized_pnl"])
|
||||
summary["pnl_source"] = "group_realized"
|
||||
g["pnl_summary"] = summary
|
||||
if summary.get("net_pnl") is not None:
|
||||
g["net_pnl"] = summary["net_pnl"]
|
||||
elif g.get("realized_pnl") is not None:
|
||||
g["net_pnl"] = float(g["realized_pnl"])
|
||||
prem1 = float(g.get("initial_premium") or 0)
|
||||
prem2 = float(g.get("initial_premium2") or 0) if is_oo else 0.0
|
||||
g["total_initial_premium"] = prem1 + prem2 if is_oo else prem1
|
||||
g.update(hold_timing(g, fills))
|
||||
info = _expiry_settle_info(g, fills)
|
||||
if info:
|
||||
@@ -154,7 +223,9 @@ def _enrich_group(g: dict, fills: list) -> dict:
|
||||
mp = _move_points(g, fills)
|
||||
g["move_points"] = mp
|
||||
g["close_index_px"] = _close_index_px(g, fills)
|
||||
g["option_leverage"] = _option_leverage(g, fills)
|
||||
g["option_leverage"] = _option_leverage_for_leg(g, fills, leg="option")
|
||||
if is_oo:
|
||||
g["option2_leverage"] = _option_leverage_for_leg(g, fills, leg="option2")
|
||||
return g
|
||||
|
||||
|
||||
|
||||
+42
-28
@@ -14,23 +14,35 @@ def _as_map(x: Any) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
|
||||
def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
"""
|
||||
价差盈亏按 fill_px;手续费另扣。
|
||||
净盈亏 = 期权盈亏 + 永续盈亏 − 全部手续费(开+平)。
|
||||
允许只有永续已平、期权尚未结算的半组。
|
||||
|
||||
手续费拆:fees_perp / fees_option;滑点合计 slip_total(SIM 记账;LIVE 应为 0)。
|
||||
"""
|
||||
rows = [_as_map(x) for x in fills]
|
||||
def _leg_option_pnl(rows: list[dict[str, Any]], leg: str) -> float | None:
|
||||
opt_open = next(
|
||||
(f for f in rows if f.get("leg") == "option" and f.get("action") == "open"),
|
||||
(f for f in rows if f.get("leg") == leg and f.get("action") == "open"),
|
||||
None,
|
||||
)
|
||||
opt_close = next(
|
||||
(f for f in rows if f.get("leg") == "option" and f.get("action") == "close"),
|
||||
(f for f in rows if f.get("leg") == leg and f.get("action") == "close"),
|
||||
None,
|
||||
)
|
||||
if not opt_open or not opt_close:
|
||||
return None
|
||||
qty = float(opt_open.get("qty_eth") or opt_close.get("qty_eth") or 0)
|
||||
return (float(opt_close["fill_px"]) - float(opt_open["fill_px"])) * qty
|
||||
|
||||
|
||||
def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
"""
|
||||
价差盈亏按 fill_px;手续费另扣。
|
||||
净盈亏 = 各腿盈亏之和 − 全部手续费(开+平)。
|
||||
支持永期(option+perp)与期期(option+option2)。
|
||||
|
||||
手续费拆:fees_perp / fees_option(含 option2)/ fees_option2;
|
||||
滑点合计 slip_total(SIM 记账;LIVE 应为 0)。
|
||||
"""
|
||||
rows = [_as_map(x) for x in fills]
|
||||
|
||||
option_pnl = _leg_option_pnl(rows, "option")
|
||||
option2_pnl = _leg_option_pnl(rows, "option2")
|
||||
|
||||
perp_open = next(
|
||||
(f for f in rows if f.get("leg") == "perp" and f.get("action") == "open"),
|
||||
None,
|
||||
@@ -40,11 +52,6 @@ def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
None,
|
||||
)
|
||||
|
||||
option_pnl: float | None = None
|
||||
if opt_open and opt_close:
|
||||
qty = float(opt_open.get("qty_eth") or opt_close.get("qty_eth") or 0)
|
||||
option_pnl = (float(opt_close["fill_px"]) - float(opt_open["fill_px"])) * qty
|
||||
|
||||
perp_pnl: float | None = None
|
||||
if perp_open and perp_close:
|
||||
qty = float(perp_open.get("qty_eth") or perp_close.get("qty_eth") or 0)
|
||||
@@ -60,27 +67,34 @@ def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
float(f.get("fee") or 0) for f in rows if str(f.get("leg") or "") == "perp"
|
||||
)
|
||||
fees_option = sum(
|
||||
float(f.get("fee") or 0) for f in rows if str(f.get("leg") or "") == "option"
|
||||
float(f.get("fee") or 0)
|
||||
for f in rows
|
||||
if str(f.get("leg") or "") in ("option", "option2")
|
||||
)
|
||||
fees_option2 = sum(
|
||||
float(f.get("fee") or 0) for f in rows if str(f.get("leg") or "") == "option2"
|
||||
)
|
||||
fees_total = fees_perp + fees_option
|
||||
slip_total = sum(float(f.get("slip") or 0) for f in rows)
|
||||
gross = None
|
||||
net = None
|
||||
if option_pnl is not None and perp_pnl is not None:
|
||||
gross = option_pnl + perp_pnl
|
||||
net = gross - fees_total
|
||||
elif option_pnl is not None:
|
||||
gross = option_pnl
|
||||
net = option_pnl - fees_total
|
||||
elif perp_pnl is not None:
|
||||
gross = perp_pnl
|
||||
net = perp_pnl - fees_total
|
||||
|
||||
parts: list[float] = []
|
||||
if option_pnl is not None:
|
||||
parts.append(option_pnl)
|
||||
if option2_pnl is not None:
|
||||
parts.append(option2_pnl)
|
||||
if perp_pnl is not None:
|
||||
parts.append(perp_pnl)
|
||||
|
||||
gross = sum(parts) if parts else None
|
||||
net = (gross - fees_total) if gross is not None else None
|
||||
|
||||
return {
|
||||
"option_pnl": option_pnl,
|
||||
"option2_pnl": option2_pnl,
|
||||
"perp_pnl": perp_pnl,
|
||||
"fees_perp": fees_perp,
|
||||
"fees_option": fees_option,
|
||||
"fees_option2": fees_option2,
|
||||
"fees_total": fees_total,
|
||||
"slip_total": slip_total,
|
||||
"gross_pnl": gross,
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""期期成交盈亏汇总:须计入 option2(Put)腿。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.sim.pnl import summarize_fills_pnl
|
||||
|
||||
|
||||
def test_summarize_oo_both_legs() -> None:
|
||||
fills = [
|
||||
{
|
||||
"leg": "option",
|
||||
"action": "open",
|
||||
"side": "long",
|
||||
"fill_px": 12.6,
|
||||
"qty_eth": 3.5,
|
||||
"fee": 0.0221,
|
||||
"slip": 0,
|
||||
},
|
||||
{
|
||||
"leg": "option2",
|
||||
"action": "open",
|
||||
"side": "long",
|
||||
"fill_px": 9.0,
|
||||
"qty_eth": 5.0,
|
||||
"fee": 0.0225,
|
||||
"slip": 0,
|
||||
},
|
||||
{
|
||||
"leg": "option",
|
||||
"action": "close",
|
||||
"side": "sell",
|
||||
"fill_px": 0.0,
|
||||
"qty_eth": 3.5,
|
||||
"fee": 0.0,
|
||||
"slip": 0,
|
||||
},
|
||||
{
|
||||
"leg": "option2",
|
||||
"action": "close",
|
||||
"side": "sell",
|
||||
"fill_px": 0.0,
|
||||
"qty_eth": 5.0,
|
||||
"fee": 0.0,
|
||||
"slip": 0,
|
||||
},
|
||||
]
|
||||
s = summarize_fills_pnl(fills)
|
||||
assert s["option_pnl"] == -12.6 * 3.5
|
||||
assert s["option2_pnl"] == -9.0 * 5.0
|
||||
assert s["perp_pnl"] is None
|
||||
assert abs(float(s["fees_option"] or 0) - 0.0446) < 1e-9
|
||||
assert abs(float(s["gross_pnl"] or 0) - (-44.1 - 45.0)) < 1e-9
|
||||
assert abs(float(s["net_pnl"] or 0) - (-89.1 - 0.0446)) < 1e-9
|
||||
|
||||
|
||||
def test_summarize_perp_option_unchanged() -> None:
|
||||
fills = [
|
||||
{"leg": "option", "action": "open", "fill_px": 10, "qty_eth": 2, "fee": 0.1, "slip": 0},
|
||||
{"leg": "perp", "action": "open", "side": "short", "fill_px": 100, "qty_eth": 1, "fee": 0.2, "slip": 0},
|
||||
{"leg": "option", "action": "close", "fill_px": 12, "qty_eth": 2, "fee": 0.1, "slip": 0},
|
||||
{"leg": "perp", "action": "close", "side": "short", "fill_px": 98, "qty_eth": 1, "fee": 0.2, "slip": 0},
|
||||
]
|
||||
s = summarize_fills_pnl(fills)
|
||||
assert s["option_pnl"] == 4.0
|
||||
assert s["perp_pnl"] == 2.0
|
||||
assert s["option2_pnl"] is None
|
||||
assert abs(float(s["net_pnl"] or 0) - (6.0 - 0.6)) < 1e-9
|
||||
Reference in New Issue
Block a user