@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,118 @@
1
+ /**
2
+ * Timeframe utilities — thin compatibility wrappers over `@forgecharts/shared/time`.
3
+ *
4
+ * All bucket-alignment logic now lives in the canonical shared module.
5
+ * These exports keep the existing `CandleEngine` and `DatafeedConnector` import
6
+ * surface stable while delegating every calculation to the single source of truth.
7
+ *
8
+ * Migration note:
9
+ * New code should import directly from `@forgecharts/shared/time`:
10
+ * import { getBucketStart, getWeekBucketStart, getMonthBucketStart } from '@forgecharts/shared/time';
11
+ *
12
+ * Legacy imports from this file continue to work unchanged.
13
+ */
14
+
15
+ import {
16
+ getBucketStart as sharedGetBucketStart,
17
+ timeframeToMs as sharedTimeframeToMs,
18
+ } from '@forgecharts/shared/time';
19
+
20
+ // ─── timeframeToMs ────────────────────────────────────────────────────────────
21
+
22
+ /**
23
+ * Convert a timeframe label to its duration in milliseconds.
24
+ *
25
+ * Returns `60_000` (1 minute) for calendar timeframes (`1w`, `1M`) and unknown
26
+ * labels — preserving the previous behaviour that `CandleEngine` depended on.
27
+ * New code should use `sharedTimeframeToMs` (which returns `null` for those
28
+ * cases) and handle `null` explicitly.
29
+ *
30
+ * @example
31
+ * timeframeToMs('1m') // → 60_000
32
+ * timeframeToMs('4h') // → 14_400_000
33
+ * timeframeToMs('1w') // → 60_000 (calendar — no fixed duration; fallback)
34
+ */
35
+ export function timeframeToMs(timeframe: string): number {
36
+ return sharedTimeframeToMs(timeframe) ?? 60_000;
37
+ }
38
+
39
+ // ─── getBucketStart (interval-based) ─────────────────────────────────────────
40
+
41
+ /**
42
+ * Floor a millisecond timestamp to the start of its fixed-interval bucket.
43
+ *
44
+ * This overload takes an explicit `intervalMs` and is used internally by
45
+ * `aggregateToBucket` for sub-daily custom timeframe synthesis.
46
+ *
47
+ * For timeframe-label–based alignment (including 1w / 1M calendar logic)
48
+ * use `getCalendarBucketStart` or import `getBucketStart` from `@forgecharts/shared`.
49
+ *
50
+ * @example
51
+ * getBucketStart(62_000, 60_000) // → 60_000
52
+ * getBucketStart(3_720_000, 3_600_000) // → 3_600_000
53
+ */
54
+ export function getBucketStart(timeMs: number, intervalMs: number): number {
55
+ return Math.floor(timeMs / intervalMs) * intervalMs;
56
+ }
57
+
58
+ // ─── getCalendarBucketStart ───────────────────────────────────────────────────
59
+
60
+ /**
61
+ * Floor a millisecond timestamp to the candle-open boundary for the given
62
+ * timeframe label, with correct calendar alignment:
63
+ *
64
+ * `1w` — Monday-aligned week (ISO 8601, UTC) via `getWeekBucketStart`
65
+ * `1M` — first of calendar month, UTC via `getMonthBucketStart`
66
+ * all others — plain epoch-floor via `getBucketStart`
67
+ *
68
+ * Delegates entirely to the canonical shared module.
69
+ */
70
+ export function getCalendarBucketStart(timeMs: number, tf: string): number {
71
+ return sharedGetBucketStart(timeMs, tf);
72
+ }
73
+
74
+ // ─── aggregateToBucket ────────────────────────────────────────────────────────
75
+
76
+ /**
77
+ * Build a custom timeframe by aggregating base-timeframe candles into larger buckets.
78
+ * Returns the aggregated candles for all buckets covered by `subBars`.
79
+ *
80
+ * This is the building block for synthesising e.g. 3m from 1m feeds.
81
+ *
82
+ * @param subBars Candles from the base timeframe, sorted ascending by timeMs.
83
+ * @param targetMs Target timeframe duration in ms (e.g. 180_000 for 3m).
84
+ *
85
+ * @throws {Error} If `targetMs` corresponds to a calendar-irregular timeframe
86
+ * (`1w` / `1M`). Use `getCalendarBucketStart` for those directly.
87
+ */
88
+ export function aggregateToBucket(
89
+ subBars: ReadonlyArray<{
90
+ timeMs: number; open: number; high: number; low: number; close: number; volume: number;
91
+ }>,
92
+ targetMs: number,
93
+ ): Array<{ timeMs: number; open: number; high: number; low: number; close: number; volume: number }> {
94
+ // 1w is 7 days exactly; 1M has no fixed duration and can never equal a targetMs.
95
+ // Guard prevents accidentally using fixed-interval bucketing for weekly candles
96
+ // where DST can make weeks slightly unequal (though UTC avoids this).
97
+ const WEEK_MS = 7 * 24 * 60 * 60 * 1000; // 604_800_000
98
+ if (targetMs === WEEK_MS) {
99
+ throw new Error('aggregateToBucket: use getCalendarBucketStart for 1w/1M aggregation');
100
+ }
101
+
102
+ const buckets = new Map<number, { timeMs: number; open: number; high: number; low: number; close: number; volume: number }>();
103
+
104
+ for (const bar of subBars) {
105
+ const bucket = getBucketStart(bar.timeMs, targetMs);
106
+ const existing = buckets.get(bucket);
107
+ if (!existing) {
108
+ buckets.set(bucket, { timeMs: bucket, open: bar.open, high: bar.high, low: bar.low, close: bar.close, volume: bar.volume });
109
+ } else {
110
+ existing.high = Math.max(existing.high, bar.high);
111
+ existing.low = Math.min(existing.low, bar.low);
112
+ existing.close = bar.close;
113
+ existing.volume += bar.volume;
114
+ }
115
+ }
116
+
117
+ return Array.from(buckets.values()).sort((a, b) => a.timeMs - b.timeMs);
118
+ }
package/src/index.ts ADDED
@@ -0,0 +1,190 @@
1
+ // ─── Primary entry point ─────────────────────────────────────────────────────
2
+ export { createChart } from './api/createChart';
3
+ export { TChart } from './api/TChart';
4
+
5
+ // ─── Reference Data API ───────────────────────────────────────────────────────
6
+ export { ReferenceAPI } from './api/ReferenceAPI';
7
+ export type { ReferenceAPIConfig } from './api/ReferenceAPI';
8
+
9
+ // ─── Datafeed ─────────────────────────────────────────────────────────────────
10
+ export { DatafeedConnector } from './datafeed/DatafeedConnector';
11
+ export type { ConnectorCallbacks } from './datafeed/DatafeedConnector';
12
+ // ─── Indicator engine ─────────────────────────────────────────────────────
13
+ export {
14
+ computeSMA,
15
+ computeEMA,
16
+ computeRSI,
17
+ computeMACD,
18
+ computeVolume,
19
+ // Series-based variants for DAG chaining
20
+ extractField,
21
+ computeSMAFromSeries,
22
+ computeEMAFromSeries,
23
+ computeWMAFromSeries,
24
+ } from './core/IndicatorEngine';
25
+ export type { IndicatorPoint, MACDPoint } from './core/IndicatorEngine';
26
+
27
+ // ─── Indicator DAG ────────────────────────────────────────────────────────────
28
+ export { IndicatorDAG } from './api/IndicatorDAG';
29
+ export type { DAGResult } from './api/IndicatorDAG';
30
+
31
+ // ─── TScript scripting engine ─────────────────────────────────────────────────
32
+ export { TScriptIndicator } from './tscript/TScriptIndicator';
33
+ export { Series as TScriptSeries } from './tscript/series';
34
+ export { TScriptRuntime } from './tscript/runtime';
35
+ export type { TValue, ScriptResult } from './tscript/runtime';
36
+
37
+ // ─── Pine Script compatibility layer ─────────────────────────────────────────
38
+ export { PineCompiler, DiagnosticBag } from './pine';
39
+ export type { PineCompileResult, Diagnostic, DiagnosticSeverity } from './pine';
40
+
41
+ // ─── Pane manager ─────────────────────────────────────────────────────────
42
+ export { PaneManager } from './api/PaneManager';
43
+ // ─── Coordinate transform system ─────────────────────────────────────────────
44
+ export { CoordTransform } from './core/CoordTransform';
45
+ export type { PlotArea } from './core/CoordTransform';
46
+
47
+ // ─── Candle engine ────────────────────────────────────────────────────────────
48
+ export { CandleEngine } from './engine/CandleEngine';
49
+ export type { CandleBar, RawOHLCV, UpdateResult, GapInfo, CandleEngineOptions } from './engine/CandleEngine';
50
+ export { timeframeToMs, getBucketStart } from './engine/timeframeUtils';
51
+ export { getMissingBarCount, mergeBars } from './engine/mergeUtils';
52
+
53
+ // ─── Trading types & overlay store ───────────────────────────────────────────
54
+ export type {
55
+ CandleInput,
56
+ ChartOrder,
57
+ ChartPosition,
58
+ ExecutionFill,
59
+ OrderRole,
60
+ OrderSide,
61
+ OrderStatus,
62
+ PositionSide,
63
+ PositionStatus,
64
+ FillSide,
65
+ } from './trading/tradingTypes';
66
+ export { TradingOverlayStore } from './trading/TradingOverlayStore';
67
+ export { UnmanagedIngestion } from './trading/UnmanagedIngestion';
68
+
69
+ // ─── Managed trading ──────────────────────────────────────────────────────────
70
+ export { ManagedTradingController } from './trading/managed/ManagedTradingController';
71
+ export type {
72
+ IExecutionProvider,
73
+ PlaceOrderInput,
74
+ ModifyOrderInput,
75
+ PlaceBracketOrderInput,
76
+ OrderAck,
77
+ BracketOrderAck,
78
+ ManagedOrderType,
79
+ TimeInForce,
80
+ } from './trading/managed/managedTypes';
81
+ export {
82
+ assertManagedMode,
83
+ assertCanPlaceOrders,
84
+ assertCanUseBrackets,
85
+ assertCanUseDraggableOrders,
86
+ assertCanUseManagedTrading,
87
+ isManagedCapable,
88
+ canPlaceOrders,
89
+ canPlaceBrackets,
90
+ canUseDraggable,
91
+ canUseManagedTradingHook,
92
+ } from './trading/managed/managedCapabilities';
93
+
94
+ // ─── Licensing ────────────────────────────────────────────────────────────────
95
+ export { LicenseManager } from './licensing/LicenseManager';
96
+ export type { LicenseMode, LicenseFeatures, LicensePayload } from './licensing/licenseTypes';
97
+
98
+ // ─── Chart runtime resolver ───────────────────────────────────────────────────
99
+ export { ChartRuntimeResolver } from './licensing/ChartRuntimeResolver';
100
+ export type { ChartCapabilities } from './licensing/ChartRuntimeResolver';
101
+ export {
102
+ // Mode checks
103
+ isManagedMode,
104
+ isUnmanagedMode,
105
+ // Feature-level capability checks
106
+ canUseExternalIngestion,
107
+ canRenderOverlays,
108
+ canUseIndicators,
109
+ canUseOrderEntry,
110
+ canUseManagedTrading,
111
+ canUseDraggableOrders,
112
+ canUseBracketOrders,
113
+ getCapabilities,
114
+ // UI render capability checks
115
+ canRenderOrderEntry,
116
+ canRenderBuySellButtons,
117
+ canRenderOrderTicket,
118
+ canRenderBracketControls,
119
+ canRenderOrderModificationControls,
120
+ canRenderManagedTradingControls,
121
+ canRenderExternalOverlayOnlyMode,
122
+ canRenderPositions,
123
+ canRenderFills,
124
+ canRenderCandles,
125
+ canRenderIndicators,
126
+ canRenderDrawings,
127
+ } from './licensing/ChartRuntimeResolver';
128
+
129
+ // ─── Shared / public types ────────────────────────────────────────────────────
130
+ export type {
131
+ // Primitives
132
+ OHLCV,
133
+ Bar,
134
+ Timeframe,
135
+ ChartOptions,
136
+ SeriesOptions,
137
+ SeriesType,
138
+ ChartTheme,
139
+ Viewport,
140
+ TimeRange,
141
+ PriceRange,
142
+ Point,
143
+ Rect,
144
+ Drawing,
145
+ DrawingType,
146
+ DrawingToolType,
147
+ PointerToolType,
148
+ DrawingPoint,
149
+ // Datafeed
150
+ IDatafeed,
151
+ MarketType,
152
+ SymbolInfo,
153
+ HistoricalBarsResult,
154
+ // Interaction
155
+ HitTestResult,
156
+ ChartKeyAction,
157
+ KeyModifiers,
158
+ // Public SDK API types
159
+ ChartInterval,
160
+ ChartConfig,
161
+ ChartLayout,
162
+ ChartEventMap,
163
+ IndicatorType,
164
+ IndicatorParams,
165
+ IndicatorConfig,
166
+ IndicatorInstance,
167
+ PriceField,
168
+ PaneDescriptor,
169
+ IPlugin,
170
+ IPublicChart,
171
+ // Reference Data Platform
172
+ RefMarket,
173
+ RefExchange,
174
+ RefSymbol,
175
+ SymbolSearchFilters,
176
+ SymbolSearchResult,
177
+ } from '@forgecharts/types';
178
+
179
+ // ─── Pointer tool descriptors & visual constants ──────────────────────────────
180
+ export { cursorTool, CURSOR_LABEL, CURSOR_STYLE }
181
+ from './api/drawing tools/pointers menu/cursor';
182
+ export { crosshairTool, CROSSHAIR_LABEL, CROSSHAIR_STYLE }
183
+ from './api/drawing tools/pointers menu/crosshair';
184
+ export { dotTool, DOT_LABEL, DOT_STYLE, DOT_RADIUS, DOT_COLOR }
185
+ from './api/drawing tools/pointers menu/dot';
186
+ export {
187
+ demonstrationTool,
188
+ DEMONSTRATION_LABEL, DEMONSTRATION_STYLE,
189
+ DEMONSTRATION_RADIUS, DEMONSTRATION_FILL, DEMONSTRATION_STROKE, DEMONSTRATION_COLOR,
190
+ } from './api/drawing tools/pointers menu/demonstration';
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @forgecharts/sdk/internal
3
+ *
4
+ * Internal SDK engine exports — for ForgeCharts package layer use only.
5
+ * Do NOT import from this path in host applications or external integrations.
6
+ * No semver stability guarantee applies to this entry point.
7
+ *
8
+ * Direct internal consumers:
9
+ * - @forgecharts/chart-components (canvas rendering layer)
10
+ * - ForgeCharts platform app layer (engine-level customization)
11
+ *
12
+ * Exports in this file are the raw canvas engine primitives that underpin
13
+ * TChart. Host applications should use TChart via createChart() instead.
14
+ */
15
+
16
+ // Re-export the full public API so this is a superset.
17
+ export * from './index';
18
+
19
+ // ─── Raw engine layer ─────────────────────────────────────────────────────────
20
+ // Direct chart/series/scale primitives beneath TChart.
21
+ // Use TChart.getChart() / TChart.getSeries() to access these from outside the SDK.
22
+ export { Chart } from './core/Chart';
23
+ export { Series } from './core/Series';
24
+ export { TimeScale } from './core/TimeScale';
25
+ export { PriceScale } from './core/PriceScale';
26
+ export { Crosshair } from './core/Crosshair';
27
+
28
+ // ─── GPU acceleration layer (PixiJS) ─────────────────────────────────────────
29
+ // Implementation details of the rendering pipeline.
30
+ export { PixiChart } from './pixi/PixiChart';
31
+ export { PixiLayerManager, DirtyFlags } from './pixi/PixiLayerManager';
32
+ export { LayerName } from './pixi/LayerName';
33
+
34
+ // ─── Interaction layer ────────────────────────────────────────────────────────
35
+ export { InteractionManager } from './core/InteractionManager';
36
+ export type { InteractionHandlers, InteractionOptions } from './core/InteractionManager';
37
+
38
+ // ─── Internal engine interfaces ───────────────────────────────────────────────
39
+ export type { IChart, IChartPlugin } from './types/IChart';
40
+ export type { ISeries } from './types/ISeries';
41
+ export type { IRenderer } from './types/IRenderer';