Fix funds bar day stats: align trades, win rate, and PL ratio.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-02 17:05:11 +08:00
parent b983b63c46
commit d97f7d20e5
2 changed files with 133 additions and 15 deletions
+10 -15
View File
@@ -66,26 +66,21 @@ async def funds_summary(_user: Annotated[str, Depends(require_user)]) -> dict[st
exchange = str(st.get("exchange") or s.exchange or "okx").upper()
trading_day = datetime.now(SH).strftime("%Y-%m-%d")
closed = db.fetchall(
"SELECT realized_pnl, close_at_ms FROM groups WHERE status='closed'"
)
pnls = [float(r["realized_pnl"] or 0) for r in closed]
n = len(pnls)
wins = sum(1 for x in pnls if x > 0)
win_rate = (wins / n) if n else 0.0
# 当日成交组
# 顶栏「总交易 / 胜率 / 盈亏比」与交易日同一口径:上海自然日开仓组 G-YYYYMMDD-*
day_prefix = trading_day.replace("-", "")
day_groups = db.fetchall(
"SELECT realized_pnl FROM groups WHERE group_id LIKE ? AND status='closed'",
(f"G-{day_prefix}-%",),
)
day_n = len(day_groups)
day_pnls = [float(r["realized_pnl"] or 0) for r in day_groups]
day_n = len(day_pnls)
day_wins = sum(1 for x in day_pnls if x > 0)
day_win_rate = (day_wins / day_n) if day_n else 0.0
pos = st.get("position") or {}
upl = pos
realtime = None
if str(pos.get("status") or "") == "open":
pos_st = str(pos.get("status") or "")
if pos_st in ("open", "half_open", "option_closed_perp_pending"):
realtime = float(pos.get("net_pnl") or 0)
if s.is_sim:
@@ -144,9 +139,9 @@ async def funds_summary(_user: Annotated[str, Depends(require_user)]) -> dict[st
"mode": mode,
"exchange": exchange,
"trading_day": trading_day,
"total_trades": day_n if day_n else n,
"win_rate": win_rate,
"profit_loss_ratio": _pl_ratio(pnls),
"total_trades": day_n,
"win_rate": day_win_rate,
"profit_loss_ratio": _pl_ratio(day_pnls),
"total_funds": total,
"funding_usdt": funding_usdt,
"trading_usdt": trading_usdt,