Apply account-PnL display pref to dashboard KPI and position tables.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-23 12:44:36 +08:00
parent 8e3c00641f
commit b6156e0049
3 changed files with 49 additions and 19 deletions
+41 -16
View File
@@ -54,8 +54,16 @@
elStatus.className = "dash-status" + (isErr ? " err" : "");
}
function showAccountPnlPref() {
if (typeof window.hubShowAccountPnlPref === "function") {
return !!window.hubShowAccountPnlPref();
}
return true;
}
function renderKpi(totals) {
if (!elKpi || !totals) return;
const showPnl = showAccountPnlPref();
const closed = Number(totals.total_pnl_u);
const floating = Number(totals.float_pnl_u);
const funding = totals.total_funding_usdt;
@@ -68,16 +76,20 @@
totals.perpetual_open_position_count != null
? Number(totals.perpetual_open_position_count) || 0
: Math.max(0, totalPos - optPos);
const items = [
kpiItem("交易日", esc(totals.trading_day || "—")),
kpiItem("资金合计", Number.isFinite(funds) ? `${fmt(funds, 2)}U` : "—"),
const items = [kpiItem("交易日", esc(totals.trading_day || "—"))];
if (showPnl) {
items.push(kpiItem("资金合计", Number.isFinite(funds) ? `${fmt(funds, 2)}U` : "—"));
}
items.push(
kpiItem("总持仓数量", `${totalPos}`),
kpiItem("期权持仓", `${optPos}`),
kpiItem("永续持仓", `${perpPos}`),
kpiItem("平仓数量", `${totals.closed_count || 0}`),
kpiItem("平仓盈亏", pnlSigned(closed, 2), pnlClass(closed)),
kpiItem("浮盈亏", pnlSigned(floating, 2), pnlClass(floating)),
];
kpiItem("平仓盈亏", pnlSigned(closed, 2), pnlClass(closed))
);
if (showPnl) {
items.push(kpiItem("浮盈亏", pnlSigned(floating, 2), pnlClass(floating)));
}
elKpi.innerHTML = `<div class="dash-kpi-summary">${items.join("")}</div>`;
}
@@ -197,6 +209,7 @@
function renderUnifiedPerpTable(rows) {
if (!rows.length) return "";
const showPnl = showAccountPnlPref();
const body = rows
.map(({ ac, ln }) => {
const source = String((ln && ln.source) || "—");
@@ -213,7 +226,7 @@
<td>${contracts}</td>
<td>${slTpCell(ln, "sl")}</td>
<td>${slTpCell(ln, "tp")}</td>
<td>${floatPnlCell(ln)}</td>
${showPnl ? `<td>${floatPnlCell(ln)}</td>` : ""}
</tr>`;
})
.join("");
@@ -222,7 +235,9 @@
<div class="dash-table-wrap">
<table class="dash-table dash-pos-table">
<thead><tr>
<th>交易所</th><th>类型</th><th>合约</th><th>方向</th><th>开仓价</th><th>标记价</th><th>张数</th><th>止损</th><th>止盈</th><th>浮盈</th>
<th>交易所</th><th>类型</th><th>合约</th><th>方向</th><th>开仓价</th><th>标记价</th><th>张数</th><th>止损</th><th>止盈</th>${
showPnl ? "<th>浮盈</th>" : ""
}
</tr></thead>
<tbody>${body}</tbody>
</table>
@@ -291,7 +306,7 @@
return null;
}
function renderOptionsLegRow(ac, p) {
function renderOptionsLegRow(ac, p, showPnl) {
const optType =
(p.opt_type || "").toUpperCase() === "C"
? "Call"
@@ -303,21 +318,25 @@
const targetCls = target && target !== "—" ? "dash-target-monitor is-on" : "dash-target-monitor";
const net = optionsNetPnl(p);
const roi = optionsRoiPct(p);
return `<tr>
let html = `<tr>
<td>${exchangeLinkCell(ac)}</td>
<td>${sourceTypeCell(source)}</td>
<td title="${esc(p.inst_id || "")}">${esc(shortDashInst(p.inst_id))}</td>
<td>${esc(optType)}</td>
<td>${dashOptionsExpiryCd(p.exp_time_ms != null ? p.exp_time_ms : p.exp_time)}</td>
<td>${p.idx_px != null ? fmt(p.idx_px, 0) : "—"}</td>
<td><span class="${targetCls}">${esc(target)}</span></td>
<td class="${pnlClass(net)}">${net != null ? pnlSigned(net, 2) : "—"}</td>
<td class="${pnlClass(roi)}">${roi != null ? esc(Number(roi).toFixed(2)) + "%" : "—"}</td>
</tr>`;
<td><span class="${targetCls}">${esc(target)}</span></td>`;
if (showPnl) {
html += `<td class="${pnlClass(net)}">${net != null ? pnlSigned(net, 2) : "—"}</td>
<td class="${pnlClass(roi)}">${roi != null ? esc(Number(roi).toFixed(2)) + "%" : "—"}</td>`;
}
html += "</tr>";
return html;
}
function renderUnifiedOptionsTable(rows) {
if (!rows.length) return "";
const showPnl = showAccountPnlPref();
// 同所同计划相邻,Call 在前 Put 在后;不额外画分组框
const sorted = rows.slice().sort((a, b) => {
const ka = optionsGroupKey(a.ac, a.p);
@@ -330,14 +349,16 @@
if (tb === "C") return 1;
return ta.localeCompare(tb);
});
const body = sorted.map(({ ac, p }) => renderOptionsLegRow(ac, p)).join("");
const body = sorted.map(({ ac, p }) => renderOptionsLegRow(ac, p, showPnl)).join("");
return `<div class="dash-pos-block dash-options-block">
<div class="dash-ac-section-label">期权持仓</div>
<div class="dash-table-wrap dash-options-table-wrap">
<table class="dash-table dash-options-table">
<thead><tr>
<th>交易所</th><th>类型</th><th>合约</th><th>Call/Put</th><th>到期倒计时</th><th>指数</th><th>目标监控</th><th>净盈亏</th><th>收益率</th>
<th>交易所</th><th>类型</th><th>合约</th><th>Call/Put</th><th>到期倒计时</th><th>指数</th><th>目标监控</th>${
showPnl ? "<th>净盈亏</th><th>收益率</th>" : ""
}
</tr></thead>
<tbody>${body}</tbody>
</table>
@@ -539,5 +560,9 @@
inited = false;
stopLive();
},
refresh() {
if (!inited) return;
void fetchDashboardSnapshot({ silent: true, force: true });
},
};
})();