@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,301 @@
1
+ /**
2
+ * Pine Script → TScript transpiler.
3
+ *
4
+ * Walks a `PineProgram` AST and emits a TScript source string that is
5
+ * directly executable by the existing `TScriptRuntime`.
6
+ */
7
+
8
+ import type {
9
+ PineProgram, PineStmt, PineExpr,
10
+ PineIndicatorDecl, PineVarDecl, PineAssign, PineExprStmt, PineIf,
11
+ PineNumberLit, PineStringLit, PineBoolLit, PineNa,
12
+ PineIdent, PineNsCall, PineCall, PineIndex,
13
+ PineBinary, PineUnary, PineTernary, PineMember,
14
+ } from './pine-ast';
15
+ import { DiagnosticBag } from './diagnostics';
16
+
17
+ // ─── Namespace → TScript function mappings ───────────────────────────────────
18
+
19
+ const TA_MAP: Record<string, string> = {
20
+ sma: 'sma',
21
+ ema: 'ema',
22
+ rsi: 'rsi',
23
+ wma: 'wma',
24
+ rma: 'rma',
25
+ stdev: 'stdev',
26
+ highest: 'highest',
27
+ lowest: 'lowest',
28
+ change: 'change',
29
+ mom: 'mom',
30
+ atr: 'atr',
31
+ crossover: 'crossover',
32
+ crossunder: 'crossunder',
33
+ barssince: 'barssince',
34
+ };
35
+
36
+ const MATH_MAP: Record<string, string> = {
37
+ abs: 'abs',
38
+ max: 'max',
39
+ min: 'min',
40
+ round: 'round',
41
+ floor: 'floor',
42
+ ceil: 'ceil',
43
+ sqrt: 'sqrt',
44
+ log: 'log',
45
+ pow: 'pow',
46
+ sign: 'sign',
47
+ exp: 'exp',
48
+ };
49
+
50
+ const COLOR_MAP: Record<string, string> = {
51
+ red: '"#f23645"',
52
+ green: '"#26a69a"',
53
+ blue: '"#2196f3"',
54
+ white: '"#ffffff"',
55
+ black: '"#000000"',
56
+ yellow: '"#ffeb3b"',
57
+ orange: '"#ff9800"',
58
+ purple: '"#9c27b0"',
59
+ aqua: '"#00bcd4"',
60
+ gray: '"#787b86"',
61
+ silver: '"#b2b5be"',
62
+ lime: '"#4caf50"',
63
+ maroon: '"#880000"',
64
+ navy: '"#000080"',
65
+ olive: '"#808000"',
66
+ teal: '"#008080"',
67
+ fuchsia:'"#ff00ff"',
68
+ };
69
+
70
+ /** Functions that produce no useful TScript equivalent — emit a warning. */
71
+ const UNSUPPORTED_FN = new Set([
72
+ 'plotshape', 'plotarrow', 'plotbar', 'plotcandle',
73
+ 'barcolor', 'bgcolor', 'alertcondition', 'alert',
74
+ 'strategy', 'label', 'line', 'box', 'table',
75
+ 'request',
76
+ ]);
77
+
78
+ export class PineTranspiler {
79
+ private _diag: DiagnosticBag;
80
+ private _indent = 0;
81
+ private _out: string[] = [];
82
+
83
+ constructor(diag: DiagnosticBag) {
84
+ this._diag = diag;
85
+ }
86
+
87
+ transpile(prog: PineProgram): string {
88
+ this._out = [];
89
+ this._indent = 0;
90
+ for (const stmt of prog.stmts) {
91
+ this._stmt(stmt);
92
+ }
93
+ return this._out.join('');
94
+ }
95
+
96
+ // ─── Statements ─────────────────────────────────────────────────────────────
97
+
98
+ private _stmt(s: PineStmt): void {
99
+ switch (s.kind) {
100
+ case 'PineIndicatorDecl': return this._indicatorDecl(s);
101
+ case 'PineVarDecl': return this._varDecl(s);
102
+ case 'PineAssign': return this._assign(s);
103
+ case 'PineExprStmt': return this._exprStmt(s);
104
+ case 'PineIf': return this._ifStmt(s);
105
+ }
106
+ }
107
+
108
+ private _indicatorDecl(s: PineIndicatorDecl): void {
109
+ // indicator("Title", overlay=true) → indicator("Title")
110
+ const title = s.args[0];
111
+ const titleStr = title ? this._expr(title) : '"Script"';
112
+ this._line(`indicator(${titleStr})`);
113
+ }
114
+
115
+ private _varDecl(s: PineVarDecl): void {
116
+ // var x = expr → x = expr (TScript vars persist by default)
117
+ this._line(`${s.name} = ${this._expr(s.value)}`);
118
+ }
119
+
120
+ private _assign(s: PineAssign): void {
121
+ // := and = both map to = in TScript
122
+ this._line(`${s.name} = ${this._expr(s.value)}`);
123
+ }
124
+
125
+ private _exprStmt(s: PineExprStmt): void {
126
+ const expr = s.expr;
127
+ // Check for unsupported top-level calls before emitting
128
+ if (
129
+ (expr.kind === 'PineCall' || expr.kind === 'PineNsCall') &&
130
+ UNSUPPORTED_FN.has(expr.kind === 'PineCall' ? expr.fn : expr.fn)
131
+ ) {
132
+ const name = expr.kind === 'PineCall' ? expr.fn : `${expr.namespace}.${expr.fn}`;
133
+ this._diag.add('warning', `'${name}()' is not supported and will be ignored`, s.loc, 'PINE_UNSUPPORTED_FN');
134
+ this._line(`// ${name}() not supported`);
135
+ return;
136
+ }
137
+ this._line(this._expr(expr));
138
+ }
139
+
140
+ private _ifStmt(s: PineIf): void {
141
+ this._line(`if ${this._expr(s.condition)}`);
142
+ this._indent++;
143
+ for (const st of s.then) this._stmt(st);
144
+ if (s.then.length === 0) this._line('0'); // empty block guard
145
+ this._indent--;
146
+ if (s.else_.length > 0) {
147
+ this._line('else');
148
+ this._indent++;
149
+ for (const st of s.else_) this._stmt(st);
150
+ this._indent--;
151
+ }
152
+ }
153
+
154
+ // ─── Expressions ────────────────────────────────────────────────────────────
155
+
156
+ private _expr(e: PineExpr): string {
157
+ switch (e.kind) {
158
+ case 'PineNumberLit': return this._numLit(e);
159
+ case 'PineStringLit': return `"${e.value.replace(/\\/g,'\\\\').replace(/"/g,'\\"')}"`;
160
+ case 'PineBoolLit': return e.value ? 'true' : 'false';
161
+ case 'PineNa': return 'na';
162
+ case 'PineIdent': return e.name;
163
+ case 'PineNsCall': return this._nsCall(e);
164
+ case 'PineCall': return this._call(e);
165
+ case 'PineIndex': return `${this._expr(e.series)}[${this._expr(e.index)}]`;
166
+ case 'PineBinary': return this._binary(e);
167
+ case 'PineUnary': return this._unary(e);
168
+ case 'PineTernary': return `${this._expr(e.condition)} ? ${this._expr(e.consequent)} : ${this._expr(e.alternate)}`;
169
+ case 'PineMember': return this._member(e);
170
+ // PineColorLit is resolved via PineMember for color.red etc
171
+ default: return '0';
172
+ }
173
+ }
174
+
175
+ private _numLit(e: PineNumberLit): string {
176
+ // Preserve integer formatting when possible
177
+ return Number.isInteger(e.value) ? String(e.value) : String(e.value);
178
+ }
179
+
180
+ private _nsCall(e: PineNsCall): string {
181
+ const { namespace: ns, fn } = e;
182
+
183
+ if (ns === 'ta') {
184
+ const mapped = TA_MAP[fn];
185
+ if (mapped) {
186
+ const args = this._positionalArgs(e.args);
187
+ return `${mapped}(${args})`;
188
+ }
189
+ this._diag.add('warning', `'ta.${fn}()' is not supported`, e.loc, 'PINE_UNSUPPORTED_FN');
190
+ return `/* ta.${fn} not supported */ 0`;
191
+ }
192
+
193
+ if (ns === 'math') {
194
+ const mapped = MATH_MAP[fn];
195
+ if (mapped) {
196
+ const args = this._positionalArgs(e.args);
197
+ return `${mapped}(${args})`;
198
+ }
199
+ this._diag.add('warning', `'math.${fn}()' is not supported`, e.loc, 'PINE_UNSUPPORTED_FN');
200
+ return `/* math.${fn} not supported */ 0`;
201
+ }
202
+
203
+ if (ns === 'input') {
204
+ return this._inputNsCall(e);
205
+ }
206
+
207
+ if (ns === 'strategy' || ns === 'label' || ns === 'line' || ns === 'box' || ns === 'table' || ns === 'request') {
208
+ this._diag.add('warning', `'${ns}.${fn}()' is not supported`, e.loc, 'PINE_UNSUPPORTED_FN');
209
+ return `/* ${ns}.${fn} not supported */ 0`;
210
+ }
211
+
212
+ // Generic fallthrough
213
+ this._diag.add('warning', `'${ns}.${fn}()' is not supported`, e.loc, 'PINE_UNSUPPORTED_FN');
214
+ return `/* ${ns}.${fn} not supported */ 0`;
215
+ }
216
+
217
+ private _inputNsCall(e: PineNsCall): string {
218
+ // input.int(v, ...) / input.float(v, ...) / input.bool(v, ...) → input(v)
219
+ // input.source(close) → the source arg value
220
+ if (e.fn === 'source') {
221
+ return e.args[0] ? this._expr(e.args[0]) : 'close';
222
+ }
223
+ const defVal = e.args[0] ?? e.namedArgs.get('defval');
224
+ return defVal ? `input(${this._expr(defVal)})` : 'input(0)';
225
+ }
226
+
227
+ private _call(e: PineCall): string {
228
+ if (UNSUPPORTED_FN.has(e.fn)) {
229
+ this._diag.add('warning', `'${e.fn}()' is not supported and will be ignored`, e.loc, 'PINE_UNSUPPORTED_FN');
230
+ return `/* ${e.fn} not supported */ 0`;
231
+ }
232
+
233
+ if (e.fn === 'input') {
234
+ const defVal = e.args[0] ?? e.namedArgs.get('defval');
235
+ return defVal ? `input(${this._expr(defVal)})` : 'input(0)';
236
+ }
237
+
238
+ if (e.fn === 'plot') {
239
+ return this._plotCall(e);
240
+ }
241
+
242
+ if (e.fn === 'indicator') {
243
+ const title = e.args[0];
244
+ const titleStr = title ? this._expr(title) : '"Script"';
245
+ return `indicator(${titleStr})`;
246
+ }
247
+
248
+ // Generic call — drop unsupported named args, keep positional
249
+ const args = this._positionalArgs(e.args);
250
+ return `${e.fn}(${args})`;
251
+ }
252
+
253
+ private _plotCall(e: PineCall): string {
254
+ // plot(series, title="...", color=..., linewidth=...) → plot(series)
255
+ // We only pass the first positional arg; named args are cosmetic only
256
+ const series = e.args[0];
257
+ if (!series) return '/* plot() missing series */';
258
+ return `plot(${this._expr(series)})`;
259
+ }
260
+
261
+ private _member(e: PineMember): string {
262
+ if (e.object === 'color') {
263
+ const hex = COLOR_MAP[e.prop];
264
+ if (hex) return hex;
265
+ this._diag.add('info', `'color.${e.prop}' is not a known color`, e.loc, 'PINE_UNKNOWN_COLOR');
266
+ return '"#888888"';
267
+ }
268
+ if (e.object === 'line' || e.object === 'label' || e.object === 'box') {
269
+ // style constants — just return as string
270
+ return `"${e.prop}"`;
271
+ }
272
+ // bar_index, time, etc. are plain idents
273
+ if (e.object === 'timeframe' || e.object === 'syminfo' || e.object === 'session') {
274
+ this._diag.add('info', `'${e.object}.${e.prop}' is not supported`, e.loc, 'PINE_UNSUPPORTED_FN');
275
+ return '0';
276
+ }
277
+ return `${e.object}_${e.prop}`; // fallback
278
+ }
279
+
280
+ private _binary(e: PineBinary): string {
281
+ const l = this._expr(e.left);
282
+ const r = this._expr(e.right);
283
+ return `(${l} ${e.op} ${r})`;
284
+ }
285
+
286
+ private _unary(e: PineUnary): string {
287
+ if (e.op === 'not') return `not ${this._expr(e.operand)}`;
288
+ return `${e.op}${this._expr(e.operand)}`;
289
+ }
290
+
291
+ private _positionalArgs(args: PineExpr[]): string {
292
+ return args.map(a => this._expr(a)).join(', ');
293
+ }
294
+
295
+ // ─── Output helpers ─────────────────────────────────────────────────────────
296
+
297
+ private _line(code: string): void {
298
+ const pad = ' '.repeat(this._indent);
299
+ this._out.push(`${pad}${code}\n`);
300
+ }
301
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * LayerName — ordered Z-index identifiers for the seven render layers.
3
+ *
4
+ * Layers are sorted ascending: lowest = farthest back, highest = topmost.
5
+ * The numeric values are used directly as PixiJS Container `zIndex` values.
6
+ */
7
+ export const enum LayerName {
8
+ Background = 0,
9
+ PriceSeries = 1,
10
+ Indicator = 2,
11
+ Drawing = 3,
12
+ TradingOverlay = 4,
13
+ Interaction = 5,
14
+ UIOverlay = 6,
15
+ }
16
+
17
+ export const LAYER_NAMES = [
18
+ LayerName.Background,
19
+ LayerName.PriceSeries,
20
+ LayerName.Indicator,
21
+ LayerName.Drawing,
22
+ LayerName.TradingOverlay,
23
+ LayerName.Interaction,
24
+ LayerName.UIOverlay,
25
+ ] as const;
26
+
27
+ export const LAYER_LABEL: Record<LayerName, string> = {
28
+ [LayerName.Background]: 'background',
29
+ [LayerName.PriceSeries]: 'priceSeries',
30
+ [LayerName.Indicator]: 'indicator',
31
+ [LayerName.Drawing]: 'drawing',
32
+ [LayerName.TradingOverlay]: 'tradingOverlay',
33
+ [LayerName.Interaction]: 'interaction',
34
+ [LayerName.UIOverlay]: 'uiOverlay',
35
+ };
@@ -0,0 +1,125 @@
1
+ import { Graphics } from 'pixi.js';
2
+ import type { Container } from 'pixi.js';
3
+ import type { OHLCV } from '@forgecharts/types';
4
+ import type { CoordTransform } from '../core/CoordTransform';
5
+
6
+ const UP_COLOR = 0x26a641; // green
7
+ const DOWN_COLOR = 0xf85149; // red
8
+ const MIN_BODY_H = 1;
9
+ const MAX_BAR_W = 20;
10
+ const MIN_BAR_W = 1;
11
+
12
+ /**
13
+ * PixiCandlestickRenderer — renders OHLCV data as GPU-batched candlesticks.
14
+ *
15
+ * Uses a single re-used `Graphics` object that is fully cleared and redrawn
16
+ * on each invalidation, taking advantage of PixiJS's WebGL batching for
17
+ * high-performance rendering of large datasets.
18
+ */
19
+ export class PixiCandlestickRenderer {
20
+ private readonly _gfx: Graphics;
21
+
22
+ constructor(
23
+ layer: Container,
24
+ private readonly _upColor: number = UP_COLOR,
25
+ private readonly _downColor: number = DOWN_COLOR,
26
+ ) {
27
+ this._gfx = new Graphics();
28
+ layer.addChild(this._gfx);
29
+ }
30
+
31
+ draw(
32
+ bars: readonly OHLCV[],
33
+ transform: CoordTransform,
34
+ ): void {
35
+ const gfx = this._gfx;
36
+ gfx.clear();
37
+
38
+ if (bars.length === 0) return;
39
+
40
+ const { plotWidth: pw, plotHeight: ph } = transform;
41
+ const { from, to } = transform.timeRange;
42
+
43
+ // Infer candle interval from median gap between consecutive bars (seconds)
44
+ let candleInterval = 0;
45
+ if (bars.length >= 2) {
46
+ const deltas: number[] = [];
47
+ for (let i = 1; i < Math.min(bars.length, 20); i++) {
48
+ const d = bars[i]!.time - bars[i - 1]!.time;
49
+ if (d > 0) deltas.push(d);
50
+ }
51
+ if (deltas.length > 0) {
52
+ deltas.sort((a, b) => a - b);
53
+ candleInterval = deltas[Math.floor(deltas.length / 2)]!;
54
+ }
55
+ }
56
+ const visibleSpan = to - from;
57
+ const pps = visibleSpan > 0 ? pw / visibleSpan : 1;
58
+ const barPitch = candleInterval > 0
59
+ ? candleInterval * pps
60
+ : pw / Math.max(1, bars.length);
61
+
62
+ const gap = Math.max(1, Math.round(barPitch * 0.15));
63
+ const barW = Math.min(MAX_BAR_W, Math.max(MIN_BAR_W, Math.floor(barPitch) - gap));
64
+ const halfW = Math.max(0.5, barW / 2);
65
+
66
+ for (const bar of bars) {
67
+ const x = transform.timeToX(bar.time);
68
+
69
+ // Skip entirely off-screen bars
70
+ if (x < -barW || x > pw + barW) continue;
71
+
72
+ const yOpen = transform.priceToY(bar.open);
73
+ const yClose = transform.priceToY(bar.close);
74
+ const yHigh = transform.priceToY(bar.high);
75
+ const yLow = transform.priceToY(bar.low);
76
+
77
+ const bullish = bar.close >= bar.open;
78
+ const color = bullish ? this._upColor : this._downColor;
79
+ const bodyTop = Math.min(yOpen, yClose);
80
+ const bodyH = Math.max(MIN_BODY_H, Math.abs(yClose - yOpen));
81
+
82
+ // Clip to plot area
83
+ if (bodyTop > ph || bodyTop + bodyH < 0) continue;
84
+
85
+ // Wick
86
+ gfx
87
+ .moveTo(x, yHigh)
88
+ .lineTo(x, bodyTop)
89
+ .moveTo(x, bodyTop + bodyH)
90
+ .lineTo(x, yLow)
91
+ .stroke({ color, width: 1 });
92
+
93
+ // Body
94
+ gfx
95
+ .rect(x - halfW, bodyTop, barW, bodyH)
96
+ .fill({ color });
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Converts OHLCV bars to Heikin-Ashi values before drawing.
102
+ */
103
+ drawHeikinAshi(bars: readonly OHLCV[], transform: CoordTransform): void {
104
+ this.draw(this._toHeikinAshi(bars), transform);
105
+ }
106
+
107
+ private _toHeikinAshi(bars: readonly OHLCV[]): OHLCV[] {
108
+ const result: OHLCV[] = [];
109
+ for (let i = 0; i < bars.length; i++) {
110
+ const bar = bars[i]!;
111
+ const prev = result[i - 1] ?? bar;
112
+ const haClose = (bar.open + bar.high + bar.low + bar.close) / 4;
113
+ const haOpen = (prev.open + prev.close) / 2;
114
+ result.push({
115
+ time: bar.time,
116
+ open: haOpen,
117
+ high: Math.max(bar.high, haOpen, haClose),
118
+ low: Math.min(bar.low, haOpen, haClose),
119
+ close: haClose,
120
+ volume: bar.volume,
121
+ });
122
+ }
123
+ return result;
124
+ }
125
+ }