Show option/perp PnL and fee-deducted net in trade details.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -14,16 +14,92 @@ def _row(r: Any) -> dict:
|
||||
return dict(r)
|
||||
|
||||
|
||||
def summarize_fills_pnl(fills: list[Any]) -> dict[str, float | None]:
|
||||
"""
|
||||
从成交明细重算腿盈亏与净盈亏。
|
||||
价差盈亏按 fill_px(成交价);手续费另扣。
|
||||
净盈亏 = 期权盈亏 + 永续盈亏 − 全部手续费(开+平)。
|
||||
"""
|
||||
rows = [dict(x) for x in fills]
|
||||
opt_open = next(
|
||||
(f for f in rows if f.get("leg") == "option" and f.get("action") == "open"),
|
||||
None,
|
||||
)
|
||||
opt_close = next(
|
||||
(f for f in rows if f.get("leg") == "option" and f.get("action") == "close"),
|
||||
None,
|
||||
)
|
||||
perp_open = next(
|
||||
(f for f in rows if f.get("leg") == "perp" and f.get("action") == "open"),
|
||||
None,
|
||||
)
|
||||
perp_close = next(
|
||||
(f for f in rows if f.get("leg") == "perp" and f.get("action") == "close"),
|
||||
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)
|
||||
side = str(perp_open.get("side") or "")
|
||||
o = float(perp_open["fill_px"])
|
||||
c = float(perp_close["fill_px"])
|
||||
if side == "long":
|
||||
perp_pnl = (c - o) * qty
|
||||
else:
|
||||
perp_pnl = (o - c) * qty
|
||||
|
||||
fees_total = sum(float(f.get("fee") 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
|
||||
|
||||
return {
|
||||
"option_pnl": option_pnl,
|
||||
"perp_pnl": perp_pnl,
|
||||
"fees_total": fees_total,
|
||||
"gross_pnl": gross,
|
||||
"net_pnl": net,
|
||||
}
|
||||
|
||||
|
||||
@router.get("/groups")
|
||||
async def list_groups(_user: Annotated[str, Depends(require_user)]) -> dict:
|
||||
rows = get_db().fetchall(
|
||||
"SELECT * FROM groups ORDER BY open_at_ms DESC LIMIT 200"
|
||||
)
|
||||
return {"groups": [_row(x) for x in rows]}
|
||||
db = get_db()
|
||||
rows = db.fetchall("SELECT * FROM groups ORDER BY open_at_ms DESC LIMIT 200")
|
||||
groups = []
|
||||
for r in rows:
|
||||
g = _row(r)
|
||||
if g.get("status") == "closed":
|
||||
fills = db.fetchall(
|
||||
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC",
|
||||
(g["group_id"],),
|
||||
)
|
||||
summary = summarize_fills_pnl(fills)
|
||||
g["pnl_summary"] = summary
|
||||
if summary.get("net_pnl") is not None:
|
||||
g["net_pnl"] = summary["net_pnl"]
|
||||
groups.append(g)
|
||||
return {"groups": groups}
|
||||
|
||||
|
||||
@router.get("/groups/{group_id}")
|
||||
async def group_detail(group_id: str, _user: Annotated[str, Depends(require_user)]) -> dict:
|
||||
async def group_detail(
|
||||
group_id: str, _user: Annotated[str, Depends(require_user)]
|
||||
) -> dict:
|
||||
db = get_db()
|
||||
g = db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
if g is None:
|
||||
@@ -31,4 +107,9 @@ async def group_detail(group_id: str, _user: Annotated[str, Depends(require_user
|
||||
fills = db.fetchall(
|
||||
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC", (group_id,)
|
||||
)
|
||||
return {"group": _row(g), "fills": [_row(x) for x in fills]}
|
||||
summary = summarize_fills_pnl(fills)
|
||||
return {
|
||||
"group": _row(g),
|
||||
"fills": [_row(x) for x in fills],
|
||||
"pnl_summary": summary,
|
||||
}
|
||||
|
||||
@@ -483,6 +483,17 @@ class Matcher:
|
||||
)
|
||||
|
||||
net = perp_pnl + opt_pnl - pf.fee - of.fee
|
||||
# 组已累计开仓手续费;实现净盈亏扣开+平全部手续费
|
||||
open_fees = float(
|
||||
(
|
||||
self.db.fetchone(
|
||||
"SELECT fees FROM groups WHERE group_id=?", (group_id,)
|
||||
)
|
||||
or {"fees": 0}
|
||||
)["fees"]
|
||||
or 0
|
||||
)
|
||||
net_after_all_fees = perp_pnl + opt_pnl - open_fees - pf.fee - of.fee
|
||||
|
||||
now = int(time.time() * 1000)
|
||||
with self.db._lock:
|
||||
@@ -535,7 +546,7 @@ class Matcher:
|
||||
self.db._conn.execute(
|
||||
"""UPDATE groups SET status=?, close_at_ms=?, close_reason=?, realized_pnl=?,
|
||||
fees=?, slip_cost=?, note=NULL WHERE group_id=?""",
|
||||
("closed", now, reason, net, fees, slip, group_id),
|
||||
("closed", now, reason, net_after_all_fees, fees, slip, group_id),
|
||||
)
|
||||
self.db._conn.execute(
|
||||
"""UPDATE positions SET
|
||||
@@ -554,7 +565,9 @@ class Matcher:
|
||||
"reason": reason,
|
||||
"perp_pnl": perp_pnl,
|
||||
"option_pnl": opt_pnl,
|
||||
"net": net,
|
||||
"net": net_after_all_fees,
|
||||
"fees_open": open_fees,
|
||||
"fees_close": pf.fee + of.fee,
|
||||
"close_sequence": ["option", "perp"],
|
||||
"cash_delta": opt_cash + perp_pnl - pf.fee,
|
||||
"option_close_bid": float(close_bid),
|
||||
|
||||
Reference in New Issue
Block a user