@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,300 @@
1
+ import type { IDatafeed, OHLCV, Timeframe } from '@forgecharts/types';
2
+ import type { ISeries } from '../types/ISeries';
3
+ import { CandleEngine } from '../engine/CandleEngine';
4
+ import type { CandleBar, RawOHLCV } from '../engine/CandleEngine';
5
+
6
+ /** Strip engine-only fields so the value is a plain OHLCV the chart series accepts. */
7
+ function toOHLCV(bar: CandleBar): OHLCV {
8
+ return { time: bar.time, open: bar.open, high: bar.high, low: bar.low, close: bar.close, volume: bar.volume };
9
+ }
10
+
11
+ /**
12
+ * Initial history window per timeframe — controls how much data the chart
13
+ * requests on first connect.
14
+ *
15
+ * TWO-LAYER ARCHITECTURE — these two concerns are independent:
16
+ * • CLIENT (this table): how many bars are fetched and held in memory for the
17
+ * initial view + scroll buffer. Larger = more data in the browser.
18
+ * • SERVER DB fill (bars.ts MAX_DB_SECONDS): the server always fills a full
19
+ * 12 months of 1m data (and all available daily history) into the DB cache
20
+ * in the background, regardless of the client window size. This means
21
+ * scroll-left lazy-loading hits the DB, not the upstream TV API.
22
+ *
23
+ * For sub-hour timeframes a large display window is impractical:
24
+ * 12 months of 1m = 525 600 bars — impossible to render, slow to transfer.
25
+ * For 4h and above, 12 months is a manageable bar count so we load it all
26
+ * upfront; the server will have it cached or will fill it synchronously.
27
+ */
28
+ const HISTORY_WINDOWS: Partial<Record<Timeframe, number>> = {
29
+ // Sub-hour: small display window; DB fills 12 months in background for scrollback.
30
+ '1m': 3 * 24 * 3600, // 3 days → ~4 320 bars
31
+ '3m': 7 * 24 * 3600, // 7 days → ~3 360 bars
32
+ '5m': 10 * 24 * 3600, // 10 days → ~2 880 bars
33
+ '15m': 30 * 24 * 3600, // 30 days → ~2 880 bars
34
+ '30m': 60 * 24 * 3600, // 60 days → ~2 880 bars
35
+ '1h': 90 * 24 * 3600, // 90 days → ~2 160 bars
36
+ // 4h and above: 12 months — bar counts are small enough to load in full.
37
+ '2h': 365 * 86400, // 12 months → ~4 380 bars
38
+ '4h': 365 * 86400, // 12 months → ~2 190 bars
39
+ '6h': 365 * 86400, // 12 months → ~1 460 bars
40
+ '12h': 365 * 86400, // 12 months → ~730 bars
41
+ '1d': 365 * 86400, // 12 months → ~365 bars
42
+ '3d': 365 * 86400, // 12 months → ~122 bars
43
+ '1w': 365 * 86400, // 12 months → ~52 bars
44
+ '1M': 365 * 86400, // 12 months → ~12 bars
45
+ };
46
+ const DEFAULT_HISTORY_WINDOW = 90 * 24 * 3600; // 90-day fallback (~200 bars at 1h)
47
+
48
+ /**
49
+ * How many seconds of history to fetch per lazy-load page (~500 candles).
50
+ * Used by `loadMoreHistory()` when the user pans past the oldest loaded bar.
51
+ */
52
+ const PAGE_SIZES: Partial<Record<Timeframe, number>> = {
53
+ '1m': 500 * 60,
54
+ '5m': 500 * 300,
55
+ '15m': 500 * 900,
56
+ '30m': 500 * 1800,
57
+ '1h': 500 * 3600,
58
+ '4h': 500 * 4 * 3600,
59
+ '1d': 500 * 86400,
60
+ '1w': 500 * 7 * 86400,
61
+ '1M': 500 * 30 * 86400,
62
+ };
63
+ const DEFAULT_PAGE_SIZE = 500 * 3600;
64
+
65
+ /** Monotonic counter so every subscriber UID is unique within the process. */
66
+ let _uidCounter = 0;
67
+
68
+ // ─── Public types ─────────────────────────────────────────────────────────────
69
+
70
+ /**
71
+ * Lifecycle callbacks injected by TChart so it can relay data-load state
72
+ * as EventBus events without coupling DatafeedConnector to the bus.
73
+ */
74
+ export type ConnectorCallbacks = {
75
+ onLoading(symbol: string, interval: Timeframe): void;
76
+ onLoaded(symbol: string, interval: Timeframe, count: number): void;
77
+ onError(symbol: string, interval: Timeframe, error: string): void;
78
+ /** Called on every real-time tick after the series is updated. Optional. */
79
+ onUpdate?: (bar: OHLCV) => void;
80
+ };
81
+
82
+ // ─── DatafeedConnector ────────────────────────────────────────────────────────
83
+
84
+ /**
85
+ * DatafeedConnector wires an {@link IDatafeed} to an {@link ISeries}.
86
+ *
87
+ * Responsibilities:
88
+ * - Generates stable, unique subscriber UIDs (`forgecharts-<symbol>-<tf>-<n>`).
89
+ * - Calls `getHistoricalBars` and pushes the result into `series.setData()`.
90
+ * - Calls `subscribeBars` so real-time ticks flow into `series.update()`.
91
+ * - Guards against stale async responses after a reconnect or destroy.
92
+ * - Tears down the active subscription cleanly on `disconnect()` / `destroy()`.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * const connector = new DatafeedConnector(datafeed, series, {
97
+ * onLoading: (s, tf) => console.log('loading', s, tf),
98
+ * onLoaded: (s, tf, n) => console.log('loaded', n, 'bars'),
99
+ * onError: (s, tf, e) => console.error(e),
100
+ * });
101
+ *
102
+ * connector.connect('BTCUSDT', '1h');
103
+ * // ...later, on symbol change:
104
+ * connector.reconnect('ETHUSDT', '1h');
105
+ * // ...on unmount:
106
+ * connector.destroy();
107
+ * ```
108
+ */
109
+ export class DatafeedConnector {
110
+ private readonly _datafeed: IDatafeed;
111
+ private readonly _series: ISeries;
112
+ private readonly _cb: ConnectorCallbacks;
113
+
114
+ /**
115
+ * UID of the outstanding subscription.
116
+ * `null` means nothing is subscribed / all responses for older UIDs are stale.
117
+ */
118
+ private _activeUID: string | null = null;
119
+ private _activeEngine: CandleEngine | null = null;
120
+ private _destroyed = false;
121
+
122
+ /** True while an async `loadMoreHistory()` request is in-flight. */
123
+ private _isLoadingMore = false;
124
+ /** True once the datafeed returned 0 bars for a backwards page — no point fetching older. */
125
+ private _noMoreHistory = false;
126
+
127
+ constructor(datafeed: IDatafeed, series: ISeries, callbacks: ConnectorCallbacks) {
128
+ this._datafeed = datafeed;
129
+ this._series = series;
130
+ this._cb = callbacks;
131
+ }
132
+
133
+ // ─── Public API ───────────────────────────────────────────────────────────────
134
+
135
+ /**
136
+ * Loads historical bars then subscribes for real-time updates.
137
+ * Safe to call while a previous subscription is still active — it will be
138
+ * cancelled first (same as calling `disconnect()` then `connect()`).
139
+ */
140
+ connect(symbol: string, timeframe: Timeframe): void {
141
+ if (this._destroyed) return;
142
+
143
+ // Cancel any in-flight request / active subscription before starting fresh
144
+ this._cancelActive();
145
+
146
+ const uid = `forgecharts-${symbol}-${timeframe}-${++_uidCounter}`;
147
+ this._activeUID = uid;
148
+
149
+ this._cb.onLoading(symbol, timeframe);
150
+
151
+ const to = Math.floor(Date.now() / 1000);
152
+ const from = to - (HISTORY_WINDOWS[timeframe] ?? DEFAULT_HISTORY_WINDOW);
153
+
154
+ // Build the engine for this subscription. Close over `uid` for stale-guard.
155
+ const engine = new CandleEngine({
156
+ // Every live tick mutates the engine's bar array and the chart series.
157
+ onBarUpdated: (bar) => {
158
+ if (this._activeUID !== uid) return;
159
+ const ohlcv = toOHLCV(bar);
160
+ this._series.update(ohlcv);
161
+ this._cb.onUpdate?.(ohlcv);
162
+ },
163
+ // When a gap is detected, fetch the missing range from REST and backfill.
164
+ // backfillGap() will call onResync which re-syncs the full chart series.
165
+ onGapDetected: (gapInfo) => {
166
+ if (this._activeUID !== uid) return;
167
+ const gapFrom = Math.floor(gapInfo.fromTimeMs / 1000);
168
+ const gapTo = Math.ceil(gapInfo.toTimeMs / 1000);
169
+ this._datafeed.getHistoricalBars(symbol, timeframe, gapFrom, gapTo)
170
+ .then(({ bars }) => {
171
+ if (this._activeUID !== uid) return;
172
+ engine.backfillGap(bars as RawOHLCV[]);
173
+ })
174
+ .catch(() => {
175
+ // Gap backfill errors are non-fatal — chart continues with live data
176
+ });
177
+ },
178
+ // Full re-paint after gap fill or reconnect reconciliation.
179
+ onResync: (bars) => {
180
+ if (this._activeUID !== uid) return;
181
+ this._series.setData(bars.map(toOHLCV));
182
+ },
183
+ });
184
+
185
+ this._activeEngine = engine;
186
+
187
+ this._datafeed
188
+ .getHistoricalBars(symbol, timeframe, from, to)
189
+ .then((result) => {
190
+ // Guard: another connect() / destroy() may have superseded this request
191
+ if (this._activeUID !== uid) return;
192
+
193
+ engine.initialize(result.bars as RawOHLCV[], timeframe);
194
+ this._series.setData(engine.getBars().map(toOHLCV));
195
+ this._cb.onLoaded(symbol, timeframe, engine.getBars().length);
196
+
197
+ // Double-check still active before subscribing (setData callback could
198
+ // have triggered a synchronous reconnect in pathological code)
199
+ if (this._activeUID !== uid) return;
200
+
201
+ this._datafeed.subscribeBars(symbol, timeframe, (bar: OHLCV) => {
202
+ if (this._activeUID === uid) {
203
+ engine.applyLiveUpdate(bar as RawOHLCV);
204
+ }
205
+ }, uid);
206
+ })
207
+ .catch((err: unknown) => {
208
+ if (this._activeUID !== uid) return;
209
+ const message = err instanceof Error ? err.message : String(err);
210
+ this._cb.onError(symbol, timeframe, message);
211
+ });
212
+ }
213
+
214
+ /**
215
+ * Cancels the active subscription without starting a new one.
216
+ * Historical-bar `Promise` results arriving after this call are silently
217
+ * discarded via the UID guard.
218
+ */
219
+ disconnect(): void {
220
+ this._cancelActive();
221
+ }
222
+
223
+ /**
224
+ * Convenience shorthand: `disconnect()` then `connect()`.
225
+ * Use this for symbol or timeframe changes.
226
+ */
227
+ reconnect(symbol: string, timeframe: Timeframe): void {
228
+ this.disconnect();
229
+ this.connect(symbol, timeframe);
230
+ }
231
+
232
+ /**
233
+ * Permanently deactivates the connector.
234
+ * Any subscription or in-flight request is cancelled; subsequent calls to
235
+ * `connect()` / `reconnect()` are no-ops.
236
+ */
237
+ destroy(): void {
238
+ this._cancelActive();
239
+ this._destroyed = true;
240
+ }
241
+
242
+ // ─── Private ─────────────────────────────────────────────────────────────────
243
+
244
+ /**
245
+ * Fetches a page of older history (up to ~500 candles) and prepends it to
246
+ * the loaded dataset. Safe to call on every pan event — it is internally
247
+ * debounced by the `_isLoadingMore` flag and stops permanently once the
248
+ * datafeed signals no more history (`_noMoreHistory = true`).
249
+ *
250
+ * The series is updated automatically via the engine's `onResync` callback.
251
+ */
252
+ loadMoreHistory(symbol: string, timeframe: Timeframe): void {
253
+ if (this._destroyed || this._isLoadingMore || this._noMoreHistory) return;
254
+ if (!this._activeEngine || !this._activeUID) return;
255
+
256
+ const engine = this._activeEngine;
257
+ const oldest = engine.getBars()[0];
258
+ if (!oldest) return;
259
+
260
+ const uid = this._activeUID;
261
+ const to = Math.floor(oldest.timeMs / 1000);
262
+ const page = PAGE_SIZES[timeframe] ?? DEFAULT_PAGE_SIZE;
263
+ const from = to - page;
264
+
265
+ this._isLoadingMore = true;
266
+
267
+ this._datafeed.getHistoricalBars(symbol, timeframe, from, to - 1)
268
+ .then(({ bars }) => {
269
+ if (this._activeUID !== uid) return;
270
+ if (bars.length === 0) {
271
+ this._noMoreHistory = true;
272
+ } else {
273
+ engine.prependHistory(bars as RawOHLCV[]);
274
+ }
275
+ })
276
+ .catch(() => {
277
+ // Non-fatal — user can scroll again to retry
278
+ })
279
+ .finally(() => {
280
+ if (this._activeUID === uid) this._isLoadingMore = false;
281
+ });
282
+ }
283
+
284
+ private _cancelActive(): void {
285
+ if (this._activeUID !== null) {
286
+ try {
287
+ this._datafeed.unsubscribeBars(this._activeUID);
288
+ } catch {
289
+ // Best-effort teardown — datafeed may already be closed
290
+ }
291
+ this._activeUID = null;
292
+ }
293
+ if (this._activeEngine !== null) {
294
+ this._activeEngine.reset();
295
+ this._activeEngine = null;
296
+ }
297
+ this._isLoadingMore = false;
298
+ this._noMoreHistory = false;
299
+ }
300
+ }