Stream real-time position quotes via tick-driven SSE with incremental UI updates.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-29 21:14:41 +08:00
parent 94c566fbe5
commit df79017b30
4 changed files with 148 additions and 8 deletions
+47 -3
View File
@@ -202,6 +202,44 @@
bindCancelOrderButtons(orderList);
}
function findPosCardByKey(key) {
if (!list || !key) return null;
var cards = list.querySelectorAll('.pos-card[data-pos-key]');
for (var i = 0; i < cards.length; i++) {
if (cards[i].getAttribute('data-pos-key') === key) return cards[i];
}
return null;
}
function applyPositionQuotes(data) {
if (!data || !data.quotes || !list) return;
data.quotes.forEach(function (q) {
var card = findPosCardByKey(q.key || q.position_key);
if (!card) return;
var markEl = card.querySelector('.pos-q-mark');
var pnlEl = card.querySelector('.pos-q-pnl');
var pnlWrap = card.querySelector('.pos-q-pnl-wrap');
if (markEl && q.mark_price != null) markEl.textContent = fmtNum(q.mark_price);
if (pnlEl && q.float_pnl != null) {
var pnl = q.float_pnl;
pnlEl.textContent = (pnl >= 0 ? '+' : '') + fmtNum(pnl) + ' 元';
if (pnlWrap) {
pnlWrap.classList.remove('pnl-pos', 'pnl-neg');
if (pnl > 0) pnlWrap.classList.add('pnl-pos');
else if (pnl < 0) pnlWrap.classList.add('pnl-neg');
}
}
var closeBtn = card.querySelector('[data-close]');
if (closeBtn && q.mark_price != null) {
try {
var payload = JSON.parse(decodeURIComponent(closeBtn.getAttribute('data-close')));
payload.mark_price = q.mark_price;
closeBtn.setAttribute('data-close', encodeURIComponent(JSON.stringify(payload)));
} catch (e) { /* ignore */ }
}
});
}
function applyPositionsData(data) {
if (!data) return;
var cap = document.getElementById('cap-display');
@@ -1115,8 +1153,9 @@
var feeLabel = row.fee_source === 'ctp' ? '已扣手续费(柜台)' : '已扣手续费';
var marginLabel = row.margin_source === 'ctp' ? '占用保证金(柜台)' : '占用保证金';
var openLabel = '开仓';
var rowKey = row.key || row.position_key || '';
return (
'<div class="pos-card">' +
'<div class="pos-card" data-pos-key="' + rowKey + '">' +
'<div class="pos-card-head"><div><div class="title">' + posSymbolTitleHtml(row,
' <span class="badge dir">' + dirBadge + '</span>') + '</div>' +
'<div class="text-muted pos-symbol-sub">' + posSymbolSubHtml(row) + '</div></div>' +
@@ -1125,10 +1164,10 @@
'<div class="pos-metrics">' +
'<div class="cell"><label>手数</label><div><strong>' + row.lots + ' 手</strong></div></div>' +
'<div class="cell"><label>均价</label><div>' + fmtNum(row.entry_price) + '</div></div>' +
'<div class="cell"><label>当前价格</label><div>' + (row.current_price != null ? fmtNum(row.current_price) : '--') + '</div></div>' +
'<div class="cell"><label>当前价格</label><div class="pos-q-mark">' + (row.current_price != null ? fmtNum(row.current_price) : '--') + '</div></div>' +
'<div class="cell"><label>' + marginLabel + '</label><div>' + (row.margin != null ? fmtNum(row.margin) + ' 元' : '--') + '</div></div>' +
'<div class="cell"><label>仓位占比</label><div>' + (row.position_pct != null ? fmtNum(row.position_pct) + '%' : '--') + '</div></div>' +
'<div class="cell ' + pnlClass + '"><label>浮盈亏</label><div>' + pnlText + '</div></div>' +
'<div class="cell pos-q-pnl-wrap ' + pnlClass + '"><label>浮盈亏</label><div class="pos-q-pnl">' + pnlText + '</div></div>' +
'<div class="cell"><label>' + feeLabel + '</label><div>' + (row.est_fee != null ? fmtNum(row.est_fee) + ' 元' : '--') + '</div></div>' +
'<div class="cell"><label>' + openLabel + '</label><div>' + (openT || '--') + '</div></div>' +
'<div class="cell"><label>持仓</label><div>' + (row.holding_duration || '--') + '</div></div>' +
@@ -1395,6 +1434,11 @@
applyPositionsData(JSON.parse(ev.data));
} catch (e) { /* ignore */ }
});
positionSource.addEventListener('position_quotes', function (ev) {
try {
applyPositionQuotes(JSON.parse(ev.data));
} catch (e) { /* ignore */ }
});
positionSource.onerror = function () {
if (positionSource) {
positionSource.close();