@lumencharts/trading-platform 0.0.0-stage → 0.1.0
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/CHANGELOG.md +25 -0
- package/LICENSE +55 -0
- package/README.md +53 -2
- package/dist/asset-entry.d.ts +3 -0
- package/dist/asset.d.ts +3 -0
- package/dist/asset.js +21689 -0
- package/dist/chart/bracket-links.d.ts +8 -0
- package/dist/chart/context-actions.d.ts +7 -0
- package/dist/chart/edit-presentation.d.ts +32 -0
- package/dist/chart/edit-preview.d.ts +26 -0
- package/dist/chart/exit-profit.d.ts +32 -0
- package/dist/chart/frame-batch.d.ts +15 -0
- package/dist/chart/overlay-coordinator.d.ts +18 -0
- package/dist/chart/overlay-events.d.ts +3 -0
- package/dist/chart/overlay-gesture.d.ts +40 -0
- package/dist/chart/overlay-index.d.ts +30 -0
- package/dist/chart/overlay-projection.d.ts +51 -0
- package/dist/chart/overlay-types.d.ts +42 -0
- package/dist/chart/parent-links.d.ts +33 -0
- package/dist/chart/position-profit.d.ts +15 -0
- package/dist/chart/review-lock.d.ts +14 -0
- package/dist/chart/ticket-projection.d.ts +36 -0
- package/dist/compat/default-formatters.d.ts +6 -0
- package/dist/compat/delegate.d.ts +11 -0
- package/dist/compat/factory.d.ts +5 -0
- package/dist/compat/formatters.d.ts +26 -0
- package/dist/compat/host-dialogs.d.ts +5 -0
- package/dist/compat/host-preferences.d.ts +8 -0
- package/dist/compat/host.d.ts +39 -0
- package/dist/compat/watched-value.d.ts +25 -0
- package/dist/conformance/index.d.ts +78 -0
- package/dist/conformance/index.js +6859 -0
- package/dist/conformance/replay.d.ts +35 -0
- package/dist/contracts/broker.d.ts +1122 -0
- package/dist/contracts/broker.js +168 -0
- package/dist/contracts/index.d.ts +3 -0
- package/dist/contracts/index.js +168 -0
- package/dist/contracts/market-data.d.ts +289 -0
- package/dist/contracts/market-data.js +11 -0
- package/dist/contracts/ports.d.ts +469 -0
- package/dist/contracts/ports.js +5 -0
- package/dist/core/account-datasets.d.ts +29 -0
- package/dist/core/account-lifecycle.d.ts +70 -0
- package/dist/core/account-types.d.ts +6 -0
- package/dist/core/bounded-map.d.ts +12 -0
- package/dist/core/bracket-input.d.ts +32 -0
- package/dist/core/bracket-modification.d.ts +6 -0
- package/dist/core/bracket-parent.d.ts +11 -0
- package/dist/core/broker-debug.d.ts +10 -0
- package/dist/core/capabilities.d.ts +34 -0
- package/dist/core/capability-metadata.d.ts +60 -0
- package/dist/core/capability-policy.d.ts +32 -0
- package/dist/core/chart-exit-level.d.ts +11 -0
- package/dist/core/chart-order-edit.d.ts +50 -0
- package/dist/core/clock.d.ts +2 -0
- package/dist/core/close-commands.d.ts +24 -0
- package/dist/core/close-flow.d.ts +14 -0
- package/dist/core/command-context.d.ts +16 -0
- package/dist/core/command-coordinator.d.ts +71 -0
- package/dist/core/command-follow.d.ts +32 -0
- package/dist/core/command-keys.d.ts +11 -0
- package/dist/core/context-orders.d.ts +19 -0
- package/dist/core/deferred.d.ts +5 -0
- package/dist/core/dialog-coordinator.d.ts +73 -0
- package/dist/core/display-preferences.d.ts +10 -0
- package/dist/core/disposal.d.ts +8 -0
- package/dist/core/entity-store.d.ts +56 -0
- package/dist/core/entity-validation.d.ts +9 -0
- package/dist/core/errors.d.ts +24 -0
- package/dist/core/execution-demand.d.ts +11 -0
- package/dist/core/execution-sound.d.ts +10 -0
- package/dist/core/exit-level-draft.d.ts +9 -0
- package/dist/core/exit-level-summary.d.ts +3 -0
- package/dist/core/exit-level-template.d.ts +16 -0
- package/dist/core/features.d.ts +39 -0
- package/dist/core/financial-rules.d.ts +70 -0
- package/dist/core/history-limit.d.ts +11 -0
- package/dist/core/identity.d.ts +14 -0
- package/dist/core/immutable.d.ts +8 -0
- package/dist/core/keyed-ref-count.d.ts +18 -0
- package/dist/core/leverage-commands.d.ts +42 -0
- package/dist/core/modification-context.d.ts +22 -0
- package/dist/core/modification-routing.d.ts +43 -0
- package/dist/core/notification-log.d.ts +33 -0
- package/dist/core/number-precision.d.ts +4 -0
- package/dist/core/order-commands.d.ts +50 -0
- package/dist/core/order-duration.d.ts +14 -0
- package/dist/core/order-labels.d.ts +7 -0
- package/dist/core/order-matching.d.ts +16 -0
- package/dist/core/order-modification.d.ts +20 -0
- package/dist/core/order-notifications.d.ts +30 -0
- package/dist/core/order-protection.d.ts +4 -0
- package/dist/core/parent-protection.d.ts +10 -0
- package/dist/core/position-commands.d.ts +45 -0
- package/dist/core/position-custom-fields.d.ts +5 -0
- package/dist/core/position-estimates.d.ts +22 -0
- package/dist/core/position-protection-permissions.d.ts +4 -0
- package/dist/core/preferences-key.d.ts +2 -0
- package/dist/core/preview-leases.d.ts +12 -0
- package/dist/core/protection-legs.d.ts +23 -0
- package/dist/core/protection-notifications.d.ts +9 -0
- package/dist/core/protection-rules.d.ts +21 -0
- package/dist/core/quantity-input.d.ts +53 -0
- package/dist/core/read-coordinator.d.ts +15 -0
- package/dist/core/readers.d.ts +6 -0
- package/dist/core/relative-price.d.ts +4 -0
- package/dist/core/review-policy.d.ts +17 -0
- package/dist/core/session-types.d.ts +1 -0
- package/dist/core/session.d.ts +114 -0
- package/dist/core/snapshot-dataset.d.ts +47 -0
- package/dist/core/symbol-catalog.d.ts +37 -0
- package/dist/core/tradability.d.ts +26 -0
- package/dist/core/trading-commands.d.ts +104 -0
- package/dist/core/trading-rules.d.ts +14 -0
- package/dist/core/trading-settings.d.ts +39 -0
- package/dist/core/trading-state.d.ts +52 -0
- package/dist/core/value-store.d.ts +11 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +6593 -0
- package/dist/market-data/depth-book.d.ts +18 -0
- package/dist/market-data/index.d.ts +3 -0
- package/dist/market-data/index.js +735 -0
- package/dist/market-data/session-demand.d.ts +15 -0
- package/dist/market-data/subscriptions.d.ts +13 -0
- package/dist/mock/broker.d.ts +75 -0
- package/dist/mock/clock.d.ts +12 -0
- package/dist/mock/feeds.d.ts +18 -0
- package/dist/mock/index.d.ts +5 -0
- package/dist/mock/index.js +1956 -0
- package/dist/mock/protection-cancellation.d.ts +7 -0
- package/dist/mock/scenarios.d.ts +5 -0
- package/dist/paper/broker.d.ts +41 -0
- package/dist/paper/index.d.ts +6 -0
- package/dist/paper/index.js +469 -0
- package/dist/paper/market.d.ts +57 -0
- package/dist/paper/rules.d.ts +40 -0
- package/dist/ui/account-manager/account-pages.d.ts +18 -0
- package/dist/ui/account-manager/column-presentation.d.ts +5 -0
- package/dist/ui/account-manager/context-menu.d.ts +13 -0
- package/dist/ui/account-manager/csv-downloads.d.ts +5 -0
- package/dist/ui/account-manager/custom-page.d.ts +16 -0
- package/dist/ui/account-manager/custom-table.d.ts +18 -0
- package/dist/ui/account-manager/format-account-value.d.ts +12 -0
- package/dist/ui/account-manager/position-reader.d.ts +19 -0
- package/dist/ui/account-manager/row-action-tracker.d.ts +12 -0
- package/dist/ui/account-manager/row-actions.d.ts +37 -0
- package/dist/ui/account-manager/status-counts.d.ts +6 -0
- package/dist/ui/account-manager/table-controls.d.ts +13 -0
- package/dist/ui/account-manager/table-export.d.ts +30 -0
- package/dist/ui/account-manager/table-model.d.ts +11 -0
- package/dist/ui/account-manager/table-toolbar.d.ts +25 -0
- package/dist/ui/account-manager/table.d.ts +24 -0
- package/dist/ui/account-manager/view.d.ts +19 -0
- package/dist/ui/account-manager/workspace.d.ts +15 -0
- package/dist/ui/chart-actions.d.ts +11 -0
- package/dist/ui/chart-controls/eligibility.d.ts +6 -0
- package/dist/ui/chart-controls/quote-buttons.d.ts +15 -0
- package/dist/ui/chart-controls/quote-controls.d.ts +12 -0
- package/dist/ui/chart-controls/quote-widths.d.ts +8 -0
- package/dist/ui/dialogs/close-amount-form.d.ts +8 -0
- package/dist/ui/dialogs/close-preview-dialog.d.ts +5 -0
- package/dist/ui/dialogs/confirm-dialog.d.ts +7 -0
- package/dist/ui/dialogs/dialog-body.d.ts +11 -0
- package/dist/ui/dialogs/leverage-form.d.ts +10 -0
- package/dist/ui/dialogs/message-dialog.d.ts +6 -0
- package/dist/ui/dialogs/position-protection.d.ts +14 -0
- package/dist/ui/dialogs/properties-dialog.d.ts +9 -0
- package/dist/ui/dialogs/sanitize-html.d.ts +2 -0
- package/dist/ui/dialogs/types.d.ts +5 -0
- package/dist/ui/dialogs/view.d.ts +5 -0
- package/dist/ui/dock-sizes.d.ts +20 -0
- package/dist/ui/dom/controller.d.ts +34 -0
- package/dist/ui/dom/ladder.d.ts +23 -0
- package/dist/ui/dom/view.d.ts +8 -0
- package/dist/ui/dom/workspace.d.ts +17 -0
- package/dist/ui/effective-mode.d.ts +11 -0
- package/dist/ui/financial-lease.d.ts +3 -0
- package/dist/ui/icons.d.ts +4 -0
- package/dist/ui/index.d.ts +9 -0
- package/dist/ui/index.js +15918 -0
- package/dist/ui/layout.d.ts +5 -0
- package/dist/ui/mount.d.ts +11 -0
- package/dist/ui/notifications/log.d.ts +6 -0
- package/dist/ui/notifications/toasts.d.ts +5 -0
- package/dist/ui/number-input.d.ts +2 -0
- package/dist/ui/order-ticket/chart-draft-controls.d.ts +8 -0
- package/dist/ui/order-ticket/chart-projection.d.ts +9 -0
- package/dist/ui/order-ticket/controller.d.ts +41 -0
- package/dist/ui/order-ticket/custom-field.d.ts +9 -0
- package/dist/ui/order-ticket/duration-input.d.ts +9 -0
- package/dist/ui/order-ticket/exit-input.d.ts +34 -0
- package/dist/ui/order-ticket/financial-controls.d.ts +10 -0
- package/dist/ui/order-ticket/layout-context.d.ts +7 -0
- package/dist/ui/order-ticket/market.d.ts +12 -0
- package/dist/ui/order-ticket/menu.d.ts +6 -0
- package/dist/ui/order-ticket/mode-rules.d.ts +4 -0
- package/dist/ui/order-ticket/modification-fields.d.ts +23 -0
- package/dist/ui/order-ticket/move-handle.d.ts +4 -0
- package/dist/ui/order-ticket/order-sent.d.ts +4 -0
- package/dist/ui/order-ticket/placement.d.ts +27 -0
- package/dist/ui/order-ticket/preview-message.d.ts +4 -0
- package/dist/ui/order-ticket/price-input.d.ts +14 -0
- package/dist/ui/order-ticket/quantity-input.d.ts +26 -0
- package/dist/ui/order-ticket/review-panel.d.ts +10 -0
- package/dist/ui/order-ticket/rules.d.ts +16 -0
- package/dist/ui/order-ticket/stop-input.d.ts +17 -0
- package/dist/ui/order-ticket/submit-bar.d.ts +13 -0
- package/dist/ui/order-ticket/symbol-search-controller.d.ts +50 -0
- package/dist/ui/order-ticket/symbol-search.d.ts +12 -0
- package/dist/ui/order-ticket/untradable-notice.d.ts +8 -0
- package/dist/ui/order-ticket/view.d.ts +16 -0
- package/dist/ui/panel-visibility.d.ts +6 -0
- package/dist/ui/pointer-drag.d.ts +20 -0
- package/dist/ui/popup-mount.d.ts +4 -0
- package/dist/ui/primitives.d.ts +29 -0
- package/dist/ui/protection/editor.d.ts +18 -0
- package/dist/ui/protection/fields.d.ts +30 -0
- package/dist/ui/protection/level.d.ts +14 -0
- package/dist/ui/protection/protected-size.d.ts +5 -0
- package/dist/ui/protection/ticket-workspace.d.ts +7 -0
- package/dist/ui/resize-handle.css +34 -0
- package/dist/ui/resize-handle.d.ts +20 -0
- package/dist/ui/resize-handle.js +185 -0
- package/dist/ui/select.d.ts +26 -0
- package/dist/ui/surface-theme.d.ts +3 -0
- package/dist/ui/switch.d.ts +7 -0
- package/dist/ui/symbol-logo.d.ts +7 -0
- package/dist/ui/tabs.d.ts +19 -0
- package/dist/ui/ticket-dock.d.ts +18 -0
- package/dist/ui/ticket-shell.d.ts +15 -0
- package/dist/ui/trading.css +1808 -0
- package/dist/ui/trap-tab.d.ts +11 -0
- package/dist/vendor/solid-ui/index.d.ts +3 -0
- package/dist/vendor/solid-ui/paged-list-window.d.ts +27 -0
- package/dist/vendor/solid-ui/virtual-list.d.ts +21 -0
- package/package.json +104 -3
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
// src/core/keyed-ref-count.ts
|
|
2
|
+
function releaseAll(releases) {
|
|
3
|
+
let failed = false, first;
|
|
4
|
+
for (const release of releases) {
|
|
5
|
+
try {
|
|
6
|
+
release();
|
|
7
|
+
} catch (cause) {
|
|
8
|
+
if (!failed) first = cause;
|
|
9
|
+
failed = true;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if (failed) throw first;
|
|
13
|
+
}
|
|
14
|
+
var KeyedRefCount = class {
|
|
15
|
+
constructor(dispose) {
|
|
16
|
+
this.dispose = dispose;
|
|
17
|
+
}
|
|
18
|
+
dispose;
|
|
19
|
+
entries = /* @__PURE__ */ new Map();
|
|
20
|
+
get(key) {
|
|
21
|
+
return this.entries.get(key)?.value;
|
|
22
|
+
}
|
|
23
|
+
*values() {
|
|
24
|
+
for (const entry of this.entries.values()) yield entry.value;
|
|
25
|
+
}
|
|
26
|
+
*[Symbol.iterator]() {
|
|
27
|
+
for (const [key, entry] of this.entries) yield [key, entry.value];
|
|
28
|
+
}
|
|
29
|
+
acquire(key, create, owner = /* @__PURE__ */ Symbol()) {
|
|
30
|
+
let entry = this.entries.get(key);
|
|
31
|
+
const first = !entry;
|
|
32
|
+
if (!entry) {
|
|
33
|
+
entry = { value: create(), owners: /* @__PURE__ */ new Set() };
|
|
34
|
+
this.entries.set(key, entry);
|
|
35
|
+
}
|
|
36
|
+
const retained = entry;
|
|
37
|
+
retained.owners.add(owner);
|
|
38
|
+
let owned = true;
|
|
39
|
+
return {
|
|
40
|
+
value: retained.value,
|
|
41
|
+
first,
|
|
42
|
+
release: () => {
|
|
43
|
+
if (!owned) return;
|
|
44
|
+
owned = false;
|
|
45
|
+
if (this.entries.get(key) !== retained || !retained.owners.delete(owner) || retained.owners.size) return;
|
|
46
|
+
this.entries.delete(key);
|
|
47
|
+
this.dispose(retained.value, key);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
releaseAll() {
|
|
52
|
+
const entries = [...this.entries];
|
|
53
|
+
this.entries.clear();
|
|
54
|
+
releaseAll(entries.map(([key, entry]) => () => this.dispose(entry.value, key)));
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/core/errors.ts
|
|
59
|
+
var TradingError = class _TradingError extends Error {
|
|
60
|
+
constructor(code, message, options) {
|
|
61
|
+
super(message, options);
|
|
62
|
+
this.code = code;
|
|
63
|
+
this.name = "TradingError";
|
|
64
|
+
}
|
|
65
|
+
code;
|
|
66
|
+
static disposed(message = "This trading session is closed.") {
|
|
67
|
+
return new _TradingError("disposed", message);
|
|
68
|
+
}
|
|
69
|
+
static accountChanged() {
|
|
70
|
+
return new _TradingError("stale-context", "The trading account changed.");
|
|
71
|
+
}
|
|
72
|
+
static disconnected() {
|
|
73
|
+
return new _TradingError("disconnected", "Reconnect before trading.");
|
|
74
|
+
}
|
|
75
|
+
static brokerUnavailable() {
|
|
76
|
+
return new _TradingError("broker-unavailable", "The broker is unavailable.");
|
|
77
|
+
}
|
|
78
|
+
static positionChanged() {
|
|
79
|
+
return new _TradingError("stale-context", "This position changed. Review its current details.");
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/core/immutable.ts
|
|
84
|
+
function ordinaryRecordPrototype(prototype) {
|
|
85
|
+
if (prototype === null || prototype === Object.prototype) return true;
|
|
86
|
+
if (Object.getPrototypeOf(prototype) !== null) return false;
|
|
87
|
+
const constructor = Object.getOwnPropertyDescriptor(prototype, "constructor");
|
|
88
|
+
return !!constructor && "value" in constructor && typeof constructor.value === "function" && Function.prototype.toString.call(constructor.value) === Function.prototype.toString.call(Object);
|
|
89
|
+
}
|
|
90
|
+
var produced = /* @__PURE__ */ new WeakSet();
|
|
91
|
+
function copyData(value, ancestors) {
|
|
92
|
+
if (value === null || value === void 0 || ["string", "number", "boolean", "bigint"].includes(typeof value))
|
|
93
|
+
return value;
|
|
94
|
+
if (typeof value === "object" && produced.has(value)) return value;
|
|
95
|
+
if (typeof value !== "object")
|
|
96
|
+
throw new TradingError(
|
|
97
|
+
"invalid-input",
|
|
98
|
+
"Broker data cannot contain executable values."
|
|
99
|
+
);
|
|
100
|
+
const prototype = Object.getPrototypeOf(value);
|
|
101
|
+
if (!Array.isArray(value) && !ordinaryRecordPrototype(prototype))
|
|
102
|
+
throw new TradingError(
|
|
103
|
+
"invalid-input",
|
|
104
|
+
"Broker data must contain ordinary records."
|
|
105
|
+
);
|
|
106
|
+
if (ancestors.has(value))
|
|
107
|
+
throw new TradingError(
|
|
108
|
+
"invalid-input",
|
|
109
|
+
"Broker data cannot contain circular records."
|
|
110
|
+
);
|
|
111
|
+
ancestors.add(value);
|
|
112
|
+
const result = Array.isArray(value) ? [] : {};
|
|
113
|
+
for (const key of Object.keys(value)) {
|
|
114
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
115
|
+
if (!descriptor || !("value" in descriptor))
|
|
116
|
+
throw new TradingError(
|
|
117
|
+
"invalid-input",
|
|
118
|
+
"Broker data cannot contain accessors."
|
|
119
|
+
);
|
|
120
|
+
Object.defineProperty(result, key, {
|
|
121
|
+
value: copyData(descriptor.value, ancestors),
|
|
122
|
+
enumerable: true,
|
|
123
|
+
writable: true,
|
|
124
|
+
configurable: true
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
ancestors.delete(value);
|
|
128
|
+
Object.freeze(result);
|
|
129
|
+
produced.add(result);
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function snapshot(value) {
|
|
133
|
+
return copyData(value, /* @__PURE__ */ new Set());
|
|
134
|
+
}
|
|
135
|
+
function equalData(a, b) {
|
|
136
|
+
if (Object.is(a, b)) return true;
|
|
137
|
+
if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
|
|
138
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
139
|
+
const keys = Object.keys(a);
|
|
140
|
+
if (keys.length !== Object.keys(b).length) return false;
|
|
141
|
+
return keys.every((key) => {
|
|
142
|
+
const left = Object.getOwnPropertyDescriptor(a, key);
|
|
143
|
+
const right = Object.getOwnPropertyDescriptor(b, key);
|
|
144
|
+
return !!left && !!right && "value" in left && "value" in right && equalData(left.value, right.value);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/core/entity-store.ts
|
|
149
|
+
var EntityStore = class {
|
|
150
|
+
constructor(validate, accept = () => true) {
|
|
151
|
+
this.validate = validate;
|
|
152
|
+
this.accept = accept;
|
|
153
|
+
}
|
|
154
|
+
validate;
|
|
155
|
+
accept;
|
|
156
|
+
entities = /* @__PURE__ */ new Map();
|
|
157
|
+
revisions = /* @__PURE__ */ new Map();
|
|
158
|
+
listeners = /* @__PURE__ */ new Set();
|
|
159
|
+
before = /* @__PURE__ */ new Map();
|
|
160
|
+
idsSnapshot;
|
|
161
|
+
sequence = 0;
|
|
162
|
+
depth = 0;
|
|
163
|
+
disposed = false;
|
|
164
|
+
ids() {
|
|
165
|
+
return this.idsSnapshot ??= Object.freeze([...this.entities.keys()]);
|
|
166
|
+
}
|
|
167
|
+
get(id) {
|
|
168
|
+
return this.entities.get(id);
|
|
169
|
+
}
|
|
170
|
+
// Missing identities share a conservative watermark; deleted IDs never need permanent tombstones.
|
|
171
|
+
revision(id) {
|
|
172
|
+
return this.revisions.get(id) ?? this.sequence;
|
|
173
|
+
}
|
|
174
|
+
subscribeChanges(listener) {
|
|
175
|
+
if (this.disposed) return () => {
|
|
176
|
+
};
|
|
177
|
+
this.listeners.add(listener);
|
|
178
|
+
return () => {
|
|
179
|
+
this.listeners.delete(listener);
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
record(id) {
|
|
183
|
+
if (!this.before.has(id)) this.before.set(id, this.entities.get(id));
|
|
184
|
+
}
|
|
185
|
+
upsert(entity, authoritative = false) {
|
|
186
|
+
if (this.disposed) return false;
|
|
187
|
+
this.validate(entity);
|
|
188
|
+
const old = this.entities.get(entity.id);
|
|
189
|
+
if (!authoritative && !this.accept(entity, old) || equalData(old, entity))
|
|
190
|
+
return false;
|
|
191
|
+
const next = snapshot(entity);
|
|
192
|
+
this.record(entity.id);
|
|
193
|
+
this.entities.set(entity.id, next);
|
|
194
|
+
this.revisions.set(entity.id, ++this.sequence);
|
|
195
|
+
if (!old) this.idsSnapshot = void 0;
|
|
196
|
+
this.publish();
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
patch(id, changes) {
|
|
200
|
+
if (changes.id !== void 0 && changes.id !== id)
|
|
201
|
+
throw new TradingError(
|
|
202
|
+
"invalid-input",
|
|
203
|
+
"An update cannot change its identity."
|
|
204
|
+
);
|
|
205
|
+
const old = this.entities.get(id);
|
|
206
|
+
if (!old) return false;
|
|
207
|
+
return this.upsert({ ...old, ...changes, id });
|
|
208
|
+
}
|
|
209
|
+
remove(id) {
|
|
210
|
+
if (this.disposed || !this.entities.has(id)) return false;
|
|
211
|
+
this.record(id);
|
|
212
|
+
this.entities.delete(id);
|
|
213
|
+
this.idsSnapshot = void 0;
|
|
214
|
+
this.revisions.delete(id);
|
|
215
|
+
this.sequence++;
|
|
216
|
+
this.publish();
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
transaction(operation) {
|
|
220
|
+
this.depth++;
|
|
221
|
+
try {
|
|
222
|
+
operation();
|
|
223
|
+
} finally {
|
|
224
|
+
this.depth--;
|
|
225
|
+
this.publish();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Replaces the snapshot atomically: one malformed row rejects the whole read. With `quarantine`, invalid rows
|
|
230
|
+
* are set aside instead: valid rows commit, a quarantined ID keeps its previous value, and each failure is
|
|
231
|
+
* returned; a read with rows but none valid still fails as a whole.
|
|
232
|
+
*/
|
|
233
|
+
replace(entities, options = {}) {
|
|
234
|
+
if (!Array.isArray(entities))
|
|
235
|
+
throw new TradingError("invalid-input", "The broker returned invalid account information.");
|
|
236
|
+
const incoming = /* @__PURE__ */ new Map();
|
|
237
|
+
const seen = /* @__PURE__ */ new Set();
|
|
238
|
+
const quarantined = [];
|
|
239
|
+
let valid = 0;
|
|
240
|
+
for (const entity of entities) {
|
|
241
|
+
const id = entity && typeof entity === "object" && typeof entity.id === "string" ? entity.id : void 0;
|
|
242
|
+
try {
|
|
243
|
+
this.validate(entity);
|
|
244
|
+
if (seen.has(entity.id))
|
|
245
|
+
throw new TradingError("invalid-input", "The broker returned duplicate identities.");
|
|
246
|
+
seen.add(entity.id);
|
|
247
|
+
if (!options.accept || options.accept(entity)) incoming.set(entity.id, snapshot(entity));
|
|
248
|
+
valid++;
|
|
249
|
+
} catch (cause) {
|
|
250
|
+
if (!options.quarantine) throw cause;
|
|
251
|
+
quarantined.push({ ...id !== void 0 ? { id } : {}, message: cause instanceof Error ? cause.message : String(cause) });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (quarantined.length && !valid)
|
|
255
|
+
throw new TradingError("invalid-input", "The broker returned invalid account information.");
|
|
256
|
+
const kept = new Set(
|
|
257
|
+
quarantined.flatMap((row) => row.id !== void 0 && !options.expire?.has(row.id) ? [row.id] : [])
|
|
258
|
+
);
|
|
259
|
+
this.transaction(() => {
|
|
260
|
+
for (const id of this.entities.keys())
|
|
261
|
+
if (!incoming.has(id) && !kept.has(id)) this.remove(id);
|
|
262
|
+
for (const entity of incoming.values()) this.upsert(entity, true);
|
|
263
|
+
});
|
|
264
|
+
return quarantined;
|
|
265
|
+
}
|
|
266
|
+
publish() {
|
|
267
|
+
if (this.depth || !this.before.size) return;
|
|
268
|
+
const added = [], updated = [], removed = [];
|
|
269
|
+
for (const [id, old] of this.before) {
|
|
270
|
+
const next = this.entities.get(id);
|
|
271
|
+
if (equalData(old, next)) continue;
|
|
272
|
+
if (!next) removed.push(id);
|
|
273
|
+
else if (!old) added.push(id);
|
|
274
|
+
else updated.push(id);
|
|
275
|
+
}
|
|
276
|
+
this.before.clear();
|
|
277
|
+
if (!added.length && !updated.length && !removed.length) return;
|
|
278
|
+
const change = Object.freeze({
|
|
279
|
+
added: Object.freeze(added),
|
|
280
|
+
updated: Object.freeze(updated),
|
|
281
|
+
removed: Object.freeze(removed),
|
|
282
|
+
revision: this.sequence
|
|
283
|
+
});
|
|
284
|
+
for (const listener of [...this.listeners]) {
|
|
285
|
+
if (this.disposed) break;
|
|
286
|
+
if (this.listeners.has(listener)) listener(change);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
destroy() {
|
|
290
|
+
this.disposed = true;
|
|
291
|
+
this.listeners.clear();
|
|
292
|
+
this.before.clear();
|
|
293
|
+
this.entities.clear();
|
|
294
|
+
this.revisions.clear();
|
|
295
|
+
this.idsSnapshot = Object.freeze([]);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
// src/core/capability-metadata.ts
|
|
300
|
+
var defaultFlags = Object.freeze({
|
|
301
|
+
"showQuantityInsteadOfAmount": false,
|
|
302
|
+
"supportOrderBrackets": false,
|
|
303
|
+
"supportStopLoss": true,
|
|
304
|
+
"supportTrailingStop": false,
|
|
305
|
+
"supportGuaranteedStop": false,
|
|
306
|
+
"supportPositions": true,
|
|
307
|
+
"supportPositionBrackets": false,
|
|
308
|
+
"supportIndividualPositionBrackets": false,
|
|
309
|
+
"supportPositionNetting": false,
|
|
310
|
+
"supportClosePosition": false,
|
|
311
|
+
"supportCloseIndividualPosition": false,
|
|
312
|
+
"supportModifyOrderPrice": true,
|
|
313
|
+
"supportEditAmount": true,
|
|
314
|
+
"supportModifyBrackets": true,
|
|
315
|
+
"supportLevel2Data": false,
|
|
316
|
+
"supportMultiposition": false,
|
|
317
|
+
"supportPLUpdate": true,
|
|
318
|
+
"supportReversePosition": false,
|
|
319
|
+
"supportNativeReversePosition": false,
|
|
320
|
+
"supportMarketOrders": true,
|
|
321
|
+
"supportLimitOrders": true,
|
|
322
|
+
"supportStopOrders": true,
|
|
323
|
+
"supportStopLimitOrders": false,
|
|
324
|
+
"supportDemoLiveSwitcher": true,
|
|
325
|
+
"supportMarketBrackets": true,
|
|
326
|
+
"supportSymbolSearch": false,
|
|
327
|
+
"supportModifyDuration": false,
|
|
328
|
+
"supportModifyTrailingStop": true,
|
|
329
|
+
"supportMargin": false,
|
|
330
|
+
"supportPlaceOrderPreview": false,
|
|
331
|
+
"supportModifyOrderPreview": false,
|
|
332
|
+
"supportLeverage": false,
|
|
333
|
+
"supportLeverageButton": true,
|
|
334
|
+
"supportOrdersHistory": true,
|
|
335
|
+
"supportAddBracketsToExistingOrder": true,
|
|
336
|
+
"supportBalances": false,
|
|
337
|
+
"closePositionCancelsOrders": false,
|
|
338
|
+
"supportOnlyPairPositionBrackets": false,
|
|
339
|
+
"supportOnlyPairOrderBrackets": false,
|
|
340
|
+
"supportCryptoExchangeOrderTicket": false,
|
|
341
|
+
"supportSymbolSpecificCryptoOrderTicket": false,
|
|
342
|
+
"positionPLInInstrumentCurrency": false,
|
|
343
|
+
"showRiskInInstrumentCurrency": false,
|
|
344
|
+
"supportPartialClosePosition": false,
|
|
345
|
+
"supportPartialCloseIndividualPosition": false,
|
|
346
|
+
"supportCancellingBothBracketsOnly": false,
|
|
347
|
+
"supportCryptoBrackets": false,
|
|
348
|
+
"showNotificationsLog": true,
|
|
349
|
+
"supportStopOrdersInBothDirections": false,
|
|
350
|
+
"supportStopLimitOrdersInBothDirections": false,
|
|
351
|
+
"supportStrictCheckingLimitOrderPrice": false,
|
|
352
|
+
"supportExecutions": true,
|
|
353
|
+
"supportModifyOrderType": false,
|
|
354
|
+
"requiresFIFOCloseIndividualPositions": false,
|
|
355
|
+
"supportMultipleExitLevels": false,
|
|
356
|
+
"supportUnpairedExitLevels": false,
|
|
357
|
+
"supportCustomOrderInfo": false,
|
|
358
|
+
"supportPreviewClosePosition": false
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// src/core/entity-validation.ts
|
|
362
|
+
var terminalStatuses = /* @__PURE__ */ new Set([1 /* Canceled */, 2 /* Filled */, 5 /* Rejected */]);
|
|
363
|
+
function finite(value, name) {
|
|
364
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new TradingError("invalid-input", `Invalid ${name}.`);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/core/trading-rules.ts
|
|
368
|
+
function decimal(value) {
|
|
369
|
+
finite(value, "number");
|
|
370
|
+
const [mantissa, exponentText] = String(value).toLowerCase().split("e");
|
|
371
|
+
const [whole, fractional = ""] = mantissa.split(".");
|
|
372
|
+
const exponent = Number(exponentText ?? 0) - fractional.length;
|
|
373
|
+
const numerator = BigInt(whole + fractional);
|
|
374
|
+
if (exponent >= 0)
|
|
375
|
+
return { numerator: numerator * 10n ** BigInt(exponent), denominator: 1n };
|
|
376
|
+
return { numerator, denominator: 10n ** BigInt(-exponent) };
|
|
377
|
+
}
|
|
378
|
+
function snapToStep(value, step) {
|
|
379
|
+
finite(value, "price");
|
|
380
|
+
finite(step, "price step");
|
|
381
|
+
if (step <= 0) throw new TradingError("invalid-input", "Invalid price step.");
|
|
382
|
+
const a = decimal(value), b = decimal(step);
|
|
383
|
+
const numerator = a.numerator * b.denominator, denominator = a.denominator * b.numerator;
|
|
384
|
+
const negative = numerator < 0n;
|
|
385
|
+
const absolute = negative ? -numerator : numerator;
|
|
386
|
+
const rounded = (absolute * 2n + denominator) / (denominator * 2n);
|
|
387
|
+
return Number((negative ? -rounded : rounded) * b.numerator) / Number(b.denominator);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// src/market-data/depth-book.ts
|
|
391
|
+
var DepthBook = class {
|
|
392
|
+
constructor(tick) {
|
|
393
|
+
this.tick = tick;
|
|
394
|
+
}
|
|
395
|
+
tick;
|
|
396
|
+
bids = new EntityStore(() => {
|
|
397
|
+
});
|
|
398
|
+
asks = new EntityStore(() => {
|
|
399
|
+
});
|
|
400
|
+
initialized = false;
|
|
401
|
+
buffered = [];
|
|
402
|
+
normalize(levels) {
|
|
403
|
+
const normalized = /* @__PURE__ */ new Map();
|
|
404
|
+
for (const level of levels) {
|
|
405
|
+
if (!Number.isFinite(level.price) || !Number.isFinite(level.volume) || level.volume < 0) throw new TradingError("invalid-input", "Invalid depth level.");
|
|
406
|
+
const price = snapToStep(level.price, this.tick);
|
|
407
|
+
normalized.set(String(price), { id: String(price), price, volume: level.volume });
|
|
408
|
+
}
|
|
409
|
+
return [...normalized.values()];
|
|
410
|
+
}
|
|
411
|
+
apply(data) {
|
|
412
|
+
const bids = this.normalize(data.bids), asks = this.normalize(data.asks);
|
|
413
|
+
if (!this.initialized && !data.snapshot) {
|
|
414
|
+
this.buffered.push({ snapshot: false, bids, asks });
|
|
415
|
+
if (this.buffered.length > 1e3) {
|
|
416
|
+
this.buffered = [];
|
|
417
|
+
throw new TradingError("broker-unavailable", "Depth needs a fresh snapshot.");
|
|
418
|
+
}
|
|
419
|
+
return void 0;
|
|
420
|
+
}
|
|
421
|
+
if (data.snapshot) {
|
|
422
|
+
this.initialized = true;
|
|
423
|
+
this.buffered = [];
|
|
424
|
+
this.bids.transaction(() => this.asks.transaction(() => {
|
|
425
|
+
this.bids.replace(bids.filter((level) => level.volume > 0));
|
|
426
|
+
this.asks.replace(asks.filter((level) => level.volume > 0));
|
|
427
|
+
}));
|
|
428
|
+
} else {
|
|
429
|
+
for (const [store, levels] of [[this.bids, bids], [this.asks, asks]]) store.transaction(() => {
|
|
430
|
+
for (const level of levels) {
|
|
431
|
+
if (level.volume === 0) store.remove(level.id);
|
|
432
|
+
else store.upsert(level);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
return Object.freeze({ snapshot: data.snapshot, bids: Object.freeze(bids.sort((a, b) => a.price - b.price)), asks: Object.freeze(asks.sort((a, b) => a.price - b.price)) });
|
|
437
|
+
}
|
|
438
|
+
snapshot() {
|
|
439
|
+
if (!this.initialized) return void 0;
|
|
440
|
+
const rows = (store) => store.ids().map((id) => store.get(id)).sort((a, b) => a.price - b.price);
|
|
441
|
+
return Object.freeze({ snapshot: true, bids: Object.freeze(rows(this.bids)), asks: Object.freeze(rows(this.asks)) });
|
|
442
|
+
}
|
|
443
|
+
destroy() {
|
|
444
|
+
this.bids.destroy();
|
|
445
|
+
this.asks.destroy();
|
|
446
|
+
this.buffered = [];
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
// src/market-data/subscriptions.ts
|
|
451
|
+
function normalizeQuotes(data) {
|
|
452
|
+
const result = {};
|
|
453
|
+
for (const key of [
|
|
454
|
+
"trade",
|
|
455
|
+
"size",
|
|
456
|
+
"bid",
|
|
457
|
+
"bid_size",
|
|
458
|
+
"ask",
|
|
459
|
+
"ask_size",
|
|
460
|
+
"spread",
|
|
461
|
+
"lastSize"
|
|
462
|
+
]) {
|
|
463
|
+
const value = data[key];
|
|
464
|
+
if (value === void 0) continue;
|
|
465
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
466
|
+
throw new TradingError("invalid-input", `Invalid quote ${key}.`);
|
|
467
|
+
result[key] = value;
|
|
468
|
+
}
|
|
469
|
+
for (const key of [
|
|
470
|
+
"isDelayed",
|
|
471
|
+
"isHalted",
|
|
472
|
+
"isHardToBorrow",
|
|
473
|
+
"isNotShortable"
|
|
474
|
+
]) {
|
|
475
|
+
const value = data[key];
|
|
476
|
+
if (value === void 0) continue;
|
|
477
|
+
if (typeof value !== "boolean")
|
|
478
|
+
throw new TradingError("invalid-input", `Invalid quote ${key}.`);
|
|
479
|
+
result[key] = value;
|
|
480
|
+
}
|
|
481
|
+
return snapshot(result);
|
|
482
|
+
}
|
|
483
|
+
function quoteValues(data) {
|
|
484
|
+
if (data.s === "error")
|
|
485
|
+
return Object.freeze({
|
|
486
|
+
status: "error",
|
|
487
|
+
error: "Quotes are unavailable for this symbol."
|
|
488
|
+
});
|
|
489
|
+
const values = {};
|
|
490
|
+
for (const key of ["bid", "ask", "spread"])
|
|
491
|
+
if (data.v[key] !== void 0) values[key] = data.v[key];
|
|
492
|
+
if (data.v.lp !== void 0) values.trade = data.v.lp;
|
|
493
|
+
return Object.freeze({ status: "ready", value: normalizeQuotes(values) });
|
|
494
|
+
}
|
|
495
|
+
function createTradingMarketData(provider, tickForSymbol, diagnostic = () => {
|
|
496
|
+
}) {
|
|
497
|
+
const quotes = new KeyedRefCount((demand) => {
|
|
498
|
+
demand.epoch++;
|
|
499
|
+
releaseAll([() => {
|
|
500
|
+
if (demand.guid) provider.unsubscribeQuotes?.(demand.guid);
|
|
501
|
+
}, () => demand.listeners.clear()]);
|
|
502
|
+
});
|
|
503
|
+
const depth = new KeyedRefCount((demand) => {
|
|
504
|
+
demand.epoch++;
|
|
505
|
+
releaseAll([
|
|
506
|
+
() => {
|
|
507
|
+
if (demand.guid) provider.unsubscribeDepth?.(demand.guid);
|
|
508
|
+
},
|
|
509
|
+
() => demand.listeners.clear(),
|
|
510
|
+
() => demand.book.destroy()
|
|
511
|
+
]);
|
|
512
|
+
});
|
|
513
|
+
const namespace = crypto.randomUUID();
|
|
514
|
+
let sequence = 0;
|
|
515
|
+
let disposed = false;
|
|
516
|
+
function publish(demand, value) {
|
|
517
|
+
if (value.status === "ready" && value.value) {
|
|
518
|
+
demand.latest = normalizeQuotes({ ...demand.latest, ...value.value });
|
|
519
|
+
value = Object.freeze({ ...value, value: demand.latest });
|
|
520
|
+
}
|
|
521
|
+
if (equalData(demand.current, value)) return;
|
|
522
|
+
demand.current = value;
|
|
523
|
+
for (const listener of [...demand.listeners.values()])
|
|
524
|
+
listener.callback(value);
|
|
525
|
+
}
|
|
526
|
+
function schedule(symbol, demand) {
|
|
527
|
+
if (demand.queued) return;
|
|
528
|
+
demand.queued = true;
|
|
529
|
+
queueMicrotask(() => {
|
|
530
|
+
demand.queued = false;
|
|
531
|
+
if (disposed || quotes.get(symbol) !== demand) return;
|
|
532
|
+
const speed = [...demand.listeners.values()].some(
|
|
533
|
+
(listener) => listener.speed === "fast"
|
|
534
|
+
) ? "fast" : "normal";
|
|
535
|
+
if (demand.guid && demand.speed === speed) return;
|
|
536
|
+
demand.epoch++;
|
|
537
|
+
if (demand.guid) provider.unsubscribeQuotes?.(demand.guid);
|
|
538
|
+
demand.guid = void 0;
|
|
539
|
+
demand.speed = speed;
|
|
540
|
+
if (!provider.getQuotes || !provider.subscribeQuotes || !provider.unsubscribeQuotes) {
|
|
541
|
+
publish(demand, {
|
|
542
|
+
status: "error",
|
|
543
|
+
source: "snapshot",
|
|
544
|
+
error: "Live quotes are unavailable."
|
|
545
|
+
});
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
const epoch = demand.epoch;
|
|
549
|
+
const guid = `${namespace}:quotes:${++sequence}`;
|
|
550
|
+
demand.guid = guid;
|
|
551
|
+
const live = () => !disposed && quotes.get(symbol) === demand && demand.epoch === epoch;
|
|
552
|
+
let initial = true;
|
|
553
|
+
const initialArrival = demand.arrival;
|
|
554
|
+
const apply = (rows, source) => {
|
|
555
|
+
if (!live()) return;
|
|
556
|
+
for (const row of rows)
|
|
557
|
+
if (row.n === symbol) {
|
|
558
|
+
try {
|
|
559
|
+
publish(demand, { ...quoteValues(row), source });
|
|
560
|
+
} catch (cause) {
|
|
561
|
+
publish(demand, {
|
|
562
|
+
status: "error",
|
|
563
|
+
source,
|
|
564
|
+
error: "The provider returned invalid quotes."
|
|
565
|
+
});
|
|
566
|
+
diagnostic(String(cause));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
const finish = (rows, error) => {
|
|
571
|
+
if (!live() || !initial) return;
|
|
572
|
+
initial = false;
|
|
573
|
+
if (demand.arrival !== initialArrival) return;
|
|
574
|
+
if (error)
|
|
575
|
+
publish(demand, { status: "error", source: "snapshot", error });
|
|
576
|
+
else apply(rows, "snapshot");
|
|
577
|
+
};
|
|
578
|
+
try {
|
|
579
|
+
provider.subscribeQuotes(
|
|
580
|
+
speed === "normal" ? [symbol] : [],
|
|
581
|
+
speed === "fast" ? [symbol] : [],
|
|
582
|
+
(rows) => {
|
|
583
|
+
if (!live()) return;
|
|
584
|
+
for (const row of rows) {
|
|
585
|
+
if (row.n !== symbol) continue;
|
|
586
|
+
demand.arrival++;
|
|
587
|
+
apply([row], "stream");
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
guid
|
|
591
|
+
);
|
|
592
|
+
provider.getQuotes(
|
|
593
|
+
[symbol],
|
|
594
|
+
(rows) => finish(rows),
|
|
595
|
+
() => finish([], "Quotes are unavailable.")
|
|
596
|
+
);
|
|
597
|
+
} catch (cause) {
|
|
598
|
+
finish([], "Live quotes are unavailable.");
|
|
599
|
+
diagnostic(String(cause));
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
function startDepth(symbol, demand) {
|
|
604
|
+
if (disposed || depth.get(symbol) !== demand) return;
|
|
605
|
+
if (!provider.subscribeDepth || !provider.unsubscribeDepth)
|
|
606
|
+
throw new TradingError("unsupported", "Depth of Market is unavailable.");
|
|
607
|
+
const epoch = ++demand.epoch;
|
|
608
|
+
demand.starting = true;
|
|
609
|
+
let guid;
|
|
610
|
+
try {
|
|
611
|
+
guid = subscribeDepth(symbol, epoch, demand);
|
|
612
|
+
} finally {
|
|
613
|
+
demand.starting = false;
|
|
614
|
+
}
|
|
615
|
+
if (disposed || depth.get(symbol) !== demand)
|
|
616
|
+
provider.unsubscribeDepth(guid);
|
|
617
|
+
else demand.guid = guid;
|
|
618
|
+
}
|
|
619
|
+
function subscribeDepth(symbol, epoch, demand) {
|
|
620
|
+
return provider.subscribeDepth(symbol, (data) => {
|
|
621
|
+
if (disposed || depth.get(symbol) !== demand || demand.epoch !== epoch)
|
|
622
|
+
return;
|
|
623
|
+
try {
|
|
624
|
+
const normalized = demand.book.apply(data);
|
|
625
|
+
if (normalized)
|
|
626
|
+
for (const listener of [...demand.listeners]) listener(normalized);
|
|
627
|
+
} catch (cause) {
|
|
628
|
+
diagnostic(String(cause));
|
|
629
|
+
if (cause instanceof TradingError && cause.code === "broker-unavailable") {
|
|
630
|
+
demand.epoch++;
|
|
631
|
+
queueMicrotask(() => {
|
|
632
|
+
if (disposed || depth.get(symbol) !== demand) return;
|
|
633
|
+
try {
|
|
634
|
+
if (demand.guid) provider.unsubscribeDepth?.(demand.guid);
|
|
635
|
+
demand.guid = void 0;
|
|
636
|
+
startDepth(symbol, demand);
|
|
637
|
+
} catch (restart) {
|
|
638
|
+
demand.guid = void 0;
|
|
639
|
+
diagnostic(String(restart));
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
return {
|
|
647
|
+
acquireQuotes(symbol, speed, callback) {
|
|
648
|
+
if (disposed)
|
|
649
|
+
throw TradingError.disposed("This market-data connection is closed.");
|
|
650
|
+
const lease = quotes.acquire(symbol, () => ({
|
|
651
|
+
listeners: /* @__PURE__ */ new Map(),
|
|
652
|
+
epoch: 0,
|
|
653
|
+
current: { status: "loading", source: "snapshot" },
|
|
654
|
+
queued: false,
|
|
655
|
+
arrival: 0
|
|
656
|
+
}));
|
|
657
|
+
const demand = lease.value;
|
|
658
|
+
const token = /* @__PURE__ */ Symbol();
|
|
659
|
+
demand.listeners.set(token, { speed, callback });
|
|
660
|
+
const owned = demand;
|
|
661
|
+
const release = () => {
|
|
662
|
+
if (!owned.listeners.delete(token)) return;
|
|
663
|
+
lease.release();
|
|
664
|
+
if (owned.listeners.size) schedule(symbol, owned);
|
|
665
|
+
};
|
|
666
|
+
try {
|
|
667
|
+
callback({ ...demand.current, source: "snapshot" });
|
|
668
|
+
schedule(symbol, demand);
|
|
669
|
+
} catch (cause) {
|
|
670
|
+
release();
|
|
671
|
+
throw cause;
|
|
672
|
+
}
|
|
673
|
+
return release;
|
|
674
|
+
},
|
|
675
|
+
acquireDepth(symbol, callback) {
|
|
676
|
+
if (disposed)
|
|
677
|
+
throw TradingError.disposed("This market-data connection is closed.");
|
|
678
|
+
if (!provider.subscribeDepth || !provider.unsubscribeDepth)
|
|
679
|
+
throw new TradingError(
|
|
680
|
+
"unsupported",
|
|
681
|
+
"Depth of Market is unavailable."
|
|
682
|
+
);
|
|
683
|
+
const lease = depth.acquire(symbol, () => ({
|
|
684
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
685
|
+
book: new DepthBook(tickForSymbol(symbol)),
|
|
686
|
+
epoch: 0,
|
|
687
|
+
starting: false
|
|
688
|
+
}), callback);
|
|
689
|
+
const demand = lease.value;
|
|
690
|
+
demand.listeners.add(callback);
|
|
691
|
+
const owned = demand;
|
|
692
|
+
const release = () => {
|
|
693
|
+
if (!owned.listeners.delete(callback)) return;
|
|
694
|
+
lease.release();
|
|
695
|
+
};
|
|
696
|
+
try {
|
|
697
|
+
const current = demand.book.snapshot();
|
|
698
|
+
if (current) callback(current);
|
|
699
|
+
if (!demand.guid && !demand.starting) startDepth(symbol, demand);
|
|
700
|
+
} catch (cause) {
|
|
701
|
+
demand.starting = false;
|
|
702
|
+
release();
|
|
703
|
+
throw cause;
|
|
704
|
+
}
|
|
705
|
+
return release;
|
|
706
|
+
},
|
|
707
|
+
pushQuotes(symbol, value) {
|
|
708
|
+
const demand = quotes.get(symbol);
|
|
709
|
+
if (!disposed && demand) {
|
|
710
|
+
demand.arrival++;
|
|
711
|
+
publish(demand, {
|
|
712
|
+
status: "ready",
|
|
713
|
+
source: "stream",
|
|
714
|
+
value: normalizeQuotes(value)
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
},
|
|
718
|
+
pushDepth(symbol, value) {
|
|
719
|
+
const demand = depth.get(symbol);
|
|
720
|
+
if (disposed || !demand) return;
|
|
721
|
+
const normalized = demand.book.apply(value);
|
|
722
|
+
if (normalized)
|
|
723
|
+
for (const listener of [...demand.listeners]) listener(normalized);
|
|
724
|
+
},
|
|
725
|
+
destroy() {
|
|
726
|
+
if (disposed) return;
|
|
727
|
+
disposed = true;
|
|
728
|
+
releaseAll([() => quotes.releaseAll(), () => depth.releaseAll()]);
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
export {
|
|
733
|
+
createTradingMarketData,
|
|
734
|
+
normalizeQuotes
|
|
735
|
+
};
|