Align funds bar and win-rate with OO expiry intrinsic repair; wire Binance balances.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -66,11 +66,26 @@ 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 FROM groups WHERE status='closed'"
|
||||
)
|
||||
pnls = [float(r["realized_pnl"] or 0) for r in closed]
|
||||
# 顶栏「总交易 / 胜率 / 盈亏比」:用展示口径净盈亏(含到期内在价值修复)
|
||||
closed = db.fetchall("SELECT * FROM groups WHERE status='closed'")
|
||||
pnls: list[float] = []
|
||||
try:
|
||||
from .trades import _enrich_group, persist_expiry_overlay_if_needed
|
||||
|
||||
for r in closed:
|
||||
g = dict(r)
|
||||
fills = db.fetchall(
|
||||
"SELECT * FROM fills WHERE group_id=? ORDER BY id ASC",
|
||||
(g["group_id"],),
|
||||
)
|
||||
gr = _enrich_group(g, fills)
|
||||
try:
|
||||
persist_expiry_overlay_if_needed(db, gr, list(fills))
|
||||
except Exception:
|
||||
pass
|
||||
pnls.append(float(gr.get("net_pnl") or gr.get("realized_pnl") or 0))
|
||||
except Exception:
|
||||
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
|
||||
@@ -124,13 +139,42 @@ async def funds_summary(_user: Annotated[str, Depends(require_user)]) -> dict[st
|
||||
}
|
||||
finally:
|
||||
client.close()
|
||||
elif exchange in ("BINANCE", "BN"):
|
||||
from ..live.binance_trade import BinanceTradeClient
|
||||
|
||||
client = BinanceTradeClient()
|
||||
try:
|
||||
bal = client.fetch_balances()
|
||||
funding_usdt = bal.get("funding_usdt")
|
||||
trading_usdt = bal.get("trading_usdt")
|
||||
funding_usdc = bal.get("funding_usdc")
|
||||
trading_usdc = bal.get("trading_usdc")
|
||||
r = float(rate) if rate and rate > 0 else 1.0
|
||||
total = round(
|
||||
(funding_usdt or 0.0)
|
||||
+ (trading_usdt or 0.0)
|
||||
+ ((funding_usdc or 0.0) + (trading_usdc or 0.0)) * r,
|
||||
2,
|
||||
)
|
||||
except Exception as e:
|
||||
return {
|
||||
"ok": False,
|
||||
"mode": mode,
|
||||
"exchange": exchange,
|
||||
"detail": str(e),
|
||||
}
|
||||
finally:
|
||||
try:
|
||||
client.close()
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
# 非 OKX LIVE:暂无统一资金接口,不回退模拟账本(避免实盘显示假资金)
|
||||
# 其它交易所:不回退模拟账本(避免实盘显示假资金)
|
||||
return {
|
||||
"ok": False,
|
||||
"mode": mode,
|
||||
"exchange": exchange,
|
||||
"detail": f"{exchange} 实盘资金摘要暂未接入,请用 OKX 或交易所 App 查看",
|
||||
"detail": f"{exchange} 实盘资金摘要暂未接入,请用交易所 App 查看",
|
||||
}
|
||||
return {
|
||||
"ok": True,
|
||||
|
||||
Reference in New Issue
Block a user