openalgo-script 0.2.0 → 0.4.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.
Files changed (276) hide show
  1. package/CHANGELOG.md +269 -0
  2. package/README.md +65 -31
  3. package/dist/adapters/charts/surfaces.d.ts +17 -0
  4. package/dist/adapters/charts/surfaces.d.ts.map +1 -1
  5. package/dist/adapters/charts/tables.js +82 -0
  6. package/dist/adapters/charts/tables.js.map +1 -1
  7. package/dist/adapters/codemirror/commands.d.ts +14 -0
  8. package/dist/adapters/codemirror/commands.d.ts.map +1 -0
  9. package/dist/adapters/codemirror/commands.js +52 -0
  10. package/dist/adapters/codemirror/commands.js.map +1 -0
  11. package/dist/adapters/codemirror/completion.d.ts +15 -0
  12. package/dist/adapters/codemirror/completion.d.ts.map +1 -0
  13. package/dist/adapters/codemirror/completion.js +64 -0
  14. package/dist/adapters/codemirror/completion.js.map +1 -0
  15. package/dist/adapters/codemirror/contract.d.ts +156 -0
  16. package/dist/adapters/codemirror/contract.d.ts.map +1 -0
  17. package/dist/adapters/codemirror/contract.js +42 -0
  18. package/dist/adapters/codemirror/contract.js.map +1 -0
  19. package/dist/adapters/codemirror/index.d.ts +43 -0
  20. package/dist/adapters/codemirror/index.d.ts.map +1 -0
  21. package/dist/adapters/codemirror/index.js +8 -0
  22. package/dist/adapters/codemirror/index.js.map +1 -0
  23. package/dist/adapters/codemirror/lint.d.ts +17 -0
  24. package/dist/adapters/codemirror/lint.d.ts.map +1 -0
  25. package/dist/adapters/codemirror/lint.js +62 -0
  26. package/dist/adapters/codemirror/lint.js.map +1 -0
  27. package/dist/adapters/codemirror/positions.d.ts +16 -0
  28. package/dist/adapters/codemirror/positions.d.ts.map +1 -0
  29. package/dist/adapters/codemirror/positions.js +65 -0
  30. package/dist/adapters/codemirror/positions.js.map +1 -0
  31. package/dist/adapters/codemirror/stream.d.ts +23 -0
  32. package/dist/adapters/codemirror/stream.d.ts.map +1 -0
  33. package/dist/adapters/codemirror/stream.js +70 -0
  34. package/dist/adapters/codemirror/stream.js.map +1 -0
  35. package/dist/adapters/codemirror/tokens.d.ts +44 -0
  36. package/dist/adapters/codemirror/tokens.d.ts.map +1 -0
  37. package/dist/adapters/codemirror/tokens.js +34 -0
  38. package/dist/adapters/codemirror/tokens.js.map +1 -0
  39. package/dist/adapters/codemirror/tooltips.d.ts +35 -0
  40. package/dist/adapters/codemirror/tooltips.d.ts.map +1 -0
  41. package/dist/adapters/codemirror/tooltips.js +108 -0
  42. package/dist/adapters/codemirror/tooltips.js.map +1 -0
  43. package/dist/core/accounting/charges.d.ts +136 -0
  44. package/dist/core/accounting/charges.d.ts.map +1 -0
  45. package/dist/core/accounting/charges.js +362 -0
  46. package/dist/core/accounting/charges.js.map +1 -0
  47. package/dist/core/accounting/equity.d.ts +158 -0
  48. package/dist/core/accounting/equity.d.ts.map +1 -0
  49. package/dist/core/accounting/equity.js +155 -0
  50. package/dist/core/accounting/equity.js.map +1 -0
  51. package/dist/core/accounting/index.d.ts +44 -0
  52. package/dist/core/accounting/index.d.ts.map +1 -0
  53. package/dist/core/accounting/index.js +36 -0
  54. package/dist/core/accounting/index.js.map +1 -0
  55. package/dist/core/accounting/markers.d.ts +43 -0
  56. package/dist/core/accounting/markers.d.ts.map +1 -0
  57. package/dist/core/accounting/markers.js +64 -0
  58. package/dist/core/accounting/markers.js.map +1 -0
  59. package/dist/core/accounting/monthly.d.ts +52 -0
  60. package/dist/core/accounting/monthly.d.ts.map +1 -0
  61. package/dist/core/accounting/monthly.js +99 -0
  62. package/dist/core/accounting/monthly.js.map +1 -0
  63. package/dist/core/accounting/report.d.ts +38 -0
  64. package/dist/core/accounting/report.d.ts.map +1 -0
  65. package/dist/core/accounting/report.js +63 -0
  66. package/dist/core/accounting/report.js.map +1 -0
  67. package/dist/core/accounting/shapes.d.ts +70 -0
  68. package/dist/core/accounting/shapes.d.ts.map +1 -0
  69. package/dist/core/accounting/shapes.js +21 -0
  70. package/dist/core/accounting/shapes.js.map +1 -0
  71. package/dist/core/accounting/statistics.d.ts +75 -0
  72. package/dist/core/accounting/statistics.d.ts.map +1 -0
  73. package/dist/core/accounting/statistics.js +246 -0
  74. package/dist/core/accounting/statistics.js.map +1 -0
  75. package/dist/core/accounting/trades.d.ts +118 -0
  76. package/dist/core/accounting/trades.d.ts.map +1 -0
  77. package/dist/core/accounting/trades.js +186 -0
  78. package/dist/core/accounting/trades.js.map +1 -0
  79. package/dist/core/backtest/compare.d.ts +38 -0
  80. package/dist/core/backtest/compare.d.ts.map +1 -0
  81. package/dist/core/backtest/compare.js +189 -0
  82. package/dist/core/backtest/compare.js.map +1 -0
  83. package/dist/core/backtest/declaration.d.ts +52 -0
  84. package/dist/core/backtest/declaration.d.ts.map +1 -0
  85. package/dist/core/backtest/declaration.js +48 -0
  86. package/dist/core/backtest/declaration.js.map +1 -0
  87. package/dist/core/backtest/drive.d.ts +29 -0
  88. package/dist/core/backtest/drive.d.ts.map +1 -0
  89. package/dist/core/backtest/drive.js +262 -0
  90. package/dist/core/backtest/drive.js.map +1 -0
  91. package/dist/core/backtest/index.d.ts +46 -0
  92. package/dist/core/backtest/index.d.ts.map +1 -0
  93. package/dist/core/backtest/index.js +36 -0
  94. package/dist/core/backtest/index.js.map +1 -0
  95. package/dist/core/backtest/range.d.ts +84 -0
  96. package/dist/core/backtest/range.d.ts.map +1 -0
  97. package/dist/core/backtest/range.js +90 -0
  98. package/dist/core/backtest/range.js.map +1 -0
  99. package/dist/core/backtest/record.d.ts +238 -0
  100. package/dist/core/backtest/record.d.ts.map +1 -0
  101. package/dist/core/backtest/record.js +176 -0
  102. package/dist/core/backtest/record.js.map +1 -0
  103. package/dist/core/backtest/replay.d.ts +48 -0
  104. package/dist/core/backtest/replay.d.ts.map +1 -0
  105. package/dist/core/backtest/replay.js +127 -0
  106. package/dist/core/backtest/replay.js.map +1 -0
  107. package/dist/core/backtest/resting.d.ts +62 -0
  108. package/dist/core/backtest/resting.d.ts.map +1 -0
  109. package/dist/core/backtest/resting.js +59 -0
  110. package/dist/core/backtest/resting.js.map +1 -0
  111. package/dist/core/backtest/settings.d.ts +117 -0
  112. package/dist/core/backtest/settings.d.ts.map +1 -0
  113. package/dist/core/backtest/settings.js +207 -0
  114. package/dist/core/backtest/settings.js.map +1 -0
  115. package/dist/core/backtest/simulate.d.ts +146 -0
  116. package/dist/core/backtest/simulate.d.ts.map +1 -0
  117. package/dist/core/backtest/simulate.js +217 -0
  118. package/dist/core/backtest/simulate.js.map +1 -0
  119. package/dist/core/catalogue/catalogue.generated.d.ts +66 -0
  120. package/dist/core/catalogue/catalogue.generated.d.ts.map +1 -1
  121. package/dist/core/catalogue/catalogue.generated.js +6 -0
  122. package/dist/core/catalogue/catalogue.generated.js.map +1 -1
  123. package/dist/core/catalogue/values.generated.d.ts +24 -0
  124. package/dist/core/catalogue/values.generated.d.ts.map +1 -1
  125. package/dist/core/check/index.d.ts +2 -1
  126. package/dist/core/check/index.d.ts.map +1 -1
  127. package/dist/core/check/index.js +1 -1
  128. package/dist/core/check/index.js.map +1 -1
  129. package/dist/core/check/library-prose.generated.d.ts +16 -0
  130. package/dist/core/check/library-prose.generated.d.ts.map +1 -0
  131. package/dist/core/check/library-prose.generated.js +353 -0
  132. package/dist/core/check/library-prose.generated.js.map +1 -0
  133. package/dist/core/check/surface.d.ts +24 -0
  134. package/dist/core/check/surface.d.ts.map +1 -1
  135. package/dist/core/check/surface.js +29 -0
  136. package/dist/core/check/surface.js.map +1 -1
  137. package/dist/core/emit/defaults.d.ts +35 -16
  138. package/dist/core/emit/defaults.d.ts.map +1 -1
  139. package/dist/core/emit/defaults.js +66 -0
  140. package/dist/core/emit/defaults.js.map +1 -1
  141. package/dist/core/emit/index.d.ts +2 -0
  142. package/dist/core/emit/index.d.ts.map +1 -1
  143. package/dist/core/emit/index.js +2 -0
  144. package/dist/core/emit/index.js.map +1 -1
  145. package/dist/core/engine/index.d.ts +1 -1
  146. package/dist/core/engine/index.d.ts.map +1 -1
  147. package/dist/core/engine/index.js +1 -1
  148. package/dist/core/engine/index.js.map +1 -1
  149. package/dist/core/engine/load.d.ts +15 -1
  150. package/dist/core/engine/load.d.ts.map +1 -1
  151. package/dist/core/engine/load.js +1 -0
  152. package/dist/core/engine/load.js.map +1 -1
  153. package/dist/core/index.d.ts +37 -3
  154. package/dist/core/index.d.ts.map +1 -1
  155. package/dist/core/index.js +17 -1
  156. package/dist/core/index.js.map +1 -1
  157. package/dist/core/version/version.generated.d.ts +1 -1
  158. package/dist/core/version/version.generated.js +1 -1
  159. package/dist/editor/complete.d.ts +40 -0
  160. package/dist/editor/complete.d.ts.map +1 -0
  161. package/dist/editor/complete.js +206 -0
  162. package/dist/editor/complete.js.map +1 -0
  163. package/dist/editor/diagnose.d.ts +18 -0
  164. package/dist/editor/diagnose.d.ts.map +1 -0
  165. package/dist/editor/diagnose.js +70 -0
  166. package/dist/editor/diagnose.js.map +1 -0
  167. package/dist/editor/format.d.ts +11 -0
  168. package/dist/editor/format.d.ts.map +1 -0
  169. package/dist/editor/format.js +50 -0
  170. package/dist/editor/format.js.map +1 -0
  171. package/dist/editor/highlight.d.ts +28 -0
  172. package/dist/editor/highlight.d.ts.map +1 -0
  173. package/dist/editor/highlight.js +65 -0
  174. package/dist/editor/highlight.js.map +1 -0
  175. package/dist/editor/hover.d.ts +40 -0
  176. package/dist/editor/hover.d.ts.map +1 -0
  177. package/dist/editor/hover.js +147 -0
  178. package/dist/editor/hover.js.map +1 -0
  179. package/dist/editor/index.d.ts +60 -0
  180. package/dist/editor/index.d.ts.map +1 -0
  181. package/dist/editor/index.js +7 -0
  182. package/dist/editor/index.js.map +1 -0
  183. package/dist/editor/kinds.d.ts +27 -0
  184. package/dist/editor/kinds.d.ts.map +1 -0
  185. package/dist/editor/kinds.js +118 -0
  186. package/dist/editor/kinds.js.map +1 -0
  187. package/dist/editor/layout.d.ts +47 -0
  188. package/dist/editor/layout.d.ts.map +1 -0
  189. package/dist/editor/layout.js +135 -0
  190. package/dist/editor/layout.js.map +1 -0
  191. package/dist/editor/manifest.d.ts +47 -0
  192. package/dist/editor/manifest.d.ts.map +1 -0
  193. package/dist/editor/manifest.js +93 -0
  194. package/dist/editor/manifest.js.map +1 -0
  195. package/dist/editor/reading.d.ts +41 -0
  196. package/dist/editor/reading.d.ts.map +1 -0
  197. package/dist/editor/reading.js +51 -0
  198. package/dist/editor/reading.js.map +1 -0
  199. package/dist/editor/scan.d.ts +26 -0
  200. package/dist/editor/scan.d.ts.map +1 -0
  201. package/dist/editor/scan.js +139 -0
  202. package/dist/editor/scan.js.map +1 -0
  203. package/dist/editor/scope.d.ts +19 -0
  204. package/dist/editor/scope.d.ts.map +1 -0
  205. package/dist/editor/scope.js +99 -0
  206. package/dist/editor/scope.js.map +1 -0
  207. package/dist/editor/signature.d.ts +38 -0
  208. package/dist/editor/signature.d.ts.map +1 -0
  209. package/dist/editor/signature.js +112 -0
  210. package/dist/editor/signature.js.map +1 -0
  211. package/dist/editor/site.d.ts +46 -0
  212. package/dist/editor/site.d.ts.map +1 -0
  213. package/dist/editor/site.js +184 -0
  214. package/dist/editor/site.js.map +1 -0
  215. package/dist/editor/spacing.d.ts +29 -0
  216. package/dist/editor/spacing.d.ts.map +1 -0
  217. package/dist/editor/spacing.js +116 -0
  218. package/dist/editor/spacing.js.map +1 -0
  219. package/package.json +30 -3
  220. package/spec/errors.json +140 -0
  221. package/src/adapters/charts/surfaces.ts +17 -0
  222. package/src/adapters/charts/tables.ts +88 -0
  223. package/src/adapters/codemirror/commands.ts +53 -0
  224. package/src/adapters/codemirror/completion.ts +74 -0
  225. package/src/adapters/codemirror/contract.ts +156 -0
  226. package/src/adapters/codemirror/index.ts +66 -0
  227. package/src/adapters/codemirror/lint.ts +66 -0
  228. package/src/adapters/codemirror/positions.ts +79 -0
  229. package/src/adapters/codemirror/stream.ts +87 -0
  230. package/src/adapters/codemirror/tokens.ts +64 -0
  231. package/src/adapters/codemirror/tooltips.ts +113 -0
  232. package/src/core/accounting/charges.ts +452 -0
  233. package/src/core/accounting/equity.ts +276 -0
  234. package/src/core/accounting/index.ts +49 -0
  235. package/src/core/accounting/markers.ts +95 -0
  236. package/src/core/accounting/monthly.ts +137 -0
  237. package/src/core/accounting/report.ts +89 -0
  238. package/src/core/accounting/shapes.ts +73 -0
  239. package/src/core/accounting/statistics.ts +350 -0
  240. package/src/core/accounting/trades.ts +313 -0
  241. package/src/core/backtest/compare.ts +244 -0
  242. package/src/core/backtest/declaration.ts +97 -0
  243. package/src/core/backtest/drive.ts +364 -0
  244. package/src/core/backtest/index.ts +52 -0
  245. package/src/core/backtest/range.ts +137 -0
  246. package/src/core/backtest/record.ts +341 -0
  247. package/src/core/backtest/replay.ts +158 -0
  248. package/src/core/backtest/resting.ts +125 -0
  249. package/src/core/backtest/settings.ts +280 -0
  250. package/src/core/backtest/simulate.ts +304 -0
  251. package/src/core/catalogue/catalogue.generated.ts +6 -0
  252. package/src/core/catalogue/values.generated.ts +6 -0
  253. package/src/core/check/index.ts +3 -0
  254. package/src/core/check/library-prose.generated.ts +367 -0
  255. package/src/core/check/surface.ts +34 -0
  256. package/src/core/emit/defaults.ts +70 -0
  257. package/src/core/emit/index.ts +2 -0
  258. package/src/core/engine/index.ts +1 -1
  259. package/src/core/engine/load.ts +16 -2
  260. package/src/core/index.ts +85 -1
  261. package/src/core/version/version.generated.ts +1 -1
  262. package/src/editor/complete.ts +266 -0
  263. package/src/editor/diagnose.ts +71 -0
  264. package/src/editor/format.ts +85 -0
  265. package/src/editor/highlight.ts +110 -0
  266. package/src/editor/hover.ts +199 -0
  267. package/src/editor/index.ts +65 -0
  268. package/src/editor/kinds.ts +157 -0
  269. package/src/editor/layout.ts +189 -0
  270. package/src/editor/manifest.ts +119 -0
  271. package/src/editor/reading.ts +69 -0
  272. package/src/editor/scan.ts +162 -0
  273. package/src/editor/scope.ts +122 -0
  274. package/src/editor/signature.ts +150 -0
  275. package/src/editor/site.ts +233 -0
  276. package/src/editor/spacing.ts +132 -0
@@ -0,0 +1,238 @@
1
+ /**
2
+ * The run record: a whole run as a plain JSON document.
3
+ *
4
+ * **The record is the conformance case.** That is the one decision this module
5
+ * exists to hold, and everything in it follows from it: no object reference, no
6
+ * function, no dependence on this implementation's internals, and nothing that
7
+ * only this engine can read. A record carries the script revision, the compiled
8
+ * program, the bars, the settings the host chose, the frames the destination
9
+ * answered in delivery order, the fills the engine folded in fold order, the
10
+ * ledger the run ended with, the diagnostics and the report.
11
+ *
12
+ * **The bars are inline or referenced, and both carry the same hash.** Inline is
13
+ * the conformance form, because a case has to hold every byte of its own input.
14
+ * Referenced is the operational form, because the smallest deployment has a one
15
+ * megabyte request body and nothing that grows with history may travel in one.
16
+ * A replay whose bars do not hash to the record's hash is OS6022 rather than a
17
+ * silently different study.
18
+ *
19
+ * **The frames name an intent by ordinal and never by an engine's own id**,
20
+ * because a case cannot know what id another engine minted. The same reason the
21
+ * ledger rows here are flat words rather than this engine's own types.
22
+ *
23
+ * The record is written through the existing canonical encoding, so there is one
24
+ * canonical writer in the repository and key order and number form are fixed for
25
+ * everybody.
26
+ */
27
+ import type { RecordedFill, Report } from '../accounting/index.js';
28
+ import type { Diagnostic } from '../diagnostics/index.js';
29
+ import type { CompiledProgram, SourceStamp } from '../emit/index.js';
30
+ import type { LedgerRow } from '../engine/index.js';
31
+ import type { BacktestSettings } from './settings.js';
32
+ /** One bar as a record holds it. */
33
+ export interface RecordedBar {
34
+ readonly time: number | null;
35
+ readonly open: number | null;
36
+ readonly high: number | null;
37
+ readonly low: number | null;
38
+ readonly close: number | null;
39
+ readonly volume: number | null;
40
+ readonly oi: number | null;
41
+ }
42
+ /**
43
+ * The bars, held or pointed at.
44
+ *
45
+ * Both forms carry the same hash, computed the same way over the canonical
46
+ * tuple of each bar, so a referenced record can be turned into an inline one by
47
+ * whoever holds the bars and the result is the same case.
48
+ */
49
+ export type BarsInRecord = {
50
+ readonly form: 'inline';
51
+ readonly hash: string;
52
+ readonly count: number;
53
+ readonly rows: readonly RecordedBar[];
54
+ } | {
55
+ readonly form: 'referenced';
56
+ readonly hash: string;
57
+ readonly count: number;
58
+ readonly firstTime: number | null;
59
+ readonly lastTime: number | null;
60
+ };
61
+ /** The columns of `conformance.md`'s frames.csv, in its order and with its names. */
62
+ export interface RecordedFrame {
63
+ readonly afterBar: number;
64
+ /** An ordinal, 1-based, never an engine's own id. */
65
+ readonly intent: number;
66
+ readonly status: string;
67
+ readonly filledQty: number;
68
+ readonly avgFillPrice: number | null;
69
+ readonly orderRef: string | null;
70
+ readonly text: string | null;
71
+ }
72
+ /** A ledger row of `stdlib.md` 17.7, flattened: the `orders` channel of expected.json. */
73
+ export interface RecordedOrder {
74
+ readonly intent: number;
75
+ readonly orderRef: string;
76
+ readonly tag: string;
77
+ readonly leg: string;
78
+ readonly positionRef: number;
79
+ readonly symbol: string | null;
80
+ readonly exchange: string | null;
81
+ readonly product: string;
82
+ readonly side: string;
83
+ readonly qty: number | null;
84
+ readonly qtyType: string;
85
+ readonly type: string | null;
86
+ readonly price: number | null;
87
+ readonly trigger: number | null;
88
+ readonly status: string;
89
+ readonly filledQty: number;
90
+ readonly avgFillPrice: number | null;
91
+ readonly rejection: string | null;
92
+ readonly placedAt: number | null;
93
+ readonly updatedAt: number | null;
94
+ readonly units: number | null;
95
+ }
96
+ /** A diagnostic as a record holds it: the code, the span and the bar. */
97
+ export interface RecordedDiagnostic {
98
+ readonly code: string;
99
+ readonly line: number;
100
+ readonly column: number;
101
+ readonly severity: string;
102
+ readonly barIndex: number | null;
103
+ }
104
+ /** One run, whole, and readable by an engine that is not this one. */
105
+ export interface RunRecord {
106
+ readonly recordVersion: number;
107
+ readonly engine: {
108
+ readonly name: string;
109
+ readonly version: string;
110
+ };
111
+ readonly languageVersion: string;
112
+ /** Already plain data. */
113
+ readonly program: CompiledProgram;
114
+ readonly programHash: string;
115
+ /** Hash, lines, file: the script revision. */
116
+ readonly source: SourceStamp;
117
+ readonly settings: BacktestSettings;
118
+ readonly bars: BarsInRecord;
119
+ /** What the destination answered, in delivery order. */
120
+ readonly frames: readonly RecordedFrame[];
121
+ /** What the engine folded, in fold order. */
122
+ readonly fills: readonly RecordedFill[];
123
+ /** The ledger at the end, oldest first. */
124
+ readonly orders: readonly RecordedOrder[];
125
+ readonly diagnostics: readonly RecordedDiagnostic[];
126
+ readonly report: Report;
127
+ }
128
+ /**
129
+ * The revision of this document's own shape.
130
+ *
131
+ * A reader is handed a record and has to know what it is looking at before it
132
+ * reads a field, so the number is first in the document and is written by this
133
+ * file alone. It moves when a channel is added or a meaning changes, never when
134
+ * a figure in a report does.
135
+ */
136
+ export declare const RECORD_VERSION = 1;
137
+ /** The parts a run hands over, each already in the shape the record holds. */
138
+ export interface RecordParts {
139
+ readonly program: CompiledProgram;
140
+ readonly settings: BacktestSettings;
141
+ readonly bars: readonly RecordedBar[];
142
+ /**
143
+ * Whether the bars travel in the record or are pointed at.
144
+ *
145
+ * Inline is the conformance form, because a case holds every byte of its own
146
+ * input. Referenced is the operational form, because nothing that grows with
147
+ * history may travel in a request body. Both carry the same hash over the
148
+ * same canonical tuples, so one becomes the other without becoming a
149
+ * different case.
150
+ */
151
+ readonly form: 'inline' | 'referenced';
152
+ readonly frames: readonly RecordedFrame[];
153
+ readonly fills: readonly RecordedFill[];
154
+ readonly orders: readonly RecordedOrder[];
155
+ readonly diagnostics: readonly RecordedDiagnostic[];
156
+ readonly report: Report;
157
+ }
158
+ /**
159
+ * One run, as the document another engine is handed.
160
+ *
161
+ * Nothing is computed here. Every channel arrives folded, because what a record
162
+ * says has to be what the run did rather than what a second fold of the same
163
+ * inputs came to: a record that recomputed its own report would agree with
164
+ * itself whatever the engine had actually done.
165
+ */
166
+ export declare function recordOf(parts: RecordParts): RunRecord;
167
+ /**
168
+ * The bars, held or pointed at, and the same hash either way.
169
+ *
170
+ * The referenced form keeps the first and last times so that a reader can say
171
+ * which bars are wanted without holding them, and the count so that a set of
172
+ * the wrong length is known to be wrong before it is hashed.
173
+ */
174
+ export declare function barsIn(bars: readonly RecordedBar[], form: 'inline' | 'referenced'): BarsInRecord;
175
+ /**
176
+ * The hash a record names its bars by.
177
+ *
178
+ * Over the canonical tuple of each bar, in the order they were supplied, and
179
+ * through the canonical encoding the compiled program is hashed with, so there
180
+ * is one canonical writer in the repository rather than two that agree until
181
+ * one of them is changed. A tuple rather than an object, because the field
182
+ * order is then the specification's and not a sort's.
183
+ */
184
+ export declare function barsHash(bars: readonly RecordedBar[]): string;
185
+ /**
186
+ * The record as the bytes another engine reads.
187
+ *
188
+ * Through the canonical encoding, so key order is fixed, every number is in the
189
+ * shortest decimal form that reads back as itself, and two runs of the same
190
+ * record produce the same bytes on every machine. That is what makes the
191
+ * reproducibility test a byte comparison rather than a tour of the fields.
192
+ */
193
+ export declare function recordToJson(record: RunRecord): string;
194
+ /**
195
+ * The run, without who produced it.
196
+ *
197
+ * **`engine` is provenance, and provenance is not part of the run.** The claim
198
+ * a rerun makes is that the run is a function of the record, and that claim has
199
+ * to survive the engine it runs on being upgraded: a record stored under
200
+ * `0.3.0` and rerun under `0.4.0` produces the same trades, the same fills and
201
+ * the same money, and a document carrying the version stamp inside the bytes
202
+ * being compared says it does not.
203
+ *
204
+ * It was compared as whole bytes until the first version bump would have broken
205
+ * it, which is a test that passes for exactly as long as nothing changes and
206
+ * then fails for the one reason that is not a defect. So the stamp stays in the
207
+ * record, because knowing which engine wrote a run is worth keeping, and the
208
+ * comparison is made over everything else.
209
+ */
210
+ export declare function runBytes(record: RunRecord): string;
211
+ /**
212
+ * A record read back from those bytes.
213
+ *
214
+ * A parse and not a validation: what comes back is the document as it was
215
+ * written, and a document this engine did not write is the caller's to trust or
216
+ * not. The one thing asserted is the revision, because reading a later
217
+ * revision's fields under this one's rules is how a record silently becomes a
218
+ * different run.
219
+ */
220
+ export declare function recordFromJson(text: string): RunRecord | null;
221
+ /**
222
+ * One ledger row, flattened into the words a case file prints.
223
+ *
224
+ * The engine's own types do not cross: a second engine has its own, and a
225
+ * channel written in this one's would be a case only this one could read. The
226
+ * intent is an ordinal for the same reason, because no engine can know what id
227
+ * another minted.
228
+ *
229
+ * `qtyType` comes from the intent rather than from the row, because the unit a
230
+ * quantity is counted in is per order and not per leg: an order the engine
231
+ * sized itself is in units whatever the declaration counts in, and a row that
232
+ * printed the declaration's unit for it would state a quantity in a unit it was
233
+ * never measured in.
234
+ */
235
+ export declare function orderIn(row: LedgerRow, intent: number, qtyType: string): RecordedOrder;
236
+ /** One diagnostic, as a record holds it: the code, the span and the bar. */
237
+ export declare function diagnosticIn(diagnostic: Diagnostic, barIndex: number | null): RecordedDiagnostic;
238
+ //# sourceMappingURL=record.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../../../src/core/backtest/record.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEtD,oCAAoC;AACpC,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,EAAE,EAAE,M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,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,SAAS,CAgBtD;AAED;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,GAAG,YAAY,GAAG,YAAY,CAYhG;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,GAAG,MAAM,CAW7D;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAEtD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAGlD;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAK7D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,aAAa,CAwBtF;AAED,4EAA4E;AAC5E,wBAAgB,YAAY,CAC1B,UAAU,EAAE,UAAU,EACtB,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,kBAAkB,CAQpB"}
@@ -0,0 +1,176 @@
1
+ import { canonicalise, programHash, sha256 } from '../emit/index.js';
2
+ import { VERSION } from '../version/index.js';
3
+ /**
4
+ * The revision of this document's own shape.
5
+ *
6
+ * A reader is handed a record and has to know what it is looking at before it
7
+ * reads a field, so the number is first in the document and is written by this
8
+ * file alone. It moves when a channel is added or a meaning changes, never when
9
+ * a figure in a report does.
10
+ */
11
+ export const RECORD_VERSION = 1;
12
+ /** What this engine calls itself in a record it wrote. */
13
+ const ENGINE_NAME = 'openscript';
14
+ /**
15
+ * One run, as the document another engine is handed.
16
+ *
17
+ * Nothing is computed here. Every channel arrives folded, because what a record
18
+ * says has to be what the run did rather than what a second fold of the same
19
+ * inputs came to: a record that recomputed its own report would agree with
20
+ * itself whatever the engine had actually done.
21
+ */
22
+ export function recordOf(parts) {
23
+ return {
24
+ recordVersion: RECORD_VERSION,
25
+ engine: { name: ENGINE_NAME, version: VERSION },
26
+ languageVersion: String(parts.program.openscript.language),
27
+ program: parts.program,
28
+ programHash: programHash(parts.program),
29
+ source: parts.program.source,
30
+ settings: parts.settings,
31
+ bars: barsIn(parts.bars, parts.form),
32
+ frames: parts.frames,
33
+ fills: parts.fills,
34
+ orders: parts.orders,
35
+ diagnostics: parts.diagnostics,
36
+ report: parts.report,
37
+ };
38
+ }
39
+ /**
40
+ * The bars, held or pointed at, and the same hash either way.
41
+ *
42
+ * The referenced form keeps the first and last times so that a reader can say
43
+ * which bars are wanted without holding them, and the count so that a set of
44
+ * the wrong length is known to be wrong before it is hashed.
45
+ */
46
+ export function barsIn(bars, form) {
47
+ const hash = barsHash(bars);
48
+ if (form === 'inline') {
49
+ return { form: 'inline', hash, count: bars.length, rows: bars };
50
+ }
51
+ return {
52
+ form: 'referenced',
53
+ hash,
54
+ count: bars.length,
55
+ firstTime: bars[0]?.time ?? null,
56
+ lastTime: bars[bars.length - 1]?.time ?? null,
57
+ };
58
+ }
59
+ /**
60
+ * The hash a record names its bars by.
61
+ *
62
+ * Over the canonical tuple of each bar, in the order they were supplied, and
63
+ * through the canonical encoding the compiled program is hashed with, so there
64
+ * is one canonical writer in the repository rather than two that agree until
65
+ * one of them is changed. A tuple rather than an object, because the field
66
+ * order is then the specification's and not a sort's.
67
+ */
68
+ export function barsHash(bars) {
69
+ const tuples = bars.map((bar) => [
70
+ bar.time,
71
+ bar.open,
72
+ bar.high,
73
+ bar.low,
74
+ bar.close,
75
+ bar.volume,
76
+ bar.oi,
77
+ ]);
78
+ return 'sha256:' + sha256(canonicalise(tuples));
79
+ }
80
+ /**
81
+ * The record as the bytes another engine reads.
82
+ *
83
+ * Through the canonical encoding, so key order is fixed, every number is in the
84
+ * shortest decimal form that reads back as itself, and two runs of the same
85
+ * record produce the same bytes on every machine. That is what makes the
86
+ * reproducibility test a byte comparison rather than a tour of the fields.
87
+ */
88
+ export function recordToJson(record) {
89
+ return canonicalise(record);
90
+ }
91
+ /**
92
+ * The run, without who produced it.
93
+ *
94
+ * **`engine` is provenance, and provenance is not part of the run.** The claim
95
+ * a rerun makes is that the run is a function of the record, and that claim has
96
+ * to survive the engine it runs on being upgraded: a record stored under
97
+ * `0.3.0` and rerun under `0.4.0` produces the same trades, the same fills and
98
+ * the same money, and a document carrying the version stamp inside the bytes
99
+ * being compared says it does not.
100
+ *
101
+ * It was compared as whole bytes until the first version bump would have broken
102
+ * it, which is a test that passes for exactly as long as nothing changes and
103
+ * then fails for the one reason that is not a defect. So the stamp stays in the
104
+ * record, because knowing which engine wrote a run is worth keeping, and the
105
+ * comparison is made over everything else.
106
+ */
107
+ export function runBytes(record) {
108
+ const { engine: _engine, ...run } = record;
109
+ return canonicalise(run);
110
+ }
111
+ /**
112
+ * A record read back from those bytes.
113
+ *
114
+ * A parse and not a validation: what comes back is the document as it was
115
+ * written, and a document this engine did not write is the caller's to trust or
116
+ * not. The one thing asserted is the revision, because reading a later
117
+ * revision's fields under this one's rules is how a record silently becomes a
118
+ * different run.
119
+ */
120
+ export function recordFromJson(text) {
121
+ const parsed = JSON.parse(text);
122
+ if (parsed === null || typeof parsed !== 'object')
123
+ return null;
124
+ const record = parsed;
125
+ return record.recordVersion === RECORD_VERSION ? record : null;
126
+ }
127
+ /**
128
+ * One ledger row, flattened into the words a case file prints.
129
+ *
130
+ * The engine's own types do not cross: a second engine has its own, and a
131
+ * channel written in this one's would be a case only this one could read. The
132
+ * intent is an ordinal for the same reason, because no engine can know what id
133
+ * another minted.
134
+ *
135
+ * `qtyType` comes from the intent rather than from the row, because the unit a
136
+ * quantity is counted in is per order and not per leg: an order the engine
137
+ * sized itself is in units whatever the declaration counts in, and a row that
138
+ * printed the declaration's unit for it would state a quantity in a unit it was
139
+ * never measured in.
140
+ */
141
+ export function orderIn(row, intent, qtyType) {
142
+ return {
143
+ intent,
144
+ orderRef: row.orderRef,
145
+ tag: row.tag,
146
+ leg: row.leg,
147
+ positionRef: row.positionRef,
148
+ symbol: row.instrument.symbol,
149
+ exchange: row.instrument.exchange,
150
+ product: row.product,
151
+ side: row.side,
152
+ qty: row.qty,
153
+ qtyType,
154
+ type: row.type,
155
+ price: row.price,
156
+ trigger: row.trigger,
157
+ status: row.status,
158
+ filledQty: row.filledQty,
159
+ avgFillPrice: row.avgFillPrice,
160
+ rejection: row.rejection === '' ? null : row.rejection,
161
+ placedAt: row.placedAt,
162
+ updatedAt: row.updatedAt,
163
+ units: row.units,
164
+ };
165
+ }
166
+ /** One diagnostic, as a record holds it: the code, the span and the bar. */
167
+ export function diagnosticIn(diagnostic, barIndex) {
168
+ return {
169
+ code: diagnostic.code,
170
+ line: diagnostic.span.line,
171
+ column: diagnostic.span.column,
172
+ severity: diagnostic.severity,
173
+ barIndex,
174
+ };
175
+ }
176
+ //# sourceMappingURL=record.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"record.js","sourceRoot":"","sources":["../../../src/core/backtest/record.ts"],"names":[],"mappings":"AA4BA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAGrE,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAwG9C;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC;AAEhC,0DAA0D;AAC1D,MAAM,WAAW,GAAG,YAAY,CAAC;AAwBjC;;;;;;;GAOG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAkB;IACzC,OAAO;QACL,aAAa,EAAE,cAAc;QAC7B,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE;QAC/C,eAAe,EAAE,MAAM,CAAC,KAAK,CAAC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U,CAAC,QAAQ;QACjC,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,GAAG,EAAE,GAAG,CAAC,GAAG;QACZ,OAAO;QACP,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,YAAY,EAAE,GAAG,CAAC,YAAY;QAC9B,SAAS,EAAE,GAAG,CAAC,SAAS,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS;QACtD,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,KAAK,EAAE,GAAG,CAAC,KAAK;KACjB,CAAC;AACJ,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,YAAY,CAC1B,UAAsB,EACtB,QAAuB;IAEvB,OAAO;QACL,IAAI,EAAE,UAAU,CAAC,IAAI;QACrB,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,IAAI;QAC1B,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC,MAAM;QAC9B,QAAQ,EAAE,UAAU,CAAC,QAAQ;QAC7B,QAAQ;KACT,CAAC;AACJ,CAAC"}
@@ -0,0 +1,48 @@
1
+ import type { Report } from '../accounting/index.js';
2
+ import type { Diagnostic } from '../diagnostics/index.js';
3
+ import type { BacktestResult } from './drive.js';
4
+ import type { RecordedBar, RunRecord } from './record.js';
5
+ /** The report a record folds to, or why it could not be folded. */
6
+ export type ReplayResult = {
7
+ readonly ok: true;
8
+ readonly report: Report;
9
+ } | {
10
+ readonly ok: false;
11
+ readonly diagnostic: Diagnostic;
12
+ };
13
+ /**
14
+ * The record's own report, computed again from its own fills.
15
+ *
16
+ * The bars are the record's where it carries them and the caller's where it
17
+ * points at them, and either way they are hashed against what the record names
18
+ * before a figure is computed from them.
19
+ */
20
+ export declare function replay(record: RunRecord, bars?: readonly RecordedBar[] | null): ReplayResult;
21
+ /**
22
+ * The same run, executed again.
23
+ *
24
+ * Everything the run depended on is in the record: the program, the bars, the
25
+ * settings and the inputs. So a rerun takes no argument the record does not
26
+ * already hold, except the bars a referenced record points at rather than
27
+ * carries, and the record it produces is comparable with the original field for
28
+ * field and byte for byte.
29
+ */
30
+ export declare function rerun(record: RunRecord, bars?: readonly RecordedBar[] | null): BacktestResult;
31
+ type BarsResult = {
32
+ readonly ok: true;
33
+ readonly bars: readonly RecordedBar[];
34
+ } | {
35
+ readonly ok: false;
36
+ readonly diagnostic: Diagnostic;
37
+ };
38
+ /**
39
+ * The bars a record was made from, proved to be those bars.
40
+ *
41
+ * A record carrying its own bars is still checked against any that are supplied
42
+ * beside them, because supplying a different set is exactly the mistake worth
43
+ * catching: the caller believes they are replaying this run and they are
44
+ * studying another one.
45
+ */
46
+ export declare function barsOf(record: RunRecord, supplied: readonly RecordedBar[] | null): BarsResult;
47
+ export {};
48
+ //# sourceMappingURL=replay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../../../src/core/backtest/replay.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAAkB,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAErE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAKjD,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE1D,mEAAmE;AACnE,MAAM,MAAM,YAAY,GACpB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC9C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC;AAE5D;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,GAAE,SAAS,WAAW,EAAE,GAAG,IAAW,GAAG,YAAY,CAoBlG;AAED;;;;;;;;GAQG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,GAAE,SAAS,WAAW,EAAE,GAAG,IAAW,GAAG,cAAc,CAInG;AAED,KAAK,UAAU,GACX;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,CAAA;CAAE,GAC5D;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC;AAE5D;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,SAAS,WAAW,EAAE,GAAG,IAAI,GAAG,UAAU,CAY7F"}
@@ -0,0 +1,127 @@
1
+ /**
2
+ * A stored run, reported again and run again.
3
+ *
4
+ * Two questions, and they are different questions, which is why they are two
5
+ * calls:
6
+ *
7
+ * - **`replay` asks whether the report is a function of the fills.** It folds
8
+ * the money again from the record's own fills and its own bar closes, with no
9
+ * bar executed and no order placed, and what comes back has to equal the
10
+ * report the record carries. If it does not, the report was reading something
11
+ * the engine happened to be holding rather than what the run settled, and
12
+ * nobody could reproduce it from the document.
13
+ * - **`rerun` asks whether the engine still does what it did.** It executes the
14
+ * record's program over the record's bars under the record's settings, and
15
+ * the two records are compared as bytes. A rerun of one record on this engine
16
+ * is bit-identical or it is a defect: the tolerance on the record is for a
17
+ * second implementation and is never this one's excuse.
18
+ *
19
+ * **The bars are proved before either is attempted.** A record names the bars it
20
+ * was made from by a hash, and bars are revised: a feed corrects a print, a
21
+ * session is extended, a split is applied to history. A replay over revised bars
22
+ * reporting the original figures is the most convincing wrong answer this system
23
+ * can produce, so a set that does not hash to the record's is OS6022.
24
+ *
25
+ * **One engine call is made, and nothing is executed by it.** A declaration
26
+ * field may be written by an `input()`, and what an input resolved to is the
27
+ * engine's rule; asking it here rather than resolving a second time is what
28
+ * keeps the replayed capital and commission the ones the run used. No bar is
29
+ * appended, so no value is computed and no order is placed: the load is a
30
+ * question about the program, not a run of it.
31
+ */
32
+ import { reportOf, scheduleFromDeclaration } from '../accounting/index.js';
33
+ import { diagnosticFor } from '../diagnostics/index.js';
34
+ import { load } from '../engine/index.js';
35
+ import { backtest } from './drive.js';
36
+ import { declarationOf } from './declaration.js';
37
+ import { marksFor, windowFor } from './range.js';
38
+ import { barsHash } from './record.js';
39
+ /**
40
+ * The record's own report, computed again from its own fills.
41
+ *
42
+ * The bars are the record's where it carries them and the caller's where it
43
+ * points at them, and either way they are hashed against what the record names
44
+ * before a figure is computed from them.
45
+ */
46
+ export function replay(record, bars = null) {
47
+ const held = barsOf(record, bars);
48
+ if (!held.ok)
49
+ return { ok: false, diagnostic: held.diagnostic };
50
+ const framed = windowFor(held.bars, record.settings.range);
51
+ if (!framed.ok)
52
+ return { ok: false, diagnostic: framed.diagnostic };
53
+ const declared = declarationIn(record);
54
+ if (!declared.ok)
55
+ return { ok: false, diagnostic: declared.diagnostic };
56
+ return {
57
+ ok: true,
58
+ report: reportOf(record.fills, marksFor(held.bars, framed.covered), scheduleIn(record, declared.declaration), record.settings.contract, declared.declaration.capital),
59
+ };
60
+ }
61
+ /**
62
+ * The same run, executed again.
63
+ *
64
+ * Everything the run depended on is in the record: the program, the bars, the
65
+ * settings and the inputs. So a rerun takes no argument the record does not
66
+ * already hold, except the bars a referenced record points at rather than
67
+ * carries, and the record it produces is comparable with the original field for
68
+ * field and byte for byte.
69
+ */
70
+ export function rerun(record, bars = null) {
71
+ const held = barsOf(record, bars);
72
+ if (!held.ok)
73
+ return { ok: false, diagnostic: held.diagnostic };
74
+ return backtest(record.program, held.bars, record.settings, { form: record.bars.form });
75
+ }
76
+ /**
77
+ * The bars a record was made from, proved to be those bars.
78
+ *
79
+ * A record carrying its own bars is still checked against any that are supplied
80
+ * beside them, because supplying a different set is exactly the mistake worth
81
+ * catching: the caller believes they are replaying this run and they are
82
+ * studying another one.
83
+ */
84
+ export function barsOf(record, supplied) {
85
+ if (supplied !== null) {
86
+ const hash = barsHash(supplied);
87
+ if (hash !== record.bars.hash)
88
+ return { ok: false, diagnostic: mismatch(hash, record) };
89
+ return { ok: true, bars: supplied };
90
+ }
91
+ if (record.bars.form === 'inline')
92
+ return { ok: true, bars: record.bars.rows };
93
+ // A referenced record points at bars it does not hold, so there is nothing to
94
+ // replay until whoever holds them supplies them. It is the same refusal as a
95
+ // wrong set, because the question it answers is the same one: are these the
96
+ // bars the record was made from.
97
+ return { ok: false, diagnostic: mismatch('no bars supplied', record) };
98
+ }
99
+ function mismatch(found, record) {
100
+ return diagnosticFor('OS6022', NO_POSITION, { found, expected: record.bars.hash });
101
+ }
102
+ /**
103
+ * What the record's own program declares, after the record's own inputs.
104
+ *
105
+ * The destination handed to the load is one that answers nothing, because
106
+ * nothing is placed: a strategy declares the orders capability and a host that
107
+ * did not offer it would be refused at load for a reason that has nothing to do
108
+ * with the record.
109
+ */
110
+ function declarationIn(record) {
111
+ const loaded = load(record.program, {
112
+ settings: record.settings.inputs,
113
+ host: { route: () => undefined },
114
+ });
115
+ if (!loaded.ok)
116
+ return { ok: false, diagnostic: loaded.diagnostic };
117
+ return { ok: true, declaration: declarationOf(loaded.engine.program, loaded.inputs) };
118
+ }
119
+ /** The cost model the record was carried out under, derived where it was derived. */
120
+ function scheduleIn(record, declared) {
121
+ if (record.settings.costs !== null)
122
+ return record.settings.costs;
123
+ return scheduleFromDeclaration(declared.commission, declared.commissionType, declared.slippage, record.settings.contract.currency, record.settings.contract.digits);
124
+ }
125
+ /** Where a refusal about a record points: nowhere in anybody's script. */
126
+ const NO_POSITION = { offset: 0, length: 0, line: 0, column: 0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