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,499 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The destination a backtest runs against: intents in, frames out.
|
|
3
|
+
*
|
|
4
|
+
* **This is a host, not part of the engine**, and the division is the one thing
|
|
5
|
+
* to keep straight while reading it. `stdlib.md` 17.1 puts slippage and
|
|
6
|
+
* commission on the destination: the engine folds the price it is told and
|
|
7
|
+
* never adjusts one, so the price worsening below is a venue giving a worse
|
|
8
|
+
* fill, which is what a venue does, and not an engine moving a number, which is
|
|
9
|
+
* what the invariant forbids. Everything here could be a broker.
|
|
10
|
+
*
|
|
11
|
+
* **A frame is cumulative and reaches the engine between bars.** Every frame
|
|
12
|
+
* restates the whole life of one order from its beginning, so a repeat costs
|
|
13
|
+
* nothing, and none of them is handed over during an execution: they are
|
|
14
|
+
* answered here and collected at the boundary, which is where
|
|
15
|
+
* `host-interface.md` 7.4 puts the intake.
|
|
16
|
+
*
|
|
17
|
+
* **When a fill is known is not when it happened.** A market order decided at
|
|
18
|
+
* the close of a bar and filled at the next bar's open is known at that open,
|
|
19
|
+
* so its frame is delivered before that bar executes and the strategy holds the
|
|
20
|
+
* position for the whole of it. An order that rested and traded somewhere
|
|
21
|
+
* inside a bar is only known to have traded once that bar is complete, so its
|
|
22
|
+
* frame is delivered before the bar after it. Both are the truthful reading of
|
|
23
|
+
* what a venue could have told anybody at the time, and the difference between
|
|
24
|
+
* them is why they are not folded together.
|
|
25
|
+
*
|
|
26
|
+
* **A bracket is answered with nothing at all**, exactly as a venue with no
|
|
27
|
+
* order to report would answer. A bracket reaches a destination as a protective
|
|
28
|
+
* instruction attached to a tag and the engine appends no row for it, so there
|
|
29
|
+
* is nothing for a frame to be about: a fill reported against one would name an
|
|
30
|
+
* intent no row holds and the fold would refuse it. The row a bracket wants is
|
|
31
|
+
* decision 55 and it is not in this release, so a stop cannot fill here and a
|
|
32
|
+
* page that says it can is ahead of the engine.
|
|
33
|
+
*
|
|
34
|
+
* **A destination that behaves badly does it on a schedule, stated before the
|
|
35
|
+
* run and never by chance.** Every case harvested before this one ran against a
|
|
36
|
+
* venue that filled every order whole and on time, so two engines were proved
|
|
37
|
+
* to agree about the half of a day that costs nobody anything. The other half
|
|
38
|
+
* is a partial fill, an order arriving in pieces, a rejection, a cancellation,
|
|
39
|
+
* an expiry and a fill that turns up after the order has ended, and
|
|
40
|
+
* `SimulatorOptions.fill` carries the schedule saying which order each of those
|
|
41
|
+
* happens to and at which boundary. There is no random number here and there
|
|
42
|
+
* will not be one: `stdlib.md` 8.2 keeps a script that answers differently on a
|
|
43
|
+
* second run out of a conformance suite, and a venue rolling a die would make
|
|
44
|
+
* every case it wrote unreproducible in the same breath. **An order the
|
|
45
|
+
* schedule names is answered by the schedule and by nothing else**, so a run
|
|
46
|
+
* stating none is the run the cases before this one were harvested from, to the
|
|
47
|
+
* bit, which is what lets this exist at all.
|
|
48
|
+
*/
|
|
49
|
+
import type { Contract } from '../accounting/index.js';
|
|
50
|
+
import type { OrderFrame, OrderIntent, OrderSide, RoutedEffect } from '../engine/index.js';
|
|
51
|
+
import type { RecordedBar } from './record.js';
|
|
52
|
+
import { testResting } from './resting.js';
|
|
53
|
+
import type { RestingOrder } from './resting.js';
|
|
54
|
+
import type { FillPolicy } from './settings.js';
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* What this destination does to one order, at one boundary.
|
|
58
|
+
*
|
|
59
|
+
* Four words reaching every status of `stdlib.md` 17.7 a host may send. `fill`
|
|
60
|
+
* carries the cumulative quantity, so one word covers the acknowledgement
|
|
61
|
+
* before anything has traded, a fill of part of the order and a fill of the
|
|
62
|
+
* whole of it: `working` and `filled` are that quantity read against the
|
|
63
|
+
* order's own rather than two instructions. The other three are the three ways
|
|
64
|
+
* an order ends carrying less than it asked for, and an engine never handed
|
|
65
|
+
* one has never been asked what it does with the quantity still working.
|
|
66
|
+
*/
|
|
67
|
+
export type VenueDoes = 'fill' | 'reject' | 'cancel' | 'expire';
|
|
68
|
+
|
|
69
|
+
/** One act of the schedule: what the destination does, to which order, when. */
|
|
70
|
+
export interface VenueAct {
|
|
71
|
+
/**
|
|
72
|
+
* The nth order this destination took, counting from one.
|
|
73
|
+
*
|
|
74
|
+
* Orders and not intents: a bracket is never taken and a cancellation is
|
|
75
|
+
* answered without being held, so neither is counted and neither can be named
|
|
76
|
+
* here. An ordinal for the reason `conformance.md` section 3 gives a case's
|
|
77
|
+
* own frames: nobody writing a schedule knows the id an engine will mint.
|
|
78
|
+
*/
|
|
79
|
+
readonly order: number;
|
|
80
|
+
/**
|
|
81
|
+
* Boundaries after the one the order arrived at, `0` being that boundary.
|
|
82
|
+
*
|
|
83
|
+
* Counted from the order rather than stated as a bar index, because the bar
|
|
84
|
+
* a strategy decides an order on moves the moment anything else about the run
|
|
85
|
+
* does, and a schedule in bar indices is one nobody can read back.
|
|
86
|
+
*/
|
|
87
|
+
readonly afterBars: number;
|
|
88
|
+
readonly does: VenueDoes;
|
|
89
|
+
/**
|
|
90
|
+
* The cumulative quantity a fill reports, in units, or absent for all of it.
|
|
91
|
+
*
|
|
92
|
+
* Cumulative because a frame is (`stdlib.md` 17.8): `0` is the
|
|
93
|
+
* acknowledgement a venue sends before anything has traded, a number below
|
|
94
|
+
* the order's own is a partial fill, and one at or above it completes the
|
|
95
|
+
* order. A quantity below what this venue has already reported is a stale
|
|
96
|
+
* frame, which a venue really sends and the fold has to swallow, so it is
|
|
97
|
+
* stated here rather than refused.
|
|
98
|
+
*/
|
|
99
|
+
readonly units?: number | null;
|
|
100
|
+
/** The destination's own text, which the ledger records against the row. */
|
|
101
|
+
readonly text?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The fill policy, and what this destination does to the orders it names.
|
|
106
|
+
*
|
|
107
|
+
* The schedule sits on the policy because it is the same kind of fact: how this
|
|
108
|
+
* destination decides a fill, chosen before the first bar and carried in the
|
|
109
|
+
* record beside the policy's own version, so a stored run replays as it ran.
|
|
110
|
+
*/
|
|
111
|
+
export interface VenuePolicy extends FillPolicy {
|
|
112
|
+
readonly schedule?: readonly VenueAct[];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** What the venue prices against and how it decides a fill. */
|
|
116
|
+
export interface SimulatorOptions {
|
|
117
|
+
readonly bars: readonly RecordedBar[];
|
|
118
|
+
readonly contract: Contract;
|
|
119
|
+
readonly fill: VenuePolicy;
|
|
120
|
+
/** Adverse always, in ticks, applied to a market fill and to a stop. */
|
|
121
|
+
readonly slippageTicks: number;
|
|
122
|
+
/** The declaration's own fill rule: where a market order is priced. */
|
|
123
|
+
readonly fillOn: string;
|
|
124
|
+
/**
|
|
125
|
+
* The unit the declaration's quantity is stated in, `language.md` 13.3.
|
|
126
|
+
*
|
|
127
|
+
* Here because converting it is the destination's job and a fill is counted
|
|
128
|
+
* in units. Only the units this destination can arrive at reach it: the rest
|
|
129
|
+
* are refused before the first bar by `checkSettings`.
|
|
130
|
+
*/
|
|
131
|
+
readonly qtyType: string;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** One order this venue holds, and what it has said about it. */
|
|
135
|
+
interface Order {
|
|
136
|
+
readonly intent: OrderIntent;
|
|
137
|
+
readonly ref: string;
|
|
138
|
+
readonly placedOn: number;
|
|
139
|
+
/** Absent on a market order, which rests on nothing. */
|
|
140
|
+
readonly rest: RestingOrder | null;
|
|
141
|
+
/** The acts the schedule states about this order, in the order they were stated. */
|
|
142
|
+
readonly acts: readonly VenueAct[];
|
|
143
|
+
filledQty: number;
|
|
144
|
+
/** This venue's average over `filledQty`, absent while nothing has filled. */
|
|
145
|
+
avgPrice: number | null;
|
|
146
|
+
live: boolean;
|
|
147
|
+
/** Whether a stop limit has reached its trigger and is now a limit. */
|
|
148
|
+
triggered: boolean;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** `language.md` 13.3: a market order priced at the close of its own bar. */
|
|
152
|
+
const AT_CLOSE = 'close';
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The word this venue reports each ending under.
|
|
156
|
+
*
|
|
157
|
+
* A destination with words of its own maps them onto the vocabulary in its
|
|
158
|
+
* adapter, which is where `stdlib.md` 17.7 puts the mapping because a status
|
|
159
|
+
* vocabulary differs per destination. This is this one's, and the whole of what
|
|
160
|
+
* a schedule's verb means.
|
|
161
|
+
*/
|
|
162
|
+
const ENDED: Readonly<Record<Exclude<VenueDoes, 'fill'>, string>> = {
|
|
163
|
+
reject: 'rejected',
|
|
164
|
+
cancel: 'cancelled',
|
|
165
|
+
expire: 'expired',
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A venue, for one run.
|
|
170
|
+
*
|
|
171
|
+
* It holds the orders it was handed and nothing else: no position, no money and
|
|
172
|
+
* no view of what the strategy is doing, because a destination has none of
|
|
173
|
+
* those about somebody else's strategy.
|
|
174
|
+
*/
|
|
175
|
+
export class Simulator {
|
|
176
|
+
private readonly options: SimulatorOptions;
|
|
177
|
+
private readonly orders: Order[] = [];
|
|
178
|
+
/** The intents handed over, in the order they were, whatever became of them. */
|
|
179
|
+
readonly intents: OrderIntent[] = [];
|
|
180
|
+
/** Brackets, held so that a run can say how many it was handed and answered. */
|
|
181
|
+
readonly brackets: OrderIntent[] = [];
|
|
182
|
+
private queued: Order[] = [];
|
|
183
|
+
private answered: OrderFrame[] = [];
|
|
184
|
+
private refs = 0;
|
|
185
|
+
private seq = 0;
|
|
186
|
+
|
|
187
|
+
constructor(options: SimulatorOptions) {
|
|
188
|
+
this.options = options;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Step 9: what the strategy decided reaches the venue. */
|
|
192
|
+
route(effect: RoutedEffect, barIndex: number): void {
|
|
193
|
+
for (const intent of effect.intents) {
|
|
194
|
+
this.intents.push(intent);
|
|
195
|
+
if (intent.kind === 'bracket') {
|
|
196
|
+
this.brackets.push(intent);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (intent.kind === 'cancel') {
|
|
200
|
+
this.withdraw(intent, barIndex);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
this.accept(intent, barIndex);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The frames a boundary hands over: everything this venue can say now.
|
|
209
|
+
*
|
|
210
|
+
* The orders that were already resting are decided against this bar first and
|
|
211
|
+
* the orders this bar sent are answered after, which is the order the venue
|
|
212
|
+
* learned of them in.
|
|
213
|
+
*/
|
|
214
|
+
framesFor(barIndex: number): readonly OrderFrame[] {
|
|
215
|
+
const bar = this.options.bars[barIndex];
|
|
216
|
+
if (bar !== undefined) {
|
|
217
|
+
for (const order of this.orders) {
|
|
218
|
+
// An order the schedule names is answered by the schedule, whether or
|
|
219
|
+
// not it is still live: a fill after a cancellation is the one thing
|
|
220
|
+
// this exists for and the order is dead by then.
|
|
221
|
+
if (order.acts.length > 0) {
|
|
222
|
+
if (order.placedOn < barIndex) this.perform(order, bar, barIndex);
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (!order.live || order.rest === null || order.placedOn >= barIndex) continue;
|
|
226
|
+
this.decide(order, bar, barIndex);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const sent = this.queued;
|
|
231
|
+
this.queued = [];
|
|
232
|
+
for (const order of sent) this.open(order, barIndex);
|
|
233
|
+
|
|
234
|
+
const out = this.answered;
|
|
235
|
+
this.answered = [];
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** How many orders this venue is still holding, which a run reports. */
|
|
240
|
+
get working(): number {
|
|
241
|
+
return this.orders.filter((order) => order.live).length;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** An order the strategy sent, which this venue now holds. */
|
|
245
|
+
private accept(intent: OrderIntent, barIndex: number): void {
|
|
246
|
+
this.refs += 1;
|
|
247
|
+
// The ordinal of this order among the orders taken, which is what an act
|
|
248
|
+
// names. Read before the order joins them, so the first is one.
|
|
249
|
+
const ordinal = this.orders.length + 1;
|
|
250
|
+
const order: Order = {
|
|
251
|
+
intent,
|
|
252
|
+
ref: refOf(this.refs),
|
|
253
|
+
placedOn: barIndex,
|
|
254
|
+
rest: restingFor(intent),
|
|
255
|
+
acts: (this.options.fill.schedule ?? []).filter((act) => act.order === ordinal),
|
|
256
|
+
filledQty: 0,
|
|
257
|
+
avgPrice: null,
|
|
258
|
+
live: true,
|
|
259
|
+
triggered: false,
|
|
260
|
+
};
|
|
261
|
+
this.orders.push(order);
|
|
262
|
+
this.queued.push(order);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* A cancellation, which is about the orders a tag names and not about itself.
|
|
267
|
+
*
|
|
268
|
+
* Every live order carrying the tag is withdrawn and answered, which is what
|
|
269
|
+
* releases the position they were claiming, and the cancellation is then
|
|
270
|
+
* answered as an order of its own, because the engine appended a row for it
|
|
271
|
+
* and a row nothing ever answers stays at `placed` for the life of the run.
|
|
272
|
+
*/
|
|
273
|
+
private withdraw(intent: OrderIntent, barIndex: number): void {
|
|
274
|
+
for (const order of this.orders) {
|
|
275
|
+
if (!order.live || order.intent.tag !== intent.tag) continue;
|
|
276
|
+
order.live = false;
|
|
277
|
+
this.say(order.intent, order.ref, 'cancelled', order.filledQty, null, barIndex);
|
|
278
|
+
}
|
|
279
|
+
// The cancellation is confirmed after what it withdrew, which is the order
|
|
280
|
+
// a venue does it in: the request is answered once it has been carried out.
|
|
281
|
+
this.refs += 1;
|
|
282
|
+
this.say(intent, refOf(this.refs), 'cancelled', 0, null, barIndex);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* The first thing a venue says about an order it has taken.
|
|
287
|
+
*
|
|
288
|
+
* A working frame before any fill, because 7.4 asks for a frame on every
|
|
289
|
+
* change of status and a script waiting for a working order to clear waits
|
|
290
|
+
* blind without one. A market order is then filled in the same breath where
|
|
291
|
+
* the declaration prices it at this bar's close, and at the next bar's open
|
|
292
|
+
* where it does not.
|
|
293
|
+
*/
|
|
294
|
+
private open(order: Order, barIndex: number): void {
|
|
295
|
+
// A scheduled order is answered by its schedule from its first breath. The
|
|
296
|
+
// acknowledgement below is a thing this venue chooses to say, so a schedule
|
|
297
|
+
// that wants one states it, and one that wants the destination to sit on an
|
|
298
|
+
// order and say nothing gets that instead.
|
|
299
|
+
if (order.acts.length > 0) {
|
|
300
|
+
const bar = this.options.bars[barIndex];
|
|
301
|
+
if (bar !== undefined) this.perform(order, bar, barIndex);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
this.say(order.intent, order.ref, 'working', 0, null, barIndex);
|
|
305
|
+
if (order.rest !== null) return;
|
|
306
|
+
|
|
307
|
+
const atClose = this.options.fillOn === AT_CLOSE;
|
|
308
|
+
const bar = this.options.bars[atClose ? barIndex : barIndex + 1];
|
|
309
|
+
const price = atClose ? (bar?.close ?? null) : (bar?.open ?? null);
|
|
310
|
+
// A market order sent on the last bar of a run and priced at the next open
|
|
311
|
+
// has no next open. It stays working and is reported as an order the run
|
|
312
|
+
// ended holding, which is what it is.
|
|
313
|
+
if (price === null) return;
|
|
314
|
+
this.complete(order, this.worsen(price, order.intent.side), barIndex);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** One resting order against one bar. */
|
|
318
|
+
private decide(order: Order, bar: RecordedBar, barIndex: number): void {
|
|
319
|
+
const rest = order.rest;
|
|
320
|
+
if (rest === null) return;
|
|
321
|
+
const outcome = testResting(order.triggered ? asLimit(rest) : rest, bar, this.options.fill);
|
|
322
|
+
if (!outcome.filled) {
|
|
323
|
+
if (outcome.triggered) order.triggered = true;
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const price = outcome.slips ? this.worsen(outcome.price, order.intent.side) : outcome.price;
|
|
327
|
+
this.complete(order, price, barIndex);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The whole of an order, filled at one price.
|
|
332
|
+
*
|
|
333
|
+
* **The quantity is converted into units here, because that is what a fill
|
|
334
|
+
* is counted in and what every money figure is folded over.** A quantity
|
|
335
|
+
* travels in the declaration's own unit (`host-interface.md` 7.1) and the
|
|
336
|
+
* destination is the party that converts it. This filled the number verbatim,
|
|
337
|
+
* so a strategy sizing in lots on a lot of sixty five traded one sixty fifth
|
|
338
|
+
* of what it asked for and the whole report was out by that factor. The units
|
|
339
|
+
* this destination cannot arrive at are refused before the first bar rather
|
|
340
|
+
* than guessed at here, so by this point the unit is one of two.
|
|
341
|
+
*/
|
|
342
|
+
private complete(order: Order, price: number, barIndex: number): void {
|
|
343
|
+
const qty = this.unitsOf(order.intent);
|
|
344
|
+
order.filledQty = qty;
|
|
345
|
+
order.avgPrice = price;
|
|
346
|
+
order.live = false;
|
|
347
|
+
this.say(order.intent, order.ref, 'filled', qty, price, barIndex);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* The acts due at this boundary, in the order the schedule stated them.
|
|
352
|
+
*
|
|
353
|
+
* Due is counted from the bar that sent the order, so an act names a moment
|
|
354
|
+
* in the life of its own order rather than a bar of the run. Two acts due
|
|
355
|
+
* together are answered as written, which is how a schedule states a
|
|
356
|
+
* cancellation and the fill that raced it.
|
|
357
|
+
*
|
|
358
|
+
* Liveness is not consulted. An order that has ended can still be spoken
|
|
359
|
+
* about, because the frame that arrives after it ended is the whole reason
|
|
360
|
+
* this is here: `stdlib.md` 17.8's fill after a terminal status, which a
|
|
361
|
+
* venue sends whenever a cancel races a fill and which an engine refusing it
|
|
362
|
+
* loses, leaving a position the strategy cannot see.
|
|
363
|
+
*/
|
|
364
|
+
private perform(order: Order, bar: RecordedBar, barIndex: number): void {
|
|
365
|
+
for (const act of order.acts) {
|
|
366
|
+
if (order.placedOn + act.afterBars !== barIndex) continue;
|
|
367
|
+
if (act.does === 'fill') {
|
|
368
|
+
this.report(order, act, bar, barIndex);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
// The order ends, reporting what it filled before it ended, because a
|
|
372
|
+
// frame is cumulative: `stdlib.md` 17.8 has the row keeping the terminal
|
|
373
|
+
// word and the quantity recording what traded, both true at once.
|
|
374
|
+
order.live = false;
|
|
375
|
+
const ended = ENDED[act.does];
|
|
376
|
+
const price = order.filledQty > 0 ? order.avgPrice : null;
|
|
377
|
+
this.say(order.intent, order.ref, ended, order.filledQty, price, barIndex, act.text ?? '');
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* A fill the schedule stated, at this bar's close and worsened like any other.
|
|
383
|
+
*
|
|
384
|
+
* **The average is this venue's own, over the cumulative quantity**, the
|
|
385
|
+
* figure `stdlib.md` 17.8 step 3 says the row takes whole. A venue reporting
|
|
386
|
+
* the last piece's price and calling it an average hands the engine a number
|
|
387
|
+
* that is not one, and the engine may not work its own out from two of them,
|
|
388
|
+
* so the lie settles into the ledger and into every trade folded from it.
|
|
389
|
+
*
|
|
390
|
+
* A stated quantity at or below what this venue has already reported adds
|
|
391
|
+
* nothing and moves nothing: that is a repeated or a stale frame, which a real
|
|
392
|
+
* destination sends and the fold has to swallow. It carries the average this
|
|
393
|
+
* venue holds now rather than then, because it keeps no history of its own
|
|
394
|
+
* averages and the fold ignores the price of a frame adding no quantity. A
|
|
395
|
+
* bar with no close prices nothing, so an act due at one says nothing rather
|
|
396
|
+
* than raising a quantity with no price against it, which step 3 refuses.
|
|
397
|
+
*/
|
|
398
|
+
private report(order: Order, act: VenueAct, bar: RecordedBar, barIndex: number): void {
|
|
399
|
+
const whole = this.unitsOf(order.intent);
|
|
400
|
+
const stated = act.units ?? whole;
|
|
401
|
+
const delta = stated - order.filledQty;
|
|
402
|
+
if (delta > 0) {
|
|
403
|
+
if (bar.close === null) return;
|
|
404
|
+
// Written in this order and left in it: the source order of a sum is
|
|
405
|
+
// what decides its last bit, and a case harvested from this venue is
|
|
406
|
+
// asserted to the bit.
|
|
407
|
+
const price = this.worsen(bar.close, order.intent.side);
|
|
408
|
+
order.avgPrice = ((order.avgPrice ?? 0) * order.filledQty + price * delta) / stated;
|
|
409
|
+
order.filledQty = stated;
|
|
410
|
+
if (order.filledQty >= whole) order.live = false;
|
|
411
|
+
}
|
|
412
|
+
// `working` is live and not completely filled, `filled` is the whole
|
|
413
|
+
// quantity: 17.7's two words read off the quantity rather than stated
|
|
414
|
+
// twice by a schedule that could disagree with the number beside them.
|
|
415
|
+
const status = order.filledQty >= whole ? 'filled' : 'working';
|
|
416
|
+
const price = stated > 0 ? order.avgPrice : null;
|
|
417
|
+
this.say(order.intent, order.ref, status, stated, price, barIndex, act.text ?? '');
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* The order's quantity in units, which is what a fill is counted in.
|
|
422
|
+
*
|
|
423
|
+
* The conversion `complete` did inline, wanted in two places the moment a
|
|
424
|
+
* schedule can fill an order in pieces: the whole a piece is measured
|
|
425
|
+
* against has to be the number the fill path would have reported.
|
|
426
|
+
*/
|
|
427
|
+
private unitsOf(intent: OrderIntent): number {
|
|
428
|
+
const stated = intent.qty ?? 0;
|
|
429
|
+
const lot = this.options.contract.lotSize;
|
|
430
|
+
return this.options.qtyType === 'lots' && lot !== null && lot > 0 ? stated * lot : stated;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* A price worsened by the slippage the run was carried out under.
|
|
435
|
+
*
|
|
436
|
+
* **Adverse always: a buy pays more and a sell receives less.** The sign is
|
|
437
|
+
* taken as written rather than as a magnitude, because a slippage below zero
|
|
438
|
+
* is refused before the first bar and taking its magnitude here would be this
|
|
439
|
+
* function quietly covering for a hole in that refusal. It went uncovered for
|
|
440
|
+
* a while: `scheduleProblem` has always refused a negative rate, and
|
|
441
|
+
* `checkSettings` asked it only about a schedule the host supplied, so a
|
|
442
|
+
* declaration stating a slippage of minus one improved both sides of every
|
|
443
|
+
* fill and the backtest paid the strategy to trade.
|
|
444
|
+
*
|
|
445
|
+
* A slippage in ticks with no tick size to measure a tick in charges nothing.
|
|
446
|
+
* The comment here once said that was refused before the first bar and it was
|
|
447
|
+
* not; it still is not, because an instrument with no tick is a fact a host
|
|
448
|
+
* may legitimately not hold, and a run that charges no slippage is a study
|
|
449
|
+
* before slippage rather than a wrong answer. What has changed is that this
|
|
450
|
+
* says so instead of claiming a refusal that never existed.
|
|
451
|
+
*/
|
|
452
|
+
private worsen(price: number, side: OrderSide | null): number {
|
|
453
|
+
const tick = this.options.contract.tickSize;
|
|
454
|
+
if (tick === null || this.options.slippageTicks === 0) return price;
|
|
455
|
+
const move = this.options.slippageTicks * tick;
|
|
456
|
+
return side === 'sell' ? price - move : price + move;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** One frame, cumulative, in the order this venue spoke. */
|
|
460
|
+
private say(
|
|
461
|
+
intent: OrderIntent,
|
|
462
|
+
ref: string,
|
|
463
|
+
status: string,
|
|
464
|
+
filledQty: number,
|
|
465
|
+
avgFillPrice: number | null,
|
|
466
|
+
barIndex: number,
|
|
467
|
+
text = '',
|
|
468
|
+
): void {
|
|
469
|
+
this.seq += 1;
|
|
470
|
+
this.answered.push({
|
|
471
|
+
intentId: intent.intentId,
|
|
472
|
+
status,
|
|
473
|
+
filledQty,
|
|
474
|
+
avgFillPrice,
|
|
475
|
+
orderRef: ref,
|
|
476
|
+
sentInstrument: intent.instrument,
|
|
477
|
+
sentProduct: intent.product,
|
|
478
|
+
time: this.options.bars[barIndex]?.time ?? null,
|
|
479
|
+
text,
|
|
480
|
+
seq: this.seq,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** This venue's own reference for an order, which the engine records and never parses. */
|
|
486
|
+
function refOf(count: number): string {
|
|
487
|
+
return 'ORD-' + String(count);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** The resting order an intent is, or nothing where it is a market order. */
|
|
491
|
+
function restingFor(intent: OrderIntent): RestingOrder | null {
|
|
492
|
+
if (intent.type === null || intent.type === 'market' || intent.side === null) return null;
|
|
493
|
+
return { side: intent.side, type: intent.type, limit: intent.limit, trigger: intent.trigger };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** A stop limit that has triggered, which is a limit from that moment on. */
|
|
497
|
+
function asLimit(rest: RestingOrder): RestingOrder {
|
|
498
|
+
return { side: rest.side, type: 'limit', limit: rest.limit, trigger: null };
|
|
499
|
+
}
|
|
@@ -137,6 +137,10 @@ export const ENTRIES = {
|
|
|
137
137
|
OS6017: { code: "OS6017", title: "The program's language version is not one this engine implements", severity: "error", stage: "host", since: 1, autofix: false, message: "This program was compiled from language version {found}, and this engine implements {versions}.", fix: "Upgrade the engine to one that implements language version {found}, or recompile the source against a version it has.", placeholders: ["found", "versions"] },
|
|
138
138
|
OS6018: { code: "OS6018", title: "The compiled program is malformed", severity: "error", stage: "host", since: 1, autofix: false, message: "The program failed verification at {location}: {reason}.", fix: "Recompile the script from its source; a program that fails verification came from a broken compiler or was edited after it was written, and neither is repairable by hand.", placeholders: ["location", "reason"] },
|
|
139
139
|
OS6019: { code: "OS6019", title: "A host setting fails the input's validation", severity: "error", stage: "host", since: 1, autofix: false, message: "The host supplied {value} for {key}, and {validation}.", fix: "Correct the value in the settings dialog, or widen the input's own min, max or options so the value is allowed.", placeholders: ["value", "key", "validation"] },
|
|
140
|
+
OS6020: { code: "OS6020", title: "The report window holds no bars", severity: "error", stage: "host", since: 1, autofix: false, message: "The window from {from} to {to} holds none of the {count} bars supplied.", fix: "Widen the window until it covers bars, or supply the bars it covers. Both bounds are inclusive and are compared against the times of the bars supplied rather than against a calendar, so a window that falls inside a gap in the data is empty however wide it looks.", placeholders: ["from", "to", "count"] },
|
|
141
|
+
OS6021: { code: "OS6021", title: "A run setting cannot be applied as stated", severity: "error", stage: "host", since: 1, autofix: false, message: "{setting} cannot be applied: {problem}.", fix: "State the setting so it can be carried out: declare a charge line after every line it is levied on, supply the tick size a slippage in ticks is measured in, write down the reason a tolerance needs a bound, or bring a bound past the cap back inside it. Nothing has been computed at the point this is refused, so correcting the setting and running again costs one run.", placeholders: ["setting", "problem"] },
|
|
142
|
+
OS6022: { code: "OS6022", title: "The bars are not the bars the record was made from", severity: "error", stage: "host", since: 1, autofix: false, message: "The bars supplied hash to {found}, and the record was made from {expected}.", fix: "Replay against the bars the record names. Where the revision is the point, make a second record over the revised bars and compare the two runs, rather than overwriting one run with the other under one name.", placeholders: ["found", "expected"] },
|
|
143
|
+
OS6023: { code: "OS6023", title: "Two cost models are stated at once", severity: "error", stage: "host", since: 1, autofix: false, message: "A charge schedule was supplied, and the declaration states a commission of {commission} in {commissionType}.", fix: "Supply the schedule and leave the declaration's commission at its default of zero, or state the commission in the declaration and supply no schedule. A schedule is the one of the two that can carry a floor, a cap, a charge levied on a charge, and a cost that falls on one side of the trade only.", placeholders: ["commission", "commissionType"] },
|
|
140
144
|
OS7001: { code: "OS7001", title: "Only a strategy can do that", severity: "error", stage: "check", since: 1, autofix: false, message: "{name} is available only in a file declared with strategy().", fix: "Change study(...) on line {line} to strategy(...), or replace {name} with signal(\"...\") to mark the bar without trading.", placeholders: ["name", "line"] },
|
|
141
145
|
OS7002: { code: "OS7002", title: "An order argument is absent", severity: "error", stage: "runtime", since: 1, autofix: false, message: "{name}'s {argument} is absent on this bar.", fix: "Guard the call with isNone({argument}), or supply a fallback with orElse() where one is genuinely correct.", placeholders: ["name", "argument"] },
|
|
142
146
|
OS7003: { code: "OS7003", title: "An order function inside a request expression", severity: "error", stage: "check", since: 1, autofix: false, message: "{name} inside a request expression would place an order from another instrument's bars.", fix: "Read the value with the request, and place the order at the top level from the result.", placeholders: ["name"] },
|
|
@@ -154,6 +158,8 @@ export const ENTRIES = {
|
|
|
154
158
|
OS7015: { code: "OS7015", title: "The strategy has no order destination", severity: "error", stage: "host", since: 1, autofix: false, message: "This strategy placed an order and the host supplied no destination.", fix: "Connect a destination in the host, or run the file as a study(): replace buy() with signal(\"BUY\").", placeholders: [] },
|
|
155
159
|
OS7016: { code: "OS7016", title: "A close names a tag nothing places", severity: "error", stage: "check", since: 1, autofix: false, message: "No order in this file is placed with the tag {tag}.", fix: "Use the tag the entry was placed with, or leave the tag out to flatten the whole leg.", placeholders: ["tag"] },
|
|
156
160
|
OS7017: { code: "OS7017", title: "A close states more than it is closing", severity: "error", stage: "runtime", since: 1, autofix: false, message: "close was given a quantity of {qty}, and {part} has {held} left to close.", fix: "Leave the quantity out and close() flattens what is left, or size the part from pos.size and keep the quantity at or under {held}.", placeholders: ["qty", "part", "held"] },
|
|
161
|
+
OS7018: { code: "OS7018", title: "A frame names an order this strategy did not place", severity: "error", stage: "host", since: 1, autofix: false, message: "The frame names intent {intent}, and this strategy holds no such order.", fix: "Answer with the intent id the engine sent. A destination's own reference is carried in the frame's reference field, where the engine records it and never parses it, and it is not what an answer is addressed by.", placeholders: ["intent"] },
|
|
162
|
+
OS7019: { code: "OS7019", title: "A fill was reported with no price", severity: "error", stage: "host", since: 1, autofix: false, message: "The frame reports {qty} filled for intent {intent}, and no average fill price.", fix: "Report the average fill price the destination computed over the cumulative quantity, on every frame that reports a quantity greater than the last one. A frame carrying no new quantity needs no price.", placeholders: ["qty", "intent"] },
|
|
157
163
|
OS8001: { code: "OS8001", title: "A stateful call inside a branch", severity: "warning", stage: "check", since: 1, autofix: false, message: "{name} advances only on the bars where this branch runs, and is absent on the rest.", fix: "Compute it unconditionally at the top level and use the result inside the branch.", placeholders: ["name"] },
|
|
158
164
|
OS8002: { code: "OS8002", title: "A higher timeframe read with onUnconfirmed", severity: "warning", stage: "check", since: 1, autofix: false, message: "This file sets onUnconfirmed = true and reads {timeframe}; together they repaint.", fix: "Drop onUnconfirmed = true, or guard every use of the read with bar.isConfirmed.", placeholders: ["timeframe"] },
|
|
159
165
|
OS8003: { code: "OS8003", title: "No version declaration", severity: "warning", stage: "check", since: 1, autofix: true, message: "This file declares no language version; it was compiled as version {version}.", fix: "Add version {version} as the first line of the file.", placeholders: ["version"] },
|
|
@@ -129,6 +129,10 @@ export interface DiagnosticValues {
|
|
|
129
129
|
readonly OS6017: { readonly found: PlaceholderValue; readonly versions: PlaceholderValue };
|
|
130
130
|
readonly OS6018: { readonly location: PlaceholderValue; readonly reason: PlaceholderValue };
|
|
131
131
|
readonly OS6019: { readonly value: PlaceholderValue; readonly key: PlaceholderValue; readonly validation: PlaceholderValue };
|
|
132
|
+
readonly OS6020: { readonly from: PlaceholderValue; readonly to: PlaceholderValue; readonly count: PlaceholderValue };
|
|
133
|
+
readonly OS6021: { readonly setting: PlaceholderValue; readonly problem: PlaceholderValue };
|
|
134
|
+
readonly OS6022: { readonly found: PlaceholderValue; readonly expected: PlaceholderValue };
|
|
135
|
+
readonly OS6023: { readonly commission: PlaceholderValue; readonly commissionType: PlaceholderValue };
|
|
132
136
|
readonly OS7001: { readonly name: PlaceholderValue; readonly line: PlaceholderValue };
|
|
133
137
|
readonly OS7002: { readonly name: PlaceholderValue; readonly argument: PlaceholderValue };
|
|
134
138
|
readonly OS7003: { readonly name: PlaceholderValue };
|
|
@@ -146,6 +150,8 @@ export interface DiagnosticValues {
|
|
|
146
150
|
readonly OS7015: Record<string, never>;
|
|
147
151
|
readonly OS7016: { readonly tag: PlaceholderValue };
|
|
148
152
|
readonly OS7017: { readonly qty: PlaceholderValue; readonly part: PlaceholderValue; readonly held: PlaceholderValue };
|
|
153
|
+
readonly OS7018: { readonly intent: PlaceholderValue };
|
|
154
|
+
readonly OS7019: { readonly qty: PlaceholderValue; readonly intent: PlaceholderValue };
|
|
149
155
|
readonly OS8001: { readonly name: PlaceholderValue };
|
|
150
156
|
readonly OS8002: { readonly timeframe: PlaceholderValue };
|
|
151
157
|
readonly OS8003: { readonly version: PlaceholderValue };
|
package/src/core/check/index.ts
CHANGED
|
@@ -55,13 +55,16 @@ export type { Warmup } from './warmup.js';
|
|
|
55
55
|
export { BAR_ZERO, allOf, atBar, atLeastBar, delayed, earlier, isNever, later } from './warmup.js';
|
|
56
56
|
|
|
57
57
|
export type { LibraryEntry, LibraryParameter, WarmupRule } from './library.js';
|
|
58
|
+
export type { LibraryProse } from './surface.js';
|
|
58
59
|
export {
|
|
59
60
|
NAMESPACES,
|
|
60
61
|
REQUEST_NAMES,
|
|
61
62
|
STRATEGY_NAMESPACES,
|
|
63
|
+
describedNames,
|
|
62
64
|
isLibraryName,
|
|
63
65
|
isNamespace,
|
|
64
66
|
libraryEntries,
|
|
65
67
|
libraryNames,
|
|
66
68
|
membersOf,
|
|
69
|
+
proseFor,
|
|
67
70
|
} from './surface.js';
|
|
@@ -240,7 +240,7 @@ const legs: readonly LibraryEntry[] = [
|
|
|
240
240
|
}),
|
|
241
241
|
entry('leg.stop(name: string, price: number) -> nothing', { ...strategyOnly, planned: true }),
|
|
242
242
|
entry('leg.target(name: string, price: number) -> nothing', { ...strategyOnly, planned: true }),
|
|
243
|
-
entry('leg.trail(name: string, distance: number,
|
|
243
|
+
entry('leg.trail(name: string, distance: number, activateAt?: number = none) -> nothing', {
|
|
244
244
|
...strategyOnly,
|
|
245
245
|
planned: true,
|
|
246
246
|
}),
|
|
@@ -258,7 +258,7 @@ const book: readonly LibraryEntry[] = [
|
|
|
258
258
|
entry('book.stop(amount: number) -> nothing', { ...strategyOnly, planned: true }),
|
|
259
259
|
entry('book.target(amount: number) -> nothing', { ...strategyOnly, planned: true }),
|
|
260
260
|
entry(
|
|
261
|
-
'book.lockProfit(
|
|
261
|
+
'book.lockProfit(activateAt: number, lock: number, step?: number = none, advance?: number = none) -> nothing',
|
|
262
262
|
{ ...strategyOnly, planned: true },
|
|
263
263
|
),
|
|
264
264
|
entry('book.trailStopsToEntry(at: number) -> nothing', { ...strategyOnly, planned: true }),
|