@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,525 @@
1
+ /**
2
+ * TScript Runtime — tree-walking interpreter.
3
+ *
4
+ * Execution model
5
+ * ───────────────
6
+ * A TScript program is executed once per compiled bar in left-to-right,
7
+ * top-to-bottom statement order. The runtime keeps a persistent `Scope`
8
+ * so that `Series` variables accumulate history across bars.
9
+ *
10
+ * Built-in series (available in every script):
11
+ * open, high, low, close, volume, hl2, hlc3, ohlc4, bar_index
12
+ *
13
+ * Built-in TA functions:
14
+ * sma(src, length) — simple moving average
15
+ * ema(src, length) — exponential moving average
16
+ * wma(src, length) — weighted moving average
17
+ * rma(src, length) — Wilder's (RMA) moving average
18
+ * stdev(src, length) — rolling standard deviation
19
+ * highest(src, n) — rolling max over n bars
20
+ * lowest(src, n) — rolling min over n bars
21
+ * change(src, n?) — src - src[n] (default n=1)
22
+ * mom(src, length) — momentum = change(src, length)
23
+ * atr(length) — Average True Range
24
+ * crossover(a, b) — a crosses above b
25
+ * crossunder(a, b) — a crosses below b
26
+ * nz(x, y?) — replace NaN with y (default 0)
27
+ * na(x) — true if x is NaN
28
+ * abs(x) — Math.abs
29
+ * max(...) — Math.max over args
30
+ * min(...) — Math.min over args
31
+ * round(x) — Math.round
32
+ * floor(x) — Math.floor
33
+ * ceil(x) — Math.ceil
34
+ * sqrt(x) — Math.sqrt
35
+ * log(x) — Math.log
36
+ * pow(x, y) — Math.pow
37
+ * sign(x) — Math.sign
38
+ *
39
+ * Built-in utility functions:
40
+ * input(default, ...) — returns the default value (params resolved externally)
41
+ * plot(value, ...) — records a value for the current bar
42
+ * barssince(cond) — bars since condition was true
43
+ */
44
+
45
+ import type { OHLCV } from '@forgecharts/types';
46
+ import { Series } from './series';
47
+ import { Parser } from './parser';
48
+ import type {
49
+ Program, Stmt, Expr,
50
+ AssignStmt, ExprStmt, IndicatorDecl,
51
+ NumberLit, StringLit, BoolLit, Identifier,
52
+ IndexExpr, CallExpr, BinaryExpr, UnaryExpr,
53
+ TernaryExpr, LogicalExpr,
54
+ } from './ast';
55
+ import type { IndicatorPoint } from '../core/IndicatorEngine';
56
+
57
+ // ─── Types ────────────────────────────────────────────────────────────────────
58
+
59
+ /** A TScript value is a number, boolean, string, or Series. */
60
+ export type TValue = number | boolean | string | Series;
61
+
62
+ /** Scope maps variable names to their TScript values. */
63
+ export type Scope = Map<string, TValue>;
64
+
65
+ /** Result produced by a single script execution run. */
66
+ export type ScriptResult = {
67
+ /** Title from indicator("...") declaration. */
68
+ title: string;
69
+ /** Collected plot() calls for this run, in declaration order. */
70
+ plots: IndicatorPoint[][];
71
+ };
72
+
73
+ // ─── Scope helpers ────────────────────────────────────────────────────────────
74
+
75
+ function _num(v: TValue, ctx: string): number {
76
+ if (v instanceof Series) return v.value;
77
+ if (typeof v === 'number') return v;
78
+ if (typeof v === 'boolean') return v ? 1 : 0;
79
+ throw new TypeError(`[TScript] ${ctx}: expected number, got ${typeof v}`);
80
+ }
81
+
82
+ function _bool(v: TValue): boolean {
83
+ if (v instanceof Series) return !isNaN(v.value) && v.value !== 0;
84
+ if (typeof v === 'number') return !isNaN(v) && v !== 0;
85
+ if (typeof v === 'boolean') return v;
86
+ return Boolean(v);
87
+ }
88
+
89
+ // ─── TScriptRuntime ───────────────────────────────────────────────────────────
90
+
91
+ export class TScriptRuntime {
92
+ private readonly _program: Program;
93
+ private readonly _scope: Scope = new Map();
94
+ private readonly _plotSeries: Series[] = []; // one entry per plot() call order
95
+
96
+ // Public metadata set by indicator() declaration
97
+ title: string = 'Script';
98
+
99
+ constructor(src: string) {
100
+ this._program = new Parser(src).parse();
101
+
102
+ // Extract title from first IndicatorDecl if present
103
+ for (const stmt of this._program.stmts) {
104
+ if (stmt.kind === 'IndicatorDecl') {
105
+ this.title = stmt.title;
106
+ break;
107
+ }
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Execute one bar.
113
+ *
114
+ * Call this in chronological order for every OHLCV bar.
115
+ * The runtime updates all built-in series with the bar's OHLCV values,
116
+ * then re-executes the full program body.
117
+ *
118
+ * @returns the current values of all `plot()` series after this bar.
119
+ */
120
+ execBar(bar: OHLCV, barIndex: number): void {
121
+ // ── Push OHLCV values into built-in series ────────────────────────────────
122
+ this._pushBuiltin('open', bar.open);
123
+ this._pushBuiltin('high', bar.high);
124
+ this._pushBuiltin('low', bar.low);
125
+ this._pushBuiltin('close', bar.close);
126
+ this._pushBuiltin('volume', bar.volume);
127
+ this._pushBuiltin('hl2', (bar.high + bar.low) / 2);
128
+ this._pushBuiltin('hlc3', (bar.high + bar.low + bar.close) / 3);
129
+ this._pushBuiltin('ohlc4', (bar.open + bar.high + bar.low + bar.close) / 4);
130
+ this._pushBuiltin('bar_index', barIndex);
131
+
132
+ // ── Reset per-bar plot accumulators ───────────────────────────────────────
133
+ this._plotIdx = 0;
134
+
135
+ // ── Execute all statements (skipping IndicatorDecl) ──────────────────────
136
+ for (const stmt of this._program.stmts) {
137
+ this._execStmt(stmt);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Run the full bar history and return IndicatorPoint arrays indexed by
143
+ * plot() call order.
144
+ *
145
+ * @param bars chronologically ordered OHLCV array
146
+ */
147
+ run(bars: readonly OHLCV[]): IndicatorPoint[][] {
148
+ this.reset();
149
+ for (let i = 0; i < bars.length; i++) {
150
+ this.execBar(bars[i]!, i);
151
+ }
152
+ return this.getPlots(bars);
153
+ }
154
+
155
+ /**
156
+ * Snapshot the current plot series as IndicatorPoint arrays.
157
+ * Must be called after `run()` or after the last `execBar()`.
158
+ * @param bars the same bar array passed to run() — used for timestamps.
159
+ */
160
+ getPlots(bars: readonly OHLCV[]): IndicatorPoint[][] {
161
+ return this._plotSeries.map((series) => {
162
+ const values = series.toArray(); // oldest→newest
163
+ const offset = bars.length - values.length;
164
+ return values
165
+ .map((value, i) => ({
166
+ time: bars[offset + i]!.time,
167
+ value,
168
+ }))
169
+ .filter((p) => !isNaN(p.value));
170
+ });
171
+ }
172
+
173
+ /** Reset all persistent series state (call on symbol/timeframe change). */
174
+ reset(): void {
175
+ for (const v of this._scope.values()) {
176
+ if (v instanceof Series) v.reset();
177
+ }
178
+ for (const s of this._plotSeries) s.reset();
179
+ this._plotIdx = 0;
180
+ }
181
+
182
+ // ─── Private: statement execution ───────────────────────────────────────────
183
+
184
+ private _execStmt(stmt: Stmt): void {
185
+ switch (stmt.kind) {
186
+ case 'IndicatorDecl': break; // handled at construction
187
+ case 'AssignStmt': this._execAssign(stmt); break;
188
+ case 'ExprStmt': this._evalExpr(stmt.expr); break;
189
+ }
190
+ }
191
+
192
+ private _execAssign(stmt: AssignStmt): void {
193
+ const rhs = this._evalExpr(stmt.value);
194
+ const existing = this._scope.get(stmt.name);
195
+
196
+ if (existing instanceof Series) {
197
+ // Persist a series variable: push the new value each bar
198
+ existing.push(_num(rhs, `assignment to '${stmt.name}'`));
199
+ } else {
200
+ // First assignment: if RHS is a number, wrap in a Series for history
201
+ if (typeof rhs === 'number' || rhs instanceof Series) {
202
+ const series = new Series(1000);
203
+ series.push(rhs instanceof Series ? rhs.value : rhs);
204
+ this._scope.set(stmt.name, series);
205
+ } else {
206
+ this._scope.set(stmt.name, rhs);
207
+ }
208
+ }
209
+ }
210
+
211
+ // ─── Private: expression evaluation ─────────────────────────────────────────
212
+
213
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
214
+ private _evalExpr(expr: Expr): TValue {
215
+ switch (expr.kind) {
216
+ case 'NumberLit': return (expr as NumberLit).value;
217
+ case 'StringLit': return (expr as StringLit).value;
218
+ case 'BoolLit': return (expr as BoolLit).value;
219
+ case 'Identifier': return this._evalIdent(expr as Identifier);
220
+ case 'IndexExpr': return this._evalIndex(expr as IndexExpr);
221
+ case 'CallExpr': return this._evalCall(expr as CallExpr);
222
+ case 'BinaryExpr': return this._evalBinary(expr as BinaryExpr);
223
+ case 'UnaryExpr': return this._evalUnary(expr as UnaryExpr);
224
+ case 'TernaryExpr':return this._evalTernary(expr as TernaryExpr);
225
+ case 'LogicalExpr':return this._evalLogical(expr as LogicalExpr);
226
+ }
227
+ }
228
+
229
+ private _evalIdent(expr: Identifier): TValue {
230
+ const v = this._scope.get(expr.name);
231
+ if (v === undefined) {
232
+ throw new ReferenceError(`[TScript] Undefined variable '${expr.name}'`);
233
+ }
234
+ // Return the Series itself; callers use _num() to coerce if needed.
235
+ return v;
236
+ }
237
+
238
+ private _evalIndex(expr: IndexExpr): number {
239
+ const series = this._evalExpr(expr.series);
240
+ const idx = _num(this._evalExpr(expr.index), 'series index');
241
+ if (!(series instanceof Series)) {
242
+ throw new TypeError('[TScript] Index operator [] can only be applied to a series');
243
+ }
244
+ return series.get(Math.round(idx));
245
+ }
246
+
247
+ private _evalBinary(expr: BinaryExpr): TValue {
248
+ const l = _num(this._evalExpr(expr.left), `left of '${expr.op}'`);
249
+ const r = _num(this._evalExpr(expr.right), `right of '${expr.op}'`);
250
+ switch (expr.op) {
251
+ case '+': return l + r;
252
+ case '-': return l - r;
253
+ case '*': return l * r;
254
+ case '/': return r === 0 ? NaN : l / r;
255
+ case '%': return l % r;
256
+ case '<': return l < r;
257
+ case '>': return l > r;
258
+ case '<=': return l <= r;
259
+ case '>=': return l >= r;
260
+ case '==': return l === r;
261
+ case '!=': return l !== r;
262
+ }
263
+ }
264
+
265
+ private _evalUnary(expr: UnaryExpr): TValue {
266
+ if (expr.op === 'not') return !_bool(this._evalExpr(expr.operand));
267
+ return -_num(this._evalExpr(expr.operand), 'unary -');
268
+ }
269
+
270
+ private _evalTernary(expr: TernaryExpr): TValue {
271
+ return _bool(this._evalExpr(expr.condition))
272
+ ? this._evalExpr(expr.consequent)
273
+ : this._evalExpr(expr.alternate);
274
+ }
275
+
276
+ private _evalLogical(expr: LogicalExpr): TValue {
277
+ const l = _bool(this._evalExpr(expr.left));
278
+ if (expr.op === 'and') return l ? _bool(this._evalExpr(expr.right)) : false;
279
+ return l ? true : _bool(this._evalExpr(expr.right));
280
+ }
281
+
282
+ // ─── Built-in function dispatch ──────────────────────────────────────────────
283
+
284
+ private _plotIdx = 0;
285
+
286
+ private _evalCall(expr: CallExpr): TValue {
287
+ const fn = expr.callee;
288
+ const args = expr.args;
289
+
290
+ switch (fn) {
291
+ // ── Utility ────────────────────────────────────────────────────────────
292
+ case 'input': {
293
+ // input(default) — return default value; full param resolution is external
294
+ if (args.length === 0) return 0;
295
+ const def = this._evalExpr(args[0]!);
296
+ return typeof def === 'string' ? parseFloat(def) || 0 : _num(def, 'input()');
297
+ }
298
+
299
+ case 'plot': {
300
+ const value = _num(this._evalExpr(args[0]!), 'plot()');
301
+ const idx = this._plotIdx++;
302
+ if (idx >= this._plotSeries.length) {
303
+ this._plotSeries.push(new Series(1000));
304
+ }
305
+ this._plotSeries[idx]!.push(value);
306
+ return value;
307
+ }
308
+
309
+ case 'na': {
310
+ if (args.length > 0) {
311
+ const v = this._evalExpr(args[0]!);
312
+ return typeof v === 'number' ? isNaN(v) : v instanceof Series ? isNaN(v.value) : false;
313
+ }
314
+ return NaN;
315
+ }
316
+
317
+ case 'nz': {
318
+ const v = _num(this._evalExpr(args[0]!), 'nz()');
319
+ const fallback = args.length > 1 ? _num(this._evalExpr(args[1]!), 'nz() fallback') : 0;
320
+ return isNaN(v) ? fallback : v;
321
+ }
322
+
323
+ case 'abs': return Math.abs(_num(this._evalExpr(args[0]!), 'abs()'));
324
+ case 'round': return Math.round(_num(this._evalExpr(args[0]!), 'round()'));
325
+ case 'floor': return Math.floor(_num(this._evalExpr(args[0]!), 'floor()'));
326
+ case 'ceil': return Math.ceil(_num(this._evalExpr(args[0]!), 'ceil()'));
327
+ case 'sqrt': return Math.sqrt(_num(this._evalExpr(args[0]!), 'sqrt()'));
328
+ case 'log': return Math.log(_num(this._evalExpr(args[0]!), 'log()'));
329
+ case 'sign': return Math.sign(_num(this._evalExpr(args[0]!), 'sign()'));
330
+ case 'pow': {
331
+ const base = _num(this._evalExpr(args[0]!), 'pow() base');
332
+ const exp = _num(this._evalExpr(args[1]!), 'pow() exponent');
333
+ return Math.pow(base, exp);
334
+ }
335
+ case 'max': return Math.max(...args.map((a) => _num(this._evalExpr(a), 'max()')));
336
+ case 'min': return Math.min(...args.map((a) => _num(this._evalExpr(a), 'min()')));
337
+
338
+ // ── TA functions ────────────────────────────────────────────────────────
339
+ case 'sma': return this._ta_sma(args);
340
+ case 'ema': return this._ta_ema(args);
341
+ case 'wma': return this._ta_wma(args);
342
+ case 'rma': return this._ta_rma(args);
343
+ case 'stdev': return this._ta_stdev(args);
344
+ case 'highest':return this._ta_rolling(args, Math.max);
345
+ case 'lowest': return this._ta_rolling(args, Math.min);
346
+ case 'change': return this._ta_change(args);
347
+ case 'mom': return this._ta_change(args);
348
+ case 'atr': return this._ta_atr(args);
349
+ case 'crossover': return this._ta_crossover(args);
350
+ case 'crossunder': return this._ta_crossunder(args);
351
+ case 'barssince': return this._ta_barssince(args);
352
+
353
+ default:
354
+ throw new ReferenceError(`[TScript] Unknown function '${fn}'`);
355
+ }
356
+ }
357
+
358
+ // ─── TA helpers ──────────────────────────────────────────────────────────────
359
+
360
+ /** Resolve a call argument as a Series (wrapping scalar if needed). */
361
+ private _argSeries(arg: Expr, ctx: string): Series {
362
+ const v = this._evalExpr(arg);
363
+ if (v instanceof Series) return v;
364
+ if (typeof v === 'number') {
365
+ // Inline scalar: treat as a constant series with only the current value
366
+ const s = new Series(2);
367
+ s.push(v);
368
+ return s;
369
+ }
370
+ throw new TypeError(`[TScript] ${ctx}: expected a series`);
371
+ }
372
+
373
+ private _argInt(arg: Expr, ctx: string): number {
374
+ return Math.round(_num(this._evalExpr(arg), ctx));
375
+ }
376
+
377
+ private _ta_sma(args: Expr[]): number {
378
+ const src = this._argSeries(args[0]!, 'sma(src)');
379
+ const period = this._argInt(args[1]!, 'sma(length)');
380
+ if (src.length < period) return NaN;
381
+ let sum = 0;
382
+ for (let i = 0; i < period; i++) sum += src.get(i);
383
+ return sum / period;
384
+ }
385
+
386
+ private _ta_ema(args: Expr[]): number {
387
+ const src = this._argSeries(args[0]!, 'ema(src)');
388
+ const period = this._argInt(args[1]!, 'ema(length)');
389
+ if (src.length < period) return NaN;
390
+ // Persistent EMA state: store the last EMA value in a named internal Series
391
+ const stateKey = `__ema_${args[0]!.kind}_${period}`;
392
+ let prevEMA = this._getInternalNum(stateKey);
393
+ const k = 2 / (period + 1);
394
+ const cur = src.get(0);
395
+ const newEMA = isNaN(prevEMA)
396
+ ? this._smaOfSeries(src, period) // seed with SMA on first valid bar
397
+ : cur * k + prevEMA * (1 - k);
398
+ this._setInternal(stateKey, newEMA);
399
+ return newEMA;
400
+ }
401
+
402
+ private _ta_wma(args: Expr[]): number {
403
+ const src = this._argSeries(args[0]!, 'wma(src)');
404
+ const period = this._argInt(args[1]!, 'wma(length)');
405
+ if (src.length < period) return NaN;
406
+ const denom = (period * (period + 1)) / 2;
407
+ let sum = 0;
408
+ for (let i = 0; i < period; i++) sum += src.get(i) * (period - i);
409
+ return sum / denom;
410
+ }
411
+
412
+ private _ta_rma(args: Expr[]): number {
413
+ // Wilder's RMA: alpha = 1/period
414
+ const src = this._argSeries(args[0]!, 'rma(src)');
415
+ const period = this._argInt(args[1]!, 'rma(length)');
416
+ if (src.length < period) return NaN;
417
+ const stateKey = `__rma_${args[0]!.kind}_${period}`;
418
+ let prev = this._getInternalNum(stateKey);
419
+ const alpha = 1 / period;
420
+ const cur = src.get(0);
421
+ const result = isNaN(prev) ? this._smaOfSeries(src, period) : alpha * cur + (1 - alpha) * prev;
422
+ this._setInternal(stateKey, result);
423
+ return result;
424
+ }
425
+
426
+ private _ta_stdev(args: Expr[]): number {
427
+ const src = this._argSeries(args[0]!, 'stdev(src)');
428
+ const period = this._argInt(args[1]!, 'stdev(length)');
429
+ if (src.length < period) return NaN;
430
+ const mean = this._smaOfSeries(src, period);
431
+ let variance = 0;
432
+ for (let i = 0; i < period; i++) {
433
+ const diff = src.get(i) - mean;
434
+ variance += diff * diff;
435
+ }
436
+ return Math.sqrt(variance / period);
437
+ }
438
+
439
+ private _ta_rolling(args: Expr[], fn: (...v: number[]) => number): number {
440
+ const src = this._argSeries(args[0]!, 'highest/lowest(src)');
441
+ const period = this._argInt(args[1]!, 'highest/lowest(length)');
442
+ if (src.length < period) return NaN;
443
+ let result = src.get(0);
444
+ for (let i = 1; i < period; i++) result = fn(result, src.get(i));
445
+ return result;
446
+ }
447
+
448
+ private _ta_change(args: Expr[]): number {
449
+ const src = this._argSeries(args[0]!, 'change(src)');
450
+ const period = args.length > 1 ? this._argInt(args[1]!, 'change(length)') : 1;
451
+ if (src.length <= period) return NaN;
452
+ return src.get(0) - src.get(period);
453
+ }
454
+
455
+ private _ta_atr(args: Expr[]): number {
456
+ const period = this._argInt(args[0]!, 'atr(length)');
457
+ const high = this._scope.get('high');
458
+ const low = this._scope.get('low');
459
+ const close = this._scope.get('close');
460
+ if (!(high instanceof Series) || !(low instanceof Series) || !(close instanceof Series)) {
461
+ return NaN;
462
+ }
463
+ if (high.length < 2) return NaN;
464
+ const h = high.get(0);
465
+ const l = low.get(0);
466
+ const pc = close.get(1); // previous close
467
+ const tr = Math.max(h - l, Math.abs(h - pc), Math.abs(l - pc));
468
+ const stateKey = `__atr_${period}`;
469
+ let prev = this._getInternalNum(stateKey);
470
+ const alpha = 1 / period;
471
+ const result = isNaN(prev) ? tr : alpha * tr + (1 - alpha) * prev;
472
+ this._setInternal(stateKey, result);
473
+ return result;
474
+ }
475
+
476
+ private _ta_crossover(args: Expr[]): boolean {
477
+ const a = this._argSeries(args[0]!, 'crossover(a)');
478
+ const b = this._argSeries(args[1]!, 'crossover(b)');
479
+ return a.get(0) > b.get(0) && a.get(1) <= b.get(1);
480
+ }
481
+
482
+ private _ta_crossunder(args: Expr[]): boolean {
483
+ const a = this._argSeries(args[0]!, 'crossunder(a)');
484
+ const b = this._argSeries(args[1]!, 'crossunder(b)');
485
+ return a.get(0) < b.get(0) && a.get(1) >= b.get(1);
486
+ }
487
+
488
+ private _ta_barssince(args: Expr[]): number {
489
+ const cond = this._argSeries(args[0]!, 'barssince(cond)');
490
+ for (let i = 0; i < cond.length; i++) {
491
+ if (_bool(cond.get(i))) return i;
492
+ }
493
+ return NaN;
494
+ }
495
+
496
+ // ─── Internal state helpers ──────────────────────────────────────────────────
497
+
498
+ /** Persistent scalar state for stateful TA functions (EMA, RMA, ATR). */
499
+ private readonly _internalState = new Map<string, number>();
500
+
501
+ private _getInternalNum(key: string): number {
502
+ return this._internalState.get(key) ?? NaN;
503
+ }
504
+
505
+ private _setInternal(key: string, value: number): void {
506
+ this._internalState.set(key, value);
507
+ }
508
+
509
+ /** Push a value into a named built-in Series, creating it if needed. */
510
+ private _pushBuiltin(name: string, value: number): void {
511
+ let series = this._scope.get(name);
512
+ if (!(series instanceof Series)) {
513
+ series = new Series(1000);
514
+ this._scope.set(name, series);
515
+ }
516
+ series.push(value);
517
+ }
518
+
519
+ /** Compute a simple rolling average directly from a Series (no state). */
520
+ private _smaOfSeries(src: Series, period: number): number {
521
+ let sum = 0;
522
+ for (let i = 0; i < period; i++) sum += src.get(i);
523
+ return sum / period;
524
+ }
525
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * TScript Series — fixed-capacity ring buffer for historical bar data.
3
+ *
4
+ * Access pattern mirrors Pine Script:
5
+ * series.value — current (bar 0) value
6
+ * series.get(0) — same as .value
7
+ * series.get(1) — 1 bar ago
8
+ * series.get(n) — n bars ago (returns NaN if out of range)
9
+ *
10
+ * Internally the ring buffer stores the `capacity` most recent values in a
11
+ * circular array, so `.push()` is O(1) with no allocation after warm-up.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const s = new Series(500);
16
+ * s.push(1); s.push(2); s.push(3);
17
+ * s.get(0); // 3 (current)
18
+ * s.get(1); // 2 (one bar ago)
19
+ * s.get(2); // 1 (two bars ago)
20
+ * ```
21
+ */
22
+ export class Series {
23
+ private readonly _buf: Float64Array;
24
+ private readonly _cap: number;
25
+ private _head: number = 0; // index of the NEXT write slot
26
+ private _len: number = 0; // number of values stored so far
27
+
28
+ constructor(capacity = 1000) {
29
+ this._cap = capacity;
30
+ this._buf = new Float64Array(capacity).fill(NaN);
31
+ }
32
+
33
+ /** Append a new value at the current bar position. */
34
+ push(value: number): void {
35
+ this._buf[this._head] = value;
36
+ this._head = (this._head + 1) % this._cap;
37
+ if (this._len < this._cap) this._len++;
38
+ }
39
+
40
+ /**
41
+ * Retrieve a historical value.
42
+ * @param lookback 0 = current bar, 1 = previous bar, etc.
43
+ */
44
+ get(lookback: number): number {
45
+ if (lookback < 0 || lookback >= this._len) return NaN;
46
+ // Head points to NEXT slot, so current value is at head-1
47
+ const idx = (this._head - 1 - lookback + this._cap * 2) % this._cap;
48
+ return this._buf[idx]!;
49
+ }
50
+
51
+ /** Alias for get(0). */
52
+ get value(): number {
53
+ return this.get(0);
54
+ }
55
+
56
+ /** Number of values stored (up to capacity). */
57
+ get length(): number {
58
+ return this._len;
59
+ }
60
+
61
+ /** Ring-buffer capacity. */
62
+ get capacity(): number {
63
+ return this._cap;
64
+ }
65
+
66
+ /**
67
+ * Snapshot: returns all stored values in chronological order
68
+ * (oldest first, newest last).
69
+ */
70
+ toArray(): number[] {
71
+ const out: number[] = new Array(this._len);
72
+ for (let i = 0; i < this._len; i++) {
73
+ out[i] = this.get(this._len - 1 - i);
74
+ }
75
+ return out;
76
+ }
77
+
78
+ /** Reset the buffer (e.g. on symbol change). */
79
+ reset(): void {
80
+ this._buf.fill(NaN);
81
+ this._head = 0;
82
+ this._len = 0;
83
+ }
84
+ }
@@ -0,0 +1,56 @@
1
+ import type { SeriesOptions, Viewport } from '@forgecharts/types';
2
+ import type { ISeries } from './ISeries';
3
+ import type { CoordTransform } from '../core/CoordTransform';
4
+
5
+ export interface IChart {
6
+ /**
7
+ * Adds a new series to the chart and returns a handle for data management.
8
+ */
9
+ addSeries(options: SeriesOptions): ISeries;
10
+
11
+ /**
12
+ * Removes a previously added series.
13
+ */
14
+ removeSeries(series: ISeries): void;
15
+
16
+ /**
17
+ * Registers a plugin that participates in the render loop.
18
+ */
19
+ addPlugin(plugin: IChartPlugin): void;
20
+
21
+ /**
22
+ * Deregisters a plugin.
23
+ */
24
+ removePlugin(plugin: IChartPlugin): void;
25
+
26
+ /**
27
+ * Applies partial chart options at runtime (theme, scale options, etc.).
28
+ */
29
+ applyOptions(options: import('@forgecharts/types').ChartOptions): void;
30
+
31
+ /**
32
+ * Manually sets the canvas dimensions. Usually called from ResizeObserver.
33
+ */
34
+ resize(width: number, height: number): void;
35
+
36
+ /**
37
+ * Tears down all resources and removes canvas elements from the DOM.
38
+ */
39
+ destroy(): void;
40
+
41
+ /**
42
+ * Returns the coordinate transform for pixel ↔ data conversions.
43
+ * The returned instance is kept up-to-date across resizes; callers may
44
+ * hold a stable reference.
45
+ */
46
+ transform(): CoordTransform;
47
+ }
48
+
49
+ export interface IChartPlugin {
50
+ /** Called once when the plugin is added to a chart. */
51
+ onAttach(chart: IChart): void;
52
+ /** Called once when the plugin is removed from a chart. */
53
+ onDetach(): void;
54
+ /** Called every animation frame after series are rendered. */
55
+ onRender(): void;
56
+ }
@@ -0,0 +1,16 @@
1
+ import type { OHLCV, SeriesOptions, Viewport, Rect } from '@forgecharts/types';
2
+
3
+ /**
4
+ * IRenderer — contract for all series drawing strategies.
5
+ * Each renderer receives only what it needs: bars, viewport, and target rect.
6
+ * No DOM or Chart references — pure canvas drawing.
7
+ */
8
+ export interface IRenderer {
9
+ draw(
10
+ ctx: CanvasRenderingContext2D,
11
+ bars: readonly OHLCV[],
12
+ viewport: Viewport,
13
+ rect: Rect,
14
+ options: SeriesOptions,
15
+ ): void;
16
+ }