Split fee stats by leg and hide LIVE slip.
SIM shows perp/option fees and slip separately; LIVE keeps real exchange fees only with slip forced to zero. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+23
-2
@@ -5,13 +5,24 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _as_map(x: Any) -> dict[str, Any]:
|
||||
if isinstance(x, dict):
|
||||
return x
|
||||
try:
|
||||
return dict(x)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
"""
|
||||
价差盈亏按 fill_px;手续费另扣。
|
||||
净盈亏 = 期权盈亏 + 永续盈亏 − 全部手续费(开+平)。
|
||||
允许只有永续已平、期权尚未结算的半组。
|
||||
|
||||
手续费拆:fees_perp / fees_option;滑点合计 slip_total(SIM 记账;LIVE 应为 0)。
|
||||
"""
|
||||
rows = [dict(x) for x in fills]
|
||||
rows = [_as_map(x) for x in fills]
|
||||
opt_open = next(
|
||||
(f for f in rows if f.get("leg") == "option" and f.get("action") == "open"),
|
||||
None,
|
||||
@@ -45,7 +56,14 @@ def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
else:
|
||||
perp_pnl = (o - c) * qty
|
||||
|
||||
fees_total = sum(float(f.get("fee") or 0) for f in rows)
|
||||
fees_perp = sum(
|
||||
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"
|
||||
)
|
||||
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:
|
||||
@@ -61,7 +79,10 @@ def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
return {
|
||||
"option_pnl": option_pnl,
|
||||
"perp_pnl": perp_pnl,
|
||||
"fees_perp": fees_perp,
|
||||
"fees_option": fees_option,
|
||||
"fees_total": fees_total,
|
||||
"slip_total": slip_total,
|
||||
"gross_pnl": gross,
|
||||
"net_pnl": net,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user