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