@forgecharts/sdk 1.1.23

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.
Files changed (101) hide show
  1. package/package.json +50 -0
  2. package/src/__tests__/backwardCompatibility.test.ts +191 -0
  3. package/src/__tests__/candleInvariant.test.ts +500 -0
  4. package/src/__tests__/public-api-surface.ts +76 -0
  5. package/src/__tests__/timeframeBoundary.test.ts +583 -0
  6. package/src/api/DrawingManager.ts +188 -0
  7. package/src/api/EventBus.ts +53 -0
  8. package/src/api/IndicatorDAG.ts +389 -0
  9. package/src/api/IndicatorRegistry.ts +47 -0
  10. package/src/api/LayoutManager.ts +72 -0
  11. package/src/api/PaneManager.ts +129 -0
  12. package/src/api/ReferenceAPI.ts +195 -0
  13. package/src/api/TChart.ts +881 -0
  14. package/src/api/createChart.ts +43 -0
  15. package/src/api/drawing tools/fib gann menu/fibRetracement.ts +27 -0
  16. package/src/api/drawing tools/lines menu/crossLine.ts +21 -0
  17. package/src/api/drawing tools/lines menu/disjointChannel.ts +74 -0
  18. package/src/api/drawing tools/lines menu/extendedLine.ts +22 -0
  19. package/src/api/drawing tools/lines menu/flatTopBottom.ts +45 -0
  20. package/src/api/drawing tools/lines menu/horizontal.ts +24 -0
  21. package/src/api/drawing tools/lines menu/horizontalRay.ts +25 -0
  22. package/src/api/drawing tools/lines menu/infoLine.ts +127 -0
  23. package/src/api/drawing tools/lines menu/insidePitchfork.ts +21 -0
  24. package/src/api/drawing tools/lines menu/modifiedSchiffPitchfork.ts +18 -0
  25. package/src/api/drawing tools/lines menu/parallelChannel.ts +47 -0
  26. package/src/api/drawing tools/lines menu/pitchfork.ts +15 -0
  27. package/src/api/drawing tools/lines menu/ray.ts +28 -0
  28. package/src/api/drawing tools/lines menu/regressionTrend.ts +157 -0
  29. package/src/api/drawing tools/lines menu/schiffPitchfork.ts +18 -0
  30. package/src/api/drawing tools/lines menu/trendAngle.ts +64 -0
  31. package/src/api/drawing tools/lines menu/trendline.ts +16 -0
  32. package/src/api/drawing tools/lines menu/vertical.ts +16 -0
  33. package/src/api/drawing tools/pointers menu/crosshair.ts +17 -0
  34. package/src/api/drawing tools/pointers menu/cursor.ts +16 -0
  35. package/src/api/drawing tools/pointers menu/demonstration.ts +35 -0
  36. package/src/api/drawing tools/pointers menu/dot.ts +26 -0
  37. package/src/api/drawing tools/shapes menu/rectangle.ts +24 -0
  38. package/src/api/drawing tools/shapes menu/text.ts +30 -0
  39. package/src/api/drawingUtils.ts +82 -0
  40. package/src/core/CanvasLayer.ts +77 -0
  41. package/src/core/Chart.ts +917 -0
  42. package/src/core/CoordTransform.ts +282 -0
  43. package/src/core/Crosshair.ts +207 -0
  44. package/src/core/IndicatorEngine.ts +216 -0
  45. package/src/core/InteractionManager.ts +899 -0
  46. package/src/core/PriceScale.ts +133 -0
  47. package/src/core/Series.ts +132 -0
  48. package/src/core/TimeScale.ts +175 -0
  49. package/src/datafeed/DatafeedConnector.ts +300 -0
  50. package/src/engine/CandleEngine.ts +458 -0
  51. package/src/engine/__tests__/CandleEngine.test.ts +402 -0
  52. package/src/engine/candleInvariants.ts +172 -0
  53. package/src/engine/mergeUtils.ts +93 -0
  54. package/src/engine/timeframeUtils.ts +118 -0
  55. package/src/index.ts +190 -0
  56. package/src/internal.ts +41 -0
  57. package/src/licensing/ChartRuntimeResolver.ts +380 -0
  58. package/src/licensing/LicenseManager.ts +131 -0
  59. package/src/licensing/__tests__/ChartRuntimeResolver.test.ts +207 -0
  60. package/src/licensing/__tests__/LicenseManager.test.ts +180 -0
  61. package/src/licensing/licenseTypes.ts +19 -0
  62. package/src/pine/PineCompiler.ts +68 -0
  63. package/src/pine/diagnostics.ts +30 -0
  64. package/src/pine/index.ts +7 -0
  65. package/src/pine/pine-ast.ts +163 -0
  66. package/src/pine/pine-lexer.ts +265 -0
  67. package/src/pine/pine-parser.ts +439 -0
  68. package/src/pine/pine-transpiler.ts +301 -0
  69. package/src/pixi/LayerName.ts +35 -0
  70. package/src/pixi/PixiCandlestickRenderer.ts +125 -0
  71. package/src/pixi/PixiChart.ts +425 -0
  72. package/src/pixi/PixiCrosshairRenderer.ts +134 -0
  73. package/src/pixi/PixiDrawingRenderer.ts +121 -0
  74. package/src/pixi/PixiGridRenderer.ts +136 -0
  75. package/src/pixi/PixiLayerManager.ts +102 -0
  76. package/src/renderers/CandlestickRenderer.ts +130 -0
  77. package/src/renderers/HistogramRenderer.ts +63 -0
  78. package/src/renderers/LineRenderer.ts +77 -0
  79. package/src/theme/colors.ts +21 -0
  80. package/src/tools/barDivergenceCheck.ts +305 -0
  81. package/src/trading/TradingOverlayStore.ts +161 -0
  82. package/src/trading/UnmanagedIngestion.ts +156 -0
  83. package/src/trading/__tests__/ManagedTradingController.test.ts +338 -0
  84. package/src/trading/__tests__/TradingOverlayStore.test.ts +323 -0
  85. package/src/trading/__tests__/UnmanagedIngestion.test.ts +205 -0
  86. package/src/trading/managed/ManagedTradingController.ts +292 -0
  87. package/src/trading/managed/managedCapabilities.ts +98 -0
  88. package/src/trading/managed/managedTypes.ts +151 -0
  89. package/src/trading/tradingTypes.ts +135 -0
  90. package/src/tscript/TScriptIndicator.ts +54 -0
  91. package/src/tscript/ast.ts +105 -0
  92. package/src/tscript/lexer.ts +190 -0
  93. package/src/tscript/parser.ts +334 -0
  94. package/src/tscript/runtime.ts +525 -0
  95. package/src/tscript/series.ts +84 -0
  96. package/src/types/IChart.ts +56 -0
  97. package/src/types/IRenderer.ts +16 -0
  98. package/src/types/ISeries.ts +30 -0
  99. package/tsconfig.json +22 -0
  100. package/tsup.config.ts +15 -0
  101. package/vitest.config.ts +25 -0
@@ -0,0 +1,402 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import { CandleEngine } from '../CandleEngine';
3
+ import type { CandleBar, RawOHLCV, UpdateResult, GapInfo } from '../CandleEngine';
4
+ import { timeframeToMs, getBucketStart } from '../timeframeUtils';
5
+ import { getMissingBarCount, mergeBars } from '../mergeUtils';
6
+
7
+ // ─── Helpers ─────────────────────────────────────────────────────────────────
8
+
9
+ /** Build a minimal raw bar with time in seconds. */
10
+ function rawBar(
11
+ timeSec: number,
12
+ open: number,
13
+ high: number,
14
+ low: number,
15
+ close: number,
16
+ volume = 1,
17
+ isClosed = false,
18
+ ): RawOHLCV {
19
+ return { time: timeSec, open, high, low, close, volume, isClosed };
20
+ }
21
+
22
+ /** Build a raw bar with time in milliseconds. */
23
+ function rawBarMs(timeSec: number, open: number, high: number, low: number, close: number): RawOHLCV {
24
+ return { time: timeSec * 1000, open, high, low, close, volume: 1 };
25
+ }
26
+
27
+ // 1-minute boundary: 2024-07-03T10:00:00Z = 1719997200 seconds
28
+ const T0 = 1_719_997_200;
29
+ const T1 = T0 + 60;
30
+ const T2 = T0 + 120;
31
+ const T3 = T0 + 180;
32
+ const T4 = T0 + 240;
33
+
34
+ // ─── 1. Same-candle update ────────────────────────────────────────────────────
35
+
36
+ describe('same-candle update (3 ticks, same bucket)', () => {
37
+ it('produces exactly one candle with correct O/H/L/C', () => {
38
+ const engine = new CandleEngine();
39
+ engine.initialize([], '1m');
40
+
41
+ engine.applyLiveUpdate(rawBar(T0, 100, 105, 98, 103));
42
+ engine.applyLiveUpdate(rawBar(T0, 100, 108, 96, 107)); // high rises, low drops
43
+ engine.applyLiveUpdate(rawBar(T0, 100, 107, 97, 106)); // high recedes — not applied
44
+
45
+ expect(engine.getBars().length).toBe(1);
46
+
47
+ const bar = engine.getLastBar()!;
48
+ expect(bar.open).toBe(100); // open preserved from first tick
49
+ expect(bar.high).toBe(108); // max of all ticks
50
+ expect(bar.low).toBe(96); // min of all ticks
51
+ expect(bar.close).toBe(106); // close from latest tick
52
+ });
53
+
54
+ it('returns UpdateResult type "update" for same-candle ticks', () => {
55
+ const engine = new CandleEngine();
56
+ engine.initialize([], '1m');
57
+
58
+ const r1 = engine.applyLiveUpdate(rawBar(T0, 100, 100, 100, 100));
59
+ const r2 = engine.applyLiveUpdate(rawBar(T0, 100, 110, 90, 105));
60
+
61
+ expect(r1.type).toBe('append'); // first tick is an append
62
+ expect(r2.type).toBe('update');
63
+ });
64
+
65
+ it('high can never decrease within a candle', () => {
66
+ const engine = new CandleEngine();
67
+ engine.initialize([], '1m');
68
+
69
+ engine.applyLiveUpdate(rawBar(T0, 50, 120, 40, 100));
70
+ engine.applyLiveUpdate(rawBar(T0, 50, 80, 60, 90)); // high=80 < 120
71
+
72
+ expect(engine.getLastBar()!.high).toBe(120);
73
+ });
74
+
75
+ it('low can never increase within a candle', () => {
76
+ const engine = new CandleEngine();
77
+ engine.initialize([], '1m');
78
+
79
+ engine.applyLiveUpdate(rawBar(T0, 50, 120, 40, 100));
80
+ engine.applyLiveUpdate(rawBar(T0, 50, 110, 75, 105)); // low=75 > 40
81
+
82
+ expect(engine.getLastBar()!.low).toBe(40);
83
+ });
84
+ });
85
+
86
+ // ─── 2. New-candle append ─────────────────────────────────────────────────────
87
+
88
+ describe('new-candle append (time advances)', () => {
89
+ it('appends a second candle and marks first as closed', () => {
90
+ const engine = new CandleEngine();
91
+ engine.initialize([], '1m');
92
+
93
+ engine.applyLiveUpdate(rawBar(T0, 100, 105, 98, 103));
94
+ const result = engine.applyLiveUpdate(rawBar(T1, 103, 110, 101, 108));
95
+
96
+ expect(result.type).toBe('append');
97
+ expect(engine.getBars().length).toBe(2);
98
+ expect(engine.getBars()[0]!.isClosed).toBe(true);
99
+ expect(engine.getBars()[1]!.isClosed).toBe(false);
100
+ });
101
+
102
+ it('appended candle uses the new tick\'s open price', () => {
103
+ const engine = new CandleEngine();
104
+ engine.initialize([], '1m');
105
+
106
+ engine.applyLiveUpdate(rawBar(T0, 100, 105, 98, 103));
107
+ engine.applyLiveUpdate(rawBar(T1, 200, 210, 195, 205));
108
+
109
+ expect(engine.getBars()[1]!.open).toBe(200);
110
+ });
111
+ });
112
+
113
+ // ─── 3. Out-of-order discard ──────────────────────────────────────────────────
114
+
115
+ describe('out-of-order ticks', () => {
116
+ it('discards ticks older than the current live candle', () => {
117
+ const engine = new CandleEngine();
118
+ engine.initialize([], '1m');
119
+
120
+ engine.applyLiveUpdate(rawBar(T1, 103, 110, 100, 107)); // at T1
121
+ const result = engine.applyLiveUpdate(rawBar(T0, 100, 120, 95, 115)); // T0 < T1
122
+
123
+ expect(result.type).toBe('ignore');
124
+ expect(engine.getBars().length).toBe(1);
125
+ expect(engine.getLastBar()!.time).toBe(T1);
126
+ });
127
+
128
+ it('does not change any OHLCV values after discarding', () => {
129
+ const engine = new CandleEngine();
130
+ engine.initialize([], '1m');
131
+
132
+ engine.applyLiveUpdate(rawBar(T1, 103, 110, 100, 107));
133
+ const snapBefore = { ...engine.getLastBar()! };
134
+
135
+ engine.applyLiveUpdate(rawBar(T0, 999, 999, 1, 999));
136
+
137
+ expect(engine.getLastBar()!.open).toBe(snapBefore.open);
138
+ expect(engine.getLastBar()!.high).toBe(snapBefore.high);
139
+ expect(engine.getLastBar()!.low).toBe(snapBefore.low);
140
+ expect(engine.getLastBar()!.close).toBe(snapBefore.close);
141
+ });
142
+ });
143
+
144
+ // ─── 4. Gap detection ─────────────────────────────────────────────────────────
145
+
146
+ describe('gap detection', () => {
147
+ it('fires onGapDetected when a bucket is skipped', () => {
148
+ const gaps: GapInfo[] = [];
149
+
150
+ const engine = new CandleEngine({ onGapDetected: (g) => gaps.push(g) });
151
+ engine.initialize([], '1m');
152
+
153
+ engine.applyLiveUpdate(rawBar(T0, 100, 105, 98, 103));
154
+ // Jump 3 minutes — T1 and T2 are missing
155
+ engine.applyLiveUpdate(rawBar(T3, 110, 115, 108, 113));
156
+
157
+ expect(gaps.length).toBe(1);
158
+ expect(gaps[0]!.missingCount).toBe(2);
159
+ expect(gaps[0]!.fromTimeMs).toBe(T0 * 1000);
160
+ expect(gaps[0]!.toTimeMs).toBe(T3 * 1000);
161
+ });
162
+
163
+ it('returns UpdateResult type "gap" when a gap occurs', () => {
164
+ const engine = new CandleEngine();
165
+ engine.initialize([], '1m');
166
+
167
+ engine.applyLiveUpdate(rawBar(T0, 100, 105, 98, 103));
168
+ const result = engine.applyLiveUpdate(rawBar(T4, 103, 108, 101, 106));
169
+
170
+ expect(result.type).toBe('gap');
171
+ if (result.type === 'gap') {
172
+ expect(result.missingCount).toBe(3);
173
+ }
174
+ });
175
+
176
+ it('does not fire onGapDetected for consecutive candles', () => {
177
+ const gapFn = vi.fn();
178
+ const engine = new CandleEngine({ onGapDetected: gapFn });
179
+ engine.initialize([], '1m');
180
+
181
+ engine.applyLiveUpdate(rawBar(T0, 100, 105, 98, 103));
182
+ engine.applyLiveUpdate(rawBar(T1, 103, 108, 101, 106));
183
+
184
+ expect(gapFn).not.toHaveBeenCalled();
185
+ });
186
+ });
187
+
188
+ // ─── 5. Reconnect reconciliation ─────────────────────────────────────────────
189
+
190
+ describe('handleReconnect', () => {
191
+ it('deduplicates bars correctly when there is overlap', () => {
192
+ const engine = new CandleEngine();
193
+ engine.initialize([
194
+ rawBar(T0, 100, 105, 98, 103, 10, true),
195
+ rawBar(T1, 103, 108, 101, 106, 12, true),
196
+ rawBar(T2, 106, 110, 104, 109, 8, false), // live
197
+ ], '1m');
198
+
199
+ // Reconnect brings T1 and T2 with corrected values
200
+ engine.handleReconnect([
201
+ rawBar(T1, 103, 112, 99, 111, 15, true), // corrected T1
202
+ rawBar(T2, 106, 111, 103, 110, 9, false), // corrected T2 (live)
203
+ ]);
204
+
205
+ expect(engine.getBars().length).toBe(3);
206
+ expect(engine.getBars()[1]!.high).toBe(112); // T1 updated
207
+ expect(engine.getBars()[2]!.high).toBe(111); // T2 updated
208
+ });
209
+
210
+ it('fires onResync after handleReconnect', () => {
211
+ const resyncFn = vi.fn();
212
+ const engine = new CandleEngine({ onResync: resyncFn });
213
+ engine.initialize([rawBar(T0, 100, 105, 98, 103)], '1m');
214
+
215
+ engine.handleReconnect([rawBar(T0, 100, 107, 96, 104)]);
216
+
217
+ expect(resyncFn).toHaveBeenCalledOnce();
218
+ });
219
+ });
220
+
221
+ // ─── 6. ms → s timestamp conversion ─────────────────────────────────────────
222
+
223
+ describe('millisecond vs second timestamp normalisation', () => {
224
+ it('converts ms timestamps to seconds correctly', () => {
225
+ const engine = new CandleEngine();
226
+ engine.initialize([], '1m');
227
+
228
+ // T0 in ms
229
+ engine.applyLiveUpdate(rawBarMs(T0, 100, 105, 98, 103));
230
+
231
+ const bar = engine.getLastBar()!;
232
+ expect(bar.time).toBe(T0); // chart field = seconds
233
+ expect(bar.timeMs).toBe(T0 * 1000); // engine field = ms
234
+ });
235
+
236
+ it('handles both ms and s inputs in the same session without corruption', () => {
237
+ const engine = new CandleEngine();
238
+ engine.initialize([], '1m');
239
+
240
+ engine.applyLiveUpdate(rawBarMs(T0, 100, 105, 98, 103)); // ms
241
+ engine.applyLiveUpdate(rawBar(T0, 100, 110, 95, 108)); // seconds, same bucket
242
+
243
+ expect(engine.getBars().length).toBe(1); // same bucket → still one candle
244
+ expect(engine.getLastBar()!.high).toBe(110);
245
+ });
246
+
247
+ it('floors ms timestamp to candle boundary', () => {
248
+ const engine = new CandleEngine();
249
+ engine.initialize([], '1m');
250
+
251
+ // T0 + 30s (mid-candle) in ms → should still map to T0 bucket
252
+ engine.applyLiveUpdate({ time: (T0 + 30) * 1000, open: 100, high: 105, low: 98, close: 103 });
253
+
254
+ expect(engine.getLastBar()!.time).toBe(T0);
255
+ });
256
+ });
257
+
258
+ // ─── 7. Custom timeframe aggregation (timeframeUtils) ────────────────────────
259
+
260
+ describe('timeframeUtils helpers', () => {
261
+ it('timeframeToMs returns correct durations', () => {
262
+ expect(timeframeToMs('1m')).toBe(60_000);
263
+ expect(timeframeToMs('5m')).toBe(300_000);
264
+ expect(timeframeToMs('1h')).toBe(3_600_000);
265
+ expect(timeframeToMs('4h')).toBe(14_400_000);
266
+ expect(timeframeToMs('1d')).toBe(86_400_000);
267
+ // 1w and 1M are calendar timeframes — no fixed ms duration.
268
+ // timeframeUtils returns the 60_000 compat fallback so CandleEngine
269
+ // always receives a finite number (calendar alignment is handled separately).
270
+ expect(timeframeToMs('1w')).toBe(60_000);
271
+ });
272
+
273
+ it('timeframeToMs falls back to 60_000 for unknown labels', () => {
274
+ // '3h' and '8h' are orphaned (excluded from canonical registry) → 60_000 fallback.
275
+ // '2m' is now registered (120_000) — use genuinely unknown keys here.
276
+ expect(timeframeToMs('3h')).toBe(60_000);
277
+ expect(timeframeToMs('')).toBe(60_000);
278
+ });
279
+
280
+ it('getBucketStart floors to candle boundary', () => {
281
+ expect(getBucketStart(62_000, 60_000)).toBe(60_000); // 1m01s → 1m00s
282
+ expect(getBucketStart(3_720_000, 3_600_000)).toBe(3_600_000); // 1h02m → 1h00m
283
+ expect(getBucketStart(0, 60_000)).toBe(0);
284
+ });
285
+
286
+ it('5m candle engine buckets a mid-bar tick to the 5m open', () => {
287
+ const engine = new CandleEngine();
288
+ engine.initialize([], '5m');
289
+
290
+ const T5m = getBucketStart(T0 * 1000, timeframeToMs('5m')); // floor T0 to 5m
291
+ const mid = T5m / 1000 + 120; // 2 minutes into the 5m bucket
292
+
293
+ engine.applyLiveUpdate({ time: mid, open: 50, high: 55, low: 48, close: 52 });
294
+
295
+ expect(engine.getLastBar()!.timeMs).toBe(T5m);
296
+ });
297
+ });
298
+
299
+ // ─── 8. mergeUtils helpers ────────────────────────────────────────────────────
300
+
301
+ describe('mergeUtils helpers', () => {
302
+ const makeBar = (timeSec: number, close: number, isClosed: boolean): CandleBar => ({
303
+ timeMs: timeSec * 1000,
304
+ time: timeSec,
305
+ open: 100, high: 110, low: 90, close,
306
+ volume: 1, isClosed,
307
+ });
308
+
309
+ it('getMissingBarCount returns 0 for consecutive bars', () => {
310
+ expect(getMissingBarCount(T0 * 1000, T1 * 1000, 60_000)).toBe(0);
311
+ });
312
+
313
+ it('getMissingBarCount returns correct count for gap', () => {
314
+ expect(getMissingBarCount(T0 * 1000, T3 * 1000, 60_000)).toBe(2); // T1 and T2
315
+ expect(getMissingBarCount(T0 * 1000, T4 * 1000, 60_000)).toBe(3); // T1, T2, T3
316
+ });
317
+
318
+ it('mergeBars deduplicates by timeMs with incoming winning', () => {
319
+ const existing = [makeBar(T0, 103, true), makeBar(T1, 106, true)];
320
+ const incoming = [makeBar(T1, 999, true), makeBar(T2, 110, false)]; // T1 override
321
+
322
+ const merged = mergeBars(existing, incoming);
323
+
324
+ expect(merged.length).toBe(3);
325
+ expect(merged[1]!.close).toBe(999); // incoming wins for closed bar
326
+ expect(merged[2]!.time).toBe(T2);
327
+ });
328
+
329
+ it('mergeBars preserves existing live candle when incoming is also live and same time', () => {
330
+ const existing = [makeBar(T0, 103, false)]; // live, close=103
331
+ const incoming = [makeBar(T0, 107, false)]; // fresher live tick, close=107
332
+
333
+ const merged = mergeBars(existing, incoming);
334
+
335
+ // Live bar: incoming wins because it is the freshest tick
336
+ expect(merged[0]!.close).toBe(107);
337
+ });
338
+
339
+ it('mergeBars sorts output by timeMs', () => {
340
+ const bars = [makeBar(T2, 100, true), makeBar(T0, 100, true), makeBar(T1, 100, true)];
341
+ const sorted = mergeBars(bars, []);
342
+ expect(sorted.map(b => b.time)).toEqual([T0, T1, T2]);
343
+ });
344
+ });
345
+
346
+ // ─── 9. backfillGap ───────────────────────────────────────────────────────────
347
+
348
+ describe('backfillGap', () => {
349
+ it('fills missing buckets and fires onResync', () => {
350
+ const resyncFn = vi.fn();
351
+
352
+ const engine = new CandleEngine({ onResync: resyncFn });
353
+ engine.initialize([
354
+ rawBar(T0, 100, 105, 98, 103),
355
+ rawBar(T3, 110, 115, 108, 113), // gap: T1, T2 missing
356
+ ], '1m');
357
+
358
+ engine.backfillGap([
359
+ rawBar(T1, 103, 108, 101, 106),
360
+ rawBar(T2, 106, 110, 104, 109),
361
+ ]);
362
+
363
+ expect(engine.getBars().length).toBe(4);
364
+ expect(resyncFn).toHaveBeenCalledOnce();
365
+
366
+ const times = engine.getBars().map(b => b.time);
367
+ expect(times).toEqual([T0, T1, T2, T3]);
368
+ });
369
+ });
370
+
371
+ // ─── 10. CandleEngine lifecycle ──────────────────────────────────────────────
372
+
373
+ describe('lifecycle', () => {
374
+ it('reset() clears all bars', () => {
375
+ const engine = new CandleEngine();
376
+ engine.initialize([rawBar(T0, 100, 105, 98, 103)], '1m');
377
+
378
+ engine.reset();
379
+
380
+ expect(engine.getBars().length).toBe(0);
381
+ expect(engine.getLastBar()).toBeNull();
382
+ });
383
+
384
+ it('handles initialize() with empty history', () => {
385
+ const engine = new CandleEngine();
386
+ engine.initialize([], '1m');
387
+
388
+ expect(engine.getBars().length).toBe(0);
389
+ expect(engine.getLastBar()).toBeNull();
390
+ });
391
+
392
+ it('handles duplicate historical bars (keeps last per timeMs)', () => {
393
+ const engine = new CandleEngine();
394
+ engine.initialize([
395
+ rawBar(T0, 100, 105, 98, 103),
396
+ rawBar(T0, 100, 110, 96, 108), // same time, last should win
397
+ ], '1m');
398
+
399
+ expect(engine.getBars().length).toBe(1);
400
+ expect(engine.getLastBar()!.high).toBe(110);
401
+ });
402
+ });
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Candle Invariant Guards
3
+ *
4
+ * Defensive assertions for the CandleEngine update pipeline.
5
+ * All guards are no-ops in production (`NODE_ENV === 'production'`).
6
+ *
7
+ * Design principles:
8
+ * - Each guard checks exactly ONE invariant and logs a descriptive warning.
9
+ * - Guards never throw by default — they warn so streaming data is never blocked.
10
+ * - Call sites pass a `context` string that identifies where the check ran.
11
+ * - A test harness can replace `_reporter` to assert on violations.
12
+ *
13
+ * Usage:
14
+ * import { assertOhlcRelationships, assertNotFinalized } from './candleInvariants';
15
+ */
16
+
17
+ import type { CandleBar } from './CandleEngine';
18
+ import { getCalendarBucketStart } from './timeframeUtils';
19
+
20
+ // ─── Reporter (swappable in tests) ───────────────────────────────────────────
21
+
22
+ /** Called for every invariant violation. Replaced in unit tests. */
23
+ export let _reporter: (message: string) => void = (msg) => console.warn(msg);
24
+
25
+ /** Replace the violation reporter (for unit-test assertions). */
26
+ export function _setReporter(fn: (message: string) => void): void {
27
+ _reporter = fn;
28
+ }
29
+
30
+ /** Re-install the default console.warn reporter. */
31
+ export function _resetReporter(): void {
32
+ _reporter = (msg) => console.warn(msg);
33
+ }
34
+
35
+ // ─── Dev guard ───────────────────────────────────────────────────────────────
36
+
37
+ function isDevMode(): boolean {
38
+ // Access process via globalThis to avoid relying on @types/node in browser builds
39
+ const nodeEnv = (globalThis as { process?: { env?: { NODE_ENV?: string } } })
40
+ .process?.env?.['NODE_ENV'];
41
+ return nodeEnv !== 'production';
42
+ }
43
+
44
+ function violation(context: string, message: string): void {
45
+ _reporter(`[CANDLE_INVARIANT] ${context}: ${message}`);
46
+ }
47
+
48
+ // ─── Guards ───────────────────────────────────────────────────────────────────
49
+
50
+ /**
51
+ * Verify that `high >= open, close, low` and `low <= open, close`.
52
+ *
53
+ * Floats from external feeds can occasionally violate these due to rounding.
54
+ * This guard surfaces them immediately rather than silently corrupting the chart.
55
+ */
56
+ export function assertOhlcRelationships(bar: CandleBar, context: string): void {
57
+ if (!isDevMode()) return;
58
+
59
+ const { open, high, low, close, timeMs } = bar;
60
+
61
+ if (high < open) violation(context, `high(${high}) < open(${open}) at timeMs=${timeMs}`);
62
+ if (high < close) violation(context, `high(${high}) < close(${close}) at timeMs=${timeMs}`);
63
+ if (high < low) violation(context, `high(${high}) < low(${low}) at timeMs=${timeMs}`);
64
+ if (low > open) violation(context, `low(${low}) > open(${open}) at timeMs=${timeMs}`);
65
+ if (low > close) violation(context, `low(${low}) > close(${close}) at timeMs=${timeMs}`);
66
+ }
67
+
68
+ /**
69
+ * Verify that `bar.timeMs` is exactly equal to the candle-open boundary for the
70
+ * given timeframe. If it is not, `normalizeOhlcv` or `getCalendarBucketStart`
71
+ * has a bug.
72
+ */
73
+ export function assertBucketAlignment(bar: CandleBar, tf: string, context: string): void {
74
+ if (!isDevMode()) return;
75
+
76
+ const expected = getCalendarBucketStart(bar.timeMs, tf);
77
+ if (bar.timeMs !== expected) {
78
+ violation(
79
+ context,
80
+ `timeMs(${bar.timeMs}) ≠ bucketStart(${expected}) for tf=${tf} ` +
81
+ `(delta=${bar.timeMs - expected} ms)`,
82
+ );
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Verify that we are NOT about to mutate a candle that has already been finalised
88
+ * (`isClosed === true`). Mutating a closed candle corrupts the historical record.
89
+ *
90
+ * Triggers on: same-bucket update landing on a bar already marked closed.
91
+ */
92
+ export function assertNotFinalized(bar: CandleBar, context: string): void {
93
+ if (!isDevMode()) return;
94
+
95
+ if (bar.isClosed) {
96
+ violation(
97
+ context,
98
+ `attempted to mutate a finalized (isClosed=true) candle at timeMs=${bar.timeMs}`,
99
+ );
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Verify that a same-bucket update did NOT silently overwrite the candle's `open`.
105
+ *
106
+ * The first tick's open is authoritative; subsequent updates for the same bucket
107
+ * must preserve it. Call this AFTER the in-place mutation with the captured
108
+ * `prevOpen` value.
109
+ */
110
+ export function assertOpenImmutability(
111
+ prevOpen: number,
112
+ bar: CandleBar,
113
+ context: string,
114
+ ): void {
115
+ if (!isDevMode()) return;
116
+
117
+ if (bar.open !== prevOpen) {
118
+ violation(
119
+ context,
120
+ `open changed from ${prevOpen} → ${bar.open} on same-bucket update ` +
121
+ `at timeMs=${bar.timeMs} — first-tick open is authoritative`,
122
+ );
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Verify that a same-bucket update does NOT append a new bar instead of
128
+ * updating the existing one.
129
+ *
130
+ * Call after an in-place update with the pre- and post-update bar counts.
131
+ */
132
+ export function assertNoDuplicateAppend(
133
+ prevCount: number,
134
+ nextCount: number,
135
+ context: string,
136
+ ): void {
137
+ if (!isDevMode()) return;
138
+
139
+ if (nextCount > prevCount) {
140
+ violation(
141
+ context,
142
+ `same-bucket update incremented bar count from ${prevCount} → ${nextCount} ` +
143
+ `(should have updated in-place, not appended)`,
144
+ );
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Verify that a new-bucket append does NOT reduce the bar count or silently
150
+ * overwrite a finalized candle (would require bar count to stay the same while
151
+ * the last bar's timeMs changes, which the engine never does).
152
+ *
153
+ * Call after appending a new bar with the previous last bar for comparison.
154
+ */
155
+ export function assertFinalizedUnchanged(
156
+ prevLastBar: Readonly<CandleBar>,
157
+ currentBars: readonly CandleBar[],
158
+ context: string,
159
+ ): void {
160
+ if (!isDevMode()) return;
161
+
162
+ const prevIndex = currentBars.findIndex(b => b.timeMs === prevLastBar.timeMs);
163
+ if (prevIndex === -1) return; // bar may have been legitimately removed by merge
164
+
165
+ const preserved = currentBars[prevIndex]!;
166
+ if (!preserved.isClosed) {
167
+ violation(
168
+ context,
169
+ `bar at timeMs=${prevLastBar.timeMs} should be isClosed=true after new-bucket append`,
170
+ );
171
+ }
172
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Merge / reconciliation helpers for the candle engine.
3
+ *
4
+ * These utilities operate on plain data objects and have no side-effects,
5
+ * making them easy to test and reuse.
6
+ */
7
+
8
+ /**
9
+ * Normalised candle shape. Defined here so that mergeUtils has no dependency
10
+ * on CandleEngine (avoids circular imports). CandleEngine re-exports this type.
11
+ */
12
+ export interface CandleBar {
13
+ /** Candle open-time in milliseconds — the primary bucket key. */
14
+ timeMs: number;
15
+ /** Candle open-time in seconds — passed directly to the chart library. */
16
+ time: number;
17
+ open: number;
18
+ high: number;
19
+ low: number;
20
+ close: number;
21
+ volume: number;
22
+ /** True once the next candle has started and this bar is fully formed. */
23
+ isClosed: boolean;
24
+ }
25
+
26
+ /**
27
+ * Count the number of missing candle buckets between two consecutive bar times.
28
+ *
29
+ * Returns 0 when bars are adjacent (no gap), ≥1 when candles are missing.
30
+ *
31
+ * @example
32
+ * getMissingBarCount(60_000, 120_000, 60_000) // → 0 (consecutive 1m bars)
33
+ * getMissingBarCount(60_000, 240_000, 60_000) // → 2 (10:01 and 10:02 missing)
34
+ * getMissingBarCount(60_000, 3_660_000, 60_000) // → 59 (whole hour gap)
35
+ */
36
+ export function getMissingBarCount(
37
+ lastTimeMs: number,
38
+ nextTimeMs: number,
39
+ intervalMs: number,
40
+ ): number {
41
+ return Math.max(0, Math.floor((nextTimeMs - lastTimeMs) / intervalMs) - 1);
42
+ }
43
+
44
+ /**
45
+ * Merge two candle arrays into a single deduplicated, time-sorted array.
46
+ *
47
+ * Conflict resolution when both arrays contain a bar with the same `timeMs`:
48
+ * - Closed bars from `incoming` always override `existing` candles —
49
+ * REST-fetched data is treated as authoritative for finished candles.
50
+ * - The live (open) candle: `incoming` also wins — it is the freshest tick.
51
+ *
52
+ * This covers both gap backfill (REST data fills missing closed bars) and
53
+ * reconnect reconciliation (recent REST window corrects stale candles).
54
+ *
55
+ * @param existing Current engine bar array (read-only — not mutated).
56
+ * @param incoming New bars to merge in (read-only — not mutated).
57
+ * @returns A new sorted array with duplicates removed.
58
+ */
59
+ export function mergeBars(
60
+ existing: readonly CandleBar[],
61
+ incoming: readonly CandleBar[],
62
+ ): CandleBar[] {
63
+ const map = new Map<number, CandleBar>();
64
+
65
+ // Load existing bars first
66
+ for (const bar of existing) {
67
+ map.set(bar.timeMs, bar);
68
+ }
69
+
70
+ // Incoming bars override based on conflict resolution rules
71
+ for (const bar of incoming) {
72
+ const prev = map.get(bar.timeMs);
73
+ // Incoming wins when:
74
+ // a) no previous bar at this time (simple insert)
75
+ // b) previous bar is closed — REST data is authoritative for closed candles
76
+ // c) incoming bar is open (live) — always prefer the latest live tick
77
+ if (!prev || prev.isClosed || !bar.isClosed) {
78
+ map.set(bar.timeMs, bar);
79
+ }
80
+ }
81
+
82
+ return Array.from(map.values()).sort((a, b) => a.timeMs - b.timeMs);
83
+ }
84
+
85
+ /**
86
+ * Deduplicate a single sorted array by `timeMs`, keeping the last occurrence
87
+ * of any duplicate (latest data wins).
88
+ */
89
+ export function deduplicateBars(bars: CandleBar[]): CandleBar[] {
90
+ const map = new Map<number, CandleBar>();
91
+ for (const bar of bars) map.set(bar.timeMs, bar);
92
+ return Array.from(map.values()).sort((a, b) => a.timeMs - b.timeMs);
93
+ }