fix: show header realtime PnL from exchange positions and order metrics

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-07 02:04:40 +08:00
parent 3b494932c7
commit 659c0969fe
7 changed files with 234 additions and 41 deletions
+30 -13
View File
@@ -1021,6 +1021,32 @@ function refreshOrderDefaults(){
}).catch(()=>{});
}
function paintRealtimePnl(v){
const pnlEl = document.getElementById("realtime-pnl");
if(!pnlEl) return;
if(v === null || v === undefined || Number.isNaN(Number(v))){
pnlEl.innerText = "—";
pnlEl.classList.remove("pnl-pos", "pnl-neg");
return;
}
const n = Number(v);
const sign = n > 0 ? "+" : "";
pnlEl.innerText = `${sign}${n.toFixed(2)}U`;
pnlEl.classList.toggle("pnl-pos", n > 0);
pnlEl.classList.toggle("pnl-neg", n < 0);
}
function sumOrdersFloatPnl(orders){
if(!orders || !orders.length) return null;
let total = 0, found = false;
orders.forEach(o=>{
if(o.float_pnl != null && !Number.isNaN(Number(o.float_pnl))){
total += Number(o.float_pnl);
found = true;
}
});
return found ? total : null;
}
function refreshAccountSnapshot(){
fetch("/api/account_snapshot").then(r=>r.json()).then(data=>{
if (typeof data.funding_usdt !== "undefined") {
@@ -1032,19 +1058,7 @@ function refreshAccountSnapshot(){
if(el) el.innerText = `${Number(data.current_capital).toFixed(2)}U`;
}
if (typeof data.unrealized_pnl !== "undefined") {
const pnlEl = document.getElementById("realtime-pnl");
if (pnlEl) {
if (data.unrealized_pnl === null || data.unrealized_pnl === undefined) {
pnlEl.innerText = "—";
pnlEl.classList.remove("pnl-pos", "pnl-neg");
} else {
const v = Number(data.unrealized_pnl);
const sign = v > 0 ? "+" : "";
pnlEl.innerText = `${sign}${v.toFixed(2)}U`;
pnlEl.classList.toggle("pnl-pos", v > 0);
pnlEl.classList.toggle("pnl-neg", v < 0);
}
}
paintRealtimePnl(data.unrealized_pnl);
}
if (typeof data.available_trading_usdt !== "undefined" && data.available_trading_usdt !== null) {
latestAvailableUsdt = Number(data.available_trading_usdt);
@@ -1281,6 +1295,9 @@ function refreshPriceSnapshotConditional(){
});
tickOrderHoldDurations();
}
if(data.order_prices && data.order_prices.length){
paintRealtimePnl(sumOrdersFloatPnl(data.order_prices));
}
}).catch(()=>{});
}
function formatLiveHoldDurationFromMs(openedMs, nowMs){