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,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The loop: a compiled program, some bars, and the record of what happened.
|
|
3
|
+
*
|
|
4
|
+
* **Everything is collected as it arrives.** Each bar hands back what it
|
|
5
|
+
* applied and what the frames delivered before it did to the ledger, and that
|
|
6
|
+
* is what the record is built from. Nothing is read back at the end except one
|
|
7
|
+
* snapshot of the ledger, and that snapshot is copied rather than kept:
|
|
8
|
+
* `engine.orders()` hands back the ledger's own array, which the fold writes
|
|
9
|
+
* into and a refused bar truncates, so a driver holding on to it would report
|
|
10
|
+
* whatever the array happened to say last rather than what the run did.
|
|
11
|
+
*
|
|
12
|
+
* **Every bar supplied executes and only the ones inside the window are
|
|
13
|
+
* reported.** A bar before the window is warmup: it runs, its orders are real,
|
|
14
|
+
* and a position opened on it is carried in. What it does not get is an equity
|
|
15
|
+
* point. `range.ts` decides which is which and refuses a window holding no bar
|
|
16
|
+
* at all, before the first bar rather than after the last.
|
|
17
|
+
*
|
|
18
|
+
* **The bar count handed over is the total.** An engine told that each bar is
|
|
19
|
+
* the only bar reports `bar.isLast` true on every one of them, and a strategy
|
|
20
|
+
* that flattens on the last bar then flattens on all of them.
|
|
21
|
+
*
|
|
22
|
+
* **Frames are delivered at the boundary and folded there.** What the venue
|
|
23
|
+
* answered after bar k reaches the engine before bar k + 1 executes, which is
|
|
24
|
+
* where `host-interface.md` 7.4 puts the intake, and the outcome of that fold
|
|
25
|
+
* is reported on bar k + 1. A frame answered after the last bar has no boundary
|
|
26
|
+
* left to fold at: it is not delivered, it is not recorded as delivered, and
|
|
27
|
+
* the order it was about is in the ledger at whatever it last said.
|
|
28
|
+
*
|
|
29
|
+
* **Two drivers, and the only difference between them is where the frames come
|
|
30
|
+
* from.** `backtest` runs against the simulated destination, which decides a
|
|
31
|
+
* fill from the bars and answers frames of its own; `backtestSupplied` is
|
|
32
|
+
* handed the frames and delivers those. Everything else about the two is one
|
|
33
|
+
* function below: the same window, the same load, the same refusals, the same
|
|
34
|
+
* loop and the same record.
|
|
35
|
+
*/
|
|
36
|
+
import { reportOf, scheduleFromDeclaration } from '../accounting/index.js';
|
|
37
|
+
import type { ChargeSchedule, Contract, RecordedFill } from '../accounting/index.js';
|
|
38
|
+
import type { Diagnostic } from '../diagnostics/index.js';
|
|
39
|
+
import type { CompiledProgram } from '../emit/index.js';
|
|
40
|
+
import { load } from '../engine/index.js';
|
|
41
|
+
import type {
|
|
42
|
+
BarResult,
|
|
43
|
+
Engine,
|
|
44
|
+
EngineHost,
|
|
45
|
+
Instrument,
|
|
46
|
+
LedgerRow,
|
|
47
|
+
OrderIntent,
|
|
48
|
+
RoutedEffect,
|
|
49
|
+
} from '../engine/index.js';
|
|
50
|
+
import { declarationOf } from './declaration.js';
|
|
51
|
+
import type { RunDeclaration } from './declaration.js';
|
|
52
|
+
import { Delivery, framedAs, ordinalsOf } from './deliver.js';
|
|
53
|
+
import type { Delivered, Destination } from './deliver.js';
|
|
54
|
+
import { marksFor, windowFor } from './range.js';
|
|
55
|
+
import type { ReportWindow } from './range.js';
|
|
56
|
+
import { diagnosticIn, orderIn, recordOf } from './record.js';
|
|
57
|
+
import type {
|
|
58
|
+
RecordedBar,
|
|
59
|
+
RecordedDiagnostic,
|
|
60
|
+
RecordedFrame,
|
|
61
|
+
RecordedOrder,
|
|
62
|
+
RunRecord,
|
|
63
|
+
} from './record.js';
|
|
64
|
+
import { Simulator } from './simulate.js';
|
|
65
|
+
import { checkSettings } from './settings.js';
|
|
66
|
+
import type { BacktestSettings } from './settings.js';
|
|
67
|
+
|
|
68
|
+
/** What a run came to, or why it could not be carried out at all. */
|
|
69
|
+
export type BacktestResult =
|
|
70
|
+
| { readonly ok: true; readonly record: RunRecord }
|
|
71
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The facts of `host-interface.md` 4.1 that the contract does not hold.
|
|
75
|
+
*
|
|
76
|
+
* The contract already states six of the twelve, the ones the money is priced
|
|
77
|
+
* under, and a fact stated in two places is a fact that can disagree with
|
|
78
|
+
* itself: a tick size the engine rounds to and a different one the venue
|
|
79
|
+
* worsens a fill by is a run under two instruments. So the other six are
|
|
80
|
+
* stated here and the six the contract holds cannot be, which the compiler
|
|
81
|
+
* enforces rather than a check at run time.
|
|
82
|
+
*/
|
|
83
|
+
export type InstrumentFacts = Omit<
|
|
84
|
+
Instrument,
|
|
85
|
+
'symbol' | 'exchange' | 'tickSize' | 'lotSize' | 'pointValue' | 'currency'
|
|
86
|
+
>;
|
|
87
|
+
|
|
88
|
+
/** The few choices a driver has that are not settings of the run itself. */
|
|
89
|
+
export interface DriveOptions {
|
|
90
|
+
/** Whether the bars travel in the record. Inline is the conformance form. */
|
|
91
|
+
readonly form?: 'inline' | 'referenced';
|
|
92
|
+
/**
|
|
93
|
+
* What the host states about the instrument beside the contract, so the
|
|
94
|
+
* record it produces is a conformance case.
|
|
95
|
+
*
|
|
96
|
+
* A case holds `instrument.json`, which `conformance.md` section 2 says is
|
|
97
|
+
* the record of `host-interface.md` 4.1, and that page requires one fact of
|
|
98
|
+
* every host: `hasVolume`, because no derivation recovers it (4.2). The
|
|
99
|
+
* engine reads the record and does not refuse a run without it, so a caller
|
|
100
|
+
* that leaves this out gets a record that replays and reruns like any other
|
|
101
|
+
* and cannot become a case, exactly as one without `sourceText` cannot.
|
|
102
|
+
*
|
|
103
|
+
* A session stated without a timezone is refused at load, OS6012, by the
|
|
104
|
+
* same rule that refuses it for any host.
|
|
105
|
+
*/
|
|
106
|
+
readonly instrument?: InstrumentFacts;
|
|
107
|
+
/**
|
|
108
|
+
* The script's own text, so the record it produces is a conformance case.
|
|
109
|
+
*
|
|
110
|
+
* A compiled program identifies its source and cannot reproduce it, and a
|
|
111
|
+
* case has to hold `script.os`. A caller holding only a program leaves this
|
|
112
|
+
* out and gets a record that replays and reruns like any other; the one
|
|
113
|
+
* thing it cannot do is be handed to somebody else's engine as a case.
|
|
114
|
+
*
|
|
115
|
+
* It is checked against the program's own source hash, so text from a
|
|
116
|
+
* different revision than the one that was compiled is refused rather than
|
|
117
|
+
* written into a case that could never make its own expected output.
|
|
118
|
+
*/
|
|
119
|
+
readonly sourceText?: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* One run against a simulated destination, from a compiled program to a record.
|
|
124
|
+
*
|
|
125
|
+
* The frames are the destination's own: it is handed the orders, it reads the
|
|
126
|
+
* bars, and what it answers is what the engine folds. A run whose frames
|
|
127
|
+
* somebody else is supplying is `backtestSupplied` below.
|
|
128
|
+
*/
|
|
129
|
+
export function backtest(
|
|
130
|
+
program: CompiledProgram,
|
|
131
|
+
bars: readonly RecordedBar[],
|
|
132
|
+
settings: BacktestSettings,
|
|
133
|
+
options: DriveOptions = {},
|
|
134
|
+
): BacktestResult {
|
|
135
|
+
return drive(program, bars, settings, options, (declared, schedule) =>
|
|
136
|
+
simulated(
|
|
137
|
+
new Simulator({
|
|
138
|
+
bars,
|
|
139
|
+
contract: settings.contract,
|
|
140
|
+
fill: settings.fill,
|
|
141
|
+
slippageTicks: schedule.slippageTicks,
|
|
142
|
+
fillOn: declared.fillOn,
|
|
143
|
+
qtyType: declared.qtyType,
|
|
144
|
+
}),
|
|
145
|
+
),
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* One run over the frames somebody else supplied, which this engine does not
|
|
151
|
+
* choose and does not add to.
|
|
152
|
+
*
|
|
153
|
+
* `conformance.md` section 3 gives a case a `frames.csv` that supplies order
|
|
154
|
+
* frames the way `bars.csv` supplies bars, so that a case asserts the fold
|
|
155
|
+
* against input the engine did not choose. Every other argument is `backtest`'s
|
|
156
|
+
* and means there what it means here. A row is delivered at the boundary it
|
|
157
|
+
* names and folded before the next bar, and the record carries the rows it was
|
|
158
|
+
* handed rather than a second spelling of them.
|
|
159
|
+
*
|
|
160
|
+
* **Nothing is invented.** No fill is priced off a bar, no order rests and no
|
|
161
|
+
* schedule is read, so an order the frames say nothing about stays where its
|
|
162
|
+
* placement left it, which is what a case saying nothing about it means.
|
|
163
|
+
*
|
|
164
|
+
* **A record made here is a record of this run and not of a simulated one.**
|
|
165
|
+
* `rerun` puts a record's program back on a simulated destination, because a
|
|
166
|
+
* record does not say which of the two answered it, so a rerun of one of these
|
|
167
|
+
* reproduces it only where that destination would answer these same frames.
|
|
168
|
+
*/
|
|
169
|
+
export function backtestSupplied(
|
|
170
|
+
program: CompiledProgram,
|
|
171
|
+
bars: readonly RecordedBar[],
|
|
172
|
+
settings: BacktestSettings,
|
|
173
|
+
frames: readonly RecordedFrame[],
|
|
174
|
+
options: DriveOptions = {},
|
|
175
|
+
): BacktestResult {
|
|
176
|
+
return drive(program, bars, settings, options, () => new Delivery(frames));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* The run both drivers are, and the one place a record is made.
|
|
181
|
+
*
|
|
182
|
+
* A refusal before the first bar comes back as a refusal and not as a record:
|
|
183
|
+
* a program that would not load, a setting that cannot be applied and a window
|
|
184
|
+
* holding no bars are all runs that did not happen, and a record of one would
|
|
185
|
+
* be a document asserting figures nothing computed. A failure during a bar is
|
|
186
|
+
* the other way round: the run happened, it stopped, and the record carries
|
|
187
|
+
* both what it did and the diagnostic that stopped it.
|
|
188
|
+
*
|
|
189
|
+
* **The destination is chosen after the program is loaded**, which is why it
|
|
190
|
+
* arrives as a function of the declaration and the schedule: the bar a market
|
|
191
|
+
* order is priced at is the declaration's, and the declaration is resolved at
|
|
192
|
+
* load. The route is wired first and reaches whatever is chosen through the
|
|
193
|
+
* binding below, which is what lets one host serve both halves.
|
|
194
|
+
*/
|
|
195
|
+
function drive(
|
|
196
|
+
program: CompiledProgram,
|
|
197
|
+
bars: readonly RecordedBar[],
|
|
198
|
+
settings: BacktestSettings,
|
|
199
|
+
options: DriveOptions,
|
|
200
|
+
choose: (declared: RunDeclaration, schedule: ChargeSchedule) => Destination,
|
|
201
|
+
): BacktestResult {
|
|
202
|
+
const framed = windowFor(bars, settings.range);
|
|
203
|
+
if (!framed.ok) return { ok: false, diagnostic: framed.diagnostic };
|
|
204
|
+
|
|
205
|
+
let sending: Destination | undefined;
|
|
206
|
+
const instrument = instrumentFor(settings.contract, options.instrument ?? {});
|
|
207
|
+
const loaded = load(program, {
|
|
208
|
+
settings: settings.inputs,
|
|
209
|
+
host: hostFor(instrument, settings.now, (effect, bar) => sending?.route(effect, bar)),
|
|
210
|
+
});
|
|
211
|
+
if (!loaded.ok) return { ok: false, diagnostic: loaded.diagnostic };
|
|
212
|
+
|
|
213
|
+
const engine = loaded.engine;
|
|
214
|
+
const declared = declarationOf(engine.program, loaded.inputs);
|
|
215
|
+
const problem = checkSettings(settings, declared);
|
|
216
|
+
if (problem !== null) return { ok: false, diagnostic: problem };
|
|
217
|
+
|
|
218
|
+
const schedule = scheduleFor(settings, declared);
|
|
219
|
+
const destination = choose(declared, schedule);
|
|
220
|
+
sending = destination;
|
|
221
|
+
|
|
222
|
+
const run = walk(engine, destination, bars, framed.covered);
|
|
223
|
+
const ordinals = ordinalsOf(destination.intents);
|
|
224
|
+
const marks = marksFor(bars, framed.covered);
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
ok: true,
|
|
228
|
+
record: recordOf({
|
|
229
|
+
program: engine.program,
|
|
230
|
+
...(options.sourceText === undefined ? {} : { sourceText: options.sourceText }),
|
|
231
|
+
settings,
|
|
232
|
+
instrument,
|
|
233
|
+
bars,
|
|
234
|
+
form: options.form ?? 'inline',
|
|
235
|
+
// The row where a case supplied one, because that row is the input and a
|
|
236
|
+
// record of a run over it says what it was given rather than what it
|
|
237
|
+
// would have written down had it decided the frame itself.
|
|
238
|
+
frames: run.frames.map((one) => one.row ?? framedAs(one.frame, one.afterBar, ordinals)),
|
|
239
|
+
fills: run.fills,
|
|
240
|
+
orders: ordersOf(engine.orders(), destination.intents, ordinals),
|
|
241
|
+
diagnostics: run.diagnostics,
|
|
242
|
+
report: reportOf(run.fills, marks, schedule, settings.contract, declared.capital),
|
|
243
|
+
}),
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The simulated destination, as a destination the loop below can drive. */
|
|
248
|
+
function simulated(venue: Simulator): Destination {
|
|
249
|
+
return {
|
|
250
|
+
route: (effect, barIndex) => venue.route(effect, barIndex),
|
|
251
|
+
answers: (barIndex) =>
|
|
252
|
+
venue.framesFor(barIndex).map((frame) => ({ frame, afterBar: barIndex, row: null })),
|
|
253
|
+
intents: venue.intents,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** What one walk of the bars produced. */
|
|
258
|
+
interface Walked {
|
|
259
|
+
readonly fills: readonly RecordedFill[];
|
|
260
|
+
readonly frames: readonly Delivered[];
|
|
261
|
+
readonly diagnostics: readonly RecordedDiagnostic[];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The bars, in order, with the venue answering between them.
|
|
266
|
+
*
|
|
267
|
+
* The one ordering rule: deliver, then append, then collect what the append
|
|
268
|
+
* reported, then ask the venue what this bar did. A driver that asked the venue
|
|
269
|
+
* before appending would price a fill against a bar the strategy had not seen.
|
|
270
|
+
*/
|
|
271
|
+
function walk(
|
|
272
|
+
engine: Engine,
|
|
273
|
+
destination: Destination,
|
|
274
|
+
bars: readonly RecordedBar[],
|
|
275
|
+
covered: ReportWindow,
|
|
276
|
+
): Walked {
|
|
277
|
+
const fills: RecordedFill[] = [];
|
|
278
|
+
const frames: Delivered[] = [];
|
|
279
|
+
const diagnostics: RecordedDiagnostic[] = [];
|
|
280
|
+
const intents = new Map<number, OrderIntent>();
|
|
281
|
+
const refs = new Map<number, string>();
|
|
282
|
+
const sizes = new Map<number, number>();
|
|
283
|
+
|
|
284
|
+
let pending: readonly Delivered[] = [];
|
|
285
|
+
let seq = 0;
|
|
286
|
+
|
|
287
|
+
for (let index = 0; index < covered.total; index += 1) {
|
|
288
|
+
const bar = bars[index];
|
|
289
|
+
if (bar === undefined) continue;
|
|
290
|
+
|
|
291
|
+
for (const one of pending) {
|
|
292
|
+
const ref = one.frame.orderRef;
|
|
293
|
+
if (typeof ref === 'string') refs.set(one.frame.intentId, ref);
|
|
294
|
+
engine.deliver(one.frame);
|
|
295
|
+
frames.push(one);
|
|
296
|
+
}
|
|
297
|
+
pending = [];
|
|
298
|
+
|
|
299
|
+
const result = engine.append(
|
|
300
|
+
{ time: bar.time, open: bar.open, high: bar.high, low: bar.low, close: bar.close,
|
|
301
|
+
volume: bar.volume, oi: bar.oi },
|
|
302
|
+
{ isConfirmed: true, isRealtime: false },
|
|
303
|
+
covered.total,
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
for (const intent of intentsIn(result)) intents.set(intent.intentId, intent);
|
|
307
|
+
seq = settle(result, index, bar, { intents, refs, sizes }, fills, seq);
|
|
308
|
+
|
|
309
|
+
if (result.diagnostic !== undefined) {
|
|
310
|
+
diagnostics.push(diagnosticIn(result.diagnostic, index));
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
pending = destination.answers(index);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return { fills, frames, diagnostics };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** What the run is holding while it folds one bar's frames into fills. */
|
|
321
|
+
interface Books {
|
|
322
|
+
readonly intents: ReadonlyMap<number, OrderIntent>;
|
|
323
|
+
readonly refs: ReadonlyMap<number, string>;
|
|
324
|
+
readonly sizes: Map<number, number>;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The fills one bar's fold settled.
|
|
329
|
+
*
|
|
330
|
+
* **The position sizes either side of step 6 are kept here and not read back
|
|
331
|
+
* from the engine**, because the engine reports a leg's net and a fill settles
|
|
332
|
+
* against one position reference: the two are different numbers whenever a leg
|
|
333
|
+
* holds more than one position, which is every flip. They are folded in the
|
|
334
|
+
* order the frames were folded in, from the same deltas, so they are the same
|
|
335
|
+
* arithmetic the position book did rather than a second reading of its result.
|
|
336
|
+
* When `FrameOutcome` carries them, these two lines come from the engine and
|
|
337
|
+
* this fold keeps only the sequence number.
|
|
338
|
+
*
|
|
339
|
+
* A refused frame settles nothing and is not a fill. Today the fold reports the
|
|
340
|
+
* refusal as a word rather than a code, so nothing here can write it into the
|
|
341
|
+
* record as a diagnostic; that is OS7018 and OS7019, and both are deferred.
|
|
342
|
+
*/
|
|
343
|
+
function settle(
|
|
344
|
+
result: BarResult,
|
|
345
|
+
index: number,
|
|
346
|
+
bar: RecordedBar,
|
|
347
|
+
books: Books,
|
|
348
|
+
fills: RecordedFill[],
|
|
349
|
+
from: number,
|
|
350
|
+
): number {
|
|
351
|
+
let seq = from;
|
|
352
|
+
for (const outcome of result.frames) {
|
|
353
|
+
if (outcome.refused !== undefined) continue;
|
|
354
|
+
if (outcome.delta <= 0 || outcome.price === null) continue;
|
|
355
|
+
const intent = books.intents.get(outcome.intentId);
|
|
356
|
+
if (intent === undefined || intent.side === null) continue;
|
|
357
|
+
|
|
358
|
+
const before = books.sizes.get(intent.positionRef) ?? 0;
|
|
359
|
+
const after = before + (intent.side === 'sell' ? -outcome.delta : outcome.delta);
|
|
360
|
+
books.sizes.set(intent.positionRef, after);
|
|
361
|
+
seq += 1;
|
|
362
|
+
|
|
363
|
+
fills.push({
|
|
364
|
+
seq,
|
|
365
|
+
intentId: outcome.intentId,
|
|
366
|
+
orderRef: books.refs.get(outcome.intentId) ?? '',
|
|
367
|
+
tag: intent.tag,
|
|
368
|
+
positionRef: intent.positionRef,
|
|
369
|
+
side: intent.side,
|
|
370
|
+
units: outcome.delta,
|
|
371
|
+
price: outcome.price,
|
|
372
|
+
// The bar the fold happened at, which is the bar the position exists
|
|
373
|
+
// from. Where the fill itself traded is the bar before it or this bar's
|
|
374
|
+
// own open, and neither is a bar the strategy could have acted on it in.
|
|
375
|
+
barIndex: index,
|
|
376
|
+
barTime: bar.time,
|
|
377
|
+
refSizeBefore: before,
|
|
378
|
+
refSizeAfter: after,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
return seq;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Every intent one bar handed over, in the order the bar applied them. */
|
|
385
|
+
function intentsIn(result: BarResult): readonly OrderIntent[] {
|
|
386
|
+
const out: OrderIntent[] = [];
|
|
387
|
+
for (const effect of result.effects) for (const intent of effect.intents) out.push(intent);
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** The ledger at the end, copied out of the engine's own array. */
|
|
392
|
+
function ordersOf(
|
|
393
|
+
rows: readonly LedgerRow[],
|
|
394
|
+
intents: readonly OrderIntent[],
|
|
395
|
+
ordinals: ReadonlyMap<number, number>,
|
|
396
|
+
): readonly RecordedOrder[] {
|
|
397
|
+
return rows.map((row) => {
|
|
398
|
+
const intent = intents.find((one) => one.intentId === row.intentId);
|
|
399
|
+
return orderIn(row, ordinals.get(row.intentId) ?? 0, intent?.qtyType ?? '');
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The cost model the run is carried out under.
|
|
405
|
+
*
|
|
406
|
+
* The host's schedule where it supplied one, and otherwise the declaration's
|
|
407
|
+
* own commission as the one kind of schedule the money layer evaluates. Both
|
|
408
|
+
* cannot be stated at once: that is OS6023 and it is refused before this is
|
|
409
|
+
* asked.
|
|
410
|
+
*/
|
|
411
|
+
function scheduleFor(settings: BacktestSettings, declared: RunDeclaration): ChargeSchedule {
|
|
412
|
+
if (settings.costs !== null) return settings.costs;
|
|
413
|
+
return scheduleFromDeclaration(
|
|
414
|
+
declared.commission,
|
|
415
|
+
declared.commissionType,
|
|
416
|
+
declared.slippage,
|
|
417
|
+
settings.contract.currency,
|
|
418
|
+
settings.contract.digits,
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* The instrument record of `host-interface.md` 4.1 the engine is handed, and
|
|
424
|
+
* the record carries verbatim.
|
|
425
|
+
*
|
|
426
|
+
* Composed once, here, from the contract's six facts and the six stated beside
|
|
427
|
+
* it, so the engine and the record read one document and there is no second
|
|
428
|
+
* composition for the two to disagree by. A fact nobody stated is left out
|
|
429
|
+
* rather than written as undefined: the record travels as JSON, which drops an
|
|
430
|
+
* undefined member, and a document that changes shape in transit is not the
|
|
431
|
+
* document the engine read.
|
|
432
|
+
*/
|
|
433
|
+
function instrumentFor(contract: Contract, facts: InstrumentFacts): Instrument {
|
|
434
|
+
return {
|
|
435
|
+
...(contract.symbol === null ? {} : { symbol: contract.symbol }),
|
|
436
|
+
...(contract.exchange === null ? {} : { exchange: contract.exchange }),
|
|
437
|
+
...(facts.interval === undefined ? {} : { interval: facts.interval }),
|
|
438
|
+
...(facts.timezone === undefined ? {} : { timezone: facts.timezone }),
|
|
439
|
+
...(contract.tickSize === null ? {} : { tickSize: contract.tickSize }),
|
|
440
|
+
...(contract.lotSize === null ? {} : { lotSize: contract.lotSize }),
|
|
441
|
+
pointValue: contract.pointValue,
|
|
442
|
+
currency: contract.currency,
|
|
443
|
+
...(facts.instrumentType === undefined ? {} : { instrumentType: facts.instrumentType }),
|
|
444
|
+
...(facts.hasVolume === undefined ? {} : { hasVolume: facts.hasVolume }),
|
|
445
|
+
...(facts.hasOpenInterest === undefined ? {} : { hasOpenInterest: facts.hasOpenInterest }),
|
|
446
|
+
...(facts.session === undefined ? {} : { session: facts.session }),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* The host a backtest is: an instrument record, a clock and a destination.
|
|
452
|
+
*
|
|
453
|
+
* Duty 3 is not served. A backtest holds the chart's own bars and nothing else,
|
|
454
|
+
* so a program that reads another instrument declares a capability this host
|
|
455
|
+
* does not have and is refused at load, by name, rather than drawing a line
|
|
456
|
+
* with nothing in it.
|
|
457
|
+
*/
|
|
458
|
+
function hostFor(
|
|
459
|
+
instrument: Instrument,
|
|
460
|
+
now: number | null,
|
|
461
|
+
route: (effect: RoutedEffect, bar: number) => void,
|
|
462
|
+
): EngineHost {
|
|
463
|
+
return {
|
|
464
|
+
instrument,
|
|
465
|
+
...(now === null ? {} : { now }),
|
|
466
|
+
route,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The backtest: a program, some bars, and a record of what happened.
|
|
3
|
+
*
|
|
4
|
+
* **A run produces a record, and the record is the product.** Not a number
|
|
5
|
+
* printed at the end and not a chart: a document that carries the program, the
|
|
6
|
+
* bars, the settings the host chose, every frame the destination answered,
|
|
7
|
+
* every fill the engine folded, the ledger at the end and the report. A second
|
|
8
|
+
* engine is handed that document as a conformance case, and this engine is
|
|
9
|
+
* handed it again months later to prove that an upgrade did not change a result.
|
|
10
|
+
*
|
|
11
|
+
* **The cost model lives in this module and not in the engine.** `stdlib.md`
|
|
12
|
+
* 17.1 puts slippage and commission on the destination, and the engine folds the
|
|
13
|
+
* price it is told; a backtest is a destination, so this is where a price is
|
|
14
|
+
* worsened by a tick and a fill is charged. The engine never adjusts a price and
|
|
15
|
+
* this module never folds one.
|
|
16
|
+
*
|
|
17
|
+
* `backtest` drives one against a simulated destination and `backtestSupplied`
|
|
18
|
+
* drives one over frames somebody else supplied, which is what `conformance.md`
|
|
19
|
+
* section 3 asks of an engine running a case. `replay` folds a stored one's
|
|
20
|
+
* money again from its own fills, and `rerun` executes a stored one again and
|
|
21
|
+
* is held to producing the same bytes. `recordToJson` and `recordFromJson` are
|
|
22
|
+
* the document itself. `compareRuns` puts two records beside each other and
|
|
23
|
+
* says whether the gap between them clears the noise. `caseFilesFrom` turns a
|
|
24
|
+
* record into the files of a conformance case, returning text and writing
|
|
25
|
+
* nothing because core does no I/O. It needed three channels the record did not
|
|
26
|
+
* carry. `conformance.md` section 2 requires a case to hold `script.os`, the
|
|
27
|
+
* source text, and a record carried only the source's hash, its line count and
|
|
28
|
+
* its file name: record version 2 carries the text, checked against that hash.
|
|
29
|
+
* The same section says `instrument.json` is the record of `host-interface.md`
|
|
30
|
+
* 4.1, and a record carried the money layer's contract, which holds six of its
|
|
31
|
+
* twelve facts: record version 3 carries the record the engine was handed,
|
|
32
|
+
* whole. And a case is handed frames, which `stdlib.md` 17.7 folds one ledger
|
|
33
|
+
* field from an instant of: a record carried a frame without the instant it
|
|
34
|
+
* arrived at, so a case projected from it asserted a field its own input could
|
|
35
|
+
* not reproduce. Record version 4 carries a frame's time.
|
|
36
|
+
*/
|
|
37
|
+
export { backtest, backtestSupplied } from './drive.js';
|
|
38
|
+
export type { BacktestResult, DriveOptions, InstrumentFacts } from './drive.js';
|
|
39
|
+
export { declarationOf } from './declaration.js';
|
|
40
|
+
export type { RunDeclaration } from './declaration.js';
|
|
41
|
+
export { marksFor, windowFor } from './range.js';
|
|
42
|
+
export type { ReportWindow, WindowResult } from './range.js';
|
|
43
|
+
export { replay, rerun } from './replay.js';
|
|
44
|
+
export type { ReplayResult } from './replay.js';
|
|
45
|
+
export { testResting } from './resting.js';
|
|
46
|
+
export type { RestOutcome, RestingOrder } from './resting.js';
|
|
47
|
+
export { Simulator } from './simulate.js';
|
|
48
|
+
export type { SimulatorOptions, VenueAct, VenueDoes, VenuePolicy } from './simulate.js';
|
|
49
|
+
export { DEFAULT_FILL, EXACT, WHOLE_RANGE, checkSettings, settingsFor } from './settings.js';
|
|
50
|
+
export { caseFilesFrom } from './case.js';
|
|
51
|
+
export type { CaseFiles, CaseIdentity, CaseResult } from './case.js';
|
|
52
|
+
export { RECORD_VERSION, barsHash, recordFromJson, recordOf, recordToJson, runBytes } from './record.js';
|
|
53
|
+
export type { RecordParts } from './record.js';
|
|
54
|
+
export type { BacktestSettings, DateRange, FillPolicy, Tolerance } from './settings.js';
|
|
55
|
+
export type {
|
|
56
|
+
BarsInRecord,
|
|
57
|
+
RecordedBar,
|
|
58
|
+
RecordedDiagnostic,
|
|
59
|
+
RecordedFrame,
|
|
60
|
+
RecordedOrder,
|
|
61
|
+
RunRecord,
|
|
62
|
+
} from './record.js';
|
|
63
|
+
export { compareRuns } from './compare.js';
|
|
64
|
+
export type { RunComparison } from './compare.js';
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which bars a report is about, and which merely got it there.
|
|
3
|
+
*
|
|
4
|
+
* **Every bar supplied executes, and only some of them are reported.** That is
|
|
5
|
+
* the whole of this file and it is the fact a driver written without it gets
|
|
6
|
+
* wrong. A study of the last year needs the indicators warm before the year
|
|
7
|
+
* starts, so bars before the window are handed to the engine exactly as the
|
|
8
|
+
* reported ones are: they execute, their orders are real, and a position opened
|
|
9
|
+
* on one of them is carried into the window rather than appearing from nowhere
|
|
10
|
+
* at its first bar. What they are not is reported: no equity point, and no
|
|
11
|
+
* calendar month of their own.
|
|
12
|
+
*
|
|
13
|
+
* **A window holding no bar at all is refused**, OS6020, rather than reported
|
|
14
|
+
* as a flat curve. A flat curve is what a strategy that did nothing also
|
|
15
|
+
* produces, and only this can tell the two apart.
|
|
16
|
+
*
|
|
17
|
+
* **Both bounds are inclusive and are compared against the times of the bars
|
|
18
|
+
* supplied**, never against a calendar. A window that falls inside a gap in the
|
|
19
|
+
* data is empty however wide it looks, which is the truthful answer: there are
|
|
20
|
+
* no bars in it.
|
|
21
|
+
*
|
|
22
|
+
* The other fact a driver gets wrong is not a window at all and is written here
|
|
23
|
+
* because this is where a driver looks: the bar count handed to the engine is
|
|
24
|
+
* the total it will be given, not the number given so far. An engine told that
|
|
25
|
+
* each bar is the only bar reports `bar.isLast` true on every one of them, and
|
|
26
|
+
* a strategy that flattens on the last bar flattens on all of them.
|
|
27
|
+
*/
|
|
28
|
+
import type { BarMark } from '../accounting/index.js';
|
|
29
|
+
import { diagnosticFor } from '../diagnostics/index.js';
|
|
30
|
+
import type { Diagnostic } from '../diagnostics/index.js';
|
|
31
|
+
import type { DateRange } from './settings.js';
|
|
32
|
+
import type { RecordedBar } from './record.js';
|
|
33
|
+
|
|
34
|
+
/** Which of the bars supplied the report is about. */
|
|
35
|
+
export interface ReportWindow {
|
|
36
|
+
/** The first reported bar, by index into the bars supplied. */
|
|
37
|
+
readonly first: number;
|
|
38
|
+
/** The last reported bar, inclusive. */
|
|
39
|
+
readonly last: number;
|
|
40
|
+
/** How many bars run before the first reported one. */
|
|
41
|
+
readonly warmup: number;
|
|
42
|
+
/** Every bar supplied, which is what the engine is told a run holds. */
|
|
43
|
+
readonly total: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type WindowResult =
|
|
47
|
+
| { readonly ok: true; readonly covered: ReportWindow }
|
|
48
|
+
| { readonly ok: false; readonly diagnostic: Diagnostic };
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The window a range names over the bars supplied.
|
|
52
|
+
*
|
|
53
|
+
* A bar with no time is inside an unstated bound and outside a stated one,
|
|
54
|
+
* because a bound is a comparison and there is nothing to compare it with. A
|
|
55
|
+
* run over bars none of which carry a time is therefore reported whole when the
|
|
56
|
+
* host named no range, which is what a host handing over a bare series means,
|
|
57
|
+
* and refused the moment it names one, which is what a host asking for a window
|
|
58
|
+
* it cannot be given deserves to be told.
|
|
59
|
+
*/
|
|
60
|
+
export function windowFor(bars: readonly RecordedBar[], range: DateRange): WindowResult {
|
|
61
|
+
let first = -1;
|
|
62
|
+
let last = -1;
|
|
63
|
+
for (let index = 0; index < bars.length; index += 1) {
|
|
64
|
+
const bar = bars[index];
|
|
65
|
+
if (bar === undefined || !inside(bar.time, range)) continue;
|
|
66
|
+
if (first < 0) first = index;
|
|
67
|
+
last = index;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (first < 0) {
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
diagnostic: diagnosticFor('OS6020', NO_POSITION, {
|
|
74
|
+
from: bound(range.from, bars[0]?.time ?? null, 'the first bar supplied'),
|
|
75
|
+
to: bound(range.to, bars[bars.length - 1]?.time ?? null, 'the last bar supplied'),
|
|
76
|
+
count: bars.length,
|
|
77
|
+
}),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { ok: true, covered: { first, last, warmup: first, total: bars.length } };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Whether one bar's own time is inside the window, both bounds inclusive. */
|
|
85
|
+
export function inside(time: number | null, range: DateRange): boolean {
|
|
86
|
+
if (range.from === null && range.to === null) return true;
|
|
87
|
+
if (time === null) return false;
|
|
88
|
+
if (range.from !== null && time < range.from) return false;
|
|
89
|
+
if (range.to !== null && time > range.to) return false;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether a bar is reported, which is the question the marks are built from.
|
|
95
|
+
*
|
|
96
|
+
* Asked of an index rather than of a time, because the window is decided once
|
|
97
|
+
* and a bar asked twice has to be answered the same way both times.
|
|
98
|
+
*/
|
|
99
|
+
export function inReport(index: number, covered: ReportWindow): boolean {
|
|
100
|
+
return index >= covered.first && index <= covered.last;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The bars as the report marks against them: the close, and whether it counts.
|
|
105
|
+
*
|
|
106
|
+
* One mark per bar supplied, warmup included, because the money layer sweeps
|
|
107
|
+
* the warmup rather than being handed a shortened list: a trade opened before
|
|
108
|
+
* the window is already in the fold at the window's first point, with its
|
|
109
|
+
* charges paid and its position marked. A mark says which of the two it is and
|
|
110
|
+
* the fold decides what to do about it.
|
|
111
|
+
*
|
|
112
|
+
* Built here rather than in the driver because a replay marks the same bars the
|
|
113
|
+
* same way, and two places deciding what a warmup bar is would be two reports
|
|
114
|
+
* of one run.
|
|
115
|
+
*/
|
|
116
|
+
export function marksFor(bars: readonly RecordedBar[], covered: ReportWindow): readonly BarMark[] {
|
|
117
|
+
return bars.map((bar, index) => ({
|
|
118
|
+
barIndex: index,
|
|
119
|
+
time: bar.time,
|
|
120
|
+
close: bar.close,
|
|
121
|
+
inReport: inReport(index, covered),
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What a refusal calls a bound the host left for the bars to decide. */
|
|
126
|
+
function bound(stated: number | null, fallback: number | null, word: string): number | string {
|
|
127
|
+
if (stated !== null) return stated;
|
|
128
|
+
return fallback === null ? word : fallback;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Where a refusal about the window points.
|
|
133
|
+
*
|
|
134
|
+
* Nowhere in the script, for the same reason every other run setting points
|
|
135
|
+
* nowhere: the window is the host's choice and the script did not make it.
|
|
136
|
+
*/
|
|
137
|
+
const NO_POSITION: Diagnostic['span'] = { offset: 0, length: 0, line: 0, column: 0 };
|