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,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A run record, turned into the files a conformance case is made of.
|
|
3
|
+
*
|
|
4
|
+
* **The suite is harvested rather than written.** A case invented by hand tests
|
|
5
|
+
* what somebody imagined a run does; a case taken from a run tests what a run
|
|
6
|
+
* actually did. `conformance.md` section 2 says a case is one directory of named
|
|
7
|
+
* files, and a record already holds every one of them: the script it ran, the
|
|
8
|
+
* bars it ran over, the settings it ran with, and what came back. So this is a
|
|
9
|
+
* projection and not a computation. Nothing here folds money, re-reads a report
|
|
10
|
+
* or decides anything a run did not already decide.
|
|
11
|
+
*
|
|
12
|
+
* **Text out, and no I/O.** Core writes no files, so this returns the bytes and
|
|
13
|
+
* the caller puts them where it likes. That also makes it testable without a
|
|
14
|
+
* disk and usable from a browser, which is where most runs happen.
|
|
15
|
+
*
|
|
16
|
+
* **What it refuses, it refuses loudly.** A case with a missing file is worse
|
|
17
|
+
* than no case: it fails on somebody else's engine and the blame lands on them.
|
|
18
|
+
* So a record that cannot make a whole case does not make a partial one, and a
|
|
19
|
+
* record that cannot make a faithful file does not guess at one: the script it
|
|
20
|
+
* has no text for and the instrument fact its host never stated are both
|
|
21
|
+
* refusals, never a hole and never a default.
|
|
22
|
+
*
|
|
23
|
+
* **Every setting of the run has a place in the case, and the table below says
|
|
24
|
+
* which.** A run once harvested to a case that said nothing about the digit
|
|
25
|
+
* count its money was rounded to, the charge schedule its host supplied or the
|
|
26
|
+
* window its report was about, so a second engine ran under other values and
|
|
27
|
+
* took the blame. `CARRIED` names the file each field of the settings is
|
|
28
|
+
* carried in, the type refuses to compile when the settings gain a field with
|
|
29
|
+
* no row, and a record whose settings hold a field the table does not know is
|
|
30
|
+
* refused by name rather than written into a case that ran under something it
|
|
31
|
+
* does not state.
|
|
32
|
+
*/
|
|
33
|
+
import { diagnosticFor } from '../diagnostics/index.js';
|
|
34
|
+
import type { Diagnostic } from '../diagnostics/index.js';
|
|
35
|
+
import { canonicalNumber, canonicalise } from '../emit/index.js';
|
|
36
|
+
import type { Instrument } from '../engine/index.js';
|
|
37
|
+
import type { RecordedBar, RecordedFrame, RunRecord } from './record.js';
|
|
38
|
+
import type { BacktestSettings, Tolerance } from './settings.js';
|
|
39
|
+
|
|
40
|
+
/** The files of one case, keyed by the name `conformance.md` section 2 gives them. */
|
|
41
|
+
export type CaseFiles = Readonly<Record<string, string>>;
|
|
42
|
+
|
|
43
|
+
/** Why a record could not become a case. */
|
|
44
|
+
export interface CaseRefusal {
|
|
45
|
+
readonly ok: false;
|
|
46
|
+
readonly reason: string;
|
|
47
|
+
/**
|
|
48
|
+
* The catalogue code the refusal is filed under, or null.
|
|
49
|
+
*
|
|
50
|
+
* A refusal about a setting of the run is the catalogue's, OS6021, because
|
|
51
|
+
* it is the same refusal the run makes of a setting it cannot be carried out
|
|
52
|
+
* under. The others here are about what the record holds, a text it never
|
|
53
|
+
* carried, a bar list it only points at, a fact its host never stated, and
|
|
54
|
+
* no catalogue entry is about those.
|
|
55
|
+
*/
|
|
56
|
+
readonly code: string | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CaseWritten {
|
|
60
|
+
readonly ok: true;
|
|
61
|
+
readonly files: CaseFiles;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CaseResult = CaseWritten | CaseRefusal;
|
|
65
|
+
|
|
66
|
+
/** What the caller has to say, because a record cannot know it. */
|
|
67
|
+
export interface CaseIdentity {
|
|
68
|
+
/**
|
|
69
|
+
* The directory path under the suite root, which is also the case id.
|
|
70
|
+
*
|
|
71
|
+
* The caller's because a record does not know where it will live, and
|
|
72
|
+
* `case.json` repeats it on purpose so a directory that is moved without its
|
|
73
|
+
* id being changed is caught by the suite rather than by a confused reader.
|
|
74
|
+
*/
|
|
75
|
+
readonly id: string;
|
|
76
|
+
/** One sentence. It is the failure message a runner prints. */
|
|
77
|
+
readonly description: string;
|
|
78
|
+
/** Section 7's categories. Defaults to the one a strategy run belongs to. */
|
|
79
|
+
readonly category?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The channels a strategy run can be held to.
|
|
84
|
+
*
|
|
85
|
+
* Only what the record carries folded, and only what another engine could
|
|
86
|
+
* produce independently. A case asserts the channels it names and no others, so
|
|
87
|
+
* a change to drawing output cannot break a case about money.
|
|
88
|
+
*/
|
|
89
|
+
const STRATEGY_ASSERTS = ['diagnostics', 'orders', 'trades', 'performance'] as const;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* `conformance.md` section 6: the loosest bounds a conformance case may declare.
|
|
93
|
+
*
|
|
94
|
+
* Core reads no page, so the two figures are written here, and
|
|
95
|
+
* `tests/backtest/case-settings.test.ts` reads them out of section 6 and holds
|
|
96
|
+
* these to the page, which is the arrangement `stdlib.md` section 20's figures
|
|
97
|
+
* are under. Past either bound a run may still be a useful comparison; what it
|
|
98
|
+
* is not is a case, so the projection makes no file of it.
|
|
99
|
+
*/
|
|
100
|
+
export const TOLERANCE_CAP: { readonly rel: number; readonly abs: number } = {
|
|
101
|
+
rel: 1e-9,
|
|
102
|
+
abs: 1e-12,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Where each setting of a run is carried in a case, sections 2 and 3.
|
|
107
|
+
*
|
|
108
|
+
* Every field of the settings has a row and the type makes a field without one
|
|
109
|
+
* a compile error, so a setting the record gains cannot be left out of a case
|
|
110
|
+
* by forgetting. `fill` is the simulated destination's policy, and what it
|
|
111
|
+
* decided is the frames, which are input: an engine handed `frames.csv` folds
|
|
112
|
+
* them and fills nothing itself.
|
|
113
|
+
*/
|
|
114
|
+
const CARRIED: Readonly<Record<keyof BacktestSettings, string>> = {
|
|
115
|
+
contract: 'instrument.json, and backtest.json for the digit count',
|
|
116
|
+
costs: 'backtest.json',
|
|
117
|
+
range: 'backtest.json',
|
|
118
|
+
inputs: 'settings.json',
|
|
119
|
+
now: 'case.json',
|
|
120
|
+
tolerance: 'case.json',
|
|
121
|
+
fill: 'frames.csv, as the frames it decided',
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Where a refusal about a setting points: nowhere in the script.
|
|
126
|
+
*
|
|
127
|
+
* A run setting is what the host stated before the first bar, and a caret
|
|
128
|
+
* under a line of the strategy would blame the one party that did not choose
|
|
129
|
+
* it. The run's own settings check states the same position for the same reason.
|
|
130
|
+
*/
|
|
131
|
+
const NO_POSITION: Diagnostic['span'] = { offset: 0, length: 0, line: 0, column: 0 };
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The files for one case, or the reason there are none.
|
|
135
|
+
*
|
|
136
|
+
* `now` is written only when the run pinned one. A case that names it when the
|
|
137
|
+
* script never asked pins a clock the run did not depend on, and the next
|
|
138
|
+
* reader has to work out whether it mattered.
|
|
139
|
+
*/
|
|
140
|
+
export function caseFilesFrom(record: RunRecord, identity: CaseIdentity): CaseResult {
|
|
141
|
+
if (record.sourceText === null) {
|
|
142
|
+
return refused(
|
|
143
|
+
'the record carries no source text, so the case would have no script.os: ' +
|
|
144
|
+
'record it with sourceText, or re-run the script to record one that has it',
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (record.bars.form !== 'inline') {
|
|
148
|
+
return refused(
|
|
149
|
+
'the record points at its bars instead of holding them, and a case holds every ' +
|
|
150
|
+
'byte of its own input: record it with form "inline"',
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (identity.id.trim() === '') return refused('a case needs an id');
|
|
154
|
+
if (identity.description.trim() === '') {
|
|
155
|
+
return refused('a case needs a one-sentence description, which is its failure message');
|
|
156
|
+
}
|
|
157
|
+
const instrument = instrumentOf(record);
|
|
158
|
+
if (!instrument.ok) return instrument;
|
|
159
|
+
const settings = settingsRefusal(record.settings);
|
|
160
|
+
if (settings !== null) return settings;
|
|
161
|
+
|
|
162
|
+
const files: Record<string, string> = {
|
|
163
|
+
'case.json': json({
|
|
164
|
+
id: identity.id,
|
|
165
|
+
category: identity.category ?? 'strategy',
|
|
166
|
+
profile: 'strategy',
|
|
167
|
+
languageVersion: Number(record.languageVersion),
|
|
168
|
+
description: identity.description,
|
|
169
|
+
asserts: [...STRATEGY_ASSERTS],
|
|
170
|
+
...(record.settings.now === null ? {} : { now: record.settings.now }),
|
|
171
|
+
tolerance: record.settings.tolerance,
|
|
172
|
+
}),
|
|
173
|
+
'script.os': endsWithNewline(record.sourceText),
|
|
174
|
+
'bars.csv': barsCsv(record.bars.rows),
|
|
175
|
+
// `conformance.md` section 4: performance is a list of one flat object
|
|
176
|
+
// holding the summary statistics and nothing nested. The trades are their
|
|
177
|
+
// own channel and are not repeated inside it, because a figure stated
|
|
178
|
+
// twice in one case is a figure that can disagree with itself. The equity
|
|
179
|
+
// curve, the monthly table and the markers are not written at all: the
|
|
180
|
+
// first two are not conformance channels, being derived from fills and
|
|
181
|
+
// closes the case already fixes, and a marker is a chart output the
|
|
182
|
+
// `markers` channel owns, which a case about money does not assert.
|
|
183
|
+
'expected.json': json({
|
|
184
|
+
diagnostics: record.diagnostics,
|
|
185
|
+
orders: record.orders,
|
|
186
|
+
trades: record.report.trades,
|
|
187
|
+
performance: [record.report.summary],
|
|
188
|
+
}),
|
|
189
|
+
// Section 2: the record of `host-interface.md` 4.1, which is what the
|
|
190
|
+
// engine was handed and not the money layer's contract. The suite's
|
|
191
|
+
// defaults for an absent file are boring on purpose and are not this run's
|
|
192
|
+
// instrument, so the file is always written.
|
|
193
|
+
'instrument.json': json(instrument.value),
|
|
194
|
+
// Section 3: what the report was folded under and the script never states.
|
|
195
|
+
// Always written, because every run rounds money to some digit count, and a
|
|
196
|
+
// strategy case that left it out would be run under whatever count a runner
|
|
197
|
+
// assumed. `costs` is null for the declaration's own schedule and `range`
|
|
198
|
+
// carries null for a bound nobody stated, so the file says what the run ran
|
|
199
|
+
// under in every case rather than leaving a default to a runner.
|
|
200
|
+
'backtest.json': json({
|
|
201
|
+
digits: record.settings.contract.digits,
|
|
202
|
+
costs: record.settings.costs,
|
|
203
|
+
range: record.settings.range,
|
|
204
|
+
}),
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Without this the case is unpassable, on every engine including the one that
|
|
208
|
+
// wrote it. `conformance.md` section 3 ends "a case with no `frames.csv` is
|
|
209
|
+
// handed no frames at all", and what `expected.json` asserts through the
|
|
210
|
+
// orders channel is what came of the frames: a status, a cumulative quantity,
|
|
211
|
+
// an average fill price. An engine handed none of them folds nothing and
|
|
212
|
+
// disagrees with every row, and the failure reads as a defect in that engine.
|
|
213
|
+
//
|
|
214
|
+
// Written only when the run had frames, because an empty file and an absent
|
|
215
|
+
// one mean the same thing here and the absent one says it in fewer bytes.
|
|
216
|
+
if (record.frames.length > 0) files['frames.csv'] = framesCsv(record.frames);
|
|
217
|
+
|
|
218
|
+
// Written only when the run had inputs to write. An empty settings.json says
|
|
219
|
+
// "these are the values" about nothing, and section 2 reads an absent one as
|
|
220
|
+
// every input taking its declared default, which is what actually happened.
|
|
221
|
+
if (Object.keys(record.settings.inputs).length > 0) {
|
|
222
|
+
files['settings.json'] = json(record.settings.inputs);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { ok: true, files };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** A refusal about what the record holds, which no catalogue entry is about. */
|
|
229
|
+
function refused(reason: string): CaseRefusal {
|
|
230
|
+
return { ok: false, reason, code: null };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The instrument record a case can state faithfully, or why there is none.
|
|
235
|
+
*
|
|
236
|
+
* Two refusals, and both are the same refusal: the file `conformance.md`
|
|
237
|
+
* section 2 names is the record of `host-interface.md` 4.1, and a record that
|
|
238
|
+
* cannot produce that record cannot produce the file.
|
|
239
|
+
*
|
|
240
|
+
* A record written before version 3 carries none, because the facts beside
|
|
241
|
+
* the contract were handed to the engine and written down nowhere. And 4.1
|
|
242
|
+
* requires one fact of every host, `hasVolume`, which is the one the engine
|
|
243
|
+
* does not refuse a run without: a run whose host never stated it ran with the
|
|
244
|
+
* flag absent, so a file stating it would hand a second engine a different
|
|
245
|
+
* study than the one the expected output came from, and a file omitting it is
|
|
246
|
+
* not a 4.1 record. The other rules that page states about a record, a session
|
|
247
|
+
* with no timezone to read it in, are refused at load, so a run that happened
|
|
248
|
+
* cannot carry one.
|
|
249
|
+
*/
|
|
250
|
+
function instrumentOf(
|
|
251
|
+
record: RunRecord,
|
|
252
|
+
): { readonly ok: true; readonly value: Instrument } | CaseRefusal {
|
|
253
|
+
if (record.instrument === null) {
|
|
254
|
+
return refused(
|
|
255
|
+
'the record carries no instrument record, so the case would have no instrument.json: ' +
|
|
256
|
+
'it was written before record version 3, and re-running the script records one',
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (typeof record.instrument.hasVolume !== 'boolean') {
|
|
260
|
+
return refused(
|
|
261
|
+
'the run was handed no hasVolume, which host-interface.md 4.1 requires of every ' +
|
|
262
|
+
'host, so instrument.json cannot be the record that page defines: run the script ' +
|
|
263
|
+
'with the instrument facts stated',
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
return { ok: true, value: record.instrument };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Whether every setting the run was carried out under has a place in the case,
|
|
271
|
+
* and whether its tolerance is one the suite accepts.
|
|
272
|
+
*
|
|
273
|
+
* The first question is asked of the record rather than of the type, because a
|
|
274
|
+
* record read back from JSON is whatever was written: a field this projection
|
|
275
|
+
* has no file for is refused with its name, never passed over into a case that
|
|
276
|
+
* then ran under a value it does not state.
|
|
277
|
+
*/
|
|
278
|
+
function settingsRefusal(settings: BacktestSettings): CaseRefusal | null {
|
|
279
|
+
for (const key of Object.keys(settings)) {
|
|
280
|
+
if (key in CARRIED) continue;
|
|
281
|
+
return refused(
|
|
282
|
+
`the record's settings carry ${key}, which no file of conformance.md section 2 has a ` +
|
|
283
|
+
'place for, so a case written from it would run under a setting it does not state: ' +
|
|
284
|
+
'give the setting a file on that page and a row in this projection first',
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
return toleranceRefusal(settings.tolerance);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* A tolerance past the cap is a comparison somebody may find useful and is not
|
|
292
|
+
* a case, `conformance.md` section 6.
|
|
293
|
+
*
|
|
294
|
+
* Refused here, where the case is written, and not only where one is read,
|
|
295
|
+
* because a directory the suite will not accept fails every runner it meets
|
|
296
|
+
* and the blame lands on the engine under test. The run's own settings check
|
|
297
|
+
* refuses a bound with no reason and a bound below zero before a record
|
|
298
|
+
* exists; the cap is the one rule about a tolerance that is the suite's rather
|
|
299
|
+
* than the run's, so it is the one asked here.
|
|
300
|
+
*/
|
|
301
|
+
function toleranceRefusal(tolerance: Tolerance): CaseRefusal | null {
|
|
302
|
+
const { abs, rel } = tolerance;
|
|
303
|
+
if (!Number.isFinite(abs) || !Number.isFinite(rel)) {
|
|
304
|
+
return settingRefusal('a bound is not a finite number');
|
|
305
|
+
}
|
|
306
|
+
if (abs <= TOLERANCE_CAP.abs && rel <= TOLERANCE_CAP.rel) return null;
|
|
307
|
+
return settingRefusal(
|
|
308
|
+
`a bound of ${canonicalNumber(abs)} absolute and ${canonicalNumber(rel)} relative is ` +
|
|
309
|
+
'past the cap conformance.md section 6 puts on a conformance case, ' +
|
|
310
|
+
`${canonicalNumber(TOLERANCE_CAP.abs)} absolute and ${canonicalNumber(TOLERANCE_CAP.rel)} ` +
|
|
311
|
+
'relative, so this run is a comparison and not a case',
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** A refusal about the comparison tolerance, filed under the run's own code for a setting. */
|
|
316
|
+
function settingRefusal(problem: string): CaseRefusal {
|
|
317
|
+
const diagnostic = diagnosticFor('OS6021', NO_POSITION, {
|
|
318
|
+
setting: 'The comparison tolerance',
|
|
319
|
+
problem,
|
|
320
|
+
});
|
|
321
|
+
return { ok: false, reason: diagnostic.message, code: diagnostic.code };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Through the canonical writer, so a case's bytes are the record's bytes.
|
|
326
|
+
*
|
|
327
|
+
* There is one canonical writer in this repository and this is not a second
|
|
328
|
+
* one: key order and number form are fixed for everybody, which is what lets a
|
|
329
|
+
* case be compared as text at all.
|
|
330
|
+
*/
|
|
331
|
+
function json(value: unknown): string {
|
|
332
|
+
return `${canonicalise(value)}\n`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** `conformance.md` section 3: header, one row per bar, oldest first, `none` for absent. */
|
|
336
|
+
function barsCsv(rows: readonly RecordedBar[]): string {
|
|
337
|
+
const lines = ['time,open,high,low,close,volume'];
|
|
338
|
+
for (const bar of rows) {
|
|
339
|
+
lines.push(
|
|
340
|
+
[bar.time, bar.open, bar.high, bar.low, bar.close, bar.volume].map(cell).join(','),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
return `${lines.join('\n')}\n`;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* `conformance.md` section 3: the fields of a frame, in the order that page names.
|
|
348
|
+
*
|
|
349
|
+
* A projection and not a translation. The record already holds an intent as an
|
|
350
|
+
* ordinal rather than as this engine's own id, for the reason the same section
|
|
351
|
+
* gives: a case cannot know the id another engine minted and must not depend on
|
|
352
|
+
* its spelling.
|
|
353
|
+
*
|
|
354
|
+
* The instant is written on every row, `none` where the destination stated
|
|
355
|
+
* none, through the same cell writer as an absent price. A case whose frames
|
|
356
|
+
* carried instants and whose file did not would assert an `updatedAt` that its
|
|
357
|
+
* own input cannot reproduce, which is what the column closes.
|
|
358
|
+
*/
|
|
359
|
+
function framesCsv(frames: readonly RecordedFrame[]): string {
|
|
360
|
+
const lines = ['afterBar,intent,status,filledQty,avgFillPrice,orderRef,text,time'];
|
|
361
|
+
for (const frame of frames) {
|
|
362
|
+
lines.push(
|
|
363
|
+
[
|
|
364
|
+
canonicalNumber(frame.afterBar),
|
|
365
|
+
canonicalNumber(frame.intent),
|
|
366
|
+
frame.status,
|
|
367
|
+
canonicalNumber(frame.filledQty),
|
|
368
|
+
cell(frame.avgFillPrice),
|
|
369
|
+
frame.orderRef ?? '',
|
|
370
|
+
frame.text ?? '',
|
|
371
|
+
cell(frame.time),
|
|
372
|
+
].join(','),
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
return `${lines.join('\n')}\n`;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* One cell, through the one number writer.
|
|
380
|
+
*
|
|
381
|
+
* An absent field is `none` rather than empty, because an empty cell between two
|
|
382
|
+
* commas is indistinguishable from a file somebody's editor trimmed, and a
|
|
383
|
+
* language whose central idea is the absent value cannot be vague about it. A
|
|
384
|
+
* case is compared as text, so the number is written by the rule of
|
|
385
|
+
* `language.md` 5.5 and by the same function that writes every other number in
|
|
386
|
+
* the repository, never by a second spelling that agrees until it does not.
|
|
387
|
+
*/
|
|
388
|
+
function cell(value: number | null): string {
|
|
389
|
+
return value === null || !Number.isFinite(value) ? 'none' : canonicalNumber(value);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** A text file ends with a newline, so appending to it never joins two lines. */
|
|
393
|
+
function endsWithNewline(text: string): string {
|
|
394
|
+
return text.endsWith('\n') ? text : `${text}\n`;
|
|
395
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two runs, and whether the difference between them is a result.
|
|
3
|
+
*
|
|
4
|
+
* **Comparable first.** Two runs over different bars or a different contract are
|
|
5
|
+
* two studies, not a comparison, and a table of deltas between them is a way of
|
|
6
|
+
* being wrong with a decimal point. So the comparison says whether the pair is
|
|
7
|
+
* comparable before it says anything about the money, and names what differs.
|
|
8
|
+
*
|
|
9
|
+
* **And then separation, which is the figure the difference has to clear.** The
|
|
10
|
+
* gap between the two expectancies over the combined standard error is what
|
|
11
|
+
* turns "this one made more" into "this one made more than the noise". It is
|
|
12
|
+
* analytic: no resampling and no random number generator, so two runs of this
|
|
13
|
+
* comparison over the same pair give the same number for ever, and a decision
|
|
14
|
+
* taken on it is a decision anybody can reproduce.
|
|
15
|
+
*/
|
|
16
|
+
import type { Summary } from '../accounting/index.js';
|
|
17
|
+
import { canonicalise } from '../emit/index.js';
|
|
18
|
+
import type { RunRecord } from './record.js';
|
|
19
|
+
|
|
20
|
+
/** What two runs came to, beside each other. */
|
|
21
|
+
export interface RunComparison {
|
|
22
|
+
/** False when the bars hash or the contract differ. */
|
|
23
|
+
readonly comparable: boolean;
|
|
24
|
+
readonly differences: readonly {
|
|
25
|
+
readonly what: 'bars' | 'contract' | 'costs' | 'fill' | 'inputs' | 'program' | 'range';
|
|
26
|
+
readonly detail: string;
|
|
27
|
+
}[];
|
|
28
|
+
readonly deltas: readonly {
|
|
29
|
+
/** A field of Summary. */
|
|
30
|
+
readonly name: string;
|
|
31
|
+
readonly before: number;
|
|
32
|
+
readonly after: number;
|
|
33
|
+
readonly delta: number;
|
|
34
|
+
}[];
|
|
35
|
+
/** (expectancyB - expectancyA) / sqrt(seA^2 + seB^2). */
|
|
36
|
+
readonly separation: number | null;
|
|
37
|
+
/** Trades opening on the same bar with the same side in both. */
|
|
38
|
+
readonly sharedTrades: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One named difference between two runs' inputs. */
|
|
42
|
+
type Difference = RunComparison['differences'][number];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A Summary field worth a delta, in the order a comparison reports them.
|
|
46
|
+
*
|
|
47
|
+
* Written out rather than walked off the object, for two reasons. A key walk
|
|
48
|
+
* would report a delta for `currency`, which is a string, and for `capital`,
|
|
49
|
+
* which is an input rather than a result. And the order a comparison prints its
|
|
50
|
+
* rows in would then be the order a literal happens to be written in, which is
|
|
51
|
+
* a thing somebody reformats without knowing they changed an output.
|
|
52
|
+
*/
|
|
53
|
+
const COMPARED: readonly string[] = [
|
|
54
|
+
'netProfit',
|
|
55
|
+
'returnPercent',
|
|
56
|
+
'grossProfit',
|
|
57
|
+
'grossLoss',
|
|
58
|
+
'charges',
|
|
59
|
+
'tradeCount',
|
|
60
|
+
'openTradeCount',
|
|
61
|
+
'wins',
|
|
62
|
+
'losses',
|
|
63
|
+
'scratches',
|
|
64
|
+
'winRate',
|
|
65
|
+
'averageWin',
|
|
66
|
+
'averageLoss',
|
|
67
|
+
'expectancy',
|
|
68
|
+
'expectancyStandardError',
|
|
69
|
+
'profitFactor',
|
|
70
|
+
'maxDrawdown',
|
|
71
|
+
'maxDrawdownPercent',
|
|
72
|
+
'longestDrawdownBars',
|
|
73
|
+
'maxRunUp',
|
|
74
|
+
'maxRunUpPercent',
|
|
75
|
+
'averageBarsHeld',
|
|
76
|
+
'barsInMarket',
|
|
77
|
+
'barCount',
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Two runs, side by side, and whether the gap between them is a result.
|
|
82
|
+
*
|
|
83
|
+
* **Comparable is about the inputs, not about the outputs.** Two runs over
|
|
84
|
+
* different bars, or on different contracts, are two studies: the money in one
|
|
85
|
+
* is not the money in the other, and subtracting them is arithmetic with no
|
|
86
|
+
* meaning behind it. A different program over the same bars is the opposite of
|
|
87
|
+
* that. It is the comparison somebody actually wants, so a differing program is
|
|
88
|
+
* reported as a difference and does not make the pair incomparable.
|
|
89
|
+
*
|
|
90
|
+
* **Every difference is named even when the pair is comparable**, because the
|
|
91
|
+
* reason a run improved is as often a setting somebody forgot they had changed
|
|
92
|
+
* as it is the change they meant to test. A comparison reporting only the money
|
|
93
|
+
* would let that through, and it would read as a result.
|
|
94
|
+
*/
|
|
95
|
+
export function compareRuns(before: RunRecord, after: RunRecord): RunComparison {
|
|
96
|
+
const differences = differencesBetween(before, after);
|
|
97
|
+
const blocking = differences.some((one) => one.what === 'bars' || one.what === 'contract');
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
comparable: !blocking,
|
|
101
|
+
differences,
|
|
102
|
+
deltas: deltasBetween(before.report.summary, after.report.summary),
|
|
103
|
+
// Withheld rather than computed on an incomparable pair. A separation
|
|
104
|
+
// between two runs over different bars is a number, and it means nothing.
|
|
105
|
+
separation: blocking ? null : separationBetween(before.report.summary, after.report.summary),
|
|
106
|
+
sharedTrades: sharedTradesBetween(before.report.trades, after.report.trades),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* What differs between two runs' inputs, in a fixed order.
|
|
112
|
+
*
|
|
113
|
+
* Compared through the repository's one canonical writer rather than field by
|
|
114
|
+
* field, so a settings shape that grows a field is compared on that field
|
|
115
|
+
* without this function being edited. Field by field is how a comparison
|
|
116
|
+
* quietly stops covering whatever was added to the settings last.
|
|
117
|
+
*/
|
|
118
|
+
function differencesBetween(before: RunRecord, after: RunRecord): readonly Difference[] {
|
|
119
|
+
const found: Difference[] = [];
|
|
120
|
+
|
|
121
|
+
if (before.bars.hash !== after.bars.hash) {
|
|
122
|
+
found.push({
|
|
123
|
+
what: 'bars',
|
|
124
|
+
detail: `different bars: ${before.bars.count} rows hashing ${before.bars.hash}, against ${after.bars.count} hashing ${after.bars.hash}`,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (canonicalise(before.settings.contract) !== canonicalise(after.settings.contract)) {
|
|
128
|
+
found.push({ what: 'contract', detail: 'the two runs are on different contracts' });
|
|
129
|
+
}
|
|
130
|
+
if (before.programHash !== after.programHash) {
|
|
131
|
+
found.push({
|
|
132
|
+
what: 'program',
|
|
133
|
+
detail: `different program: ${before.programHash}, against ${after.programHash}`,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if (canonicalise(before.settings.range) !== canonicalise(after.settings.range)) {
|
|
137
|
+
found.push({ what: 'range', detail: 'the two runs cover different date ranges' });
|
|
138
|
+
}
|
|
139
|
+
if (canonicalise(before.settings.costs) !== canonicalise(after.settings.costs)) {
|
|
140
|
+
found.push({ what: 'costs', detail: 'the two runs were charged under different cost models' });
|
|
141
|
+
}
|
|
142
|
+
if (canonicalise(before.settings.fill) !== canonicalise(after.settings.fill)) {
|
|
143
|
+
found.push({ what: 'fill', detail: 'the two runs were filled under different policies' });
|
|
144
|
+
}
|
|
145
|
+
if (canonicalise(before.settings.inputs) !== canonicalise(after.settings.inputs)) {
|
|
146
|
+
found.push({ what: 'inputs', detail: 'the two scripts ran with different input values' });
|
|
147
|
+
}
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Every compared figure, before and after.
|
|
153
|
+
*
|
|
154
|
+
* A summary carries null where a figure has no meaning: no win rate until a
|
|
155
|
+
* trade closes, no profit factor without a loss. Null on one side and a number
|
|
156
|
+
* on the other is a real thing to report, and a delta between them is not, so
|
|
157
|
+
* the row appears with both values and a delta of zero. Subtracting from null
|
|
158
|
+
* would invent a movement out of a figure that was never there.
|
|
159
|
+
*/
|
|
160
|
+
function deltasBetween(before: Summary, after: Summary): RunComparison['deltas'] {
|
|
161
|
+
// Widened once, here. COMPARED is a list of names and a summary is a closed
|
|
162
|
+
// shape, so the lookup is by string either way; doing it in one place keeps
|
|
163
|
+
// the rest of this file reading the real type.
|
|
164
|
+
const a0 = before as unknown as Readonly<Record<string, unknown>>;
|
|
165
|
+
const b0 = after as unknown as Readonly<Record<string, unknown>>;
|
|
166
|
+
const deltas: { name: string; before: number; after: number; delta: number }[] = [];
|
|
167
|
+
for (const name of COMPARED) {
|
|
168
|
+
const a = numberOf(a0[name]);
|
|
169
|
+
const b = numberOf(b0[name]);
|
|
170
|
+
// Neither side holds a figure: a field of no summary, which is nothing to
|
|
171
|
+
// report rather than a row of three zeroes.
|
|
172
|
+
if (a === null && b === null) continue;
|
|
173
|
+
deltas.push({
|
|
174
|
+
name,
|
|
175
|
+
before: a ?? 0,
|
|
176
|
+
after: b ?? 0,
|
|
177
|
+
delta: a === null || b === null ? 0 : (b ?? 0) - (a ?? 0),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return deltas;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A figure, or none where the summary carries null or a string for it. */
|
|
184
|
+
function numberOf(value: unknown): number | null {
|
|
185
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* How many standard errors apart the two expectancies are.
|
|
190
|
+
*
|
|
191
|
+
* Analytic, with no resampling and no random number generator, so two runs of
|
|
192
|
+
* this comparison over the same pair give the same figure for ever and a
|
|
193
|
+
* decision taken on it is one anybody can reproduce.
|
|
194
|
+
*
|
|
195
|
+
* **Null rather than a large number where there is no noise to measure.** A run
|
|
196
|
+
* with fewer than two closed trades has a standard error of zero, because one
|
|
197
|
+
* trade has no spread, and where both are zero the ratio divides by it.
|
|
198
|
+
* Returning infinity there would read as infinite confidence, which is the
|
|
199
|
+
* exact opposite of what one trade tells anybody. Null says the question cannot
|
|
200
|
+
* be answered from these two runs, which is the truth about them.
|
|
201
|
+
*/
|
|
202
|
+
function separationBetween(
|
|
203
|
+
before: { readonly expectancy: number; readonly expectancyStandardError: number },
|
|
204
|
+
after: { readonly expectancy: number; readonly expectancyStandardError: number },
|
|
205
|
+
): number | null {
|
|
206
|
+
const noise = Math.sqrt(
|
|
207
|
+
before.expectancyStandardError * before.expectancyStandardError +
|
|
208
|
+
after.expectancyStandardError * after.expectancyStandardError,
|
|
209
|
+
);
|
|
210
|
+
if (!Number.isFinite(noise) || noise === 0) return null;
|
|
211
|
+
const gap = after.expectancy - before.expectancy;
|
|
212
|
+
return Number.isFinite(gap) ? gap / noise : null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* How many trades the two runs took alike.
|
|
217
|
+
*
|
|
218
|
+
* Alike means opened on the same bar on the same side, which is the coarsest
|
|
219
|
+
* thing two runs can agree about and the only one that survives a change to
|
|
220
|
+
* sizing or to costs. It is what says whether a change moved the money by
|
|
221
|
+
* trading differently or by trading the same and paying differently, and those
|
|
222
|
+
* are not the same result however alike the two totals look.
|
|
223
|
+
*
|
|
224
|
+
* Counted as a multiset, so two long trades opening on one bar in one run
|
|
225
|
+
* against one in the other share one trade rather than two.
|
|
226
|
+
*/
|
|
227
|
+
function sharedTradesBetween(
|
|
228
|
+
before: readonly { readonly openedOnBar: number; readonly side: string }[],
|
|
229
|
+
after: readonly { readonly openedOnBar: number; readonly side: string }[],
|
|
230
|
+
): number {
|
|
231
|
+
const counts = new Map<string, number>();
|
|
232
|
+
for (const trade of before) {
|
|
233
|
+
const key = `${trade.openedOnBar}:${trade.side}`;
|
|
234
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
235
|
+
}
|
|
236
|
+
let shared = 0;
|
|
237
|
+
for (const trade of after) {
|
|
238
|
+
const key = `${trade.openedOnBar}:${trade.side}`;
|
|
239
|
+
const left = counts.get(key) ?? 0;
|
|
240
|
+
if (left > 0) {
|
|
241
|
+
counts.set(key, left - 1);
|
|
242
|
+
shared += 1;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return shared;
|
|
246
|
+
}
|