Add fleet stats table with funds, fees, loss streak, and totals.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -12,6 +12,17 @@ from .auth import require_user
|
||||
router = APIRouter(prefix="/api/stats", tags=["stats"])
|
||||
|
||||
|
||||
def _current_loss_streak(curve: list[dict[str, Any]]) -> int:
|
||||
"""从最近已平仓组往前数连续亏损次数。"""
|
||||
streak = 0
|
||||
for item in reversed(curve):
|
||||
if float(item.get("realized_pnl") or 0) < 0:
|
||||
streak += 1
|
||||
else:
|
||||
break
|
||||
return streak
|
||||
|
||||
|
||||
def build_stats_summary(db: Any | None = None) -> dict[str, Any]:
|
||||
"""已平仓组汇总(策略页统计 / 中控 Fleet 共用)。"""
|
||||
database = db or get_db()
|
||||
@@ -21,6 +32,10 @@ def build_stats_summary(db: Any | None = None) -> dict[str, Any]:
|
||||
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)
|
||||
pnls = [float(r["realized_pnl"] or 0) for r in rows]
|
||||
max_single_loss = min(pnls) if pnls else 0.0
|
||||
if max_single_loss > 0:
|
||||
max_single_loss = 0.0
|
||||
|
||||
fees_perp = 0.0
|
||||
fees_option = 0.0
|
||||
@@ -51,6 +66,19 @@ def build_stats_summary(db: Any | None = None) -> dict[str, Any]:
|
||||
}
|
||||
for r in sorted(rows, key=lambda x: int(x["close_at_ms"] or 0))
|
||||
]
|
||||
latest_funds = 0.0
|
||||
try:
|
||||
from ..sim.funds_wallets import FundsWallets
|
||||
|
||||
latest_funds = float(FundsWallets(database).total_usdt_equiv())
|
||||
except Exception:
|
||||
try:
|
||||
from ..sim.ledger import Ledger
|
||||
|
||||
latest_funds = float(Ledger(database).snapshot().get("equity") or 0)
|
||||
except Exception:
|
||||
latest_funds = 0.0
|
||||
|
||||
return {
|
||||
"mode": mode,
|
||||
"show_slip": mode == "SIM",
|
||||
@@ -64,6 +92,9 @@ def build_stats_summary(db: Any | None = None) -> dict[str, Any]:
|
||||
"total_slip": total_slip if mode == "SIM" else 0.0,
|
||||
"close_reasons": reasons,
|
||||
"equity_curve": curve,
|
||||
"latest_funds": latest_funds,
|
||||
"max_single_loss": max_single_loss,
|
||||
"loss_streak": _current_loss_streak(curve),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user