fix(hub): show contract-based unrealized PnL in monitor and chart
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -336,15 +336,73 @@
|
||||
return "—";
|
||||
}
|
||||
|
||||
function resolveTrendFloatingPnl(pos, trendPlan) {
|
||||
let upnl = pos && pos.unrealized_pnl;
|
||||
if (upnl != null && upnl !== "" && Number.isFinite(Number(upnl))) return Number(upnl);
|
||||
const t = trendPlan || {};
|
||||
if (t.floating_pnl != null && t.floating_pnl !== "") {
|
||||
const n = Number(t.floating_pnl);
|
||||
if (Number.isFinite(n)) return n;
|
||||
function estimateLinearSwapUpnl(side, entry, mark, contracts, contractSize) {
|
||||
const e = Number(entry);
|
||||
const m = Number(mark);
|
||||
const c = Math.abs(Number(contracts));
|
||||
let mult = Number(contractSize);
|
||||
if (!Number.isFinite(mult) || mult <= 0) mult = 1;
|
||||
if (!Number.isFinite(e) || !Number.isFinite(m) || !Number.isFinite(c) || c <= 0) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
const diff =
|
||||
(side || "long").toLowerCase() === "long" ? m - e : e - m;
|
||||
return Math.round(diff * c * mult * 100) / 100;
|
||||
}
|
||||
|
||||
/** 展示浮盈:子代理 unrealized_pnl;与 entry/mark/张数 推算偏差 >20% 时用推算值 */
|
||||
function resolvePositionUpnlUsdt(pos, trendPlan, markOverride) {
|
||||
const p = pos || {};
|
||||
const t = trendPlan || {};
|
||||
let exchange =
|
||||
p.unrealized_pnl != null && p.unrealized_pnl !== ""
|
||||
? Number(p.unrealized_pnl)
|
||||
: null;
|
||||
if (exchange != null && !Number.isFinite(exchange)) exchange = null;
|
||||
const entry =
|
||||
p.entry_price != null && p.entry_price !== ""
|
||||
? Number(p.entry_price)
|
||||
: t.trigger_price != null
|
||||
? Number(t.trigger_price)
|
||||
: null;
|
||||
let mark =
|
||||
markOverride != null && Number.isFinite(Number(markOverride))
|
||||
? Number(markOverride)
|
||||
: p.mark_price != null && p.mark_price !== ""
|
||||
? Number(p.mark_price)
|
||||
: t.floating_mark != null
|
||||
? Number(t.floating_mark)
|
||||
: t.last_mark_price != null
|
||||
? Number(t.last_mark_price)
|
||||
: null;
|
||||
const contracts = p.contracts;
|
||||
const cs =
|
||||
p.contract_size != null && p.contract_size !== ""
|
||||
? Number(p.contract_size)
|
||||
: 1;
|
||||
const computed = estimateLinearSwapUpnl(
|
||||
p.side || t.direction,
|
||||
entry,
|
||||
mark,
|
||||
contracts,
|
||||
cs
|
||||
);
|
||||
if (computed == null) {
|
||||
if (exchange != null) return exchange;
|
||||
if (t.floating_pnl != null && t.floating_pnl !== "") {
|
||||
const n = Number(t.floating_pnl);
|
||||
if (Number.isFinite(n)) return n;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (exchange == null) return computed;
|
||||
const ref = Math.max(Math.abs(computed), 1);
|
||||
if (Math.abs(exchange - computed) / ref > 0.2) return computed;
|
||||
return exchange;
|
||||
}
|
||||
|
||||
function resolveTrendFloatingPnl(pos, trendPlan, markOverride) {
|
||||
return resolvePositionUpnlUsdt(pos, trendPlan, markOverride);
|
||||
}
|
||||
|
||||
function formatFloatingPnlText(upnl, notionalUsdt) {
|
||||
@@ -1393,7 +1451,10 @@
|
||||
amount: num(o.amount),
|
||||
});
|
||||
});
|
||||
const upnl = resolveTrendFloatingPnl(pos, trendPlan);
|
||||
const entryPx = num(pos.entry_price != null ? pos.entry_price : tpsl.entry);
|
||||
const markPx = num(pos.mark_price);
|
||||
const contractSize = num(pos.contract_size);
|
||||
const upnl = resolvePositionUpnlUsdt(pos, trendPlan, markPx);
|
||||
const planMargin =
|
||||
trendPlan && trendPlan.plan_margin_capital != null
|
||||
? num(trendPlan.plan_margin_capital)
|
||||
@@ -1410,12 +1471,14 @@
|
||||
exchange_id: exchangeId || null,
|
||||
symbol: (pos.symbol || "").trim(),
|
||||
side: (pos.side || "long").toLowerCase(),
|
||||
entry: num(tpsl.entry),
|
||||
entry: entryPx,
|
||||
mark_price: markPx,
|
||||
stop_loss: num(tpsl.sl),
|
||||
take_profit: num(tpsl.tp),
|
||||
tp_monitored: !!tpsl.tp_monitored,
|
||||
is_trend: !!tpsl.is_trend,
|
||||
contracts: num(pos.contracts),
|
||||
contract_size: contractSize != null ? contractSize : 1,
|
||||
unrealized_pnl: upnl != null ? Number(upnl) : null,
|
||||
notional_usdt: num(pos.notional_usdt),
|
||||
plan_margin: planMargin,
|
||||
|
||||
Reference in New Issue
Block a user