@tradejs/cli 1.0.9 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/cli.js +29497 -13537
  2. package/dist/lib/aiPocketSearch.js +1316 -0
  3. package/dist/lib/aiTrainCharts.js +711 -0
  4. package/dist/lib/aiTrainDataset.js +40 -0
  5. package/dist/lib/aiTrainEvaluationDump.js +76 -0
  6. package/dist/lib/aiTrainMetrics.js +148 -8
  7. package/dist/lib/aiTrainOptions.js +130 -0
  8. package/dist/lib/aiTrainQuarantine.js +199 -0
  9. package/dist/lib/aiTrainResearch.js +467 -0
  10. package/dist/lib/binanceMarketContextBackfill.js +841 -0
  11. package/dist/lib/binanceMarketData.js +392 -0
  12. package/dist/lib/coinMarketCapContextBackfill.js +1088 -0
  13. package/dist/lib/continuity.js +60 -0
  14. package/dist/lib/derivativesContextBackfill.js +490 -77
  15. package/dist/lib/executionCalibration.js +705 -0
  16. package/dist/lib/marketContextPrepare.js +4403 -0
  17. package/dist/lib/paritySummary.js +101 -0
  18. package/dist/lib/runBot.js +1 -0
  19. package/dist/lib/runEnvironment.js +2254 -0
  20. package/dist/lib/runFormatting.js +1657 -0
  21. package/dist/lib/runtimeDebugEvidence.js +526 -0
  22. package/dist/lib/runtimeModeConfig.js +53 -0
  23. package/dist/lib/runtimeParity.js +88 -11
  24. package/dist/lib/runtimeParityDetails.js +751 -0
  25. package/dist/lib/runtimeRedis.js +157 -0
  26. package/dist/lib/runtimeSignalsLoader.js +40 -6
  27. package/dist/lib/runtimeSignalsStorage.js +45 -22
  28. package/dist/lib/runtimeStrategyBacktest.js +134 -0
  29. package/dist/lib/runtimeTradeSync.js +263 -0
  30. package/dist/lib/telegramReports.js +9 -0
  31. package/dist/lib/tickerUniverseCache.js +116 -0
  32. package/dist/lib/timeWindow.js +3 -3
  33. package/dist/scripts/agentRun.js +14 -8
  34. package/dist/scripts/aiExport.js +1721 -7
  35. package/dist/scripts/aiExportSelect.js +16 -4
  36. package/dist/scripts/aiPocketSearch.js +4190 -0
  37. package/dist/scripts/aiTrain.js +1866 -87
  38. package/dist/scripts/backtest.js +4509 -818
  39. package/dist/scripts/binanceMarketIngest.js +565 -0
  40. package/dist/scripts/bot.js +16 -2
  41. package/dist/scripts/candlesMigrateProvider.js +32 -22
  42. package/dist/scripts/cleanDir.js +15 -2
  43. package/dist/scripts/cleanRedis.js +15 -2
  44. package/dist/scripts/cleanTests.js +38 -2
  45. package/dist/scripts/cleanupMarketContext.js +69 -0
  46. package/dist/scripts/continuity.js +148 -67
  47. package/dist/scripts/derivativesIngest.js +14 -4
  48. package/dist/scripts/derivativesIngestCoinalyzeAll.js +14 -4
  49. package/dist/scripts/doctor.js +14 -4
  50. package/dist/scripts/executionCalibration.js +792 -0
  51. package/dist/scripts/infraCommon.js +10 -0
  52. package/dist/scripts/infraDown.js +14 -4
  53. package/dist/scripts/infraInit.js +24 -4
  54. package/dist/scripts/infraUp.js +14 -4
  55. package/dist/scripts/marketWs.js +222 -0
  56. package/dist/scripts/migration.js +15 -2
  57. package/dist/scripts/mlExport.js +1698 -10
  58. package/dist/scripts/mlExportSelect.js +16 -4
  59. package/dist/scripts/mlInspect.js +13 -3
  60. package/dist/scripts/mlTrainLatestSelect.js +14 -4
  61. package/dist/scripts/replay.js +8824 -0
  62. package/dist/scripts/replayRunner.js +8827 -0
  63. package/dist/scripts/replayRuntimeEvidence.js +868 -0
  64. package/dist/scripts/researchAuto.js +286 -59
  65. package/dist/scripts/results.js +3 -3
  66. package/dist/scripts/runtimeEvidence.js +605 -0
  67. package/dist/scripts/runtimeParity.js +6362 -1043
  68. package/dist/scripts/serverHealth.js +588 -0
  69. package/dist/scripts/signals.js +5590 -511
  70. package/dist/scripts/signalsDaemon.js +6167 -0
  71. package/dist/scripts/signalsSummary.js +833 -248
  72. package/dist/scripts/test-ml.js +25 -3
  73. package/dist/scripts/test.js +26 -1
  74. package/dist/scripts/user-add.js +15 -2
  75. package/dist/workers/testerWorker.js +130 -21
  76. package/package.json +18 -12
@@ -0,0 +1,792 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/scripts/executionCalibration.ts
31
+ var executionCalibration_exports = {};
32
+ __export(executionCalibration_exports, {
33
+ executionCalibration: () => executionCalibration,
34
+ main: () => main
35
+ });
36
+ module.exports = __toCommonJS(executionCalibration_exports);
37
+ var import_promises = __toESM(require("fs/promises"));
38
+ var import_node_path = __toESM(require("path"));
39
+ var import_args = __toESM(require("args"));
40
+ var import_chalk = __toESM(require("chalk"));
41
+
42
+ // src/lib/executionCalibration.ts
43
+ var import_constants = require("@tradejs/core/constants");
44
+ var import_data = require("@tradejs/core/data");
45
+ var import_trade = require("@tradejs/core/trade");
46
+ var roundMetric = (value, decimals = 6) => typeof value === "number" && Number.isFinite(value) ? Number(value.toFixed(decimals)) : null;
47
+ var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : null;
48
+ var asArray = (value) => Array.isArray(value) ? value : [];
49
+ var finiteNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : null;
50
+ var finiteString = (value) => typeof value === "string" && value.trim() ? value.trim() : null;
51
+ var finiteDirection = (value) => value === "LONG" || value === "SHORT" ? value : null;
52
+ var metricValues = (samples, key) => samples.map((sample) => sample[key]).filter(
53
+ (value) => typeof value === "number" && Number.isFinite(value)
54
+ );
55
+ var quantile = (values, probability) => {
56
+ if (!values.length) {
57
+ return null;
58
+ }
59
+ const sorted = [...values].sort((left, right) => left - right);
60
+ const index = (sorted.length - 1) * probability;
61
+ const lowerIndex = Math.floor(index);
62
+ const upperIndex = Math.ceil(index);
63
+ const lower = sorted[lowerIndex];
64
+ const upper = sorted[upperIndex];
65
+ if (lower == null || upper == null) {
66
+ return null;
67
+ }
68
+ if (lowerIndex === upperIndex) {
69
+ return lower;
70
+ }
71
+ return lower + (upper - lower) * (index - lowerIndex);
72
+ };
73
+ var summarizeNumbers = (values) => {
74
+ const finite = values.filter((value) => Number.isFinite(value));
75
+ if (!finite.length) {
76
+ return {
77
+ count: 0,
78
+ min: null,
79
+ max: null,
80
+ avg: null,
81
+ p50: null,
82
+ p75: null,
83
+ p90: null,
84
+ p95: null
85
+ };
86
+ }
87
+ const total = finite.reduce((sum, value) => sum + value, 0);
88
+ return {
89
+ count: finite.length,
90
+ min: roundMetric(Math.min(...finite)),
91
+ max: roundMetric(Math.max(...finite)),
92
+ avg: roundMetric(total / finite.length),
93
+ p50: roundMetric(quantile(finite, 0.5)),
94
+ p75: roundMetric(quantile(finite, 0.75)),
95
+ p90: roundMetric(quantile(finite, 0.9)),
96
+ p95: roundMetric(quantile(finite, 0.95))
97
+ };
98
+ };
99
+ var getIntervalMs = (interval) => {
100
+ if (!interval) {
101
+ return null;
102
+ }
103
+ try {
104
+ return (0, import_data.intervalToMs)(interval);
105
+ } catch {
106
+ return null;
107
+ }
108
+ };
109
+ var getSignalKey = ({
110
+ strategy,
111
+ symbol,
112
+ timestamp
113
+ }) => strategy && symbol && timestamp != null ? `${strategy}:${symbol}:${timestamp}` : null;
114
+ var addSignalToMap = (signalsById, signalsByShape, signal) => {
115
+ if (!signal) {
116
+ return;
117
+ }
118
+ const signalId = finiteString(signal.signalId);
119
+ if (signalId) {
120
+ signalsById.set(signalId, signal);
121
+ }
122
+ const shapeKey = getSignalKey({
123
+ strategy: finiteString(signal.strategy),
124
+ symbol: finiteString(signal.symbol),
125
+ timestamp: finiteNumber(signal.timestamp)
126
+ });
127
+ if (shapeKey) {
128
+ signalsByShape.set(shapeKey, signal);
129
+ }
130
+ };
131
+ var getRuntimePayload = ({
132
+ runtimeArtifact,
133
+ replayEvidenceArtifact
134
+ }) => {
135
+ const explicitRuntime = asRecord(runtimeArtifact);
136
+ if (explicitRuntime) {
137
+ return asRecord(explicitRuntime.runtime) ?? explicitRuntime;
138
+ }
139
+ const replayEvidence = asRecord(replayEvidenceArtifact);
140
+ return asRecord(replayEvidence?.runtime);
141
+ };
142
+ var extractRuntimeTradeRows = (runtimePayload) => {
143
+ if (!runtimePayload) {
144
+ return [];
145
+ }
146
+ const signalsById = /* @__PURE__ */ new Map();
147
+ const signalsByShape = /* @__PURE__ */ new Map();
148
+ for (const signalRow of asArray(runtimePayload.signals)) {
149
+ const record = asRecord(signalRow);
150
+ addSignalToMap(
151
+ signalsById,
152
+ signalsByShape,
153
+ asRecord(record?.signal) ?? record
154
+ );
155
+ }
156
+ return asArray(runtimePayload.trades).map((row) => {
157
+ const record = asRecord(row);
158
+ if (!record) {
159
+ return null;
160
+ }
161
+ const redisValues = asRecord(record.redisValues);
162
+ const trade = asRecord(record.trade) ?? asRecord(redisValues?.trade) ?? record;
163
+ const signalId = finiteString(trade.signalId);
164
+ const shapeKey = getSignalKey({
165
+ strategy: finiteString(trade.strategy),
166
+ symbol: finiteString(trade.symbol),
167
+ timestamp: finiteNumber(trade.signalTimestamp ?? trade.entryTimestamp)
168
+ });
169
+ const signal = asRecord(redisValues?.signal) ?? (signalId ? signalsById.get(signalId) ?? null : null) ?? (shapeKey ? signalsByShape.get(shapeKey) ?? null : null);
170
+ return {
171
+ trade,
172
+ signal
173
+ };
174
+ }).filter((row) => row != null);
175
+ };
176
+ var normalizeReplayMatch = (value) => {
177
+ const item = asRecord(value);
178
+ if (!item) {
179
+ return null;
180
+ }
181
+ const runtime = asRecord(item.runtime);
182
+ const backtest = asRecord(item.backtest);
183
+ return {
184
+ orderId: finiteString(
185
+ item.orderId ?? runtime?.orderId ?? backtest?.orderId
186
+ ),
187
+ orderLinkId: finiteString(item.orderLinkId ?? runtime?.orderLinkId),
188
+ signalId: finiteString(item.signalId ?? backtest?.signalId),
189
+ strategy: finiteString(
190
+ item.strategy ?? backtest?.strategy ?? runtime?.inferredStrategy
191
+ ),
192
+ symbol: finiteString(item.symbol ?? backtest?.symbol ?? runtime?.symbol),
193
+ direction: finiteDirection(
194
+ item.direction ?? backtest?.direction ?? runtime?.direction
195
+ ),
196
+ backtestPrice: finiteNumber(item.backtestPrice ?? backtest?.price),
197
+ runtimePrice: finiteNumber(item.runtimePrice ?? runtime?.price)
198
+ };
199
+ };
200
+ var extractReplayMatches = (replayEvidenceArtifact) => {
201
+ const artifact = asRecord(replayEvidenceArtifact);
202
+ if (!artifact) {
203
+ return [];
204
+ }
205
+ const replay = asRecord(artifact.replay) ?? artifact;
206
+ const runtimeComparison = asRecord(replay.runtimeComparison);
207
+ const details = asRecord(runtimeComparison?.details);
208
+ const matched = asArray(runtimeComparison?.matched).length ? asArray(runtimeComparison?.matched) : asArray(details?.matched);
209
+ return matched.map(normalizeReplayMatch).filter((match) => match != null);
210
+ };
211
+ var buildReplayMatchMaps = (matches) => {
212
+ const maps = {
213
+ byOrderId: /* @__PURE__ */ new Map(),
214
+ bySignalId: /* @__PURE__ */ new Map()
215
+ };
216
+ for (const match of matches) {
217
+ for (const orderId of [match.orderLinkId, match.orderId]) {
218
+ if (orderId) {
219
+ maps.byOrderId.set(orderId, match);
220
+ }
221
+ }
222
+ if (match.signalId) {
223
+ maps.bySignalId.set(match.signalId, match);
224
+ }
225
+ }
226
+ return maps;
227
+ };
228
+ var getSignalCandles = (signal, interval) => {
229
+ const indicators = asRecord(signal?.indicators);
230
+ if (!indicators) {
231
+ return null;
232
+ }
233
+ const intervalKey = (() => {
234
+ switch (interval) {
235
+ case "15":
236
+ return "candles15m";
237
+ case "60":
238
+ return "candles1h";
239
+ case "240":
240
+ return "candles4h";
241
+ case "D":
242
+ return "candles1d";
243
+ default:
244
+ return null;
245
+ }
246
+ })();
247
+ const keys = [
248
+ intervalKey,
249
+ "candles15m",
250
+ "candles1h",
251
+ "candles4h",
252
+ "candles1d",
253
+ "candles"
254
+ ].filter((key) => Boolean(key));
255
+ for (const key of keys) {
256
+ const value = indicators[key];
257
+ if (Array.isArray(value) && value.length > 1) {
258
+ return value;
259
+ }
260
+ }
261
+ const candle = indicators.candle;
262
+ const prevCandle = indicators.prevCandle;
263
+ return prevCandle && candle ? [prevCandle, candle] : null;
264
+ };
265
+ var adverseEntryBps = ({
266
+ direction,
267
+ expectedPrice,
268
+ actualPrice
269
+ }) => {
270
+ if (!direction || expectedPrice == null || actualPrice == null || expectedPrice <= 0 || actualPrice <= 0) {
271
+ return null;
272
+ }
273
+ const value = direction === "LONG" ? (actualPrice / expectedPrice - 1) * 1e4 : (expectedPrice / actualPrice - 1) * 1e4;
274
+ return roundMetric(value);
275
+ };
276
+ var feeBps = ({
277
+ fee,
278
+ fillAvgPrice,
279
+ qty
280
+ }) => {
281
+ const notional = fillAvgPrice != null && qty != null && fillAvgPrice > 0 && qty > 0 ? fillAvgPrice * qty : null;
282
+ return fee != null && notional != null && notional > 0 ? roundMetric(fee / notional * 1e4) : null;
283
+ };
284
+ var latencyDelta = (end, start) => end != null && start != null ? roundMetric(end - start, 3) : null;
285
+ var resolveTelemetryQuality = ({
286
+ signalClosePrice,
287
+ arrivalMid,
288
+ orderSubmitTime,
289
+ orderAckTime,
290
+ fillAvgPrice,
291
+ fillTime
292
+ }) => {
293
+ if (signalClosePrice != null && arrivalMid != null && orderSubmitTime != null && orderAckTime != null && fillAvgPrice != null && fillTime != null) {
294
+ return "full";
295
+ }
296
+ if (fillAvgPrice != null && (arrivalMid != null || orderSubmitTime != null)) {
297
+ return "partial";
298
+ }
299
+ if (fillAvgPrice != null) {
300
+ return "price_only";
301
+ }
302
+ return "none";
303
+ };
304
+ var finiteTelemetryQuality = (value) => {
305
+ const raw = finiteString(value);
306
+ return raw === "full" || raw === "partial" || raw === "price_only" || raw === "none" ? raw : null;
307
+ };
308
+ var countBy = (samples, keyGetter) => {
309
+ const counts = /* @__PURE__ */ new Map();
310
+ for (const sample of samples) {
311
+ const key = keyGetter(sample) ?? "unknown";
312
+ counts.set(key, (counts.get(key) ?? 0) + 1);
313
+ }
314
+ return Object.fromEntries(
315
+ [...counts.entries()].sort(([left], [right]) => left.localeCompare(right))
316
+ );
317
+ };
318
+ var toLatencyBucket = (sample) => {
319
+ const latencyMs = sample.signalCloseToSubmitMs ?? sample.signalToSubmitMs;
320
+ if (latencyMs == null || !Number.isFinite(latencyMs)) return "unknown";
321
+ if (latencyMs <= 1e3) return "0-1s";
322
+ if (latencyMs <= 5e3) return "1-5s";
323
+ if (latencyMs <= 3e4) return "5-30s";
324
+ if (latencyMs <= 12e4) return "30-120s";
325
+ return "120s+";
326
+ };
327
+ var toSpreadBucket = (sample) => {
328
+ const spreadBps = sample.spreadBps;
329
+ if (spreadBps == null || !Number.isFinite(spreadBps)) return "unknown";
330
+ if (spreadBps <= 5) return "0-5bps";
331
+ if (spreadBps <= 20) return "5-20bps";
332
+ if (spreadBps <= 50) return "20-50bps";
333
+ return "50bps+";
334
+ };
335
+ var buildSample = ({
336
+ row,
337
+ match
338
+ }) => {
339
+ const { trade, signal } = row;
340
+ const strategy = finiteString(trade.strategy ?? signal?.strategy);
341
+ const symbol = finiteString(trade.symbol ?? signal?.symbol);
342
+ const interval = finiteString(trade.interval ?? signal?.interval);
343
+ const direction = finiteDirection(trade.direction ?? signal?.direction);
344
+ const orderId = finiteString(trade.orderId);
345
+ const signalId = finiteString(trade.signalId ?? signal?.signalId);
346
+ const qty = finiteNumber(trade.qty);
347
+ const signalTimestamp = finiteNumber(
348
+ trade.signalTimestamp ?? signal?.timestamp
349
+ );
350
+ const intervalMs = getIntervalMs(interval);
351
+ const signalCloseTimestamp = signalTimestamp != null && intervalMs != null ? signalTimestamp + intervalMs : signalTimestamp;
352
+ const signalClosePrice = finiteNumber(trade.signalClosePrice);
353
+ const arrivalSnapshotTime = finiteNumber(trade.arrivalSnapshotTime);
354
+ const arrivalSource = finiteString(trade.arrivalSource);
355
+ const arrivalMid = finiteNumber(trade.arrivalMid);
356
+ const bid = finiteNumber(trade.bid);
357
+ const ask = finiteNumber(trade.ask);
358
+ const spreadBps = finiteNumber(trade.spreadBps);
359
+ const orderSubmitTime = finiteNumber(trade.orderSubmitTime);
360
+ const orderAckTime = finiteNumber(trade.orderAckTime);
361
+ const fillAvgPrice = finiteNumber(trade.fillAvgPrice);
362
+ const fillSource = finiteString(trade.fillSource);
363
+ const fillTime = finiteNumber(trade.fillTime);
364
+ const telemetryQuality = finiteTelemetryQuality(trade.telemetryQuality) ?? resolveTelemetryQuality({
365
+ signalClosePrice,
366
+ arrivalMid,
367
+ orderSubmitTime,
368
+ orderAckTime,
369
+ fillAvgPrice,
370
+ fillTime
371
+ });
372
+ const fee = finiteNumber(trade.fee ?? trade.openFee);
373
+ const arrivalSnapshotAgeMs = latencyDelta(
374
+ orderSubmitTime,
375
+ arrivalSnapshotTime
376
+ );
377
+ const signalToSubmitMs = latencyDelta(orderSubmitTime, signalTimestamp);
378
+ const signalCloseToSubmitMs = latencyDelta(
379
+ orderSubmitTime,
380
+ signalCloseTimestamp
381
+ );
382
+ const submitToAckMs = latencyDelta(orderAckTime, orderSubmitTime);
383
+ const submitToFillMs = latencyDelta(fillTime, orderSubmitTime);
384
+ const orderAckToFillMs = latencyDelta(fillTime, orderAckTime);
385
+ const signalToFillMs = latencyDelta(fillTime, signalTimestamp);
386
+ const observedDelayMs = signalCloseToSubmitMs != null && signalCloseToSubmitMs >= 0 ? signalCloseToSubmitMs : signalToSubmitMs != null && signalToSubmitMs >= 0 ? signalToSubmitMs : null;
387
+ const candles = getSignalCandles(signal, interval);
388
+ const currentDelayRiskBps = roundMetric(
389
+ (0, import_trade.calculateDelayRiskBps)({
390
+ candles,
391
+ intervalMs
392
+ })
393
+ );
394
+ const rawConfiguredDelayRiskBps = roundMetric(
395
+ (0, import_trade.calculateDelayRiskBps)({
396
+ candles,
397
+ intervalMs,
398
+ multiplier: 1,
399
+ maxBps: Number.POSITIVE_INFINITY
400
+ })
401
+ );
402
+ const rawObservedDelayRiskBps = roundMetric(
403
+ (0, import_trade.calculateDelayRiskBps)({
404
+ candles,
405
+ intervalMs,
406
+ expectedDelayMs: observedDelayMs,
407
+ multiplier: 1,
408
+ maxBps: Number.POSITIVE_INFINITY
409
+ })
410
+ );
411
+ const signalToArrivalAdverseBps = adverseEntryBps({
412
+ direction,
413
+ expectedPrice: signalClosePrice,
414
+ actualPrice: arrivalMid
415
+ });
416
+ const arrivalToFillAdverseBps = adverseEntryBps({
417
+ direction,
418
+ expectedPrice: arrivalMid,
419
+ actualPrice: fillAvgPrice
420
+ });
421
+ const signalToFillAdverseBps = adverseEntryBps({
422
+ direction,
423
+ expectedPrice: signalClosePrice,
424
+ actualPrice: fillAvgPrice
425
+ });
426
+ const currentModelEntrySlippageBps = signalToFillAdverseBps != null ? roundMetric(
427
+ import_constants.BACKTEST_BASE_SLIPPAGE_BPS + (spreadBps ?? 0) * import_constants.BACKTEST_SPREAD_SLIPPAGE_MULTIPLIER + import_constants.BACKTEST_MARKET_IMPACT_BPS + (currentDelayRiskBps ?? 0)
428
+ ) : null;
429
+ const residualVsCurrentModelBps = signalToFillAdverseBps != null && currentModelEntrySlippageBps != null ? roundMetric(signalToFillAdverseBps - currentModelEntrySlippageBps) : null;
430
+ return {
431
+ strategy,
432
+ symbol,
433
+ interval,
434
+ direction,
435
+ orderId,
436
+ signalId,
437
+ qty,
438
+ signalTimestamp,
439
+ signalCloseTimestamp,
440
+ signalClosePrice,
441
+ arrivalSnapshotTime,
442
+ arrivalSource,
443
+ arrivalMid,
444
+ bid,
445
+ ask,
446
+ spreadBps,
447
+ orderSubmitTime,
448
+ orderAckTime,
449
+ fillAvgPrice,
450
+ fillSource,
451
+ fillTime,
452
+ telemetryQuality,
453
+ fee,
454
+ feeBps: feeBps({ fee, fillAvgPrice, qty }),
455
+ arrivalSnapshotAgeMs,
456
+ signalToSubmitMs,
457
+ signalCloseToSubmitMs,
458
+ submitToAckMs,
459
+ submitToFillMs,
460
+ orderAckToFillMs,
461
+ signalToFillMs,
462
+ signalToArrivalAdverseBps,
463
+ arrivalToFillAdverseBps,
464
+ signalToFillAdverseBps,
465
+ currentDelayRiskBps,
466
+ rawConfiguredDelayRiskBps,
467
+ rawObservedDelayRiskBps,
468
+ currentModelEntrySlippageBps,
469
+ residualVsCurrentModelBps,
470
+ replayEntryResidualBps: adverseEntryBps({
471
+ direction: direction ?? match?.direction ?? null,
472
+ expectedPrice: match?.backtestPrice ?? null,
473
+ actualPrice: match?.runtimePrice ?? null
474
+ })
475
+ };
476
+ };
477
+ var buildReplayOnlySample = (match) => ({
478
+ strategy: match.strategy,
479
+ symbol: match.symbol,
480
+ interval: null,
481
+ direction: match.direction,
482
+ orderId: match.orderLinkId ?? match.orderId,
483
+ signalId: match.signalId,
484
+ qty: null,
485
+ signalTimestamp: null,
486
+ signalCloseTimestamp: null,
487
+ signalClosePrice: null,
488
+ arrivalSnapshotTime: null,
489
+ arrivalSource: null,
490
+ arrivalMid: null,
491
+ bid: null,
492
+ ask: null,
493
+ spreadBps: null,
494
+ orderSubmitTime: null,
495
+ orderAckTime: null,
496
+ fillAvgPrice: match.runtimePrice,
497
+ fillSource: "replay_runtime_price",
498
+ fillTime: null,
499
+ telemetryQuality: match.runtimePrice != null ? "price_only" : "none",
500
+ fee: null,
501
+ feeBps: null,
502
+ arrivalSnapshotAgeMs: null,
503
+ signalToSubmitMs: null,
504
+ signalCloseToSubmitMs: null,
505
+ submitToAckMs: null,
506
+ submitToFillMs: null,
507
+ orderAckToFillMs: null,
508
+ signalToFillMs: null,
509
+ signalToArrivalAdverseBps: null,
510
+ arrivalToFillAdverseBps: null,
511
+ signalToFillAdverseBps: null,
512
+ currentDelayRiskBps: null,
513
+ rawConfiguredDelayRiskBps: null,
514
+ rawObservedDelayRiskBps: null,
515
+ currentModelEntrySlippageBps: null,
516
+ residualVsCurrentModelBps: null,
517
+ replayEntryResidualBps: adverseEntryBps({
518
+ direction: match.direction,
519
+ expectedPrice: match.backtestPrice,
520
+ actualPrice: match.runtimePrice
521
+ })
522
+ });
523
+ var hasTelemetry = (sample) => sample.signalClosePrice != null || sample.arrivalMid != null || sample.orderSubmitTime != null || sample.fillAvgPrice != null || sample.fillTime != null || sample.fee != null;
524
+ var hasFullTelemetry = (sample) => sample.signalClosePrice != null && sample.arrivalMid != null && sample.orderSubmitTime != null && sample.orderAckTime != null && sample.fillAvgPrice != null && sample.fillTime != null;
525
+ var buildGroupSummary = (samples) => ({
526
+ trades: samples.length,
527
+ fullTelemetryTrades: samples.filter(hasFullTelemetry).length,
528
+ replayMatchedTrades: metricValues(samples, "replayEntryResidualBps").length,
529
+ telemetryQuality: countBy(samples, (sample) => sample.telemetryQuality),
530
+ fillSource: countBy(samples, (sample) => sample.fillSource),
531
+ arrivalSource: countBy(samples, (sample) => sample.arrivalSource),
532
+ signalToArrivalAdverseBps: summarizeNumbers(
533
+ metricValues(samples, "signalToArrivalAdverseBps")
534
+ ),
535
+ arrivalToFillAdverseBps: summarizeNumbers(
536
+ metricValues(samples, "arrivalToFillAdverseBps")
537
+ ),
538
+ signalToFillAdverseBps: summarizeNumbers(
539
+ metricValues(samples, "signalToFillAdverseBps")
540
+ ),
541
+ residualVsCurrentModelBps: summarizeNumbers(
542
+ metricValues(samples, "residualVsCurrentModelBps")
543
+ ),
544
+ replayEntryResidualBps: summarizeNumbers(
545
+ metricValues(samples, "replayEntryResidualBps")
546
+ ),
547
+ spreadBps: summarizeNumbers(metricValues(samples, "spreadBps")),
548
+ feeBps: summarizeNumbers(metricValues(samples, "feeBps")),
549
+ arrivalSnapshotAgeMs: summarizeNumbers(
550
+ metricValues(samples, "arrivalSnapshotAgeMs")
551
+ ),
552
+ signalToSubmitMs: summarizeNumbers(metricValues(samples, "signalToSubmitMs")),
553
+ signalCloseToSubmitMs: summarizeNumbers(
554
+ metricValues(samples, "signalCloseToSubmitMs")
555
+ ),
556
+ submitToAckMs: summarizeNumbers(metricValues(samples, "submitToAckMs")),
557
+ submitToFillMs: summarizeNumbers(metricValues(samples, "submitToFillMs")),
558
+ orderAckToFillMs: summarizeNumbers(metricValues(samples, "orderAckToFillMs")),
559
+ currentDelayRiskBps: summarizeNumbers(
560
+ metricValues(samples, "currentDelayRiskBps")
561
+ ),
562
+ rawConfiguredDelayRiskBps: summarizeNumbers(
563
+ metricValues(samples, "rawConfiguredDelayRiskBps")
564
+ ),
565
+ rawObservedDelayRiskBps: summarizeNumbers(
566
+ metricValues(samples, "rawObservedDelayRiskBps")
567
+ )
568
+ });
569
+ var groupSamples = (samples, keyGetter) => {
570
+ const groups = /* @__PURE__ */ new Map();
571
+ for (const sample of samples) {
572
+ const key = keyGetter(sample);
573
+ if (!key) {
574
+ continue;
575
+ }
576
+ groups.set(key, [...groups.get(key) ?? [], sample]);
577
+ }
578
+ return Object.fromEntries(
579
+ [...groups.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([key, group]) => [key, buildGroupSummary(group)])
580
+ );
581
+ };
582
+ var positiveMetricValues = (samples, key) => metricValues(samples, key).filter((value) => value > 0);
583
+ var buildRecommendation = (samples) => {
584
+ const fullTelemetrySamples = samples.filter(hasFullTelemetry);
585
+ const notes = [];
586
+ if (!fullTelemetrySamples.length) {
587
+ return {
588
+ confidence: "none",
589
+ baseSlippageBps: null,
590
+ spreadMultiplier: null,
591
+ delayRiskMultiplier: null,
592
+ delayRiskMaxBps: null,
593
+ expectedDelayMs: null,
594
+ notes: [
595
+ "No full live execution telemetry was found. Use a newer runtime debug artifact collected after telemetry persistence was enabled.",
596
+ "Replay residuals can still diagnose model drift, but they are not enough to calibrate signal-to-arrival and arrival-to-fill components separately."
597
+ ]
598
+ };
599
+ }
600
+ if (fullTelemetrySamples.length < 20) {
601
+ notes.push(
602
+ "Sample is small; treat recommendations as diagnostic until at least 20-50 live fills are available."
603
+ );
604
+ }
605
+ const spreadRatios = fullTelemetrySamples.map((sample) => {
606
+ if (sample.arrivalToFillAdverseBps == null || sample.spreadBps == null || sample.spreadBps <= 0) {
607
+ return null;
608
+ }
609
+ return Math.max(0, sample.arrivalToFillAdverseBps) / sample.spreadBps;
610
+ }).filter(
611
+ (value) => value != null && Number.isFinite(value)
612
+ );
613
+ const spreadMultiplier = roundMetric(quantile(spreadRatios, 0.75), 4);
614
+ const baseResiduals = fullTelemetrySamples.map((sample) => {
615
+ if (sample.arrivalToFillAdverseBps == null) {
616
+ return null;
617
+ }
618
+ return Math.max(
619
+ 0,
620
+ sample.arrivalToFillAdverseBps - (sample.spreadBps ?? 0) * (spreadMultiplier ?? import_constants.BACKTEST_SPREAD_SLIPPAGE_MULTIPLIER)
621
+ );
622
+ }).filter(
623
+ (value) => value != null && Number.isFinite(value)
624
+ );
625
+ const closeDelayMs = positiveMetricValues(
626
+ fullTelemetrySamples,
627
+ "signalCloseToSubmitMs"
628
+ );
629
+ const fallbackDelayMs = positiveMetricValues(
630
+ fullTelemetrySamples,
631
+ "signalToSubmitMs"
632
+ );
633
+ if (!spreadRatios.length) {
634
+ notes.push(
635
+ "Spread multiplier could not be calibrated because spreadBps or arrival-to-fill telemetry is missing."
636
+ );
637
+ }
638
+ notes.push(
639
+ "Delay risk bps is disabled; signal-to-arrival latency is modeled by delayed lower-timeframe backtest fills."
640
+ );
641
+ return {
642
+ confidence: fullTelemetrySamples.length >= 50 ? "high" : fullTelemetrySamples.length >= 20 ? "medium" : "low",
643
+ baseSlippageBps: roundMetric(quantile(baseResiduals, 0.75), 2),
644
+ spreadMultiplier,
645
+ delayRiskMultiplier: null,
646
+ delayRiskMaxBps: null,
647
+ expectedDelayMs: roundMetric(
648
+ quantile(closeDelayMs.length ? closeDelayMs : fallbackDelayMs, 0.5),
649
+ 0
650
+ ),
651
+ notes
652
+ };
653
+ };
654
+ var buildExecutionCalibrationReport = ({
655
+ runtimeArtifact,
656
+ replayEvidenceArtifact,
657
+ sourcePaths = {},
658
+ nowMs = Date.now()
659
+ }) => {
660
+ const runtimePayload = getRuntimePayload({
661
+ runtimeArtifact,
662
+ replayEvidenceArtifact
663
+ });
664
+ const runtimeRows = extractRuntimeTradeRows(runtimePayload);
665
+ const replayMatches = extractReplayMatches(replayEvidenceArtifact);
666
+ const replayMaps = buildReplayMatchMaps(replayMatches);
667
+ const usedReplayMatches = /* @__PURE__ */ new Set();
668
+ const samples = runtimeRows.map((row) => {
669
+ const orderId = finiteString(row.trade.orderId);
670
+ const signalId = finiteString(row.trade.signalId);
671
+ const match = (orderId ? replayMaps.byOrderId.get(orderId) ?? null : null) ?? (signalId ? replayMaps.bySignalId.get(signalId) ?? null : null);
672
+ if (match) {
673
+ usedReplayMatches.add(match);
674
+ }
675
+ return buildSample({ row, match });
676
+ });
677
+ const replayOnlySamples = replayMatches.filter((match) => !usedReplayMatches.has(match)).map(buildReplayOnlySample);
678
+ return {
679
+ reportType: "execution-calibration",
680
+ generatedAt: nowMs,
681
+ sources: sourcePaths,
682
+ currentModel: {
683
+ baseSlippageBps: import_constants.BACKTEST_BASE_SLIPPAGE_BPS,
684
+ spreadMultiplier: import_constants.BACKTEST_SPREAD_SLIPPAGE_MULTIPLIER,
685
+ marketImpactBps: import_constants.BACKTEST_MARKET_IMPACT_BPS
686
+ },
687
+ counts: {
688
+ runtimeTrades: runtimeRows.length,
689
+ telemetryTrades: samples.filter(hasTelemetry).length,
690
+ fullTelemetryTrades: samples.filter(hasFullTelemetry).length,
691
+ replayMatched: replayMatches.length,
692
+ replayMatchedRuntimeTrades: usedReplayMatches.size,
693
+ replayOnlyMatches: replayOnlySamples.length
694
+ },
695
+ summary: {
696
+ all: buildGroupSummary([...samples, ...replayOnlySamples]),
697
+ byStrategy: groupSamples(
698
+ [...samples, ...replayOnlySamples],
699
+ (sample) => sample.strategy
700
+ ),
701
+ bySymbol: groupSamples(samples, (sample) => sample.symbol),
702
+ byInterval: groupSamples(samples, (sample) => sample.interval),
703
+ byTelemetryQuality: groupSamples(
704
+ [...samples, ...replayOnlySamples],
705
+ (sample) => sample.telemetryQuality
706
+ ),
707
+ byFillSource: groupSamples(
708
+ [...samples, ...replayOnlySamples],
709
+ (sample) => sample.fillSource
710
+ ),
711
+ byLatencyBucket: groupSamples(samples, toLatencyBucket),
712
+ bySpreadBucket: groupSamples(samples, toSpreadBucket)
713
+ },
714
+ recommendation: buildRecommendation(samples),
715
+ samples,
716
+ replayOnlySamples
717
+ };
718
+ };
719
+
720
+ // src/scripts/executionCalibration.ts
721
+ import_args.default.option(
722
+ "runtimeEvidence",
723
+ "Runtime debug JSON collected on the runtime server"
724
+ );
725
+ import_args.default.option(
726
+ "replayEvidence",
727
+ "Replay runtime evidence JSON produced by replay-runtime-evidence"
728
+ );
729
+ import_args.default.option(
730
+ ["o", "out"],
731
+ "Output JSON path",
732
+ "output/execution-calibration.json"
733
+ );
734
+ var flags = import_args.default.parse(process.argv);
735
+ var projectRoot = String(process.env.PROJECT_CWD || process.cwd()).trim() || process.cwd();
736
+ var readJsonFile = async (filePath) => JSON.parse(await import_promises.default.readFile(filePath, "utf8"));
737
+ var resolveInputPath = (value) => {
738
+ const raw = String(value ?? "").trim();
739
+ return raw ? import_node_path.default.resolve(projectRoot, raw) : null;
740
+ };
741
+ var executionCalibration = async () => {
742
+ const runtimeEvidencePath = resolveInputPath(flags.runtimeEvidence);
743
+ const replayEvidencePath = resolveInputPath(flags.replayEvidence);
744
+ if (!runtimeEvidencePath && !replayEvidencePath) {
745
+ throw new Error(
746
+ "Provide --runtimeEvidence, --replayEvidence, or both for execution calibration."
747
+ );
748
+ }
749
+ const [runtimeArtifact, replayEvidenceArtifact] = await Promise.all([
750
+ runtimeEvidencePath ? readJsonFile(runtimeEvidencePath) : Promise.resolve(void 0),
751
+ replayEvidencePath ? readJsonFile(replayEvidencePath) : Promise.resolve(void 0)
752
+ ]);
753
+ const report = buildExecutionCalibrationReport({
754
+ runtimeArtifact,
755
+ replayEvidenceArtifact,
756
+ sourcePaths: {
757
+ runtimeEvidence: runtimeEvidencePath,
758
+ replayEvidence: replayEvidencePath
759
+ }
760
+ });
761
+ const outPath = import_node_path.default.resolve(projectRoot, String(flags.out));
762
+ await import_promises.default.mkdir(import_node_path.default.dirname(outPath), { recursive: true });
763
+ await import_promises.default.writeFile(outPath, `${JSON.stringify(report, null, 2)}
764
+ `, "utf8");
765
+ console.log(import_chalk.default.green(`Wrote ${outPath}`));
766
+ console.log(
767
+ JSON.stringify(
768
+ {
769
+ counts: report.counts,
770
+ currentModel: report.currentModel,
771
+ recommendation: report.recommendation,
772
+ summary: {
773
+ signalToArrivalAdverseBps: report.summary.all.signalToArrivalAdverseBps,
774
+ arrivalToFillAdverseBps: report.summary.all.arrivalToFillAdverseBps,
775
+ signalToFillAdverseBps: report.summary.all.signalToFillAdverseBps,
776
+ residualVsCurrentModelBps: report.summary.all.residualVsCurrentModelBps,
777
+ replayEntryResidualBps: report.summary.all.replayEntryResidualBps,
778
+ telemetryQuality: report.summary.all.telemetryQuality,
779
+ fillSource: report.summary.all.fillSource
780
+ }
781
+ },
782
+ null,
783
+ 2
784
+ )
785
+ );
786
+ };
787
+ var main = executionCalibration;
788
+ // Annotate the CommonJS export names for ESM import in node:
789
+ 0 && (module.exports = {
790
+ executionCalibration,
791
+ main
792
+ });