Speed up equity/position display after CTP reconnect and auto-refresh stale sessions before market open.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
var positionsRendered = false;
|
||||
var posFastPollTimer = null;
|
||||
var posFastPollCount = 0;
|
||||
var posFastPollMax = 120;
|
||||
var lastPosRowCount = 0;
|
||||
var selectedMaxLots = null;
|
||||
var recommendMaxByProduct = {};
|
||||
@@ -259,16 +260,40 @@
|
||||
posFastPollCount = 0;
|
||||
}
|
||||
|
||||
function startPosFastPoll() {
|
||||
function startPosFastPoll(maxTicks) {
|
||||
if (typeof maxTicks === 'number' && maxTicks > 0) {
|
||||
posFastPollMax = maxTicks;
|
||||
} else {
|
||||
posFastPollMax = 120;
|
||||
}
|
||||
if (posFastPollTimer) return;
|
||||
posFastPollCount = 0;
|
||||
posFastPollTimer = setInterval(function () {
|
||||
pollPositions();
|
||||
refreshCtpAccountQuick();
|
||||
posFastPollCount += 1;
|
||||
if (posFastPollCount >= 120) stopPosFastPoll();
|
||||
if (posFastPollCount >= posFastPollMax) stopPosFastPoll();
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
function refreshCtpAccountQuick() {
|
||||
if (!ctpConnected && !ctpConnecting) return;
|
||||
fetch('/api/ctp/status')
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (d) {
|
||||
if (!d || !d.account) return;
|
||||
var cap = document.getElementById('cap-display');
|
||||
if (cap && d.account.balance != null) {
|
||||
cap.textContent = Number(d.account.balance).toFixed(2);
|
||||
}
|
||||
var avail = document.getElementById('avail-display');
|
||||
if (avail && d.account.available != null) {
|
||||
avail.textContent = Number(d.account.available).toFixed(2);
|
||||
}
|
||||
})
|
||||
.catch(function () {});
|
||||
}
|
||||
|
||||
function applyPositionsData(data) {
|
||||
if (!data) return;
|
||||
var cap = document.getElementById('cap-display');
|
||||
@@ -302,6 +327,10 @@
|
||||
}
|
||||
if (connected) {
|
||||
showCtpError('');
|
||||
if (data.sync_state === 'syncing') {
|
||||
posFastPollMax = Math.max(posFastPollMax, 240);
|
||||
if (!posFastPollTimer) startPosFastPoll(240);
|
||||
}
|
||||
} else if (!connecting && data.ctp_status && data.ctp_status.last_error) {
|
||||
showCtpError(data.ctp_status.last_error);
|
||||
if (isCtpLoginBanError(data.ctp_status.last_error)) {
|
||||
@@ -683,7 +712,9 @@
|
||||
if (st.connected) {
|
||||
syncCtpBadgeFromStatus(st);
|
||||
showCtpError('');
|
||||
refreshCtpAccountQuick();
|
||||
pollPositions();
|
||||
startPosFastPoll(180);
|
||||
return d;
|
||||
}
|
||||
if ((st.login_cooldown_sec || 0) > 0 || d.cooldown) {
|
||||
@@ -693,6 +724,7 @@
|
||||
}
|
||||
if (d.connecting || st.connecting) {
|
||||
updateCtpBadge(false, true);
|
||||
startPosFastPoll(180);
|
||||
return waitForCtpConnected(70000).then(function (ok) {
|
||||
if (!ok && d.error) showCtpError(d.error);
|
||||
else if (!ok && st.last_error) showCtpError(st.last_error);
|
||||
|
||||
Reference in New Issue
Block a user