market-data-tradingview-ws 2.1.3 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client.js +209 -61
- package/lib/tradingView.js +0 -1
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -38,10 +38,15 @@ module.exports = class Client {
|
|
|
38
38
|
broken: data.broken === true,
|
|
39
39
|
limit: Number(data.limit || 1500),
|
|
40
40
|
requestedLimit: Number(data.requestedLimit || data.limit || 1500),
|
|
41
|
+
initialLimit: data.initialLimit,
|
|
42
|
+
batchSize: data.batchSize,
|
|
43
|
+
onHistoryProgress: data.onHistoryProgress,
|
|
44
|
+
historySignal: data.historySignal,
|
|
41
45
|
historyPending: data.historyPending === true,
|
|
42
46
|
historyBars: data.historyBars && typeof data.historyBars === 'object' ? data.historyBars : {},
|
|
47
|
+
historyBatch: data.historyBatch ?? null,
|
|
48
|
+
latestTimestamp: data.latestTimestamp ?? null,
|
|
43
49
|
seriesGeneration: Number(data.seriesGeneration || 0),
|
|
44
|
-
historyCompletionsPending: Number(data.historyCompletionsPending || 0),
|
|
45
50
|
seriesCreated: data.seriesCreated === true,
|
|
46
51
|
end: data.end ?? null,
|
|
47
52
|
};
|
|
@@ -51,7 +56,9 @@ module.exports = class Client {
|
|
|
51
56
|
if (!data) return null;
|
|
52
57
|
data.deleted = true;
|
|
53
58
|
data.historyPending = false;
|
|
54
|
-
data.
|
|
59
|
+
data.onHistoryProgress = null;
|
|
60
|
+
data.historySignal = null;
|
|
61
|
+
data.historyBatch = null;
|
|
55
62
|
data.historyBars = {};
|
|
56
63
|
data.seriesCreated = false;
|
|
57
64
|
data.end = null;
|
|
@@ -64,7 +71,9 @@ module.exports = class Client {
|
|
|
64
71
|
data.deleted = true;
|
|
65
72
|
data.broken = true;
|
|
66
73
|
data.historyPending = false;
|
|
67
|
-
data.
|
|
74
|
+
data.onHistoryProgress = null;
|
|
75
|
+
data.historySignal = null;
|
|
76
|
+
data.historyBatch = null;
|
|
68
77
|
data.historyBars = {};
|
|
69
78
|
data.seriesCreated = false;
|
|
70
79
|
data.end = null;
|
|
@@ -186,8 +195,14 @@ module.exports = class Client {
|
|
|
186
195
|
#intentionalClose = false;
|
|
187
196
|
#watchdogTimer = null;
|
|
188
197
|
#lastMessageAt = 0;
|
|
198
|
+
#chartInitialLimitMax = 1500;
|
|
189
199
|
|
|
190
200
|
constructor(config = {}) {
|
|
201
|
+
const initialLimitMax = Number(process.env.chart_initial_limit_max ?? 1500);
|
|
202
|
+
if (!Number.isSafeInteger(initialLimitMax) || initialLimitMax <= 0 || initialLimitMax > 40000) {
|
|
203
|
+
throw new RangeError('chart_initial_limit_max must be a positive safe integer no greater than 40000');
|
|
204
|
+
}
|
|
205
|
+
this.#chartInitialLimitMax = initialLimitMax;
|
|
191
206
|
const cfg = config && typeof config === 'object' ? config : {};
|
|
192
207
|
this.#configuredMaxChartSessions = utils.positive(cfg.max, this.#configuredMaxChartSessions);
|
|
193
208
|
this.#maxChartSessions = this.#configuredMaxChartSessions;
|
|
@@ -214,6 +229,7 @@ module.exports = class Client {
|
|
|
214
229
|
}
|
|
215
230
|
|
|
216
231
|
#resetRemoteState() {
|
|
232
|
+
for (const session of Object.values(this.#charts.values)) this.#closeHistoryBatch(session, 'upstream_error');
|
|
217
233
|
this.#charts.values = {};
|
|
218
234
|
this.#quotes.values = {};
|
|
219
235
|
this.#symbols.values = {};
|
|
@@ -250,12 +266,13 @@ module.exports = class Client {
|
|
|
250
266
|
return { ok: true, symbol: normalizedSymbol, period: normalizedPeriod };
|
|
251
267
|
}
|
|
252
268
|
|
|
253
|
-
#validateChartRequest({ symbol, period, limit }) {
|
|
269
|
+
#validateChartRequest({ symbol, period, limit, maxLimit = this.#chartInitialLimitMax }) {
|
|
254
270
|
const identity = this.#validateChartIdentity({ symbol, period });
|
|
255
271
|
if (!identity.ok) return identity;
|
|
256
272
|
|
|
273
|
+
if (typeof limit !== 'number' && typeof limit !== 'string') return { ok: false, reason: 'invalid_limit' };
|
|
257
274
|
const normalizedLimit = Number(limit);
|
|
258
|
-
if (!Number.
|
|
275
|
+
if (!Number.isSafeInteger(normalizedLimit) || normalizedLimit <= 0 || normalizedLimit > maxLimit) return { ok: false, reason: 'invalid_limit' };
|
|
259
276
|
|
|
260
277
|
return { ...identity, limit: normalizedLimit };
|
|
261
278
|
}
|
|
@@ -301,7 +318,13 @@ module.exports = class Client {
|
|
|
301
318
|
session.historyBars = {};
|
|
302
319
|
}
|
|
303
320
|
|
|
321
|
+
const batch = session.historyPending ? session.historyBatch : null;
|
|
322
|
+
if (batch) batch.received += chart.length;
|
|
304
323
|
for (const bar of chart) {
|
|
324
|
+
if (batch && !Object.hasOwn(session.historyBars, bar.timestamp)) {
|
|
325
|
+
batch.newUnique++;
|
|
326
|
+
if (batch.before !== null && bar.timestamp < batch.before) batch.earlier = true;
|
|
327
|
+
}
|
|
305
328
|
session.historyBars[String(bar.timestamp)] = bar;
|
|
306
329
|
}
|
|
307
330
|
}
|
|
@@ -311,6 +334,75 @@ module.exports = class Client {
|
|
|
311
334
|
return Object.values(session.historyBars || {}).sort((left, right) => left.timestamp - right.timestamp).slice(-requestedLimit);
|
|
312
335
|
}
|
|
313
336
|
|
|
337
|
+
#beginHistoryBatch(session, requested, before = null) {
|
|
338
|
+
const batch = { requested, before, startedAt: Date.now(), received: 0, newUnique: 0, earlier: false };
|
|
339
|
+
session.historyBatch = batch;
|
|
340
|
+
session.historyPending = true;
|
|
341
|
+
return batch;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
#logHistoryBatch(session, batch, chart, stopReason) {
|
|
345
|
+
console.debug('tv chart history batch:', {
|
|
346
|
+
symbol: session.symbol,
|
|
347
|
+
period: session.period,
|
|
348
|
+
requested: batch.requested,
|
|
349
|
+
received: batch.received,
|
|
350
|
+
newUnique: batch.newUnique,
|
|
351
|
+
accumulated: chart.length,
|
|
352
|
+
earliestTimestamp: chart[0]?.timestamp ?? null,
|
|
353
|
+
durationMs: Date.now() - batch.startedAt,
|
|
354
|
+
stopReason,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
#closeHistoryBatch(session, reason) {
|
|
359
|
+
const batch = session.historyBatch;
|
|
360
|
+
if (!batch) return;
|
|
361
|
+
session.historyBatch = null;
|
|
362
|
+
batch.cleanup?.();
|
|
363
|
+
if (session.historyPending) {
|
|
364
|
+
this.#logHistoryBatch(session, batch, this.#getChartHistory({ session }), session.historySignal?.aborted ? session.historySignal.reason : reason);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#failHistorySend(session, chart) {
|
|
369
|
+
session.historyPending = false;
|
|
370
|
+
const progress = session.onHistoryProgress;
|
|
371
|
+
session.onHistoryProgress = null;
|
|
372
|
+
session.historySignal = null;
|
|
373
|
+
session.requestedLimit = session.limit = chart.length;
|
|
374
|
+
progress?.(chart, 'send_failed');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
#requestMoreHistory({ session, chart, limit, batchSize }) {
|
|
378
|
+
const add = Math.min(limit - chart.length, batchSize);
|
|
379
|
+
const batch = this.#beginHistoryBatch(session, add, chart[0]?.timestamp ?? Infinity);
|
|
380
|
+
const signal = session.historySignal;
|
|
381
|
+
const abort = () => {
|
|
382
|
+
this.#logHistoryBatch(session, batch, this.#getChartHistory({ session }), signal.reason);
|
|
383
|
+
// Keep the wire request until series_completed, even after its waiter has left.
|
|
384
|
+
session.historyPending = false;
|
|
385
|
+
session.requestedLimit = session.limit = chart.length;
|
|
386
|
+
session.onHistoryProgress = null;
|
|
387
|
+
session.historySignal = null;
|
|
388
|
+
};
|
|
389
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
390
|
+
batch.cleanup = () => signal?.removeEventListener('abort', abort);
|
|
391
|
+
const sent = this.#send(tv.moreData({ chartSession: session.id, frame: session.frame, add }), () => {
|
|
392
|
+
if (session.historyBatch !== batch || this.#charts.get({ key: session.id }) !== session) return;
|
|
393
|
+
this.#closeHistoryBatch(session, 'send_failed');
|
|
394
|
+
if (session.historyPending || session.onHistoryProgress) this.#failHistorySend(session, chart);
|
|
395
|
+
});
|
|
396
|
+
if (!sent) {
|
|
397
|
+
if (session.historyBatch === batch) {
|
|
398
|
+
this.#closeHistoryBatch(session, 'send_failed');
|
|
399
|
+
session.historyPending = false;
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
|
|
314
406
|
#scheduleQueue(delay = 0) {
|
|
315
407
|
if (this.#chartQueuePaused || !this.#isOpen() || this.#queueTimer) return;
|
|
316
408
|
this.#queueTimer = setTimeout(() => {
|
|
@@ -330,7 +422,7 @@ module.exports = class Client {
|
|
|
330
422
|
return this.#ws?.readyState === WebSocket.OPEN;
|
|
331
423
|
}
|
|
332
424
|
|
|
333
|
-
#send(data) {
|
|
425
|
+
#send(data, onError) {
|
|
334
426
|
const socket = this.#ws;
|
|
335
427
|
|
|
336
428
|
if (!socket || socket.readyState !== WebSocket.OPEN) return false;
|
|
@@ -342,6 +434,7 @@ module.exports = class Client {
|
|
|
342
434
|
|
|
343
435
|
socket.send(data, (error) => {
|
|
344
436
|
if (!error) return;
|
|
437
|
+
onError?.();
|
|
345
438
|
this.#emit('error', { type: 'ws_send_error', message: error.message });
|
|
346
439
|
});
|
|
347
440
|
|
|
@@ -362,12 +455,17 @@ module.exports = class Client {
|
|
|
362
455
|
});
|
|
363
456
|
}
|
|
364
457
|
|
|
365
|
-
#enqueueChart({ symbol, period, limit }) {
|
|
458
|
+
#enqueueChart({ symbol, period, limit, batchSize, onHistoryProgress, historySignal }) {
|
|
366
459
|
const queueKey = this.#chartQueueKey({ symbol, period });
|
|
367
460
|
const existing = this.#chartQueue.find((item) => item.queueKey === queueKey);
|
|
368
461
|
|
|
369
462
|
if (existing) {
|
|
370
|
-
|
|
463
|
+
if (limit >= existing.limit) {
|
|
464
|
+
existing.limit = limit;
|
|
465
|
+
existing.batchSize = batchSize;
|
|
466
|
+
existing.onHistoryProgress = onHistoryProgress;
|
|
467
|
+
existing.historySignal = historySignal;
|
|
468
|
+
}
|
|
371
469
|
existing.expiresAt = Date.now() + this.#chartQueueTtlMs;
|
|
372
470
|
return 'queued';
|
|
373
471
|
}
|
|
@@ -379,7 +477,7 @@ module.exports = class Client {
|
|
|
379
477
|
}
|
|
380
478
|
|
|
381
479
|
this.#queuedChartKeys.add(queueKey);
|
|
382
|
-
this.#chartQueue.push({ symbol, period, limit, queueKey, expiresAt: Date.now() + this.#chartQueueTtlMs });
|
|
480
|
+
this.#chartQueue.push({ symbol, period, limit, batchSize, onHistoryProgress, historySignal, queueKey, expiresAt: Date.now() + this.#chartQueueTtlMs });
|
|
383
481
|
this.#scheduleQueue();
|
|
384
482
|
|
|
385
483
|
return 'queued';
|
|
@@ -444,7 +542,7 @@ module.exports = class Client {
|
|
|
444
542
|
const existKey = this.#charts.getBySymbolPeriod({ symbol: next.symbol, period: next.period });
|
|
445
543
|
if (existKey) continue;
|
|
446
544
|
|
|
447
|
-
const status = this.#addChartSymbolNow(
|
|
545
|
+
const status = this.#addChartSymbolNow(next);
|
|
448
546
|
if (status === 'not_connected') {
|
|
449
547
|
this.#emit('error', { type: 'chart_send_failed', symbol: next.symbol, period: next.period, limit: next.limit, retryable: true });
|
|
450
548
|
return;
|
|
@@ -813,7 +911,7 @@ module.exports = class Client {
|
|
|
813
911
|
return 'delete';
|
|
814
912
|
}
|
|
815
913
|
|
|
816
|
-
#reuseDeletedChartSession({ key, symbol, period, limit }) {
|
|
914
|
+
#reuseDeletedChartSession({ key, symbol, period, limit, batchSize, onHistoryProgress, historySignal }) {
|
|
817
915
|
const data = this.#charts.get({ key });
|
|
818
916
|
if (!data) return 'unlisted';
|
|
819
917
|
const resolved = this.#resolveSymbol({ chartSession: key, symbol });
|
|
@@ -824,17 +922,22 @@ module.exports = class Client {
|
|
|
824
922
|
data.period = period;
|
|
825
923
|
data.frame = this.#nextChartFrame();
|
|
826
924
|
data.i = 's1';
|
|
827
|
-
data.historyPending = true;
|
|
828
925
|
data.requestedLimit = Number(limit);
|
|
829
926
|
data.limit = Number(limit);
|
|
927
|
+
data.initialLimit = Math.min(limit, this.#chartInitialLimitMax);
|
|
928
|
+
data.batchSize = batchSize;
|
|
929
|
+
data.onHistoryProgress = onHistoryProgress;
|
|
930
|
+
data.historySignal = historySignal;
|
|
830
931
|
data.historyBars = {};
|
|
831
|
-
data.
|
|
932
|
+
data.latestTimestamp = null;
|
|
832
933
|
data.seriesGeneration = Number(data.seriesGeneration || 0) + 1;
|
|
833
934
|
data.seriesCreated = true;
|
|
834
935
|
data.end = null;
|
|
936
|
+
this.#beginHistoryBatch(data, data.initialLimit);
|
|
835
937
|
|
|
836
|
-
const sent = this.#send(tv.createSeries({ chartSession: key, frame: data.frame, increment: data.i, symbolKey: data.symbolKey, period: periods[period], limit}));
|
|
938
|
+
const sent = this.#send(tv.createSeries({ chartSession: key, frame: data.frame, increment: data.i, symbolKey: data.symbolKey, period: periods[period], limit: data.initialLimit }));
|
|
837
939
|
if (!sent) {
|
|
940
|
+
this.#closeHistoryBatch(data, 'send_failed');
|
|
838
941
|
this.#charts.delete({ key });
|
|
839
942
|
return 'not_connected';
|
|
840
943
|
}
|
|
@@ -844,32 +947,15 @@ module.exports = class Client {
|
|
|
844
947
|
return 'add';
|
|
845
948
|
}
|
|
846
949
|
|
|
847
|
-
#addChartSymbolNow({ symbol, period, limit
|
|
950
|
+
#addChartSymbolNow({ symbol, period, limit, batchSize, onHistoryProgress, historySignal }) {
|
|
848
951
|
if (!symbol || typeof symbol !== 'string') console.debug('Invalid symbol parameter', symbol); // throw new Error('Invalid symbol parameter', symbol);
|
|
849
952
|
if (!period || typeof period !== 'number') console.debug('Invalid period parameter', period); // throw new Error('Invalid period parameter', period);
|
|
850
953
|
if (!limit || typeof limit !== 'number') console.debug('Invalid limit parameter', limit); // throw new Error('Invalid limit parameter', limit);
|
|
851
954
|
|
|
852
|
-
let key = this.#charts.getBySymbolPeriod({ symbol, period });
|
|
853
|
-
if (key) {
|
|
854
|
-
const data = this.#charts.get({ key });
|
|
855
|
-
if (data && Number(limit) > Number(data.limit || 0)) {
|
|
856
|
-
const add = Number(limit) - Number(data.limit || 0);
|
|
857
|
-
const sent = this.#send(tv.moreData({ chartSession: key, frame: data.frame, add }));
|
|
858
|
-
if (!sent) return 'not_connected';
|
|
859
|
-
data.historyPending = true;
|
|
860
|
-
data.requestedLimit = Number(limit);
|
|
861
|
-
data.limit = Number(limit);
|
|
862
|
-
this.#charts.set({ key, data });
|
|
863
|
-
console.debug('addChartSymbol extend:', key, symbol, period, { add, limit });
|
|
864
|
-
return 'extend';
|
|
865
|
-
}
|
|
866
|
-
console.debug('addChartSymbol exist:', key, symbol, period);
|
|
867
|
-
return 'exist';
|
|
868
|
-
}
|
|
869
955
|
const deleted = this.#charts.getDeleted();
|
|
870
|
-
if (deleted !== null) return this.#reuseDeletedChartSession({ key: deleted.key, symbol, period, limit });
|
|
956
|
+
if (deleted !== null) return this.#reuseDeletedChartSession({ key: deleted.key, symbol, period, limit, batchSize, onHistoryProgress, historySignal });
|
|
871
957
|
|
|
872
|
-
key = this.createChartSession();
|
|
958
|
+
const key = this.createChartSession();
|
|
873
959
|
if (!key) return 'not_connected';
|
|
874
960
|
const data = this.#charts.get({ key });
|
|
875
961
|
|
|
@@ -884,17 +970,21 @@ module.exports = class Client {
|
|
|
884
970
|
data.period = period;
|
|
885
971
|
data.frame = this.#nextChartFrame();
|
|
886
972
|
data.i = 's1';
|
|
887
|
-
data.historyPending = true;
|
|
888
973
|
data.requestedLimit = Number(limit);
|
|
889
974
|
data.limit = Number(limit);
|
|
975
|
+
data.initialLimit = Math.min(limit, this.#chartInitialLimitMax);
|
|
976
|
+
data.batchSize = batchSize;
|
|
977
|
+
data.onHistoryProgress = onHistoryProgress;
|
|
978
|
+
data.historySignal = historySignal;
|
|
890
979
|
data.historyBars = {};
|
|
891
|
-
data.historyCompletionsPending = 1;
|
|
892
980
|
data.seriesGeneration = Number(data.seriesGeneration || 0) + 1;
|
|
893
981
|
data.seriesCreated = true;
|
|
894
982
|
data.end = null;
|
|
983
|
+
this.#beginHistoryBatch(data, data.initialLimit);
|
|
895
984
|
|
|
896
|
-
const sent = this.#send(tv.createSeries({ chartSession: key, frame: data.frame, increment: data.i, symbolKey: data.symbolKey, period: periods[period], limit}));
|
|
985
|
+
const sent = this.#send(tv.createSeries({ chartSession: key, frame: data.frame, increment: data.i, symbolKey: data.symbolKey, period: periods[period], limit: data.initialLimit }));
|
|
897
986
|
if (!sent) {
|
|
987
|
+
this.#closeHistoryBatch(data, 'send_failed');
|
|
898
988
|
this.#charts.delete({ key });
|
|
899
989
|
return 'not_connected';
|
|
900
990
|
}
|
|
@@ -904,28 +994,31 @@ module.exports = class Client {
|
|
|
904
994
|
return 'add';
|
|
905
995
|
}
|
|
906
996
|
|
|
907
|
-
addChartSymbol({ symbol, period, limit = 1500 }) {
|
|
908
|
-
const validation = this.#validateChartRequest({ symbol, period, limit });
|
|
997
|
+
addChartSymbol({ symbol, period, limit = 1500, batchSize = 1500, onHistoryProgress, historySignal }) {
|
|
998
|
+
const validation = this.#validateChartRequest({ symbol, period, limit, maxLimit: Number.MAX_SAFE_INTEGER });
|
|
909
999
|
if (!validation.ok) return { status: 'invalid', reason: validation.reason };
|
|
1000
|
+
if (!Number.isSafeInteger(batchSize) || batchSize <= 0 || batchSize > 40000) return { status: 'invalid', reason: 'invalid_batch_size' };
|
|
910
1001
|
if (!this.#isOpen()) return { status: 'not_connected', symbol: validation.symbol, period: validation.period };
|
|
911
1002
|
|
|
912
1003
|
const key = this.#charts.getBySymbolPeriod({ symbol: validation.symbol, period: validation.period });
|
|
913
1004
|
if (key) {
|
|
914
1005
|
const data = this.#charts.get({ key });
|
|
915
|
-
if (data && validation.limit >
|
|
916
|
-
const
|
|
917
|
-
|
|
918
|
-
if (!
|
|
919
|
-
|
|
920
|
-
|
|
1006
|
+
if (data && (validation.limit > data.limit || (data.historySignal?.aborted && data.historyBatch))) {
|
|
1007
|
+
const chart = this.#getChartHistory({ session: data });
|
|
1008
|
+
data.historySignal = historySignal;
|
|
1009
|
+
if (!data.historyBatch && !this.#requestMoreHistory({ session: data, chart, limit: validation.limit, batchSize })) {
|
|
1010
|
+
data.historySignal = null;
|
|
1011
|
+
return { status: 'not_connected', reason: 'history_send_failed' };
|
|
1012
|
+
}
|
|
921
1013
|
data.requestedLimit = validation.limit;
|
|
922
1014
|
data.limit = validation.limit;
|
|
923
|
-
|
|
1015
|
+
data.batchSize = batchSize;
|
|
1016
|
+
data.onHistoryProgress = onHistoryProgress;
|
|
924
1017
|
return { status: 'extend', symbol: validation.symbol, period: validation.period, limit: validation.limit };
|
|
925
1018
|
}
|
|
926
1019
|
return { status: 'exist', symbol: validation.symbol, period: validation.period, limit: Number(data?.limit || 0) };
|
|
927
1020
|
}
|
|
928
|
-
const status = this.#enqueueChart({ symbol: validation.symbol, period: validation.period, limit: validation.limit });
|
|
1021
|
+
const status = this.#enqueueChart({ symbol: validation.symbol, period: validation.period, limit: validation.limit, batchSize, onHistoryProgress, historySignal });
|
|
929
1022
|
return { status, symbol: validation.symbol, period: validation.period, limit: validation.limit };
|
|
930
1023
|
}
|
|
931
1024
|
|
|
@@ -945,6 +1038,7 @@ module.exports = class Client {
|
|
|
945
1038
|
return { status: 'retry', reason: 'remove_series_not_sent' };
|
|
946
1039
|
}
|
|
947
1040
|
}
|
|
1041
|
+
this.#closeHistoryBatch(data, 'cancellation');
|
|
948
1042
|
|
|
949
1043
|
const resolved = this.#resolveSymbol({ chartSession: key, symbol: validation.symbol });
|
|
950
1044
|
if (!resolved) {
|
|
@@ -960,20 +1054,24 @@ module.exports = class Client {
|
|
|
960
1054
|
data.i = 's1';
|
|
961
1055
|
data.limit = validation.limit;
|
|
962
1056
|
data.requestedLimit = validation.limit;
|
|
1057
|
+
data.initialLimit = validation.limit;
|
|
1058
|
+
data.batchSize = null;
|
|
1059
|
+
data.onHistoryProgress = null;
|
|
1060
|
+
data.historySignal = null;
|
|
963
1061
|
data.historyBars = {};
|
|
964
|
-
data.
|
|
965
|
-
data.historyCompletionsPending = 1;
|
|
1062
|
+
data.latestTimestamp = null;
|
|
966
1063
|
data.seriesGeneration = Number(data.seriesGeneration || 0) + 1;
|
|
967
1064
|
data.seriesCreated = true;
|
|
968
1065
|
data.deleted = false;
|
|
969
1066
|
data.end = null;
|
|
1067
|
+
this.#beginHistoryBatch(data, data.initialLimit);
|
|
970
1068
|
|
|
971
1069
|
const created = this.#send(tv.createSeries({ chartSession: key, frame: data.frame, increment: data.i, symbolKey: data.symbolKey, period: periods[validation.period], limit: validation.limit }));
|
|
972
1070
|
if (!created) {
|
|
1071
|
+
this.#closeHistoryBatch(data, 'send_failed');
|
|
973
1072
|
data.deleted = true;
|
|
974
1073
|
data.seriesCreated = false;
|
|
975
1074
|
data.historyPending = false;
|
|
976
|
-
data.historyCompletionsPending = 0;
|
|
977
1075
|
data.historyBars = {};
|
|
978
1076
|
this.#charts.set({ key, data });
|
|
979
1077
|
this.#stopQueueTimer();
|
|
@@ -1000,6 +1098,7 @@ module.exports = class Client {
|
|
|
1000
1098
|
if (!remoteRemoved) return { status: 'retry', reason: 'remove_series_not_sent' };
|
|
1001
1099
|
}
|
|
1002
1100
|
|
|
1101
|
+
this.#closeHistoryBatch(data, 'cancellation');
|
|
1003
1102
|
this.#charts.delete({ key });
|
|
1004
1103
|
this.#stopQueueTimer();
|
|
1005
1104
|
this.#scheduleQueue(0);
|
|
@@ -1115,6 +1214,7 @@ module.exports = class Client {
|
|
|
1115
1214
|
const period = parseInt(session.period);
|
|
1116
1215
|
const message = detail.slice(2).map((value) => typeof value === 'string' ? value : JSON.stringify(value)).join(' ') || 'TradingView series error';
|
|
1117
1216
|
|
|
1217
|
+
this.#closeHistoryBatch(session, 'upstream_error');
|
|
1118
1218
|
this.#charts.break({ key: chartSessionKey });
|
|
1119
1219
|
this.#logQueueState('series_error_session_quarantined', { session: chartSessionKey, frame: errorFrame, symbol, period });
|
|
1120
1220
|
this.#stopQueueTimer();
|
|
@@ -1130,14 +1230,49 @@ module.exports = class Client {
|
|
|
1130
1230
|
const completedFrame = detail[1];
|
|
1131
1231
|
const session = chartSessionKey ? this.#charts.get({ key: chartSessionKey }) : null;
|
|
1132
1232
|
|
|
1133
|
-
|
|
1233
|
+
const batch = session?.historyBatch;
|
|
1234
|
+
if (!batch || session.deleted) return;
|
|
1134
1235
|
if (completedFrame && completedFrame !== session.frame) return;
|
|
1135
1236
|
|
|
1136
|
-
|
|
1137
|
-
if (session.historyCompletionsPending > 0) return;
|
|
1138
|
-
|
|
1237
|
+
const frame = session.frame;
|
|
1139
1238
|
const chart = this.#getChartHistory({ session });
|
|
1239
|
+
const abandoned = !session.historyPending;
|
|
1240
|
+
session.historyBatch = null;
|
|
1241
|
+
batch.cleanup?.();
|
|
1242
|
+
if (abandoned && (!session.onHistoryProgress || session.historySignal?.aborted)) {
|
|
1243
|
+
session.onHistoryProgress = null;
|
|
1244
|
+
session.historySignal = null;
|
|
1245
|
+
session.requestedLimit = session.limit = chart.length;
|
|
1246
|
+
return;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
// Only an earlier unique bar proves that a backfill moved further back.
|
|
1250
|
+
const progressed = chart.length > 0 && ((batch.before === null && session.batchSize !== null) || batch.earlier || abandoned);
|
|
1251
|
+
let limit = session.limit;
|
|
1252
|
+
if (progressed && chart.length < limit && typeof session.onHistoryProgress === 'function') {
|
|
1253
|
+
limit = Math.min(limit, session.onHistoryProgress(chart));
|
|
1254
|
+
}
|
|
1255
|
+
if (session.deleted || this.#charts.get({ key: chartSessionKey }) !== session || session.frame !== frame) {
|
|
1256
|
+
if (!abandoned) this.#logHistoryBatch(session, batch, chart, 'cancellation');
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
const aborted = session.historySignal?.aborted;
|
|
1260
|
+
const stopReason = aborted ? session.historySignal.reason
|
|
1261
|
+
: chart.length >= limit ? 'target_reached' : progressed ? null : 'no_progress';
|
|
1262
|
+
if (!abandoned) this.#logHistoryBatch(session, batch, chart, stopReason);
|
|
1263
|
+
if (limit < session.limit) session.requestedLimit = session.limit = Math.max(chart.length, limit);
|
|
1264
|
+
if (!stopReason) {
|
|
1265
|
+
if (this.#requestMoreHistory({ session, chart, limit, batchSize: session.batchSize })) return;
|
|
1266
|
+
if (!session.deleted && this.#charts.get({ key: chartSessionKey }) === session && session.frame === frame) this.#failHistorySend(session, chart);
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1140
1270
|
session.historyPending = false;
|
|
1271
|
+
session.onHistoryProgress = null;
|
|
1272
|
+
session.historySignal = null;
|
|
1273
|
+
if (aborted) {
|
|
1274
|
+
session.requestedLimit = session.limit = chart.length;
|
|
1275
|
+
}
|
|
1141
1276
|
session.historyBars = Object.fromEntries(chart.map((bar) => [String(bar.timestamp), bar]));
|
|
1142
1277
|
console.debug('tv chart history completed:', { symbol: session.symbol, period: session.period, count: chart.length, session: chartSessionKey, frame: session.frame, generation: session.seriesGeneration });
|
|
1143
1278
|
this.#emit('chart_history', { symbol: session.symbol, period: parseInt(session.period), chart, end: session.end ?? null });
|
|
@@ -1154,16 +1289,29 @@ module.exports = class Client {
|
|
|
1154
1289
|
const chart = this.#parseChartBars({ frame });
|
|
1155
1290
|
if (chart.length === 0) return;
|
|
1156
1291
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
if (session
|
|
1292
|
+
const newest = chart.reduce((latest, bar) => (bar.timestamp >= latest.timestamp ? bar : latest));
|
|
1293
|
+
const live = session.latestTimestamp !== null && newest.timestamp >= session.latestTimestamp;
|
|
1294
|
+
session.latestTimestamp = Math.max(newest.timestamp, session.latestTimestamp ?? -Infinity);
|
|
1295
|
+
const batch = session.historyBatch;
|
|
1296
|
+
const abandoned = batch && !session.historyPending;
|
|
1297
|
+
if (!batch || batch.before === null || live) session.end = frame?.lbs?.bar_close_time ?? session.end ?? null;
|
|
1298
|
+
if (!abandoned) this.#mergeChartBars({ session, chart });
|
|
1299
|
+
|
|
1300
|
+
if (batch) {
|
|
1301
|
+
if (batch.before !== null && packet.type === 'du' && live) {
|
|
1302
|
+
if (abandoned) {
|
|
1303
|
+
this.#mergeChartBars({ session, chart: [newest] });
|
|
1304
|
+
const stored = this.#getChartHistory({ session });
|
|
1305
|
+
session.historyBars = Object.fromEntries(stored.map((bar) => [String(bar.timestamp), bar]));
|
|
1306
|
+
}
|
|
1307
|
+
this.#emit('chart_update', { symbol: session.symbol, period: parseInt(session.period), chart: [newest], end: session.end });
|
|
1308
|
+
}
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1164
1311
|
|
|
1165
1312
|
const stored = this.#getChartHistory({ session });
|
|
1166
1313
|
session.historyBars = Object.fromEntries(stored.map((bar) => [String(bar.timestamp), bar]));
|
|
1314
|
+
if (chart[0] !== newest) chart.sort((left, right) => right.timestamp - left.timestamp);
|
|
1167
1315
|
this.#emit('chart_update', { symbol: session.symbol, period: parseInt(session.period), chart, end: session.end });
|
|
1168
1316
|
}
|
|
1169
1317
|
};
|
package/lib/tradingView.js
CHANGED