@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.
- package/package.json +50 -0
- package/src/__tests__/backwardCompatibility.test.ts +191 -0
- package/src/__tests__/candleInvariant.test.ts +500 -0
- package/src/__tests__/public-api-surface.ts +76 -0
- package/src/__tests__/timeframeBoundary.test.ts +583 -0
- package/src/api/DrawingManager.ts +188 -0
- package/src/api/EventBus.ts +53 -0
- package/src/api/IndicatorDAG.ts +389 -0
- package/src/api/IndicatorRegistry.ts +47 -0
- package/src/api/LayoutManager.ts +72 -0
- package/src/api/PaneManager.ts +129 -0
- package/src/api/ReferenceAPI.ts +195 -0
- package/src/api/TChart.ts +881 -0
- package/src/api/createChart.ts +43 -0
- package/src/api/drawing tools/fib gann menu/fibRetracement.ts +27 -0
- package/src/api/drawing tools/lines menu/crossLine.ts +21 -0
- package/src/api/drawing tools/lines menu/disjointChannel.ts +74 -0
- package/src/api/drawing tools/lines menu/extendedLine.ts +22 -0
- package/src/api/drawing tools/lines menu/flatTopBottom.ts +45 -0
- package/src/api/drawing tools/lines menu/horizontal.ts +24 -0
- package/src/api/drawing tools/lines menu/horizontalRay.ts +25 -0
- package/src/api/drawing tools/lines menu/infoLine.ts +127 -0
- package/src/api/drawing tools/lines menu/insidePitchfork.ts +21 -0
- package/src/api/drawing tools/lines menu/modifiedSchiffPitchfork.ts +18 -0
- package/src/api/drawing tools/lines menu/parallelChannel.ts +47 -0
- package/src/api/drawing tools/lines menu/pitchfork.ts +15 -0
- package/src/api/drawing tools/lines menu/ray.ts +28 -0
- package/src/api/drawing tools/lines menu/regressionTrend.ts +157 -0
- package/src/api/drawing tools/lines menu/schiffPitchfork.ts +18 -0
- package/src/api/drawing tools/lines menu/trendAngle.ts +64 -0
- package/src/api/drawing tools/lines menu/trendline.ts +16 -0
- package/src/api/drawing tools/lines menu/vertical.ts +16 -0
- package/src/api/drawing tools/pointers menu/crosshair.ts +17 -0
- package/src/api/drawing tools/pointers menu/cursor.ts +16 -0
- package/src/api/drawing tools/pointers menu/demonstration.ts +35 -0
- package/src/api/drawing tools/pointers menu/dot.ts +26 -0
- package/src/api/drawing tools/shapes menu/rectangle.ts +24 -0
- package/src/api/drawing tools/shapes menu/text.ts +30 -0
- package/src/api/drawingUtils.ts +82 -0
- package/src/core/CanvasLayer.ts +77 -0
- package/src/core/Chart.ts +917 -0
- package/src/core/CoordTransform.ts +282 -0
- package/src/core/Crosshair.ts +207 -0
- package/src/core/IndicatorEngine.ts +216 -0
- package/src/core/InteractionManager.ts +899 -0
- package/src/core/PriceScale.ts +133 -0
- package/src/core/Series.ts +132 -0
- package/src/core/TimeScale.ts +175 -0
- package/src/datafeed/DatafeedConnector.ts +300 -0
- package/src/engine/CandleEngine.ts +458 -0
- package/src/engine/__tests__/CandleEngine.test.ts +402 -0
- package/src/engine/candleInvariants.ts +172 -0
- package/src/engine/mergeUtils.ts +93 -0
- package/src/engine/timeframeUtils.ts +118 -0
- package/src/index.ts +190 -0
- package/src/internal.ts +41 -0
- package/src/licensing/ChartRuntimeResolver.ts +380 -0
- package/src/licensing/LicenseManager.ts +131 -0
- package/src/licensing/__tests__/ChartRuntimeResolver.test.ts +207 -0
- package/src/licensing/__tests__/LicenseManager.test.ts +180 -0
- package/src/licensing/licenseTypes.ts +19 -0
- package/src/pine/PineCompiler.ts +68 -0
- package/src/pine/diagnostics.ts +30 -0
- package/src/pine/index.ts +7 -0
- package/src/pine/pine-ast.ts +163 -0
- package/src/pine/pine-lexer.ts +265 -0
- package/src/pine/pine-parser.ts +439 -0
- package/src/pine/pine-transpiler.ts +301 -0
- package/src/pixi/LayerName.ts +35 -0
- package/src/pixi/PixiCandlestickRenderer.ts +125 -0
- package/src/pixi/PixiChart.ts +425 -0
- package/src/pixi/PixiCrosshairRenderer.ts +134 -0
- package/src/pixi/PixiDrawingRenderer.ts +121 -0
- package/src/pixi/PixiGridRenderer.ts +136 -0
- package/src/pixi/PixiLayerManager.ts +102 -0
- package/src/renderers/CandlestickRenderer.ts +130 -0
- package/src/renderers/HistogramRenderer.ts +63 -0
- package/src/renderers/LineRenderer.ts +77 -0
- package/src/theme/colors.ts +21 -0
- package/src/tools/barDivergenceCheck.ts +305 -0
- package/src/trading/TradingOverlayStore.ts +161 -0
- package/src/trading/UnmanagedIngestion.ts +156 -0
- package/src/trading/__tests__/ManagedTradingController.test.ts +338 -0
- package/src/trading/__tests__/TradingOverlayStore.test.ts +323 -0
- package/src/trading/__tests__/UnmanagedIngestion.test.ts +205 -0
- package/src/trading/managed/ManagedTradingController.ts +292 -0
- package/src/trading/managed/managedCapabilities.ts +98 -0
- package/src/trading/managed/managedTypes.ts +151 -0
- package/src/trading/tradingTypes.ts +135 -0
- package/src/tscript/TScriptIndicator.ts +54 -0
- package/src/tscript/ast.ts +105 -0
- package/src/tscript/lexer.ts +190 -0
- package/src/tscript/parser.ts +334 -0
- package/src/tscript/runtime.ts +525 -0
- package/src/tscript/series.ts +84 -0
- package/src/types/IChart.ts +56 -0
- package/src/types/IRenderer.ts +16 -0
- package/src/types/ISeries.ts +30 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +15 -0
- package/vitest.config.ts +25 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First-Divergence Bar Comparison Tool
|
|
3
|
+
*
|
|
4
|
+
* Compares OHLCV bars from multiple data sources (provider REST, database,
|
|
5
|
+
* aggregated from 1m, SDK engine) and reports the **first** mismatch found.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
*
|
|
9
|
+
* import { compareSources, checkAllSources, formatReport } from '@forgecharts/sdk/tools/barDivergenceCheck';
|
|
10
|
+
*
|
|
11
|
+
* const report = compareSources(
|
|
12
|
+
* { source: 'provider', bars: providerBars },
|
|
13
|
+
* { source: 'database', bars: databaseBars },
|
|
14
|
+
* '1h',
|
|
15
|
+
* 'BINANCE:BTCUSDT',
|
|
16
|
+
* from,
|
|
17
|
+
* to,
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* if (report.diverged) console.log(formatReport(report));
|
|
21
|
+
*
|
|
22
|
+
* All bar arrays are expected to contain Unix **second** timestamps in the
|
|
23
|
+
* `time` field (matching the CandleBar.time convention). Use `from`/`to`
|
|
24
|
+
* as Unix seconds to scope the comparison window.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { getBucketStart } from '@forgecharts/shared';
|
|
28
|
+
|
|
29
|
+
// ─── Public types ─────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
/** Identifies which pipeline stage produced a bar array. */
|
|
32
|
+
export type BarSource = 'provider' | 'database' | 'aggregated' | 'sdk';
|
|
33
|
+
|
|
34
|
+
/** Minimal OHLCV shape accepted by the divergence checker. */
|
|
35
|
+
export interface OhlcvBar {
|
|
36
|
+
/** Unix seconds (same as CandleBar.time). */
|
|
37
|
+
time: number;
|
|
38
|
+
open: number;
|
|
39
|
+
high: number;
|
|
40
|
+
low: number;
|
|
41
|
+
close: number;
|
|
42
|
+
volume?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A labelled set of bars from one pipeline source. */
|
|
46
|
+
export interface SourceBars {
|
|
47
|
+
source: BarSource;
|
|
48
|
+
bars: readonly OhlcvBar[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A single field-level difference between two bars at the same timestamp. */
|
|
52
|
+
export interface FieldDiff {
|
|
53
|
+
field: 'open' | 'high' | 'low' | 'close' | 'volume';
|
|
54
|
+
valueA: number;
|
|
55
|
+
valueB: number;
|
|
56
|
+
/** valueA − valueB */
|
|
57
|
+
delta: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Full description of a single mismatched candle. */
|
|
61
|
+
export interface BarMismatch {
|
|
62
|
+
/** Unix seconds — the candle open time where divergence was first detected. */
|
|
63
|
+
time: number;
|
|
64
|
+
/** Canonical candle-open boundary in milliseconds for the given timeframe. */
|
|
65
|
+
bucketStartMs: number;
|
|
66
|
+
sourceA: BarSource;
|
|
67
|
+
sourceB: BarSource;
|
|
68
|
+
/** OHLCV field deltas — empty when the only problem is a missing bar. */
|
|
69
|
+
differences: FieldDiff[];
|
|
70
|
+
/**
|
|
71
|
+
* True when the two bars carry the same `time` but their canonical
|
|
72
|
+
* `bucketStart` values differ — indicates a misaligned timestamp on one side.
|
|
73
|
+
*/
|
|
74
|
+
bucketMismatch: boolean;
|
|
75
|
+
/** Human-readable diagnostic notes. */
|
|
76
|
+
notes: string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Summary of a pairwise source comparison. */
|
|
80
|
+
export interface DivergenceReport {
|
|
81
|
+
symbol: string;
|
|
82
|
+
timeframe: string;
|
|
83
|
+
/** Unix seconds — inclusive window start. */
|
|
84
|
+
from: number;
|
|
85
|
+
/** Unix seconds — inclusive window end. */
|
|
86
|
+
to: number;
|
|
87
|
+
diverged: boolean;
|
|
88
|
+
/** The first mismatch found, or null when the sources are identical. */
|
|
89
|
+
firstMismatch: BarMismatch | null;
|
|
90
|
+
totalMismatches: number;
|
|
91
|
+
/** Number of bars from source A inside [from, to]. */
|
|
92
|
+
sourceACount: number;
|
|
93
|
+
/** Number of bars from source B inside [from, to]. */
|
|
94
|
+
sourceBCount: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
/** Index bars by their Unix-second open time for O(1) lookup. */
|
|
100
|
+
function indexByTime(bars: readonly OhlcvBar[]): Map<number, OhlcvBar> {
|
|
101
|
+
const map = new Map<number, OhlcvBar>();
|
|
102
|
+
for (const bar of bars) {
|
|
103
|
+
// Last-write-wins if the source has duplicate timestamps
|
|
104
|
+
map.set(bar.time, bar);
|
|
105
|
+
}
|
|
106
|
+
return map;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Compare two numeric values with a floating-point epsilon tolerance.
|
|
111
|
+
* Returns true when they differ by more than the tolerance.
|
|
112
|
+
*/
|
|
113
|
+
function differ(a: number, b: number, eps = 1e-9): boolean {
|
|
114
|
+
return Math.abs(a - b) > eps;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ─── Core comparison ──────────────────────────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Compare all timestamps from both sources inside [from, to] and return the
|
|
121
|
+
* first mismatch found, scanning oldest-first.
|
|
122
|
+
*
|
|
123
|
+
* Mismatch criteria (any one triggers a report):
|
|
124
|
+
* 1. Bar present in one source but absent in the other.
|
|
125
|
+
* 2. OHLCV field values differ by more than floating-point epsilon.
|
|
126
|
+
* 3. Canonical `getBucketStart` for the bar's time differs between sources
|
|
127
|
+
* (indicates one source has a misaligned timestamp).
|
|
128
|
+
*
|
|
129
|
+
* @param a Source A bars.
|
|
130
|
+
* @param b Source B bars.
|
|
131
|
+
* @param timeframe Canonical timeframe label (e.g. `'1h'`, `'1w'`).
|
|
132
|
+
* @param symbol Symbol string — stored in the report for identification.
|
|
133
|
+
* @param from Window start, Unix seconds (inclusive).
|
|
134
|
+
* @param to Window end, Unix seconds (inclusive).
|
|
135
|
+
*/
|
|
136
|
+
export function compareSources(
|
|
137
|
+
a: SourceBars,
|
|
138
|
+
b: SourceBars,
|
|
139
|
+
timeframe: string,
|
|
140
|
+
symbol: string,
|
|
141
|
+
from: number,
|
|
142
|
+
to: number,
|
|
143
|
+
): DivergenceReport {
|
|
144
|
+
const aIndex = indexByTime(a.bars);
|
|
145
|
+
const bIndex = indexByTime(b.bars);
|
|
146
|
+
|
|
147
|
+
// All timestamps seen in either source, filtered to the requested window
|
|
148
|
+
const allTimes = new Set([...aIndex.keys(), ...bIndex.keys()]);
|
|
149
|
+
const sortedTimes = Array.from(allTimes)
|
|
150
|
+
.filter(t => t >= from && t <= to)
|
|
151
|
+
.sort((x, y) => x - y);
|
|
152
|
+
|
|
153
|
+
let totalMismatches = 0;
|
|
154
|
+
let firstMismatch: BarMismatch | null = null;
|
|
155
|
+
|
|
156
|
+
const countA = a.bars.filter(bar => bar.time >= from && bar.time <= to).length;
|
|
157
|
+
const countB = b.bars.filter(bar => bar.time >= from && bar.time <= to).length;
|
|
158
|
+
|
|
159
|
+
for (const time of sortedTimes) {
|
|
160
|
+
const barA = aIndex.get(time);
|
|
161
|
+
const barB = bIndex.get(time);
|
|
162
|
+
|
|
163
|
+
// ── Missing bar ────────────────────────────────────────────────────────
|
|
164
|
+
if (!barA || !barB) {
|
|
165
|
+
totalMismatches++;
|
|
166
|
+
if (!firstMismatch) {
|
|
167
|
+
const notes: string[] = [];
|
|
168
|
+
if (!barA) notes.push(`${a.source} is missing bar at time=${time}`);
|
|
169
|
+
if (!barB) notes.push(`${b.source} is missing bar at time=${time}`);
|
|
170
|
+
firstMismatch = {
|
|
171
|
+
time,
|
|
172
|
+
bucketStartMs: getBucketStart(time * 1000, timeframe),
|
|
173
|
+
sourceA: a.source,
|
|
174
|
+
sourceB: b.source,
|
|
175
|
+
differences: [],
|
|
176
|
+
bucketMismatch: false,
|
|
177
|
+
notes,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ── OHLCV field comparison ─────────────────────────────────────────────
|
|
184
|
+
const diffs: FieldDiff[] = [];
|
|
185
|
+
const OHLCV_FIELDS: Array<'open' | 'high' | 'low' | 'close' | 'volume'> = [
|
|
186
|
+
'open', 'high', 'low', 'close', 'volume',
|
|
187
|
+
];
|
|
188
|
+
for (const field of OHLCV_FIELDS) {
|
|
189
|
+
const vA = barA[field] ?? 0;
|
|
190
|
+
const vB = barB[field] ?? 0;
|
|
191
|
+
if (differ(vA, vB)) {
|
|
192
|
+
diffs.push({ field, valueA: vA, valueB: vB, delta: vA - vB });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ── Bucket alignment comparison ────────────────────────────────────────
|
|
197
|
+
const bucketA = getBucketStart(barA.time * 1000, timeframe);
|
|
198
|
+
const bucketB = getBucketStart(barB.time * 1000, timeframe);
|
|
199
|
+
const bucketMismatch = bucketA !== bucketB;
|
|
200
|
+
|
|
201
|
+
const notes: string[] = [];
|
|
202
|
+
if (bucketMismatch) {
|
|
203
|
+
notes.push(
|
|
204
|
+
`bucket start mismatch: ${a.source}=${bucketA}ms vs ${b.source}=${bucketB}ms ` +
|
|
205
|
+
`(delta=${bucketA - bucketB} ms)`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
for (const diff of diffs) {
|
|
209
|
+
notes.push(
|
|
210
|
+
`${diff.field}: ${a.source}=${diff.valueA} vs ${b.source}=${diff.valueB} ` +
|
|
211
|
+
`(delta=${diff.delta.toPrecision(6)})`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (diffs.length > 0 || bucketMismatch) {
|
|
216
|
+
totalMismatches++;
|
|
217
|
+
if (!firstMismatch) {
|
|
218
|
+
firstMismatch = {
|
|
219
|
+
time,
|
|
220
|
+
bucketStartMs: bucketA,
|
|
221
|
+
sourceA: a.source,
|
|
222
|
+
sourceB: b.source,
|
|
223
|
+
differences: diffs,
|
|
224
|
+
bucketMismatch,
|
|
225
|
+
notes,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
symbol,
|
|
233
|
+
timeframe,
|
|
234
|
+
from,
|
|
235
|
+
to,
|
|
236
|
+
diverged: totalMismatches > 0,
|
|
237
|
+
firstMismatch,
|
|
238
|
+
totalMismatches,
|
|
239
|
+
sourceACount: countA,
|
|
240
|
+
sourceBCount: countB,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Run all pairwise comparisons across every combination of the provided sources.
|
|
246
|
+
*
|
|
247
|
+
* For N sources this produces N*(N-1)/2 reports. Use `formatReport` to render
|
|
248
|
+
* each one for console output.
|
|
249
|
+
*/
|
|
250
|
+
export function checkAllSources(
|
|
251
|
+
sources: SourceBars[],
|
|
252
|
+
symbol: string,
|
|
253
|
+
timeframe: string,
|
|
254
|
+
from: number,
|
|
255
|
+
to: number,
|
|
256
|
+
): DivergenceReport[] {
|
|
257
|
+
const reports: DivergenceReport[] = [];
|
|
258
|
+
for (let i = 0; i < sources.length; i++) {
|
|
259
|
+
for (let j = i + 1; j < sources.length; j++) {
|
|
260
|
+
reports.push(compareSources(sources[i]!, sources[j]!, timeframe, symbol, from, to));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return reports;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ─── Formatter ───────────────────────────────────────────────────────────────
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Render a `DivergenceReport` as a multi-line human-readable string suitable
|
|
270
|
+
* for console output or log files.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* const report = compareSources(a, b, '1h', 'BINANCE:BTCUSDT', from, to);
|
|
274
|
+
* console.log(formatReport(report));
|
|
275
|
+
*/
|
|
276
|
+
export function formatReport(report: DivergenceReport): string {
|
|
277
|
+
const lines: string[] = [];
|
|
278
|
+
|
|
279
|
+
const status = report.diverged
|
|
280
|
+
? `DIVERGED (${report.totalMismatches} mismatch${report.totalMismatches === 1 ? '' : 'es'})`
|
|
281
|
+
: 'IDENTICAL';
|
|
282
|
+
|
|
283
|
+
lines.push('─'.repeat(72));
|
|
284
|
+
lines.push(`Bar Divergence Report — ${status}`);
|
|
285
|
+
lines.push(` Symbol : ${report.symbol}`);
|
|
286
|
+
lines.push(` Timeframe : ${report.timeframe}`);
|
|
287
|
+
lines.push(` Window : ${report.from} → ${report.to} (Unix seconds)`);
|
|
288
|
+
lines.push(` Bars : sourceA=${report.sourceACount} sourceB=${report.sourceBCount}`);
|
|
289
|
+
|
|
290
|
+
if (report.firstMismatch) {
|
|
291
|
+
const m = report.firstMismatch;
|
|
292
|
+
lines.push('');
|
|
293
|
+
lines.push(` First mismatch at time=${m.time} (bucketStart=${m.bucketStartMs} ms)`);
|
|
294
|
+
lines.push(` Sources : ${m.sourceA} vs ${m.sourceB}`);
|
|
295
|
+
if (m.bucketMismatch) {
|
|
296
|
+
lines.push(` ✗ Bucket alignment differs`);
|
|
297
|
+
}
|
|
298
|
+
for (const note of m.notes) {
|
|
299
|
+
lines.push(` · ${note}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
lines.push('─'.repeat(72));
|
|
304
|
+
return lines.join('\n');
|
|
305
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TradingOverlayStore
|
|
3
|
+
*
|
|
4
|
+
* Shared, mode-agnostic store for trade overlay state.
|
|
5
|
+
* Used by both unmanaged mode (externally supplied data) and managed mode
|
|
6
|
+
* (internally generated data from the managed trading service).
|
|
7
|
+
*
|
|
8
|
+
* Supports:
|
|
9
|
+
* - Full replace (setOrders / setPositions)
|
|
10
|
+
* - Incremental (upsertOrder / upsertPosition / pushExecution)
|
|
11
|
+
* - Remove by id (removeOrder / removePosition)
|
|
12
|
+
*
|
|
13
|
+
* Subscribers are notified via callbacks registered with onChange().
|
|
14
|
+
* The store is intentionally free of React/DOM dependencies so it can be
|
|
15
|
+
* consumed from any rendering layer (canvas, React, etc.).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const store = new TradingOverlayStore();
|
|
20
|
+
*
|
|
21
|
+
* // Subscribe to changes (e.g. trigger a canvas repaint)
|
|
22
|
+
* store.onChange(() => chart.markDirty());
|
|
23
|
+
*
|
|
24
|
+
* // Feed data
|
|
25
|
+
* store.setOrders([...]);
|
|
26
|
+
* store.upsertOrder({ id: 'o1', role: 'entry', side: 'buy', ... });
|
|
27
|
+
* store.removeOrder('o1');
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import type { ChartOrder, ChartPosition, ExecutionFill } from './tradingTypes';
|
|
32
|
+
|
|
33
|
+
type ChangeListener = () => void;
|
|
34
|
+
|
|
35
|
+
export class TradingOverlayStore {
|
|
36
|
+
private _orders = new Map<string, ChartOrder>();
|
|
37
|
+
private _positions = new Map<string, ChartPosition>();
|
|
38
|
+
private _fills: ExecutionFill[] = [];
|
|
39
|
+
|
|
40
|
+
private _listeners: ChangeListener[] = [];
|
|
41
|
+
|
|
42
|
+
// ── Change subscription ──────────────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Register a callback that fires whenever any state changes.
|
|
46
|
+
* Returns an unsubscribe function.
|
|
47
|
+
*/
|
|
48
|
+
onChange(listener: ChangeListener): () => void {
|
|
49
|
+
this._listeners.push(listener);
|
|
50
|
+
return () => {
|
|
51
|
+
this._listeners = this._listeners.filter(l => l !== listener);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private _notify(): void {
|
|
56
|
+
for (const l of this._listeners) l();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ── Orders ───────────────────────────────────────────────────────────────────
|
|
60
|
+
|
|
61
|
+
/** Replace the entire order set. */
|
|
62
|
+
setOrders(orders: ChartOrder[]): void {
|
|
63
|
+
this._orders.clear();
|
|
64
|
+
for (const o of orders) this._orders.set(o.id, o);
|
|
65
|
+
this._notify();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Insert or replace a single order.
|
|
70
|
+
* Matching is done by `order.id`.
|
|
71
|
+
*/
|
|
72
|
+
upsertOrder(order: ChartOrder): void {
|
|
73
|
+
this._orders.set(order.id, order);
|
|
74
|
+
this._notify();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Remove an order by id. Silent no-op if not found. */
|
|
78
|
+
removeOrder(orderId: string): void {
|
|
79
|
+
if (this._orders.delete(orderId)) this._notify();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Returns a snapshot of all orders as an array (insertion order). */
|
|
83
|
+
getOrders(): ChartOrder[] {
|
|
84
|
+
return Array.from(this._orders.values());
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Returns orders that share the given `groupId`. */
|
|
88
|
+
getOrdersByGroup(groupId: string): ChartOrder[] {
|
|
89
|
+
return this.getOrders().filter(o => o.groupId === groupId);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ── Positions ────────────────────────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
/** Replace the entire position set. */
|
|
95
|
+
setPositions(positions: ChartPosition[]): void {
|
|
96
|
+
this._positions.clear();
|
|
97
|
+
for (const p of positions) this._positions.set(p.id, p);
|
|
98
|
+
this._notify();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Insert or replace a single position.
|
|
103
|
+
* Matching is done by `position.id`.
|
|
104
|
+
*/
|
|
105
|
+
upsertPosition(position: ChartPosition): void {
|
|
106
|
+
this._positions.set(position.id, position);
|
|
107
|
+
this._notify();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Remove a position by id. Silent no-op if not found. */
|
|
111
|
+
removePosition(positionId: string): void {
|
|
112
|
+
if (this._positions.delete(positionId)) this._notify();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Returns a snapshot of all positions as an array (insertion order). */
|
|
116
|
+
getPositions(): ChartPosition[] {
|
|
117
|
+
return Array.from(this._positions.values());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ── Fills ────────────────────────────────────────────────────────────────────
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Append an execution fill to the fill log.
|
|
124
|
+
* Fills are append-only — they are never updated or removed.
|
|
125
|
+
*/
|
|
126
|
+
pushExecution(fill: ExecutionFill): void {
|
|
127
|
+
this._fills.push(fill);
|
|
128
|
+
this._notify();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Returns all fills in insertion (chronological) order. */
|
|
132
|
+
getFills(): readonly ExecutionFill[] {
|
|
133
|
+
return this._fills;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Returns fills associated with a specific order id. */
|
|
137
|
+
getFillsByOrder(orderId: string): ExecutionFill[] {
|
|
138
|
+
return this._fills.filter(f => f.orderId === orderId);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ── Bulk clear ───────────────────────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
/** Wipes all overlay state (orders, positions, fills). */
|
|
144
|
+
clear(): void {
|
|
145
|
+
this._orders.clear();
|
|
146
|
+
this._positions.clear();
|
|
147
|
+
this._fills = [];
|
|
148
|
+
this._notify();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ── Snapshot ─────────────────────────────────────────────────────────────────
|
|
152
|
+
|
|
153
|
+
/** Returns the full overlay state as a plain object — useful for debug / serialisation. */
|
|
154
|
+
snapshot(): { orders: ChartOrder[]; positions: ChartPosition[]; fills: readonly ExecutionFill[] } {
|
|
155
|
+
return {
|
|
156
|
+
orders: this.getOrders(),
|
|
157
|
+
positions: this.getPositions(),
|
|
158
|
+
fills: this.getFills(),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UnmanagedIngestion
|
|
3
|
+
*
|
|
4
|
+
* External candle data ingestion for unmanaged mode.
|
|
5
|
+
*
|
|
6
|
+
* In unmanaged mode the client owns their data pipeline. They pass
|
|
7
|
+
* normalised candle data to us; this layer validates the license gate,
|
|
8
|
+
* delegates to the existing CandleEngine for bucket management, and
|
|
9
|
+
* notifies callers via callbacks mirroring the CandleEngine contract.
|
|
10
|
+
*
|
|
11
|
+
* Critical constraint: the default RapidAPI-backed datafeed is completely
|
|
12
|
+
* untouched. UnmanagedIngestion is an additive, opt-in path.
|
|
13
|
+
*
|
|
14
|
+
* Candle rules (matches CandleEngine behaviour):
|
|
15
|
+
* same timeMs → update current candle
|
|
16
|
+
* newer timeMs → append / new bucket
|
|
17
|
+
* older timeMs → silently ignored (logged in debug mode)
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const ingestion = new UnmanagedIngestion('1m', {
|
|
22
|
+
* onBarUpdated: (bar) => series.update(bar),
|
|
23
|
+
* onResync: (bars) => series.setData(bars),
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Feed a full history snapshot first
|
|
27
|
+
* ingestion.setHistory(historicalBars);
|
|
28
|
+
*
|
|
29
|
+
* // Then stream live ticks
|
|
30
|
+
* ingestion.pushCandle(latestBar);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { CandleEngine } from '../engine/CandleEngine';
|
|
35
|
+
import type { CandleBar, UpdateResult, GapInfo } from '../engine/CandleEngine';
|
|
36
|
+
import { isUnmanagedMode } from '../licensing/ChartRuntimeResolver';
|
|
37
|
+
import type { CandleInput } from './tradingTypes';
|
|
38
|
+
import type { Timeframe } from '@forgecharts/types';
|
|
39
|
+
|
|
40
|
+
export interface UnmanagedIngestionOptions {
|
|
41
|
+
/** Enable debug logging on the underlying CandleEngine. */
|
|
42
|
+
debug?: boolean;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Fired after every bar mutation (update or append from a live push).
|
|
46
|
+
* Corresponds to CandleEngine.onBarUpdated.
|
|
47
|
+
*/
|
|
48
|
+
onBarUpdated?: (bar: CandleBar, result: UpdateResult) => void;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Fired when the full bar array changes (after setHistory or gap backfill).
|
|
52
|
+
* The chart adapter should call `series.setData(bars)`.
|
|
53
|
+
*/
|
|
54
|
+
onResync?: (bars: readonly CandleBar[]) => void;
|
|
55
|
+
|
|
56
|
+
/** Fired when a candle bucket closes. */
|
|
57
|
+
onBarClosed?: (bar: CandleBar) => void;
|
|
58
|
+
|
|
59
|
+
/** Fired when the engine detects skipped buckets in the live stream. */
|
|
60
|
+
onGapDetected?: (info: GapInfo) => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class UnmanagedIngestion {
|
|
64
|
+
private readonly _engine: CandleEngine;
|
|
65
|
+
private readonly _opts: UnmanagedIngestionOptions;
|
|
66
|
+
private _timeframe: Timeframe;
|
|
67
|
+
|
|
68
|
+
constructor(timeframe: Timeframe, opts: UnmanagedIngestionOptions = {}) {
|
|
69
|
+
this._opts = opts;
|
|
70
|
+
this._timeframe = timeframe;
|
|
71
|
+
this._engine = new CandleEngine({
|
|
72
|
+
...(opts.debug !== undefined ? { debug: opts.debug } : {}),
|
|
73
|
+
...(opts.onBarUpdated !== undefined ? { onBarUpdated: opts.onBarUpdated } : {}),
|
|
74
|
+
...(opts.onBarClosed !== undefined ? { onBarClosed: opts.onBarClosed } : {}),
|
|
75
|
+
...(opts.onGapDetected !== undefined ? { onGapDetected: opts.onGapDetected } : {}),
|
|
76
|
+
...(opts.onResync !== undefined ? { onResync: opts.onResync } : {}),
|
|
77
|
+
});
|
|
78
|
+
// Prime the engine so it knows its timeframe before any data arrives
|
|
79
|
+
this._engine.initialize([], timeframe);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ── Guard ────────────────────────────────────────────────────────────────────
|
|
83
|
+
|
|
84
|
+
private _assertUnmanaged(): void {
|
|
85
|
+
if (!isUnmanagedMode()) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
'[ForgeCharts] External data ingestion is only available in unmanaged mode. ' +
|
|
88
|
+
'The active license is in managed mode.',
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ── History ──────────────────────────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Load a full historical bar snapshot.
|
|
97
|
+
* Replaces the engine's bar array and triggers `onResync` so the chart
|
|
98
|
+
* series is fully redrawn.
|
|
99
|
+
*
|
|
100
|
+
* Call this once at startup (or on symbol/interval change) before
|
|
101
|
+
* streaming live candles via `pushCandle`.
|
|
102
|
+
*
|
|
103
|
+
* @throws If the active license is not in unmanaged mode.
|
|
104
|
+
*/
|
|
105
|
+
setHistory(bars: CandleInput[]): void {
|
|
106
|
+
this._assertUnmanaged();
|
|
107
|
+
|
|
108
|
+
// Sort ascending — initialize expects oldest → newest
|
|
109
|
+
const sorted = [...bars].sort((a, b) => a.time - b.time);
|
|
110
|
+
this._engine.initialize(sorted, this._timeframe);
|
|
111
|
+
// CandleEngine.initialize() does not fire onResync — invoke it explicitly
|
|
112
|
+
this._opts.onResync?.(this._engine.getBars());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ── Live push ────────────────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Push a single live candle tick.
|
|
119
|
+
*
|
|
120
|
+
* Behaviour follows CandleEngine rules:
|
|
121
|
+
* - same bucket → updates existing bar (OHLCV merging)
|
|
122
|
+
* - new bucket → appends a new bar (previous bar is finalised)
|
|
123
|
+
* - older bucket → silently ignored; debug-logged when `debug: true`
|
|
124
|
+
*
|
|
125
|
+
* @throws If the active license is not in unmanaged mode.
|
|
126
|
+
*/
|
|
127
|
+
pushCandle(bar: CandleInput): UpdateResult {
|
|
128
|
+
this._assertUnmanaged();
|
|
129
|
+
return this._engine.applyLiveUpdate(bar);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── Accessors ────────────────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
/** Returns the current bar array (chronological, ascending). */
|
|
135
|
+
getBars(): readonly CandleBar[] {
|
|
136
|
+
return this._engine.getBars();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Returns the latest candle, or null if no data has been loaded. */
|
|
140
|
+
getLatestBar(): CandleBar | null {
|
|
141
|
+
const bars = this._engine.getBars();
|
|
142
|
+
return bars.length > 0 ? bars[bars.length - 1]! : null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Changes the active timeframe and clears all bar data. */
|
|
146
|
+
setTimeframe(timeframe: Timeframe): void {
|
|
147
|
+
this._timeframe = timeframe;
|
|
148
|
+
// Re-initialise with empty history — caller must follow up with setHistory()
|
|
149
|
+
this._engine.initialize([], timeframe);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Clears all bars (e.g. on symbol change before calling setHistory again). */
|
|
153
|
+
reset(): void {
|
|
154
|
+
this._engine.reset();
|
|
155
|
+
}
|
|
156
|
+
}
|