@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,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UnmanagedIngestion — unit tests
|
|
3
|
+
*
|
|
4
|
+
* Covers:
|
|
5
|
+
* - setHistory loads bars in ascending order
|
|
6
|
+
* - pushCandle appends a new bar
|
|
7
|
+
* - pushCandle updates the current bar when same bucket
|
|
8
|
+
* - getLatestBar returns correct data
|
|
9
|
+
* - getBars() returns the full bar array
|
|
10
|
+
* - setTimeframe clears bars and resets engine
|
|
11
|
+
* - reset() wipes all bars
|
|
12
|
+
* - Throws when called in managed mode
|
|
13
|
+
* - onBarUpdated / onResync callbacks fire correctly
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
17
|
+
import { LicenseManager } from '../../licensing/LicenseManager';
|
|
18
|
+
import { UnmanagedIngestion } from '../UnmanagedIngestion';
|
|
19
|
+
import type { CandleInput } from '../tradingTypes';
|
|
20
|
+
import type { CandleBar, UpdateResult } from '../../engine/CandleEngine';
|
|
21
|
+
|
|
22
|
+
// ─── License helpers ──────────────────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
function installUnmanaged() {
|
|
25
|
+
LicenseManager.getInstance().loadLicense({ licenseKey: 'u', mode: 'unmanaged' });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function installManaged() {
|
|
29
|
+
LicenseManager.getInstance().loadLicense({ licenseKey: 'm', mode: 'managed' });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function clearLicense() {
|
|
33
|
+
LicenseManager.getInstance().clear();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ─── Bar builders ─────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
// 2024-01-02 00:00:00 UTC in seconds
|
|
39
|
+
const BASE_SEC = 1_704_153_600;
|
|
40
|
+
const MIN = 60; // seconds per minute
|
|
41
|
+
|
|
42
|
+
function bar(offsetMinutes: number, close = 100, extra?: Partial<CandleInput>): CandleInput {
|
|
43
|
+
return {
|
|
44
|
+
time: BASE_SEC + offsetMinutes * MIN,
|
|
45
|
+
open: 100,
|
|
46
|
+
high: 105,
|
|
47
|
+
low: 95,
|
|
48
|
+
close,
|
|
49
|
+
...extra,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ─── Mode gate ────────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
describe('UnmanagedIngestion — mode gate', () => {
|
|
56
|
+
afterEach(clearLicense);
|
|
57
|
+
|
|
58
|
+
it('setHistory() throws in managed mode', () => {
|
|
59
|
+
installManaged();
|
|
60
|
+
const ing = new UnmanagedIngestion('1m');
|
|
61
|
+
expect(() => ing.setHistory([bar(0)])).toThrow(/unmanaged/i);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('pushCandle() throws in managed mode', () => {
|
|
65
|
+
installManaged();
|
|
66
|
+
const ing = new UnmanagedIngestion('1m');
|
|
67
|
+
expect(() => ing.pushCandle(bar(0))).toThrow(/unmanaged/i);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('setHistory() works when no license is installed (defaults to unmanaged)', () => {
|
|
71
|
+
clearLicense();
|
|
72
|
+
const ing = new UnmanagedIngestion('1m');
|
|
73
|
+
expect(() => ing.setHistory([bar(0)])).not.toThrow();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('pushCandle() works when no license is installed', () => {
|
|
77
|
+
clearLicense();
|
|
78
|
+
const ing = new UnmanagedIngestion('1m');
|
|
79
|
+
expect(() => ing.pushCandle(bar(0))).not.toThrow();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// ─── setHistory ───────────────────────────────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
describe('UnmanagedIngestion — setHistory', () => {
|
|
86
|
+
beforeEach(installUnmanaged);
|
|
87
|
+
afterEach(clearLicense);
|
|
88
|
+
|
|
89
|
+
it('loads bars and getBars() returns them', () => {
|
|
90
|
+
const ing = new UnmanagedIngestion('1m');
|
|
91
|
+
ing.setHistory([bar(0), bar(1), bar(2)]);
|
|
92
|
+
expect(ing.getBars()).toHaveLength(3);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('sorts bars ascending by time (even if fed out of order)', () => {
|
|
96
|
+
const ing = new UnmanagedIngestion('1m');
|
|
97
|
+
ing.setHistory([bar(2), bar(0), bar(1)]);
|
|
98
|
+
const times = ing.getBars().map(b => b.time);
|
|
99
|
+
expect(times).toEqual([...times].sort((a, b) => a - b));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('replaces previous history on second call', () => {
|
|
103
|
+
const ing = new UnmanagedIngestion('1m');
|
|
104
|
+
ing.setHistory([bar(0), bar(1), bar(2)]);
|
|
105
|
+
ing.setHistory([bar(5), bar(6)]);
|
|
106
|
+
expect(ing.getBars()).toHaveLength(2);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('getLatestBar() returns the newest bar after loading', () => {
|
|
110
|
+
const ing = new UnmanagedIngestion('1m');
|
|
111
|
+
ing.setHistory([bar(0, 100), bar(1, 200), bar(2, 300)]);
|
|
112
|
+
expect(ing.getLatestBar()!.close).toBe(300);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('fires onResync with the loaded bars', () => {
|
|
116
|
+
const onResync = vi.fn<(bars: readonly CandleBar[]) => void>();
|
|
117
|
+
const ing = new UnmanagedIngestion('1m', { onResync });
|
|
118
|
+
ing.setHistory([bar(0), bar(1)]);
|
|
119
|
+
expect(onResync).toHaveBeenCalledTimes(1);
|
|
120
|
+
const passed = onResync.mock.calls[0]![0];
|
|
121
|
+
expect(passed).toHaveLength(2);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// ─── pushCandle ───────────────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
describe('UnmanagedIngestion — pushCandle', () => {
|
|
128
|
+
beforeEach(installUnmanaged);
|
|
129
|
+
afterEach(clearLicense);
|
|
130
|
+
|
|
131
|
+
it('appends a new bar when pushing beyond current candle', () => {
|
|
132
|
+
const ing = new UnmanagedIngestion('1m');
|
|
133
|
+
ing.setHistory([bar(0), bar(1)]);
|
|
134
|
+
const result = ing.pushCandle(bar(2));
|
|
135
|
+
expect(result.type).toBe('append');
|
|
136
|
+
expect(ing.getBars()).toHaveLength(3);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('updates the current bar when pushing same bucket', () => {
|
|
140
|
+
const ing = new UnmanagedIngestion('1m');
|
|
141
|
+
ing.setHistory([bar(0)]);
|
|
142
|
+
ing.pushCandle(bar(0, 110)); // same timestamp, new close
|
|
143
|
+
expect(ing.getBars()).toHaveLength(1);
|
|
144
|
+
expect(ing.getLatestBar()!.close).toBe(110);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('high is never lowered within a candle', () => {
|
|
148
|
+
const ing = new UnmanagedIngestion('1m');
|
|
149
|
+
ing.pushCandle(bar(0, 100, { high: 120 }));
|
|
150
|
+
ing.pushCandle(bar(0, 90, { high: 80 })); // high 80 < 120
|
|
151
|
+
expect(ing.getLatestBar()!.high).toBe(120);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('low is never raised within a candle', () => {
|
|
155
|
+
const ing = new UnmanagedIngestion('1m');
|
|
156
|
+
ing.pushCandle(bar(0, 100, { low: 80 }));
|
|
157
|
+
ing.pushCandle(bar(0, 95, { low: 90 })); // low 90 > 80
|
|
158
|
+
expect(ing.getLatestBar()!.low).toBe(80);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('fires onBarUpdated for every push', () => {
|
|
162
|
+
const onBarUpdated = vi.fn<(bar: CandleBar, result: UpdateResult) => void>();
|
|
163
|
+
const ing = new UnmanagedIngestion('1m', { onBarUpdated });
|
|
164
|
+
ing.pushCandle(bar(0));
|
|
165
|
+
ing.pushCandle(bar(0, 110));
|
|
166
|
+
ing.pushCandle(bar(1));
|
|
167
|
+
expect(onBarUpdated).toHaveBeenCalledTimes(3);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('getLatestBar() returns null before any data', () => {
|
|
171
|
+
const ing = new UnmanagedIngestion('1m');
|
|
172
|
+
expect(ing.getLatestBar()).toBeNull();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// ─── setTimeframe & reset ─────────────────────────────────────────────────────
|
|
177
|
+
|
|
178
|
+
describe('UnmanagedIngestion — setTimeframe and reset', () => {
|
|
179
|
+
beforeEach(installUnmanaged);
|
|
180
|
+
afterEach(clearLicense);
|
|
181
|
+
|
|
182
|
+
it('setTimeframe clears all existing bars', () => {
|
|
183
|
+
const ing = new UnmanagedIngestion('1m');
|
|
184
|
+
ing.setHistory([bar(0), bar(1), bar(2)]);
|
|
185
|
+
ing.setTimeframe('5m');
|
|
186
|
+
expect(ing.getBars()).toHaveLength(0);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('reset() clears all bars', () => {
|
|
190
|
+
const ing = new UnmanagedIngestion('1m');
|
|
191
|
+
ing.setHistory([bar(0), bar(1)]);
|
|
192
|
+
ing.reset();
|
|
193
|
+
expect(ing.getBars()).toHaveLength(0);
|
|
194
|
+
expect(ing.getLatestBar()).toBeNull();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('new history can be loaded after setTimeframe', () => {
|
|
198
|
+
const ing = new UnmanagedIngestion('1m');
|
|
199
|
+
ing.setHistory([bar(0), bar(1)]);
|
|
200
|
+
ing.setTimeframe('5m');
|
|
201
|
+
// 5-minute bars (offset in multiples of 5)
|
|
202
|
+
ing.setHistory([bar(0), bar(5)]);
|
|
203
|
+
expect(ing.getBars()).toHaveLength(2);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ManagedTradingController
|
|
3
|
+
*
|
|
4
|
+
* The single managed-mode entry point for order routing.
|
|
5
|
+
*
|
|
6
|
+
* Responsibilities:
|
|
7
|
+
* 1. Enforce license/capability gates before any action.
|
|
8
|
+
* 2. Delegate execution to the registered IExecutionProvider.
|
|
9
|
+
* 3. Write results (orders, fills, positions) into the shared TradingOverlayStore
|
|
10
|
+
* so managed and unmanaged modes share one visual layer.
|
|
11
|
+
*
|
|
12
|
+
* STUB STATUS
|
|
13
|
+
* ──────────────────────────────────────────────────────────────────────────────
|
|
14
|
+
* All execution methods are implemented as stubs that:
|
|
15
|
+
* a) enforce license gates (will throw today if called without managed mode)
|
|
16
|
+
* b) throw NotImplementedError when no provider is registered
|
|
17
|
+
* c) are annotated with TODO markers for the implementation phase
|
|
18
|
+
*
|
|
19
|
+
* To implement a real provider, create a class implementing IExecutionProvider
|
|
20
|
+
* and call:
|
|
21
|
+
* controller.registerProvider(new MyBrokerProvider(...));
|
|
22
|
+
*
|
|
23
|
+
* The controller will then delegate to it automatically.
|
|
24
|
+
* ──────────────────────────────────────────────────────────────────────────────
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { TradingOverlayStore } from '../TradingOverlayStore';
|
|
28
|
+
import type { ChartOrder, ExecutionFill } from '../tradingTypes';
|
|
29
|
+
import type {
|
|
30
|
+
IExecutionProvider,
|
|
31
|
+
PlaceOrderInput,
|
|
32
|
+
ModifyOrderInput,
|
|
33
|
+
PlaceBracketOrderInput,
|
|
34
|
+
OrderAck,
|
|
35
|
+
BracketOrderAck,
|
|
36
|
+
} from './managedTypes';
|
|
37
|
+
import {
|
|
38
|
+
assertCanPlaceOrders,
|
|
39
|
+
assertCanUseBrackets,
|
|
40
|
+
assertCanUseManagedTrading,
|
|
41
|
+
} from './managedCapabilities';
|
|
42
|
+
|
|
43
|
+
// ── Internal helpers ──────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
function notImplemented(method: string): never {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`[ForgeCharts:ManagedTrading] ${method} — no execution provider is registered. ` +
|
|
48
|
+
'Call controller.registerProvider(provider) before using managed trading methods.',
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function makeId(): string {
|
|
53
|
+
return `order_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ── Controller ────────────────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
export class ManagedTradingController {
|
|
59
|
+
|
|
60
|
+
private _provider: IExecutionProvider | null = null;
|
|
61
|
+
private readonly _store: TradingOverlayStore;
|
|
62
|
+
|
|
63
|
+
constructor(store: TradingOverlayStore) {
|
|
64
|
+
this._store = store;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ── Provider registration ─────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Register the execution provider that will handle order routing.
|
|
71
|
+
*
|
|
72
|
+
* Call this before any order placement methods. Only one provider may be
|
|
73
|
+
* active at a time — calling this again replaces the previous provider.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { RithmicProvider } from '@forgecharts/rithmic';
|
|
78
|
+
* controller.registerProvider(new RithmicProvider(credentials));
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
registerProvider(provider: IExecutionProvider): void {
|
|
82
|
+
assertCanUseManagedTrading();
|
|
83
|
+
this._provider = provider;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Returns the id of the currently registered provider, or null if none. */
|
|
87
|
+
getProviderId(): string | null {
|
|
88
|
+
return this._provider?.providerId ?? null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** True when a provider is registered and managed trading is licensed. */
|
|
92
|
+
isReady(): boolean {
|
|
93
|
+
return this._provider !== null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── Order placement ───────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Place a single order via the registered execution provider.
|
|
100
|
+
*
|
|
101
|
+
* On success:
|
|
102
|
+
* - Upserts a ChartOrder into the overlay store (status: 'pending' → 'open').
|
|
103
|
+
* - Returns the provider acknowledgement.
|
|
104
|
+
*
|
|
105
|
+
* @throws If not in managed mode, orderEntry feature is off, or no provider.
|
|
106
|
+
*
|
|
107
|
+
* TODO: After provider.placeOrder resolves, subscribe to order status updates
|
|
108
|
+
* (provider event stream) and call _store.upsertOrder() as status changes.
|
|
109
|
+
*/
|
|
110
|
+
async placeOrder(input: PlaceOrderInput): Promise<OrderAck> {
|
|
111
|
+
assertCanPlaceOrders();
|
|
112
|
+
if (!this._provider) notImplemented('placeOrder');
|
|
113
|
+
|
|
114
|
+
// Create an optimistic overlay entry immediately
|
|
115
|
+
const clientId = makeId();
|
|
116
|
+
const overlayOrder: ChartOrder = {
|
|
117
|
+
id: clientId,
|
|
118
|
+
role: input.type === 'stop_market' || input.type === 'stop_limit' ? 'stop' : 'entry',
|
|
119
|
+
side: input.side,
|
|
120
|
+
status: 'pending',
|
|
121
|
+
price: input.limitPrice ?? input.stopPrice ?? 0,
|
|
122
|
+
qty: input.qty,
|
|
123
|
+
timestamp: Date.now(),
|
|
124
|
+
...(input.groupId !== undefined ? { groupId: input.groupId } : {}),
|
|
125
|
+
...(input.label !== undefined ? { label: input.label } : {}),
|
|
126
|
+
...(input.meta !== undefined ? { meta: input.meta } : {}),
|
|
127
|
+
};
|
|
128
|
+
this._store.upsertOrder(overlayOrder);
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
// TODO: provider.placeOrder should also start streaming status updates
|
|
132
|
+
const ack = await this._provider.placeOrder(input);
|
|
133
|
+
|
|
134
|
+
// Update overlay with provider-assigned id
|
|
135
|
+
this._store.upsertOrder({ ...overlayOrder, id: ack.orderId, status: 'open' });
|
|
136
|
+
// Remove the optimistic entry now the real id is known
|
|
137
|
+
if (ack.orderId !== clientId) this._store.removeOrder(clientId);
|
|
138
|
+
|
|
139
|
+
return ack;
|
|
140
|
+
} catch (err) {
|
|
141
|
+
// Roll back optimistic entry on placement failure
|
|
142
|
+
this._store.upsertOrder({ ...overlayOrder, status: 'rejected' });
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Cancel a working order by its provider-assigned id.
|
|
149
|
+
*
|
|
150
|
+
* On success:
|
|
151
|
+
* - Updates the order status in the overlay store to 'cancelled'.
|
|
152
|
+
*
|
|
153
|
+
* @throws If not in managed mode, orderEntry feature is off, or no provider.
|
|
154
|
+
*
|
|
155
|
+
* TODO: Remove the overlay entry after a configurable delay (so the line
|
|
156
|
+
* visually fades out rather than disappearing instantly).
|
|
157
|
+
*/
|
|
158
|
+
async cancelOrder(orderId: string): Promise<void> {
|
|
159
|
+
assertCanPlaceOrders();
|
|
160
|
+
if (!this._provider) notImplemented('cancelOrder');
|
|
161
|
+
|
|
162
|
+
await this._provider.cancelOrder(orderId);
|
|
163
|
+
|
|
164
|
+
// Reflect cancellation in overlay
|
|
165
|
+
const existing = this._store.getOrders().find(o => o.id === orderId);
|
|
166
|
+
if (existing) {
|
|
167
|
+
this._store.upsertOrder({ ...existing, status: 'cancelled' });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Modify a working order in place.
|
|
173
|
+
*
|
|
174
|
+
* On success:
|
|
175
|
+
* - Updates the ChartOrder price/qty in the overlay store.
|
|
176
|
+
*
|
|
177
|
+
* @throws If not in managed mode, orderEntry feature is off, or no provider.
|
|
178
|
+
*
|
|
179
|
+
* TODO: Validate that the modified price is within instrument tick constraints
|
|
180
|
+
* before forwarding to the provider.
|
|
181
|
+
*/
|
|
182
|
+
async modifyOrder(orderId: string, updates: ModifyOrderInput): Promise<OrderAck> {
|
|
183
|
+
assertCanPlaceOrders();
|
|
184
|
+
if (!this._provider) notImplemented('modifyOrder');
|
|
185
|
+
|
|
186
|
+
const ack = await this._provider.modifyOrder(orderId, updates);
|
|
187
|
+
|
|
188
|
+
// Reflect modifications in overlay
|
|
189
|
+
const existing = this._store.getOrders().find(o => o.id === orderId);
|
|
190
|
+
if (existing) {
|
|
191
|
+
this._store.upsertOrder({
|
|
192
|
+
...existing,
|
|
193
|
+
...(updates.limitPrice !== undefined ? { price: updates.limitPrice } : {}),
|
|
194
|
+
...(updates.qty !== undefined ? { qty: updates.qty } : {}),
|
|
195
|
+
...(updates.label !== undefined ? { label: updates.label } : {}),
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return ack;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Place a bracket order: entry + stop-loss + optional take-profit.
|
|
204
|
+
*
|
|
205
|
+
* On success:
|
|
206
|
+
* - Upserts all bracket legs as ChartOrders sharing the same groupId.
|
|
207
|
+
* - Returns acknowledgements for each leg.
|
|
208
|
+
*
|
|
209
|
+
* @throws If not in managed mode, bracketOrders feature is off, or no provider.
|
|
210
|
+
*
|
|
211
|
+
* TODO: Expose drag handles for each bracket leg in the chart overlay so
|
|
212
|
+
* users can adjust prices before confirming.
|
|
213
|
+
*/
|
|
214
|
+
async placeBracketOrder(input: PlaceBracketOrderInput): Promise<BracketOrderAck> {
|
|
215
|
+
assertCanUseBrackets();
|
|
216
|
+
if (!this._provider) notImplemented('placeBracketOrder');
|
|
217
|
+
|
|
218
|
+
const groupId = input.entry.groupId ?? makeId();
|
|
219
|
+
|
|
220
|
+
const ack = await this._provider.placeBracketOrder({
|
|
221
|
+
...input,
|
|
222
|
+
entry: { ...input.entry, groupId },
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// Upsert all bracket legs into the overlay store
|
|
226
|
+
this._store.upsertOrder({
|
|
227
|
+
id: ack.entry.orderId,
|
|
228
|
+
role: 'entry',
|
|
229
|
+
side: input.entry.side,
|
|
230
|
+
status: 'open',
|
|
231
|
+
price: input.entry.limitPrice ?? input.entry.stopPrice ?? 0,
|
|
232
|
+
qty: input.entry.qty,
|
|
233
|
+
groupId,
|
|
234
|
+
label: input.entry.label ?? 'Entry',
|
|
235
|
+
timestamp: ack.entry.timestamp,
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
this._store.upsertOrder({
|
|
239
|
+
id: ack.stopLoss.orderId,
|
|
240
|
+
role: 'stop_loss',
|
|
241
|
+
side: input.entry.side === 'buy' ? 'sell' : 'buy',
|
|
242
|
+
status: 'open',
|
|
243
|
+
price: input.stopLossPrice,
|
|
244
|
+
qty: input.entry.qty,
|
|
245
|
+
groupId,
|
|
246
|
+
label: 'Stop Loss',
|
|
247
|
+
timestamp: ack.stopLoss.timestamp,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
if (ack.takeProfit && input.takeProfitPrice !== undefined) {
|
|
251
|
+
this._store.upsertOrder({
|
|
252
|
+
id: ack.takeProfit.orderId,
|
|
253
|
+
role: 'take_profit',
|
|
254
|
+
side: input.entry.side === 'buy' ? 'sell' : 'buy',
|
|
255
|
+
status: 'open',
|
|
256
|
+
price: input.takeProfitPrice,
|
|
257
|
+
qty: input.entry.qty,
|
|
258
|
+
groupId,
|
|
259
|
+
label: 'Take Profit',
|
|
260
|
+
timestamp: ack.takeProfit.timestamp,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return ack;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ── Fill reporting ─────────────────────────────────────────────────────────
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Report an execution fill received from the provider's event stream.
|
|
271
|
+
*
|
|
272
|
+
* Writes the fill into the overlay store and updates the parent order status.
|
|
273
|
+
* Called by the provider adapter whenever a fill notification arrives.
|
|
274
|
+
*
|
|
275
|
+
* TODO: Also update the related ChartPosition (create new or update qty/avgPrice).
|
|
276
|
+
*/
|
|
277
|
+
reportFill(fill: ExecutionFill): void {
|
|
278
|
+
assertCanUseManagedTrading();
|
|
279
|
+
this._store.pushExecution(fill);
|
|
280
|
+
|
|
281
|
+
// Update parent order status if fill is total
|
|
282
|
+
if (fill.orderId) {
|
|
283
|
+
const order = this._store.getOrders().find(o => o.id === fill.orderId);
|
|
284
|
+
if (order) {
|
|
285
|
+
// TODO: track partially-filled qty; use 'partial' when qty > fill.qty
|
|
286
|
+
this._store.upsertOrder({ ...order, status: 'filled' });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// TODO: upsert ChartPosition based on fill side/qty/price
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* managedCapabilities.ts
|
|
3
|
+
*
|
|
4
|
+
* Feature-gate helpers specific to managed trading.
|
|
5
|
+
* Thin wrappers around ChartRuntimeResolver that produce clear, actionable
|
|
6
|
+
* error messages when a managed method is called without a suitable license.
|
|
7
|
+
*
|
|
8
|
+
* Usage (inside a managed trading method):
|
|
9
|
+
*
|
|
10
|
+
* assertManagedMode(); // throws if not in managed mode
|
|
11
|
+
* assertCanPlaceOrders(); // throws if orderEntry flag is off
|
|
12
|
+
* assertCanUseBrackets(); // throws if bracketOrders flag is off
|
|
13
|
+
* assertCanUseDraggableOrders(); // throws if draggableOrders flag is off
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { ChartRuntimeResolver } from '../../licensing/ChartRuntimeResolver';
|
|
17
|
+
|
|
18
|
+
const resolver = () => ChartRuntimeResolver.getInstance();
|
|
19
|
+
|
|
20
|
+
// ── Guard helpers (throw on failure) ─────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Asserts that the active license is in managed mode.
|
|
24
|
+
* @throws {Error} with a clear message if mode is unmanaged.
|
|
25
|
+
*/
|
|
26
|
+
export function assertManagedMode(): void {
|
|
27
|
+
if (!resolver().isManagedMode()) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
'[ForgeCharts] This feature requires a managed-mode license. ' +
|
|
30
|
+
'The current license is in unmanaged mode (or no license is installed). ' +
|
|
31
|
+
'Install a managed license via LicenseManager.validateLicense().',
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Asserts that the license permits order placement (orderEntry feature).
|
|
38
|
+
* Implicitly checks managed mode first.
|
|
39
|
+
*/
|
|
40
|
+
export function assertCanPlaceOrders(): void {
|
|
41
|
+
assertManagedMode();
|
|
42
|
+
if (!resolver().canUseOrderEntry()) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
'[ForgeCharts] Order entry is not enabled on the active license. ' +
|
|
45
|
+
'Contact your license provider to enable the orderEntry feature.',
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Asserts that the license permits bracket / OCO orders.
|
|
52
|
+
* Implicitly checks managed mode and order entry first.
|
|
53
|
+
*/
|
|
54
|
+
export function assertCanUseBrackets(): void {
|
|
55
|
+
assertCanPlaceOrders();
|
|
56
|
+
if (!resolver().canUseBracketOrders()) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
'[ForgeCharts] Bracket orders are not enabled on the active license. ' +
|
|
59
|
+
'Contact your license provider to enable the bracketOrders feature.',
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Asserts that the license permits drag-to-price order placement.
|
|
66
|
+
* Implicitly checks managed mode and order entry first.
|
|
67
|
+
*/
|
|
68
|
+
export function assertCanUseDraggableOrders(): void {
|
|
69
|
+
assertCanPlaceOrders();
|
|
70
|
+
if (!resolver().canUseDraggableOrders()) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
'[ForgeCharts] Draggable orders are not enabled on the active license. ' +
|
|
73
|
+
'Contact your license provider to enable the draggableOrders feature.',
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Asserts that the license permits the managed trading service hooks.
|
|
80
|
+
* Implicitly checks managed mode first.
|
|
81
|
+
*/
|
|
82
|
+
export function assertCanUseManagedTrading(): void {
|
|
83
|
+
assertManagedMode();
|
|
84
|
+
if (!resolver().canUseManagedTrading()) {
|
|
85
|
+
throw new Error(
|
|
86
|
+
'[ForgeCharts] Managed trading is not enabled on the active license. ' +
|
|
87
|
+
'Contact your license provider to enable the managedTrading feature.',
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ── Boolean helpers (for conditional rendering, no throw) ────────────────────
|
|
93
|
+
|
|
94
|
+
export const isManagedCapable = (): boolean => resolver().isManagedMode();
|
|
95
|
+
export const canPlaceOrders = (): boolean => resolver().isManagedMode() && resolver().canUseOrderEntry();
|
|
96
|
+
export const canPlaceBrackets = (): boolean => canPlaceOrders() && resolver().canUseBracketOrders();
|
|
97
|
+
export const canUseDraggable = (): boolean => canPlaceOrders() && resolver().canUseDraggableOrders();
|
|
98
|
+
export const canUseManagedTradingHook = (): boolean => resolver().isManagedMode() && resolver().canUseManagedTrading();
|