openalgo-script 0.2.0 → 0.5.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 +1198 -0
- package/README.md +127 -40
- package/dist/adapters/charts/driving.d.ts +50 -0
- package/dist/adapters/charts/driving.d.ts.map +1 -0
- package/dist/adapters/charts/driving.js +57 -0
- package/dist/adapters/charts/driving.js.map +1 -0
- package/dist/adapters/charts/run.d.ts +20 -0
- package/dist/adapters/charts/run.d.ts.map +1 -1
- package/dist/adapters/charts/run.js +83 -16
- package/dist/adapters/charts/run.js.map +1 -1
- package/dist/adapters/charts/surfaces.d.ts +17 -0
- package/dist/adapters/charts/surfaces.d.ts.map +1 -1
- package/dist/adapters/charts/tables.js +82 -0
- package/dist/adapters/charts/tables.js.map +1 -1
- package/dist/adapters/charts/venue.d.ts +73 -0
- package/dist/adapters/charts/venue.d.ts.map +1 -0
- package/dist/adapters/charts/venue.js +104 -0
- package/dist/adapters/charts/venue.js.map +1 -0
- package/dist/adapters/codemirror/commands.d.ts +14 -0
- package/dist/adapters/codemirror/commands.d.ts.map +1 -0
- package/dist/adapters/codemirror/commands.js +52 -0
- package/dist/adapters/codemirror/commands.js.map +1 -0
- package/dist/adapters/codemirror/completion.d.ts +15 -0
- package/dist/adapters/codemirror/completion.d.ts.map +1 -0
- package/dist/adapters/codemirror/completion.js +64 -0
- package/dist/adapters/codemirror/completion.js.map +1 -0
- package/dist/adapters/codemirror/contract.d.ts +156 -0
- package/dist/adapters/codemirror/contract.d.ts.map +1 -0
- package/dist/adapters/codemirror/contract.js +42 -0
- package/dist/adapters/codemirror/contract.js.map +1 -0
- package/dist/adapters/codemirror/index.d.ts +43 -0
- package/dist/adapters/codemirror/index.d.ts.map +1 -0
- package/dist/adapters/codemirror/index.js +8 -0
- package/dist/adapters/codemirror/index.js.map +1 -0
- package/dist/adapters/codemirror/lint.d.ts +17 -0
- package/dist/adapters/codemirror/lint.d.ts.map +1 -0
- package/dist/adapters/codemirror/lint.js +62 -0
- package/dist/adapters/codemirror/lint.js.map +1 -0
- package/dist/adapters/codemirror/positions.d.ts +16 -0
- package/dist/adapters/codemirror/positions.d.ts.map +1 -0
- package/dist/adapters/codemirror/positions.js +65 -0
- package/dist/adapters/codemirror/positions.js.map +1 -0
- package/dist/adapters/codemirror/stream.d.ts +23 -0
- package/dist/adapters/codemirror/stream.d.ts.map +1 -0
- package/dist/adapters/codemirror/stream.js +70 -0
- package/dist/adapters/codemirror/stream.js.map +1 -0
- package/dist/adapters/codemirror/tokens.d.ts +44 -0
- package/dist/adapters/codemirror/tokens.d.ts.map +1 -0
- package/dist/adapters/codemirror/tokens.js +34 -0
- package/dist/adapters/codemirror/tokens.js.map +1 -0
- package/dist/adapters/codemirror/tooltips.d.ts +35 -0
- package/dist/adapters/codemirror/tooltips.d.ts.map +1 -0
- package/dist/adapters/codemirror/tooltips.js +108 -0
- package/dist/adapters/codemirror/tooltips.js.map +1 -0
- package/dist/core/accounting/analysis.d.ts +111 -0
- package/dist/core/accounting/analysis.d.ts.map +1 -0
- package/dist/core/accounting/analysis.js +123 -0
- package/dist/core/accounting/analysis.js.map +1 -0
- package/dist/core/accounting/charges.d.ts +136 -0
- package/dist/core/accounting/charges.d.ts.map +1 -0
- package/dist/core/accounting/charges.js +362 -0
- package/dist/core/accounting/charges.js.map +1 -0
- package/dist/core/accounting/equity.d.ts +191 -0
- package/dist/core/accounting/equity.d.ts.map +1 -0
- package/dist/core/accounting/equity.js +167 -0
- package/dist/core/accounting/equity.js.map +1 -0
- package/dist/core/accounting/index.d.ts +46 -0
- package/dist/core/accounting/index.d.ts.map +1 -0
- package/dist/core/accounting/index.js +37 -0
- package/dist/core/accounting/index.js.map +1 -0
- package/dist/core/accounting/markers.d.ts +43 -0
- package/dist/core/accounting/markers.d.ts.map +1 -0
- package/dist/core/accounting/markers.js +64 -0
- package/dist/core/accounting/markers.js.map +1 -0
- package/dist/core/accounting/monthly.d.ts +52 -0
- package/dist/core/accounting/monthly.d.ts.map +1 -0
- package/dist/core/accounting/monthly.js +99 -0
- package/dist/core/accounting/monthly.js.map +1 -0
- package/dist/core/accounting/report.d.ts +41 -0
- package/dist/core/accounting/report.d.ts.map +1 -0
- package/dist/core/accounting/report.js +65 -0
- package/dist/core/accounting/report.js.map +1 -0
- package/dist/core/accounting/shapes.d.ts +70 -0
- package/dist/core/accounting/shapes.d.ts.map +1 -0
- package/dist/core/accounting/shapes.js +21 -0
- package/dist/core/accounting/shapes.js.map +1 -0
- package/dist/core/accounting/statistics.d.ts +87 -0
- package/dist/core/accounting/statistics.d.ts.map +1 -0
- package/dist/core/accounting/statistics.js +268 -0
- package/dist/core/accounting/statistics.js.map +1 -0
- package/dist/core/accounting/trades.d.ts +118 -0
- package/dist/core/accounting/trades.d.ts.map +1 -0
- package/dist/core/accounting/trades.js +186 -0
- package/dist/core/accounting/trades.js.map +1 -0
- package/dist/core/backtest/case.d.ts +60 -0
- package/dist/core/backtest/case.d.ts.map +1 -0
- package/dist/core/backtest/case.js +319 -0
- package/dist/core/backtest/case.js.map +1 -0
- package/dist/core/backtest/compare.d.ts +38 -0
- package/dist/core/backtest/compare.d.ts.map +1 -0
- package/dist/core/backtest/compare.js +191 -0
- package/dist/core/backtest/compare.js.map +1 -0
- package/dist/core/backtest/declaration.d.ts +52 -0
- package/dist/core/backtest/declaration.d.ts.map +1 -0
- package/dist/core/backtest/declaration.js +48 -0
- package/dist/core/backtest/declaration.js.map +1 -0
- package/dist/core/backtest/deliver.d.ts +93 -0
- package/dist/core/backtest/deliver.d.ts.map +1 -0
- package/dist/core/backtest/deliver.js +94 -0
- package/dist/core/backtest/deliver.js.map +1 -0
- package/dist/core/backtest/drive.d.ts +87 -0
- package/dist/core/backtest/drive.d.ts.map +1 -0
- package/dist/core/backtest/drive.js +312 -0
- package/dist/core/backtest/drive.js.map +1 -0
- package/dist/core/backtest/index.d.ts +58 -0
- package/dist/core/backtest/index.d.ts.map +1 -0
- package/dist/core/backtest/index.js +47 -0
- package/dist/core/backtest/index.js.map +1 -0
- package/dist/core/backtest/range.d.ts +84 -0
- package/dist/core/backtest/range.d.ts.map +1 -0
- package/dist/core/backtest/range.js +90 -0
- package/dist/core/backtest/range.js.map +1 -0
- package/dist/core/backtest/record.d.ts +299 -0
- package/dist/core/backtest/record.d.ts.map +1 -0
- package/dist/core/backtest/record.js +243 -0
- package/dist/core/backtest/record.js.map +1 -0
- package/dist/core/backtest/replay.d.ts +48 -0
- package/dist/core/backtest/replay.d.ts.map +1 -0
- package/dist/core/backtest/replay.js +137 -0
- package/dist/core/backtest/replay.js.map +1 -0
- package/dist/core/backtest/resting.d.ts +62 -0
- package/dist/core/backtest/resting.d.ts.map +1 -0
- package/dist/core/backtest/resting.js +59 -0
- package/dist/core/backtest/resting.js.map +1 -0
- package/dist/core/backtest/settings.d.ts +117 -0
- package/dist/core/backtest/settings.d.ts.map +1 -0
- package/dist/core/backtest/settings.js +207 -0
- package/dist/core/backtest/settings.js.map +1 -0
- package/dist/core/backtest/simulate.d.ts +258 -0
- package/dist/core/backtest/simulate.d.ts.map +1 -0
- package/dist/core/backtest/simulate.js +335 -0
- package/dist/core/backtest/simulate.js.map +1 -0
- package/dist/core/catalogue/catalogue.generated.d.ts +66 -0
- package/dist/core/catalogue/catalogue.generated.d.ts.map +1 -1
- package/dist/core/catalogue/catalogue.generated.js +6 -0
- package/dist/core/catalogue/catalogue.generated.js.map +1 -1
- package/dist/core/catalogue/values.generated.d.ts +24 -0
- package/dist/core/catalogue/values.generated.d.ts.map +1 -1
- package/dist/core/check/index.d.ts +2 -1
- package/dist/core/check/index.d.ts.map +1 -1
- package/dist/core/check/index.js +1 -1
- package/dist/core/check/index.js.map +1 -1
- package/dist/core/check/library-orders.js +2 -2
- package/dist/core/check/library-orders.js.map +1 -1
- package/dist/core/check/library-prose.generated.d.ts +16 -0
- package/dist/core/check/library-prose.generated.d.ts.map +1 -0
- package/dist/core/check/library-prose.generated.js +353 -0
- package/dist/core/check/library-prose.generated.js.map +1 -0
- package/dist/core/check/surface.d.ts +24 -0
- package/dist/core/check/surface.d.ts.map +1 -1
- package/dist/core/check/surface.js +29 -0
- package/dist/core/check/surface.js.map +1 -1
- package/dist/core/emit/canonical.d.ts +22 -8
- package/dist/core/emit/canonical.d.ts.map +1 -1
- package/dist/core/emit/canonical.js +67 -6
- package/dist/core/emit/canonical.js.map +1 -1
- package/dist/core/emit/defaults.d.ts +35 -16
- package/dist/core/emit/defaults.d.ts.map +1 -1
- package/dist/core/emit/defaults.js +66 -0
- package/dist/core/emit/defaults.js.map +1 -1
- package/dist/core/emit/index.d.ts +2 -0
- package/dist/core/emit/index.d.ts.map +1 -1
- package/dist/core/emit/index.js +2 -0
- package/dist/core/emit/index.js.map +1 -1
- package/dist/core/engine/arithmetic.d.ts +6 -25
- package/dist/core/engine/arithmetic.d.ts.map +1 -1
- package/dist/core/engine/arithmetic.js +48 -3
- package/dist/core/engine/arithmetic.js.map +1 -1
- package/dist/core/engine/index.d.ts +2 -2
- package/dist/core/engine/index.d.ts.map +1 -1
- package/dist/core/engine/index.js +2 -2
- package/dist/core/engine/index.js.map +1 -1
- package/dist/core/engine/library/arrays.d.ts.map +1 -1
- package/dist/core/engine/library/arrays.js +8 -2
- package/dist/core/engine/library/arrays.js.map +1 -1
- package/dist/core/engine/library/code-points.d.ts +40 -0
- package/dist/core/engine/library/code-points.d.ts.map +1 -0
- package/dist/core/engine/library/code-points.js +74 -0
- package/dist/core/engine/library/code-points.js.map +1 -0
- package/dist/core/engine/library/index.d.ts +5 -0
- package/dist/core/engine/library/index.d.ts.map +1 -1
- package/dist/core/engine/library/index.js +5 -0
- package/dist/core/engine/library/index.js.map +1 -1
- package/dist/core/engine/library/text.d.ts.map +1 -1
- package/dist/core/engine/library/text.js +51 -34
- package/dist/core/engine/library/text.js.map +1 -1
- package/dist/core/engine/load.d.ts +35 -1
- package/dist/core/engine/load.d.ts.map +1 -1
- package/dist/core/engine/load.js +63 -0
- package/dist/core/engine/load.js.map +1 -1
- package/dist/core/engine/verify-tables.d.ts.map +1 -1
- package/dist/core/engine/verify-tables.js +41 -0
- package/dist/core/engine/verify-tables.js.map +1 -1
- package/dist/core/index.d.ts +43 -4
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +23 -2
- package/dist/core/index.js.map +1 -1
- package/dist/core/stdlib/index.d.ts +1 -1
- package/dist/core/stdlib/index.d.ts.map +1 -1
- package/dist/core/stdlib/index.js +1 -1
- package/dist/core/stdlib/index.js.map +1 -1
- package/dist/core/stdlib/maths/index.d.ts +1 -1
- package/dist/core/stdlib/maths/index.d.ts.map +1 -1
- package/dist/core/stdlib/maths/index.js +1 -1
- package/dist/core/stdlib/maths/index.js.map +1 -1
- package/dist/core/stdlib/maths/rounding.d.ts +5 -0
- package/dist/core/stdlib/maths/rounding.d.ts.map +1 -1
- package/dist/core/stdlib/maths/rounding.js +19 -1
- package/dist/core/stdlib/maths/rounding.js.map +1 -1
- package/dist/core/version/version.generated.d.ts +1 -1
- package/dist/core/version/version.generated.js +1 -1
- package/dist/editor/complete.d.ts +40 -0
- package/dist/editor/complete.d.ts.map +1 -0
- package/dist/editor/complete.js +206 -0
- package/dist/editor/complete.js.map +1 -0
- package/dist/editor/diagnose.d.ts +18 -0
- package/dist/editor/diagnose.d.ts.map +1 -0
- package/dist/editor/diagnose.js +70 -0
- package/dist/editor/diagnose.js.map +1 -0
- package/dist/editor/format.d.ts +11 -0
- package/dist/editor/format.d.ts.map +1 -0
- package/dist/editor/format.js +50 -0
- package/dist/editor/format.js.map +1 -0
- package/dist/editor/highlight.d.ts +28 -0
- package/dist/editor/highlight.d.ts.map +1 -0
- package/dist/editor/highlight.js +65 -0
- package/dist/editor/highlight.js.map +1 -0
- package/dist/editor/hover.d.ts +40 -0
- package/dist/editor/hover.d.ts.map +1 -0
- package/dist/editor/hover.js +147 -0
- package/dist/editor/hover.js.map +1 -0
- package/dist/editor/index.d.ts +60 -0
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.js +7 -0
- package/dist/editor/index.js.map +1 -0
- package/dist/editor/kinds.d.ts +27 -0
- package/dist/editor/kinds.d.ts.map +1 -0
- package/dist/editor/kinds.js +118 -0
- package/dist/editor/kinds.js.map +1 -0
- package/dist/editor/layout.d.ts +47 -0
- package/dist/editor/layout.d.ts.map +1 -0
- package/dist/editor/layout.js +135 -0
- package/dist/editor/layout.js.map +1 -0
- package/dist/editor/manifest.d.ts +47 -0
- package/dist/editor/manifest.d.ts.map +1 -0
- package/dist/editor/manifest.js +93 -0
- package/dist/editor/manifest.js.map +1 -0
- package/dist/editor/reading.d.ts +41 -0
- package/dist/editor/reading.d.ts.map +1 -0
- package/dist/editor/reading.js +51 -0
- package/dist/editor/reading.js.map +1 -0
- package/dist/editor/scan.d.ts +26 -0
- package/dist/editor/scan.d.ts.map +1 -0
- package/dist/editor/scan.js +139 -0
- package/dist/editor/scan.js.map +1 -0
- package/dist/editor/scope.d.ts +19 -0
- package/dist/editor/scope.d.ts.map +1 -0
- package/dist/editor/scope.js +99 -0
- package/dist/editor/scope.js.map +1 -0
- package/dist/editor/signature.d.ts +38 -0
- package/dist/editor/signature.d.ts.map +1 -0
- package/dist/editor/signature.js +112 -0
- package/dist/editor/signature.js.map +1 -0
- package/dist/editor/site.d.ts +46 -0
- package/dist/editor/site.d.ts.map +1 -0
- package/dist/editor/site.js +184 -0
- package/dist/editor/site.js.map +1 -0
- package/dist/editor/spacing.d.ts +29 -0
- package/dist/editor/spacing.d.ts.map +1 -0
- package/dist/editor/spacing.js +116 -0
- package/dist/editor/spacing.js.map +1 -0
- package/package.json +42 -3
- package/spec/README.md +2 -1
- package/spec/errors.json +141 -1
- package/src/adapters/charts/driving.ts +109 -0
- package/src/adapters/charts/run.ts +120 -28
- package/src/adapters/charts/surfaces.ts +17 -0
- package/src/adapters/charts/tables.ts +88 -0
- package/src/adapters/charts/venue.ts +132 -0
- package/src/adapters/codemirror/commands.ts +53 -0
- package/src/adapters/codemirror/completion.ts +74 -0
- package/src/adapters/codemirror/contract.ts +156 -0
- package/src/adapters/codemirror/index.ts +66 -0
- package/src/adapters/codemirror/lint.ts +66 -0
- package/src/adapters/codemirror/positions.ts +79 -0
- package/src/adapters/codemirror/stream.ts +87 -0
- package/src/adapters/codemirror/tokens.ts +64 -0
- package/src/adapters/codemirror/tooltips.ts +113 -0
- package/src/core/accounting/analysis.ts +188 -0
- package/src/core/accounting/charges.ts +452 -0
- package/src/core/accounting/equity.ts +314 -0
- package/src/core/accounting/index.ts +51 -0
- package/src/core/accounting/markers.ts +95 -0
- package/src/core/accounting/monthly.ts +137 -0
- package/src/core/accounting/report.ts +94 -0
- package/src/core/accounting/shapes.ts +73 -0
- package/src/core/accounting/statistics.ts +387 -0
- package/src/core/accounting/trades.ts +313 -0
- package/src/core/backtest/case.ts +395 -0
- package/src/core/backtest/compare.ts +246 -0
- package/src/core/backtest/declaration.ts +97 -0
- package/src/core/backtest/deliver.ts +161 -0
- package/src/core/backtest/drive.ts +468 -0
- package/src/core/backtest/index.ts +64 -0
- package/src/core/backtest/range.ts +137 -0
- package/src/core/backtest/record.ts +470 -0
- package/src/core/backtest/replay.ts +168 -0
- package/src/core/backtest/resting.ts +125 -0
- package/src/core/backtest/settings.ts +280 -0
- package/src/core/backtest/simulate.ts +499 -0
- package/src/core/catalogue/catalogue.generated.ts +6 -0
- package/src/core/catalogue/values.generated.ts +6 -0
- package/src/core/check/index.ts +3 -0
- package/src/core/check/library-orders.ts +2 -2
- package/src/core/check/library-prose.generated.ts +367 -0
- package/src/core/check/surface.ts +34 -0
- package/src/core/emit/canonical.ts +67 -9
- package/src/core/emit/defaults.ts +70 -0
- package/src/core/emit/index.ts +2 -0
- package/src/core/engine/arithmetic.ts +23 -3
- package/src/core/engine/index.ts +2 -2
- package/src/core/engine/library/arrays.ts +8 -2
- package/src/core/engine/library/code-points.ts +73 -0
- package/src/core/engine/library/index.ts +6 -0
- package/src/core/engine/library/text.ts +53 -35
- package/src/core/engine/load.ts +84 -2
- package/src/core/engine/verify-tables.ts +43 -0
- package/src/core/index.ts +100 -2
- package/src/core/stdlib/index.ts +1 -0
- package/src/core/stdlib/maths/index.ts +1 -0
- package/src/core/stdlib/maths/rounding.ts +23 -1
- package/src/core/version/version.generated.ts +1 -1
- package/src/editor/complete.ts +266 -0
- package/src/editor/diagnose.ts +71 -0
- package/src/editor/format.ts +85 -0
- package/src/editor/highlight.ts +110 -0
- package/src/editor/hover.ts +199 -0
- package/src/editor/index.ts +65 -0
- package/src/editor/kinds.ts +157 -0
- package/src/editor/layout.ts +189 -0
- package/src/editor/manifest.ts +119 -0
- package/src/editor/reading.ts +69 -0
- package/src/editor/scan.ts +162 -0
- package/src/editor/scope.ts +122 -0
- package/src/editor/signature.ts +150 -0
- package/src/editor/site.ts +233 -0
- package/src/editor/spacing.ts +132 -0
|
@@ -0,0 +1,470 @@
|
|
|
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 { canonicalNumber, canonicalise, programHash, sha256, sourceHash } from '../emit/index.js';
|
|
30
|
+
import type { CompiledProgram, SourceStamp } from '../emit/index.js';
|
|
31
|
+
import type { Instrument, LedgerRow } from '../engine/index.js';
|
|
32
|
+
import { VERSION } from '../version/index.js';
|
|
33
|
+
import type { BacktestSettings } from './settings.js';
|
|
34
|
+
|
|
35
|
+
/** One bar as a record holds it. */
|
|
36
|
+
export interface RecordedBar {
|
|
37
|
+
readonly time: number | null;
|
|
38
|
+
readonly open: number | null;
|
|
39
|
+
readonly high: number | null;
|
|
40
|
+
readonly low: number | null;
|
|
41
|
+
readonly close: number | null;
|
|
42
|
+
readonly volume: number | null;
|
|
43
|
+
readonly oi: number | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The bars, held or pointed at.
|
|
48
|
+
*
|
|
49
|
+
* Both forms carry the same hash, computed the same way over the canonical
|
|
50
|
+
* tuple of each bar, so a referenced record can be turned into an inline one by
|
|
51
|
+
* whoever holds the bars and the result is the same case.
|
|
52
|
+
*/
|
|
53
|
+
export type BarsInRecord =
|
|
54
|
+
| {
|
|
55
|
+
readonly form: 'inline';
|
|
56
|
+
readonly hash: string;
|
|
57
|
+
readonly count: number;
|
|
58
|
+
readonly rows: readonly RecordedBar[];
|
|
59
|
+
}
|
|
60
|
+
| {
|
|
61
|
+
readonly form: 'referenced';
|
|
62
|
+
readonly hash: string;
|
|
63
|
+
readonly count: number;
|
|
64
|
+
readonly firstTime: number | null;
|
|
65
|
+
readonly lastTime: number | null;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/** The columns of `conformance.md`'s frames.csv, in its order and with its names. */
|
|
69
|
+
export interface RecordedFrame {
|
|
70
|
+
readonly afterBar: number;
|
|
71
|
+
/** An ordinal, 1-based, never an engine's own id. */
|
|
72
|
+
readonly intent: number;
|
|
73
|
+
readonly status: string;
|
|
74
|
+
readonly filledQty: number;
|
|
75
|
+
readonly avgFillPrice: number | null;
|
|
76
|
+
readonly orderRef: string | null;
|
|
77
|
+
readonly text: string | null;
|
|
78
|
+
/**
|
|
79
|
+
* The destination's own instant for this frame, or null where it stated none.
|
|
80
|
+
*
|
|
81
|
+
* **One field of the ledger is folded from it.** `host-interface.md` 7.2
|
|
82
|
+
* gives a frame a `time` and `stdlib.md` 17.7 moves `updatedAt` to it, so a
|
|
83
|
+
* record that dropped it wrote a ledger no engine reading the case back could
|
|
84
|
+
* fold to: handed frames with no instant, that engine leaves every
|
|
85
|
+
* `updatedAt` at `placedAt`, and the two disagree on exactly the rows whose
|
|
86
|
+
* destination answered later than the bar that placed the order.
|
|
87
|
+
*
|
|
88
|
+
* Absent rather than substituted, because 7.2 lets a destination state none
|
|
89
|
+
* and a row whose frame stated none keeps the instant it had.
|
|
90
|
+
*/
|
|
91
|
+
readonly time: number | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A ledger row of `stdlib.md` 17.7, flattened: the `orders` channel of expected.json. */
|
|
95
|
+
export interface RecordedOrder {
|
|
96
|
+
readonly intent: number;
|
|
97
|
+
readonly orderRef: string;
|
|
98
|
+
readonly tag: string;
|
|
99
|
+
readonly leg: string;
|
|
100
|
+
readonly positionRef: number;
|
|
101
|
+
readonly symbol: string | null;
|
|
102
|
+
readonly exchange: string | null;
|
|
103
|
+
readonly product: string;
|
|
104
|
+
readonly side: string;
|
|
105
|
+
readonly qty: number | null;
|
|
106
|
+
readonly qtyType: string;
|
|
107
|
+
readonly type: string | null;
|
|
108
|
+
readonly price: number | null;
|
|
109
|
+
readonly trigger: number | null;
|
|
110
|
+
readonly status: string;
|
|
111
|
+
readonly filledQty: number;
|
|
112
|
+
readonly avgFillPrice: number | null;
|
|
113
|
+
readonly rejection: string | null;
|
|
114
|
+
readonly placedAt: number | null;
|
|
115
|
+
readonly updatedAt: number | null;
|
|
116
|
+
readonly units: number | null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A diagnostic as a record holds it: the code, the span and the bar. */
|
|
120
|
+
export interface RecordedDiagnostic {
|
|
121
|
+
readonly code: string;
|
|
122
|
+
readonly line: number;
|
|
123
|
+
readonly column: number;
|
|
124
|
+
readonly severity: string;
|
|
125
|
+
readonly barIndex: number | null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** One run, whole, and readable by an engine that is not this one. */
|
|
129
|
+
export interface RunRecord {
|
|
130
|
+
readonly recordVersion: number;
|
|
131
|
+
readonly engine: { readonly name: string; readonly version: string };
|
|
132
|
+
readonly languageVersion: string;
|
|
133
|
+
/** Already plain data. */
|
|
134
|
+
readonly program: CompiledProgram;
|
|
135
|
+
readonly programHash: string;
|
|
136
|
+
/** Hash, lines, file: the script revision. */
|
|
137
|
+
readonly source: SourceStamp;
|
|
138
|
+
/**
|
|
139
|
+
* The source text itself, or null on a record written before version 2.
|
|
140
|
+
*
|
|
141
|
+
* **A record is the conformance case, and a case has to hold `script.os`.**
|
|
142
|
+
* `source` above identifies the script and cannot reproduce it: a hash is a
|
|
143
|
+
* fingerprint, so it settles whether two files are the same and yields
|
|
144
|
+
* neither of them. Without the text a stored record could be replayed months
|
|
145
|
+
* later and still not be handed to anybody else to run, which is the whole
|
|
146
|
+
* claim the suite exists to test.
|
|
147
|
+
*
|
|
148
|
+
* It lives here and not on the compiled program on purpose. A program is
|
|
149
|
+
* executable data that no engine needs the source to run, and it is the
|
|
150
|
+
* versioned artefact adopters depend on; putting the text there would send a
|
|
151
|
+
* script everywhere its program travels and widen the format every engine
|
|
152
|
+
* has to read. The record is the thing that wants to be self-contained.
|
|
153
|
+
*/
|
|
154
|
+
readonly sourceText: string | null;
|
|
155
|
+
readonly settings: BacktestSettings;
|
|
156
|
+
/**
|
|
157
|
+
* The instrument record of `host-interface.md` 4.1, as the engine was handed
|
|
158
|
+
* it, or null on a record written before version 3.
|
|
159
|
+
*
|
|
160
|
+
* **A case holds `instrument.json`, and `conformance.md` section 2 says that
|
|
161
|
+
* file is this record.** The contract in `settings` is the money layer's
|
|
162
|
+
* snapshot of the same instrument and holds six of the twelve facts; the
|
|
163
|
+
* ones a script reads and the money never does, the interval, the timezone,
|
|
164
|
+
* the session and the volume flag, were handed to the engine and written
|
|
165
|
+
* down nowhere. A case harvested from such a record either omitted a fact
|
|
166
|
+
* the page requires or stated one the run never had, and either way the
|
|
167
|
+
* second engine ran a different study than the one the expected output
|
|
168
|
+
* came from.
|
|
169
|
+
*
|
|
170
|
+
* It is the whole record and not the six facts beside the contract, because
|
|
171
|
+
* what is recorded is what the engine read at load, verbatim, and a reader
|
|
172
|
+
* should not have to compose it.
|
|
173
|
+
*/
|
|
174
|
+
readonly instrument: Instrument | null;
|
|
175
|
+
readonly bars: BarsInRecord;
|
|
176
|
+
/** What the destination answered, in delivery order. */
|
|
177
|
+
readonly frames: readonly RecordedFrame[];
|
|
178
|
+
/** What the engine folded, in fold order. */
|
|
179
|
+
readonly fills: readonly RecordedFill[];
|
|
180
|
+
/** The ledger at the end, oldest first. */
|
|
181
|
+
readonly orders: readonly RecordedOrder[];
|
|
182
|
+
readonly diagnostics: readonly RecordedDiagnostic[];
|
|
183
|
+
readonly report: Report;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The revision of this document's own shape.
|
|
188
|
+
*
|
|
189
|
+
* A reader is handed a record and has to know what it is looking at before it
|
|
190
|
+
* reads a field, so the number is first in the document and is written by this
|
|
191
|
+
* file alone. It moves when a channel is added or a meaning changes, never when
|
|
192
|
+
* a figure in a report does.
|
|
193
|
+
*/
|
|
194
|
+
export const RECORD_VERSION = 4;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The revision each later channel arrived in.
|
|
198
|
+
*
|
|
199
|
+
* A record written before a channel existed reads with that channel absent,
|
|
200
|
+
* and this table is what `recordFromJson` reads it from: one row per channel
|
|
201
|
+
* added since version 1, so the rule for an old record is stated once and
|
|
202
|
+
* grows by a line when the next channel does.
|
|
203
|
+
*
|
|
204
|
+
* `frameTime` is a field of a row rather than a channel of the document, and it
|
|
205
|
+
* is a row here for the same reason the other two are: what a reader has to
|
|
206
|
+
* know is which revision it arrived in. Where the absence is written differs,
|
|
207
|
+
* and that is the reader's business below, not this table's.
|
|
208
|
+
*/
|
|
209
|
+
const ADDED_IN = { sourceText: 2, instrument: 3, frameTime: 4 } as const;
|
|
210
|
+
|
|
211
|
+
/** What this engine calls itself in a record it wrote. */
|
|
212
|
+
const ENGINE_NAME = 'openscript';
|
|
213
|
+
|
|
214
|
+
/** The parts a run hands over, each already in the shape the record holds. */
|
|
215
|
+
export interface RecordParts {
|
|
216
|
+
readonly program: CompiledProgram;
|
|
217
|
+
/**
|
|
218
|
+
* The script's own text, so the record can become a conformance case.
|
|
219
|
+
*
|
|
220
|
+
* Optional because a caller that has only a compiled program cannot invent
|
|
221
|
+
* it, and a run is still worth recording without it. `caseFilesFrom` is the
|
|
222
|
+
* one thing that then cannot be served, and it says so rather than writing a
|
|
223
|
+
* case with a hole in it.
|
|
224
|
+
*/
|
|
225
|
+
readonly sourceText?: string;
|
|
226
|
+
readonly settings: BacktestSettings;
|
|
227
|
+
/** The instrument record the engine was handed, `host-interface.md` 4.1. */
|
|
228
|
+
readonly instrument: Instrument;
|
|
229
|
+
readonly bars: readonly RecordedBar[];
|
|
230
|
+
/**
|
|
231
|
+
* Whether the bars travel in the record or are pointed at.
|
|
232
|
+
*
|
|
233
|
+
* Inline is the conformance form, because a case holds every byte of its own
|
|
234
|
+
* input. Referenced is the operational form, because nothing that grows with
|
|
235
|
+
* history may travel in a request body. Both carry the same hash over the
|
|
236
|
+
* same canonical tuples, so one becomes the other without becoming a
|
|
237
|
+
* different case.
|
|
238
|
+
*/
|
|
239
|
+
readonly form: 'inline' | 'referenced';
|
|
240
|
+
readonly frames: readonly RecordedFrame[];
|
|
241
|
+
readonly fills: readonly RecordedFill[];
|
|
242
|
+
readonly orders: readonly RecordedOrder[];
|
|
243
|
+
readonly diagnostics: readonly RecordedDiagnostic[];
|
|
244
|
+
readonly report: Report;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* One run, as the document another engine is handed.
|
|
249
|
+
*
|
|
250
|
+
* Nothing is computed here. Every channel arrives folded, because what a record
|
|
251
|
+
* says has to be what the run did rather than what a second fold of the same
|
|
252
|
+
* inputs came to: a record that recomputed its own report would agree with
|
|
253
|
+
* itself whatever the engine had actually done.
|
|
254
|
+
*/
|
|
255
|
+
export function recordOf(parts: RecordParts): RunRecord {
|
|
256
|
+
return {
|
|
257
|
+
recordVersion: RECORD_VERSION,
|
|
258
|
+
engine: { name: ENGINE_NAME, version: VERSION },
|
|
259
|
+
languageVersion: canonicalNumber(parts.program.openscript.language),
|
|
260
|
+
program: parts.program,
|
|
261
|
+
programHash: programHash(parts.program),
|
|
262
|
+
source: parts.program.source,
|
|
263
|
+
sourceText: textFor(parts),
|
|
264
|
+
settings: parts.settings,
|
|
265
|
+
instrument: parts.instrument,
|
|
266
|
+
bars: barsIn(parts.bars, parts.form),
|
|
267
|
+
frames: parts.frames,
|
|
268
|
+
fills: parts.fills,
|
|
269
|
+
orders: parts.orders,
|
|
270
|
+
diagnostics: parts.diagnostics,
|
|
271
|
+
report: parts.report,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* The source text, checked against the hash the program already carries.
|
|
277
|
+
*
|
|
278
|
+
* A check rather than a promise, and it costs one hash of a few kilobytes. The
|
|
279
|
+
* failure it exists for is quiet: a caller that passes the text of a different
|
|
280
|
+
* revision than the one it compiled produces a case whose script does not make
|
|
281
|
+
* its own expected output, and the engine being tested gets the blame for a
|
|
282
|
+
* disagreement that was in the case all along.
|
|
283
|
+
*/
|
|
284
|
+
function textFor(parts: RecordParts): string | null {
|
|
285
|
+
const text = parts.sourceText;
|
|
286
|
+
if (text === undefined) return null;
|
|
287
|
+
if (sourceHash(text) !== parts.program.source.hash) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
'openscript: the source text does not hash to the source hash the program carries',
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
return text;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The bars, held or pointed at, and the same hash either way.
|
|
297
|
+
*
|
|
298
|
+
* The referenced form keeps the first and last times so that a reader can say
|
|
299
|
+
* which bars are wanted without holding them, and the count so that a set of
|
|
300
|
+
* the wrong length is known to be wrong before it is hashed.
|
|
301
|
+
*/
|
|
302
|
+
export function barsIn(bars: readonly RecordedBar[], form: 'inline' | 'referenced'): BarsInRecord {
|
|
303
|
+
const hash = barsHash(bars);
|
|
304
|
+
if (form === 'inline') {
|
|
305
|
+
return { form: 'inline', hash, count: bars.length, rows: bars };
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
form: 'referenced',
|
|
309
|
+
hash,
|
|
310
|
+
count: bars.length,
|
|
311
|
+
firstTime: bars[0]?.time ?? null,
|
|
312
|
+
lastTime: bars[bars.length - 1]?.time ?? null,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The hash a record names its bars by.
|
|
318
|
+
*
|
|
319
|
+
* Over the canonical tuple of each bar, in the order they were supplied, and
|
|
320
|
+
* through the canonical encoding the compiled program is hashed with, so there
|
|
321
|
+
* is one canonical writer in the repository rather than two that agree until
|
|
322
|
+
* one of them is changed. A tuple rather than an object, because the field
|
|
323
|
+
* order is then the specification's and not a sort's.
|
|
324
|
+
*/
|
|
325
|
+
export function barsHash(bars: readonly RecordedBar[]): string {
|
|
326
|
+
const tuples = bars.map((bar) => [
|
|
327
|
+
bar.time,
|
|
328
|
+
bar.open,
|
|
329
|
+
bar.high,
|
|
330
|
+
bar.low,
|
|
331
|
+
bar.close,
|
|
332
|
+
bar.volume,
|
|
333
|
+
bar.oi,
|
|
334
|
+
]);
|
|
335
|
+
return 'sha256:' + sha256(canonicalise(tuples));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* The record as the bytes another engine reads.
|
|
340
|
+
*
|
|
341
|
+
* Through the canonical encoding, so key order is fixed, every number is in the
|
|
342
|
+
* shortest decimal form that reads back as itself, and two runs of the same
|
|
343
|
+
* record produce the same bytes on every machine. That is what makes the
|
|
344
|
+
* reproducibility test a byte comparison rather than a tour of the fields.
|
|
345
|
+
*/
|
|
346
|
+
export function recordToJson(record: RunRecord): string {
|
|
347
|
+
return canonicalise(record);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* The run, without who produced it.
|
|
352
|
+
*
|
|
353
|
+
* **`engine` is provenance, and provenance is not part of the run.** The claim
|
|
354
|
+
* a rerun makes is that the run is a function of the record, and that claim has
|
|
355
|
+
* to survive the engine it runs on being upgraded: a record stored under
|
|
356
|
+
* `0.3.0` and rerun under `0.4.0` produces the same trades, the same fills and
|
|
357
|
+
* the same money, and a document carrying the version stamp inside the bytes
|
|
358
|
+
* being compared says it does not.
|
|
359
|
+
*
|
|
360
|
+
* It was compared as whole bytes until the first version bump would have broken
|
|
361
|
+
* it, which is a test that passes for exactly as long as nothing changes and
|
|
362
|
+
* then fails for the one reason that is not a defect. So the stamp stays in the
|
|
363
|
+
* record, because knowing which engine wrote a run is worth keeping, and the
|
|
364
|
+
* comparison is made over everything else.
|
|
365
|
+
*/
|
|
366
|
+
export function runBytes(record: RunRecord): string {
|
|
367
|
+
const { engine: _engine, ...run } = record;
|
|
368
|
+
return canonicalise(run);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* A record read back from those bytes.
|
|
373
|
+
*
|
|
374
|
+
* A parse and not a validation: what comes back is the document as it was
|
|
375
|
+
* written, and a document this engine did not write is the caller's to trust or
|
|
376
|
+
* not. The one thing asserted is the revision, because reading a later
|
|
377
|
+
* revision's fields under this one's rules is how a record silently becomes a
|
|
378
|
+
* different run.
|
|
379
|
+
*/
|
|
380
|
+
export function recordFromJson(text: string): RunRecord | null {
|
|
381
|
+
const parsed: unknown = JSON.parse(text);
|
|
382
|
+
if (parsed === null || typeof parsed !== 'object') return null;
|
|
383
|
+
const record = parsed as RunRecord;
|
|
384
|
+
const version = record.recordVersion;
|
|
385
|
+
if (typeof version !== 'number' || version < 1 || version > RECORD_VERSION) return null;
|
|
386
|
+
// An earlier revision is readable and a later one is not, and the asymmetry
|
|
387
|
+
// is the point. A later revision may mean something by a field this one
|
|
388
|
+
// thinks it knows, which is how a record silently becomes a different run. An
|
|
389
|
+
// earlier one only ever has fewer: every channel it carries means here what
|
|
390
|
+
// it meant there, and the ones added since are absent rather than wrong. So a
|
|
391
|
+
// run stored months ago still reads, which is the whole of what it was stored
|
|
392
|
+
// for, and a channel it never carried reads as absent.
|
|
393
|
+
if (version === RECORD_VERSION) return record;
|
|
394
|
+
return {
|
|
395
|
+
...record,
|
|
396
|
+
sourceText: version >= ADDED_IN.sourceText ? record.sourceText : null,
|
|
397
|
+
instrument: version >= ADDED_IN.instrument ? record.instrument : null,
|
|
398
|
+
frames: version >= ADDED_IN.frameTime ? record.frames : timeless(record.frames),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* The frames of a record written before a frame carried an instant.
|
|
404
|
+
*
|
|
405
|
+
* The absence is written on every frame rather than on the record, because the
|
|
406
|
+
* channel is a field of a row: a reader that left the field undefined would
|
|
407
|
+
* hand the case projection an undefined where the column's absent spelling
|
|
408
|
+
* belongs, and the file would read `undefined` back to the next engine. The
|
|
409
|
+
* frames are left as they are when they are not an array, because this is a
|
|
410
|
+
* parse and not a validation and a document this engine did not write is the
|
|
411
|
+
* caller's to trust or not.
|
|
412
|
+
*/
|
|
413
|
+
function timeless(frames: readonly RecordedFrame[]): readonly RecordedFrame[] {
|
|
414
|
+
if (!Array.isArray(frames)) return frames;
|
|
415
|
+
return frames.map((frame) => ({ ...frame, time: null }));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* One ledger row, flattened into the words a case file prints.
|
|
420
|
+
*
|
|
421
|
+
* The engine's own types do not cross: a second engine has its own, and a
|
|
422
|
+
* channel written in this one's would be a case only this one could read. The
|
|
423
|
+
* intent is an ordinal for the same reason, because no engine can know what id
|
|
424
|
+
* another minted.
|
|
425
|
+
*
|
|
426
|
+
* `qtyType` comes from the intent rather than from the row, because the unit a
|
|
427
|
+
* quantity is counted in is per order and not per leg: an order the engine
|
|
428
|
+
* sized itself is in units whatever the declaration counts in, and a row that
|
|
429
|
+
* printed the declaration's unit for it would state a quantity in a unit it was
|
|
430
|
+
* never measured in.
|
|
431
|
+
*/
|
|
432
|
+
export function orderIn(row: LedgerRow, intent: number, qtyType: string): RecordedOrder {
|
|
433
|
+
return {
|
|
434
|
+
intent,
|
|
435
|
+
orderRef: row.orderRef,
|
|
436
|
+
tag: row.tag,
|
|
437
|
+
leg: row.leg,
|
|
438
|
+
positionRef: row.positionRef,
|
|
439
|
+
symbol: row.instrument.symbol,
|
|
440
|
+
exchange: row.instrument.exchange,
|
|
441
|
+
product: row.product,
|
|
442
|
+
side: row.side,
|
|
443
|
+
qty: row.qty,
|
|
444
|
+
qtyType,
|
|
445
|
+
type: row.type,
|
|
446
|
+
price: row.price,
|
|
447
|
+
trigger: row.trigger,
|
|
448
|
+
status: row.status,
|
|
449
|
+
filledQty: row.filledQty,
|
|
450
|
+
avgFillPrice: row.avgFillPrice,
|
|
451
|
+
rejection: row.rejection === '' ? null : row.rejection,
|
|
452
|
+
placedAt: row.placedAt,
|
|
453
|
+
updatedAt: row.updatedAt,
|
|
454
|
+
units: row.units,
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** One diagnostic, as a record holds it: the code, the span and the bar. */
|
|
459
|
+
export function diagnosticIn(
|
|
460
|
+
diagnostic: Diagnostic,
|
|
461
|
+
barIndex: number | null,
|
|
462
|
+
): RecordedDiagnostic {
|
|
463
|
+
return {
|
|
464
|
+
code: diagnostic.code,
|
|
465
|
+
line: diagnostic.span.line,
|
|
466
|
+
column: diagnostic.span.column,
|
|
467
|
+
severity: diagnostic.severity,
|
|
468
|
+
barIndex,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
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 type { ChargeSchedule, Report } from '../accounting/index.js';
|
|
34
|
+
import { diagnosticFor } from '../diagnostics/index.js';
|
|
35
|
+
import type { Diagnostic } from '../diagnostics/index.js';
|
|
36
|
+
import { load } from '../engine/index.js';
|
|
37
|
+
import { backtest } from './drive.js';
|
|
38
|
+
import type { BacktestResult } from './drive.js';
|
|
39
|
+
import { declarationOf } from './declaration.js';
|
|
40
|
+
import type { RunDeclaration } from './declaration.js';
|
|
41
|
+
import { marksFor, windowFor } from './range.js';
|
|
42
|
+
import { barsHash } from './record.js';
|
|
43
|
+
import type { RecordedBar, RunRecord } from './record.js';
|
|
44
|
+
|
|
45
|
+
/** The report a record folds to, or why it could not be folded. */
|
|
46
|
+
export type ReplayResult =
|
|
47
|
+
| { readonly ok: true; readonly report: Report }
|
|
48
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The record's own report, computed again from its own fills.
|
|
52
|
+
*
|
|
53
|
+
* The bars are the record's where it carries them and the caller's where it
|
|
54
|
+
* points at them, and either way they are hashed against what the record names
|
|
55
|
+
* before a figure is computed from them.
|
|
56
|
+
*/
|
|
57
|
+
export function replay(record: RunRecord, bars: readonly RecordedBar[] | null = null): ReplayResult {
|
|
58
|
+
const held = barsOf(record, bars);
|
|
59
|
+
if (!held.ok) return { ok: false, diagnostic: held.diagnostic };
|
|
60
|
+
|
|
61
|
+
const framed = windowFor(held.bars, record.settings.range);
|
|
62
|
+
if (!framed.ok) return { ok: false, diagnostic: framed.diagnostic };
|
|
63
|
+
|
|
64
|
+
const declared = declarationIn(record);
|
|
65
|
+
if (!declared.ok) return { ok: false, diagnostic: declared.diagnostic };
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
ok: true,
|
|
69
|
+
report: reportOf(
|
|
70
|
+
record.fills,
|
|
71
|
+
marksFor(held.bars, framed.covered),
|
|
72
|
+
scheduleIn(record, declared.declaration),
|
|
73
|
+
record.settings.contract,
|
|
74
|
+
declared.declaration.capital,
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The same run, executed again.
|
|
81
|
+
*
|
|
82
|
+
* Everything the run depended on is in the record: the program, the bars, the
|
|
83
|
+
* settings and the inputs. So a rerun takes no argument the record does not
|
|
84
|
+
* already hold, except the bars a referenced record points at rather than
|
|
85
|
+
* carries, and the record it produces is comparable with the original field for
|
|
86
|
+
* field and byte for byte.
|
|
87
|
+
*/
|
|
88
|
+
export function rerun(record: RunRecord, bars: readonly RecordedBar[] | null = null): BacktestResult {
|
|
89
|
+
const held = barsOf(record, bars);
|
|
90
|
+
if (!held.ok) return { ok: false, diagnostic: held.diagnostic };
|
|
91
|
+
// Everything the run depended on, and that includes the two channels that
|
|
92
|
+
// arrived after the sentence above was written. The instrument record is what
|
|
93
|
+
// the engine read at load, so a rerun handed the contract alone runs under
|
|
94
|
+
// different session facts and does not reproduce the bytes; the text is what
|
|
95
|
+
// makes the rerun's record harvestable, and dropping it would turn a record
|
|
96
|
+
// that could become a case into one that cannot, by being rerun.
|
|
97
|
+
return backtest(record.program, held.bars, record.settings, {
|
|
98
|
+
form: record.bars.form,
|
|
99
|
+
...(record.instrument === null ? {} : { instrument: record.instrument }),
|
|
100
|
+
...(record.sourceText === null ? {} : { sourceText: record.sourceText }),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type BarsResult =
|
|
105
|
+
| { readonly ok: true; readonly bars: readonly RecordedBar[] }
|
|
106
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The bars a record was made from, proved to be those bars.
|
|
110
|
+
*
|
|
111
|
+
* A record carrying its own bars is still checked against any that are supplied
|
|
112
|
+
* beside them, because supplying a different set is exactly the mistake worth
|
|
113
|
+
* catching: the caller believes they are replaying this run and they are
|
|
114
|
+
* studying another one.
|
|
115
|
+
*/
|
|
116
|
+
export function barsOf(record: RunRecord, supplied: readonly RecordedBar[] | null): BarsResult {
|
|
117
|
+
if (supplied !== null) {
|
|
118
|
+
const hash = barsHash(supplied);
|
|
119
|
+
if (hash !== record.bars.hash) return { ok: false, diagnostic: mismatch(hash, record) };
|
|
120
|
+
return { ok: true, bars: supplied };
|
|
121
|
+
}
|
|
122
|
+
if (record.bars.form === 'inline') return { ok: true, bars: record.bars.rows };
|
|
123
|
+
// A referenced record points at bars it does not hold, so there is nothing to
|
|
124
|
+
// replay until whoever holds them supplies them. It is the same refusal as a
|
|
125
|
+
// wrong set, because the question it answers is the same one: are these the
|
|
126
|
+
// bars the record was made from.
|
|
127
|
+
return { ok: false, diagnostic: mismatch('no bars supplied', record) };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function mismatch(found: string, record: RunRecord): Diagnostic {
|
|
131
|
+
return diagnosticFor('OS6022', NO_POSITION, { found, expected: record.bars.hash });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type DeclarationResult =
|
|
135
|
+
| { readonly ok: true; readonly declaration: RunDeclaration }
|
|
136
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* What the record's own program declares, after the record's own inputs.
|
|
140
|
+
*
|
|
141
|
+
* The destination handed to the load is one that answers nothing, because
|
|
142
|
+
* nothing is placed: a strategy declares the orders capability and a host that
|
|
143
|
+
* did not offer it would be refused at load for a reason that has nothing to do
|
|
144
|
+
* with the record.
|
|
145
|
+
*/
|
|
146
|
+
function declarationIn(record: RunRecord): DeclarationResult {
|
|
147
|
+
const loaded = load(record.program, {
|
|
148
|
+
settings: record.settings.inputs,
|
|
149
|
+
host: { route: () => undefined },
|
|
150
|
+
});
|
|
151
|
+
if (!loaded.ok) return { ok: false, diagnostic: loaded.diagnostic };
|
|
152
|
+
return { ok: true, declaration: declarationOf(loaded.engine.program, loaded.inputs) };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The cost model the record was carried out under, derived where it was derived. */
|
|
156
|
+
function scheduleIn(record: RunRecord, declared: RunDeclaration): ChargeSchedule {
|
|
157
|
+
if (record.settings.costs !== null) return record.settings.costs;
|
|
158
|
+
return scheduleFromDeclaration(
|
|
159
|
+
declared.commission,
|
|
160
|
+
declared.commissionType,
|
|
161
|
+
declared.slippage,
|
|
162
|
+
record.settings.contract.currency,
|
|
163
|
+
record.settings.contract.digits,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Where a refusal about a record points: nowhere in anybody's script. */
|
|
168
|
+
const NO_POSITION: Diagnostic['span'] = { offset: 0, length: 0, line: 0, column: 0 };
|