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:
@@ -4,7 +4,9 @@ from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
|
||||
from ..config import get_settings
|
||||
from ..models.db import get_db
|
||||
from ..sim.pnl import summarize_fills_pnl
|
||||
from .auth import require_user
|
||||
|
||||
router = APIRouter(prefix="/api/stats", tags=["stats"])
|
||||
@@ -13,12 +15,30 @@ router = APIRouter(prefix="/api/stats", tags=["stats"])
|
||||
@router.get("/summary")
|
||||
async def stats_summary(_user: Annotated[str, Depends(require_user)]) -> dict:
|
||||
db = get_db()
|
||||
s = get_settings()
|
||||
mode = "LIVE" if not s.is_sim else "SIM"
|
||||
rows = db.fetchall("SELECT * FROM groups WHERE status='closed'")
|
||||
n = len(rows)
|
||||
wins = sum(1 for r in rows if float(r["realized_pnl"] or 0) > 0)
|
||||
total_pnl = sum(float(r["realized_pnl"] or 0) for r in rows)
|
||||
total_fees = sum(float(r["fees"] or 0) for r in rows)
|
||||
total_slip = sum(float(r["slip_cost"] or 0) for r in rows)
|
||||
|
||||
fees_perp = 0.0
|
||||
fees_option = 0.0
|
||||
total_slip = 0.0
|
||||
for r in rows:
|
||||
fills = db.fetchall(
|
||||
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC",
|
||||
(r["group_id"],),
|
||||
)
|
||||
summary = summarize_fills_pnl(list(fills))
|
||||
fees_perp += float(summary.get("fees_perp") or 0)
|
||||
fees_option += float(summary.get("fees_option") or 0)
|
||||
# LIVE 不展示、不计入滑点;按组成交模式判断(可混有历史 SIM 组)
|
||||
exec_mode = str(r["exec_mode"] or mode).upper()
|
||||
if exec_mode != "LIVE":
|
||||
total_slip += float(summary.get("slip_total") or 0)
|
||||
|
||||
total_fees = fees_perp + fees_option
|
||||
reasons: dict[str, int] = {}
|
||||
for r in rows:
|
||||
k = str(r["close_reason"] or "unknown")
|
||||
@@ -32,12 +52,16 @@ async def stats_summary(_user: Annotated[str, Depends(require_user)]) -> dict:
|
||||
for r in sorted(rows, key=lambda x: int(x["close_at_ms"] or 0))
|
||||
]
|
||||
return {
|
||||
"mode": mode,
|
||||
"show_slip": mode == "SIM",
|
||||
"groups": n,
|
||||
"wins": wins,
|
||||
"win_rate": (wins / n) if n else 0.0,
|
||||
"total_pnl": total_pnl,
|
||||
"fees_perp": fees_perp,
|
||||
"fees_option": fees_option,
|
||||
"total_fees": total_fees,
|
||||
"total_slip": total_slip,
|
||||
"total_slip": total_slip if mode == "SIM" else 0.0,
|
||||
"close_reasons": reasons,
|
||||
"equity_curve": curve,
|
||||
}
|
||||
|
||||
@@ -555,7 +555,7 @@ class BinanceLiveExecutor(Matcher):
|
||||
of_px = float(prev["fill_px"])
|
||||
of_fee = float(prev["fee"] or 0)
|
||||
of_notional = float(prev["notional"] or (of_px * opt_qty))
|
||||
of_slip = float(prev["slip"] or 0)
|
||||
of_slip = 0.0 # LIVE 不计模拟滑点
|
||||
else:
|
||||
# 含到期:优先交易所真实平期权;失败且无内在价值时可本地结算
|
||||
try:
|
||||
@@ -576,12 +576,12 @@ class BinanceLiveExecutor(Matcher):
|
||||
of = option_expiry_settle(
|
||||
intrinsic=float(intrinsic), qty_eth=opt_qty, fee_rate=fee_rate
|
||||
)
|
||||
of_px, of_fee, of_slip, of_notional = (
|
||||
of_px, of_fee, of_notional = (
|
||||
of.fill_px,
|
||||
of.fee,
|
||||
of.slip,
|
||||
of.notional,
|
||||
)
|
||||
of_slip = 0.0 # LIVE 不计模拟滑点
|
||||
logger.warning(
|
||||
"expiry option exchange close failed, local settle: %s", e
|
||||
)
|
||||
@@ -759,9 +759,9 @@ class BinanceLiveExecutor(Matcher):
|
||||
g = self.db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
base_fees = float((g["fees"] if g else 0) or 0)
|
||||
fees = base_fees + (0.0 if skip_option_cash else of_fee) + pf_fee
|
||||
slip = float((g["slip_cost"] if g else 0) or 0) + (
|
||||
0.0 if option_fill_already_written else of_slip
|
||||
)
|
||||
# LIVE:真实成交价已含盘口冲击,不另计/不计模拟滑点
|
||||
of_slip = 0.0
|
||||
slip = 0.0
|
||||
from ..sim.pnl import summarize_fills_pnl
|
||||
|
||||
with self.db._lock:
|
||||
|
||||
@@ -570,7 +570,7 @@ class OkxLiveExecutor(Matcher):
|
||||
of_px = float(prev["fill_px"])
|
||||
of_fee = float(prev["fee"] or 0)
|
||||
of_notional = float(prev["notional"] or (of_px * opt_qty))
|
||||
of_slip = float(prev["slip"] or 0)
|
||||
of_slip = 0.0 # LIVE 不计模拟滑点
|
||||
else:
|
||||
# 含到期:优先交易所真实平期权;失败且无内在价值时可本地结算
|
||||
try:
|
||||
@@ -593,12 +593,12 @@ class OkxLiveExecutor(Matcher):
|
||||
of = option_expiry_settle(
|
||||
intrinsic=float(intrinsic), qty_eth=opt_qty, fee_rate=fee_rate
|
||||
)
|
||||
of_px, of_fee, of_slip, of_notional = (
|
||||
of_px, of_fee, of_notional = (
|
||||
of.fill_px,
|
||||
of.fee,
|
||||
of.slip,
|
||||
of.notional,
|
||||
)
|
||||
of_slip = 0.0 # LIVE 不计模拟滑点
|
||||
logger.warning(
|
||||
"expiry option exchange close failed, local settle: %s", e
|
||||
)
|
||||
@@ -779,9 +779,9 @@ class OkxLiveExecutor(Matcher):
|
||||
g = self.db.fetchone("SELECT * FROM groups WHERE group_id=?", (group_id,))
|
||||
base_fees = float((g["fees"] if g else 0) or 0)
|
||||
fees = base_fees + (0.0 if skip_option_cash else of_fee) + pf_fee
|
||||
slip = float((g["slip_cost"] if g else 0) or 0) + (
|
||||
0.0 if option_fill_already_written else of_slip
|
||||
)
|
||||
# LIVE:真实成交价已含盘口冲击,不另计/不计模拟滑点
|
||||
of_slip = 0.0
|
||||
slip = 0.0
|
||||
from ..sim.pnl import summarize_fills_pnl
|
||||
|
||||
with self.db._lock:
|
||||
|
||||
+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,
|
||||
}
|
||||
|
||||
@@ -5,6 +5,16 @@
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-29 — 统计拆分手续费;LIVE 不展示/不计滑点
|
||||
|
||||
### 变更
|
||||
|
||||
1. 统计页:`永续手续费` / `期权手续费` / `手续费合计`;SIM 另列 `滑点合计`。
|
||||
2. 交易明细同步拆分手续费;SIM 显示滑点,LIVE 不显示。
|
||||
3. LIVE 成交滑点字段强制为 0,不把模拟滑点计入实盘盈亏。
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-29 — 到期结算展示:指数 / 行权价 / 内在价值
|
||||
|
||||
### 变更
|
||||
|
||||
+2
-1
@@ -98,7 +98,8 @@
|
||||
### 3.4 费用(SIM)
|
||||
|
||||
- 永续、期权均按可配 `fee_rate`(默认 0.0005)计费。
|
||||
- 滑点按约 1 倍费率计入成交价,手续费另扣。
|
||||
- **SIM**:滑点按约 1 倍费率计入成交价,手续费另扣;统计拆分「永续手续费 / 期权手续费 / 滑点」。
|
||||
- **LIVE**:用交易所真实成交价与真实手续费,**不计、不展示**模拟滑点。
|
||||
- 开仓现金:支付期权权利金 + 开仓手续费;永续开仓主要扣费。
|
||||
|
||||
---
|
||||
|
||||
@@ -3,10 +3,14 @@ import { apiFetch } from "../api/client";
|
||||
import { closeReasonZh } from "../labels";
|
||||
|
||||
type Summary = {
|
||||
mode?: "SIM" | "LIVE" | string;
|
||||
show_slip?: boolean;
|
||||
groups: number;
|
||||
wins: number;
|
||||
win_rate: number;
|
||||
total_pnl: number;
|
||||
fees_perp?: number;
|
||||
fees_option?: number;
|
||||
total_fees: number;
|
||||
total_slip: number;
|
||||
close_reasons: Record<string, number>;
|
||||
@@ -26,6 +30,7 @@ export default function StatsPage() {
|
||||
const reasonEntries = s
|
||||
? Object.entries(s.close_reasons).sort((a, b) => b[1] - a[1])
|
||||
: [];
|
||||
const showSlip = s?.show_slip ?? s?.mode !== "LIVE";
|
||||
|
||||
return (
|
||||
<div className="card">
|
||||
@@ -46,11 +51,27 @@ export default function StatsPage() {
|
||||
<span className="mono">{s.total_pnl.toFixed(2)}</span>
|
||||
</div>
|
||||
<div className="kv">
|
||||
<span>总手续费 / 滑点</span>
|
||||
<span>永续手续费</span>
|
||||
<span className="mono">
|
||||
{s.total_fees.toFixed(2)} / {s.total_slip.toFixed(2)}
|
||||
{(s.fees_perp ?? 0).toFixed(4)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="kv">
|
||||
<span>期权手续费</span>
|
||||
<span className="mono">
|
||||
{(s.fees_option ?? 0).toFixed(4)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="kv">
|
||||
<span>手续费合计</span>
|
||||
<span className="mono">{s.total_fees.toFixed(4)}</span>
|
||||
</div>
|
||||
{showSlip ? (
|
||||
<div className="kv">
|
||||
<span>滑点合计</span>
|
||||
<span className="mono">{s.total_slip.toFixed(4)}</span>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="kv" style={{ alignItems: "flex-start" }}>
|
||||
<span>平仓原因</span>
|
||||
<span className="mono" style={{ textAlign: "right" }}>
|
||||
|
||||
@@ -10,7 +10,10 @@ import {
|
||||
type PnlSummary = {
|
||||
option_pnl: number | null;
|
||||
perp_pnl: number | null;
|
||||
fees_perp?: number;
|
||||
fees_option?: number;
|
||||
fees_total: number;
|
||||
slip_total?: number;
|
||||
gross_pnl: number | null;
|
||||
net_pnl: number | null;
|
||||
};
|
||||
@@ -41,6 +44,7 @@ type Group = {
|
||||
hold_basis?: string | null;
|
||||
strike?: number | null;
|
||||
settle_index_px?: number | null;
|
||||
exec_mode?: string | null;
|
||||
expiry_settle?: ExpirySettle | null;
|
||||
pnl_summary?: PnlSummary;
|
||||
};
|
||||
@@ -349,10 +353,31 @@ export default function TradesPage() {
|
||||
{fmt(summary.perp_pnl)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="kv">
|
||||
<span>永续手续费</span>
|
||||
<span className="mono">
|
||||
{fmt(summary.fees_perp ?? 0, 4)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="kv">
|
||||
<span>期权手续费</span>
|
||||
<span className="mono">
|
||||
{fmt(summary.fees_option ?? 0, 4)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="kv">
|
||||
<span>手续费合计</span>
|
||||
<span className="mono">{fmt(summary.fees_total, 4)}</span>
|
||||
</div>
|
||||
{String(selectedGroup.exec_mode || "").toUpperCase() !==
|
||||
"LIVE" ? (
|
||||
<div className="kv">
|
||||
<span>滑点</span>
|
||||
<span className="mono">
|
||||
{fmt(summary.slip_total ?? 0, 4)}
|
||||
</span>
|
||||
</div>
|
||||
) : null}
|
||||
<div className="kv">
|
||||
<span>
|
||||
<strong>净盈亏(扣费后)</strong>
|
||||
|
||||
Reference in New Issue
Block a user