openalgo-script 0.4.0 → 0.6.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 +1050 -0
- package/README.md +111 -27
- 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 +89 -16
- package/dist/adapters/charts/run.js.map +1 -1
- package/dist/adapters/charts/tables.d.ts +6 -19
- package/dist/adapters/charts/tables.d.ts.map +1 -1
- package/dist/adapters/charts/tables.js.map +1 -1
- package/dist/adapters/charts/undrawable.d.ts +4 -0
- package/dist/adapters/charts/undrawable.d.ts.map +1 -0
- package/dist/adapters/charts/undrawable.js +47 -0
- package/dist/adapters/charts/undrawable.js.map +1 -0
- 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/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/equity.d.ts +33 -0
- package/dist/core/accounting/equity.d.ts.map +1 -1
- package/dist/core/accounting/equity.js +12 -0
- package/dist/core/accounting/equity.js.map +1 -1
- package/dist/core/accounting/index.d.ts +2 -0
- package/dist/core/accounting/index.d.ts.map +1 -1
- package/dist/core/accounting/index.js +1 -0
- package/dist/core/accounting/index.js.map +1 -1
- package/dist/core/accounting/report.d.ts +3 -0
- package/dist/core/accounting/report.d.ts.map +1 -1
- package/dist/core/accounting/report.js +2 -0
- package/dist/core/accounting/report.js.map +1 -1
- package/dist/core/accounting/statistics.d.ts +12 -0
- package/dist/core/accounting/statistics.d.ts.map +1 -1
- package/dist/core/accounting/statistics.js +23 -1
- package/dist/core/accounting/statistics.js.map +1 -1
- 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.map +1 -1
- package/dist/core/backtest/compare.js +2 -0
- package/dist/core/backtest/compare.js.map +1 -1
- 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 +101 -8
- package/dist/core/backtest/drive.d.ts.map +1 -1
- package/dist/core/backtest/drive.js +114 -158
- package/dist/core/backtest/drive.js.map +1 -1
- package/dist/core/backtest/index.d.ts +26 -12
- package/dist/core/backtest/index.d.ts.map +1 -1
- package/dist/core/backtest/index.js +21 -10
- package/dist/core/backtest/index.js.map +1 -1
- package/dist/core/backtest/record.d.ts +63 -2
- package/dist/core/backtest/record.d.ts.map +1 -1
- package/dist/core/backtest/record.js +71 -4
- package/dist/core/backtest/record.js.map +1 -1
- package/dist/core/backtest/replay.d.ts.map +1 -1
- package/dist/core/backtest/replay.js +11 -1
- package/dist/core/backtest/replay.js.map +1 -1
- package/dist/core/backtest/simulate.d.ts +113 -1
- package/dist/core/backtest/simulate.d.ts.map +1 -1
- package/dist/core/backtest/simulate.js +123 -5
- package/dist/core/backtest/simulate.js.map +1 -1
- package/dist/core/backtest/walk.d.ts +39 -0
- package/dist/core/backtest/walk.d.ts.map +1 -0
- package/dist/core/backtest/walk.js +125 -0
- package/dist/core/backtest/walk.js.map +1 -0
- package/dist/core/catalogue/catalogue.generated.d.ts +179 -2
- package/dist/core/catalogue/catalogue.generated.d.ts.map +1 -1
- package/dist/core/catalogue/catalogue.generated.js +19 -2
- package/dist/core/catalogue/catalogue.generated.js.map +1 -1
- package/dist/core/catalogue/types.d.ts +8 -2
- package/dist/core/catalogue/types.d.ts.map +1 -1
- package/dist/core/catalogue/values.generated.d.ts +51 -0
- package/dist/core/catalogue/values.generated.d.ts.map +1 -1
- package/dist/core/check/assigned.d.ts +19 -0
- package/dist/core/check/assigned.d.ts.map +1 -0
- package/dist/core/check/assigned.js +14 -0
- package/dist/core/check/assigned.js.map +1 -0
- package/dist/core/check/call-sites.d.ts.map +1 -1
- package/dist/core/check/call-sites.js +9 -4
- package/dist/core/check/call-sites.js.map +1 -1
- package/dist/core/check/calls.d.ts.map +1 -1
- package/dist/core/check/calls.js +12 -4
- package/dist/core/check/calls.js.map +1 -1
- package/dist/core/check/check.d.ts.map +1 -1
- package/dist/core/check/check.js +15 -2
- package/dist/core/check/check.js.map +1 -1
- package/dist/core/check/checker.d.ts +26 -0
- package/dist/core/check/checker.d.ts.map +1 -1
- package/dist/core/check/checker.js +38 -2
- package/dist/core/check/checker.js.map +1 -1
- package/dist/core/check/constant.d.ts +16 -0
- package/dist/core/check/constant.d.ts.map +1 -1
- package/dist/core/check/constant.js +52 -0
- package/dist/core/check/constant.js.map +1 -1
- package/dist/core/check/deleted.d.ts +3 -0
- package/dist/core/check/deleted.d.ts.map +1 -0
- package/dist/core/check/deleted.js +109 -0
- package/dist/core/check/deleted.js.map +1 -0
- package/dist/core/check/expressions.js +1 -1
- package/dist/core/check/expressions.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-output.d.ts.map +1 -1
- package/dist/core/check/library-output.js +1 -0
- package/dist/core/check/library-output.js.map +1 -1
- package/dist/core/check/library-prose.generated.js +12 -12
- package/dist/core/check/library-prose.generated.js.map +1 -1
- package/dist/core/check/library.d.ts +7 -0
- package/dist/core/check/library.d.ts.map +1 -1
- package/dist/core/check/library.js +1 -0
- package/dist/core/check/library.js.map +1 -1
- package/dist/core/check/surface.d.ts +9 -0
- package/dist/core/check/surface.d.ts.map +1 -1
- package/dist/core/check/surface.js +13 -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/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/bar-source.d.ts +65 -0
- package/dist/core/engine/bar-source.d.ts.map +1 -0
- package/dist/core/engine/bar-source.js +58 -0
- package/dist/core/engine/bar-source.js.map +1 -0
- package/dist/core/engine/engine.d.ts +9 -11
- package/dist/core/engine/engine.d.ts.map +1 -1
- package/dist/core/engine/engine.js +24 -24
- package/dist/core/engine/engine.js.map +1 -1
- package/dist/core/engine/guard.d.ts.map +1 -1
- package/dist/core/engine/guard.js +10 -0
- package/dist/core/engine/guard.js.map +1 -1
- package/dist/core/engine/host.d.ts +4 -2
- package/dist/core/engine/host.d.ts.map +1 -1
- package/dist/core/engine/host.js.map +1 -1
- package/dist/core/engine/index.d.ts +2 -1
- package/dist/core/engine/index.d.ts.map +1 -1
- package/dist/core/engine/index.js +1 -1
- 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/binding.d.ts +2 -0
- package/dist/core/engine/library/binding.d.ts.map +1 -1
- package/dist/core/engine/library/binding.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 +13 -0
- package/dist/core/engine/library/index.d.ts.map +1 -1
- package/dist/core/engine/library/index.js +15 -0
- package/dist/core/engine/library/index.js.map +1 -1
- package/dist/core/engine/library/objects.d.ts.map +1 -1
- package/dist/core/engine/library/objects.js +1 -1
- package/dist/core/engine/library/objects.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 +20 -0
- package/dist/core/engine/load.d.ts.map +1 -1
- package/dist/core/engine/load.js +62 -0
- package/dist/core/engine/load.js.map +1 -1
- package/dist/core/engine/request-body.d.ts +3 -1
- package/dist/core/engine/request-body.d.ts.map +1 -1
- package/dist/core/engine/request-body.js +3 -1
- package/dist/core/engine/request-body.js.map +1 -1
- package/dist/core/engine/request-plan.d.ts +3 -3
- package/dist/core/engine/request-plan.js +3 -3
- package/dist/core/engine/requests.d.ts +2 -2
- package/dist/core/engine/requests.d.ts.map +1 -1
- package/dist/core/engine/requests.js +5 -4
- package/dist/core/engine/requests.js.map +1 -1
- package/dist/core/engine/series.d.ts +18 -6
- package/dist/core/engine/series.d.ts.map +1 -1
- package/dist/core/engine/series.js +24 -5
- package/dist/core/engine/series.js.map +1 -1
- package/dist/core/engine/session/record.d.ts +4 -4
- package/dist/core/engine/session/record.js +4 -4
- 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/importer/calls.d.ts +29 -0
- package/dist/core/importer/calls.d.ts.map +1 -0
- package/dist/core/importer/calls.js +267 -0
- package/dist/core/importer/calls.js.map +1 -0
- package/dist/core/importer/context.d.ts +118 -0
- package/dist/core/importer/context.d.ts.map +1 -0
- package/dist/core/importer/context.js +120 -0
- package/dist/core/importer/context.js.map +1 -0
- package/dist/core/importer/expressions.d.ts +44 -0
- package/dist/core/importer/expressions.d.ts.map +1 -0
- package/dist/core/importer/expressions.js +223 -0
- package/dist/core/importer/expressions.js.map +1 -0
- package/dist/core/importer/importer.d.ts +13 -0
- package/dist/core/importer/importer.d.ts.map +1 -0
- package/dist/core/importer/importer.js +220 -0
- package/dist/core/importer/importer.js.map +1 -0
- package/dist/core/importer/index.d.ts +20 -0
- package/dist/core/importer/index.d.ts.map +1 -0
- package/dist/core/importer/index.js +18 -0
- package/dist/core/importer/index.js.map +1 -0
- package/dist/core/importer/lexer.d.ts +68 -0
- package/dist/core/importer/lexer.d.ts.map +1 -0
- package/dist/core/importer/lexer.js +207 -0
- package/dist/core/importer/lexer.js.map +1 -0
- package/dist/core/importer/names.d.ts +41 -0
- package/dist/core/importer/names.d.ts.map +1 -0
- package/dist/core/importer/names.js +78 -0
- package/dist/core/importer/names.js.map +1 -0
- package/dist/core/importer/orders.d.ts +8 -0
- package/dist/core/importer/orders.d.ts.map +1 -0
- package/dist/core/importer/orders.js +332 -0
- package/dist/core/importer/orders.js.map +1 -0
- package/dist/core/importer/outputs.d.ts +19 -0
- package/dist/core/importer/outputs.d.ts.map +1 -0
- package/dist/core/importer/outputs.js +266 -0
- package/dist/core/importer/outputs.js.map +1 -0
- package/dist/core/importer/parser.d.ts +5 -0
- package/dist/core/importer/parser.d.ts.map +1 -0
- package/dist/core/importer/parser.js +439 -0
- package/dist/core/importer/parser.js.map +1 -0
- package/dist/core/importer/presence.d.ts +32 -0
- package/dist/core/importer/presence.d.ts.map +1 -0
- package/dist/core/importer/presence.js +146 -0
- package/dist/core/importer/presence.js.map +1 -0
- package/dist/core/importer/settle.d.ts +23 -0
- package/dist/core/importer/settle.d.ts.map +1 -0
- package/dist/core/importer/settle.js +135 -0
- package/dist/core/importer/settle.js.map +1 -0
- package/dist/core/importer/statements.d.ts +4 -0
- package/dist/core/importer/statements.d.ts.map +1 -0
- package/dist/core/importer/statements.js +339 -0
- package/dist/core/importer/statements.js.map +1 -0
- package/dist/core/importer/syntax.d.ts +182 -0
- package/dist/core/importer/syntax.d.ts.map +1 -0
- package/dist/core/importer/syntax.js +14 -0
- package/dist/core/importer/syntax.js.map +1 -0
- package/dist/core/importer/table.d.ts +52 -0
- package/dist/core/importer/table.d.ts.map +1 -0
- package/dist/core/importer/table.js +156 -0
- package/dist/core/importer/table.js.map +1 -0
- package/dist/core/importer/values.d.ts +27 -0
- package/dist/core/importer/values.d.ts.map +1 -0
- package/dist/core/importer/values.js +236 -0
- package/dist/core/importer/values.js.map +1 -0
- package/dist/core/index.d.ts +22 -5
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +19 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/stdlib/calendar/civil.d.ts.map +1 -1
- package/dist/core/stdlib/calendar/civil.js +5 -2
- package/dist/core/stdlib/calendar/civil.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/package.json +17 -2
- package/spec/README.md +2 -1
- package/spec/errors.json +383 -23
- package/src/adapters/charts/driving.ts +109 -0
- package/src/adapters/charts/run.ts +126 -28
- package/src/adapters/charts/tables.ts +6 -19
- package/src/adapters/charts/undrawable.ts +51 -0
- package/src/adapters/charts/venue.ts +132 -0
- package/src/core/accounting/analysis.ts +188 -0
- package/src/core/accounting/equity.ts +38 -0
- package/src/core/accounting/index.ts +2 -0
- package/src/core/accounting/report.ts +5 -0
- package/src/core/accounting/statistics.ts +38 -1
- package/src/core/backtest/case.ts +395 -0
- package/src/core/backtest/compare.ts +2 -0
- package/src/core/backtest/deliver.ts +161 -0
- package/src/core/backtest/drive.ts +233 -217
- package/src/core/backtest/index.ts +26 -12
- package/src/core/backtest/record.ts +134 -5
- package/src/core/backtest/replay.ts +11 -1
- package/src/core/backtest/simulate.ts +201 -6
- package/src/core/backtest/walk.ts +182 -0
- package/src/core/catalogue/catalogue.generated.ts +19 -2
- package/src/core/catalogue/types.ts +8 -2
- package/src/core/catalogue/values.generated.ts +16 -0
- package/src/core/check/assigned.ts +31 -0
- package/src/core/check/call-sites.ts +11 -4
- package/src/core/check/calls.ts +13 -4
- package/src/core/check/check.ts +14 -2
- package/src/core/check/checker.ts +39 -2
- package/src/core/check/constant.ts +57 -0
- package/src/core/check/deleted.ts +135 -0
- package/src/core/check/expressions.ts +1 -1
- package/src/core/check/library-orders.ts +2 -2
- package/src/core/check/library-output.ts +1 -0
- package/src/core/check/library-prose.generated.ts +12 -12
- package/src/core/check/library.ts +8 -0
- package/src/core/check/surface.ts +16 -0
- package/src/core/emit/canonical.ts +67 -9
- package/src/core/engine/arithmetic.ts +23 -3
- package/src/core/engine/bar-source.ts +113 -0
- package/src/core/engine/engine.ts +26 -25
- package/src/core/engine/guard.ts +12 -1
- package/src/core/engine/host.ts +4 -2
- package/src/core/engine/index.ts +2 -1
- package/src/core/engine/library/arrays.ts +8 -2
- package/src/core/engine/library/binding.ts +2 -0
- package/src/core/engine/library/code-points.ts +73 -0
- package/src/core/engine/library/index.ts +17 -0
- package/src/core/engine/library/objects.ts +1 -6
- package/src/core/engine/library/text.ts +53 -35
- package/src/core/engine/load.ts +68 -0
- package/src/core/engine/request-body.ts +5 -7
- package/src/core/engine/request-plan.ts +3 -3
- package/src/core/engine/requests.ts +11 -8
- package/src/core/engine/series.ts +30 -6
- package/src/core/engine/session/record.ts +4 -4
- package/src/core/engine/verify-tables.ts +43 -0
- package/src/core/importer/calls.ts +274 -0
- package/src/core/importer/context.ts +197 -0
- package/src/core/importer/expressions.ts +247 -0
- package/src/core/importer/importer.ts +226 -0
- package/src/core/importer/index.ts +19 -0
- package/src/core/importer/lexer.ts +285 -0
- package/src/core/importer/names.ts +104 -0
- package/src/core/importer/orders.ts +307 -0
- package/src/core/importer/outputs.ts +245 -0
- package/src/core/importer/parser.ts +436 -0
- package/src/core/importer/presence.ts +164 -0
- package/src/core/importer/settle.ts +141 -0
- package/src/core/importer/statements.ts +331 -0
- package/src/core/importer/syntax.ts +144 -0
- package/src/core/importer/table.ts +191 -0
- package/src/core/importer/values.ts +241 -0
- package/src/core/index.ts +29 -2
- package/src/core/stdlib/calendar/civil.ts +5 -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
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walking the bars, for a study and for a strategy.
|
|
3
|
+
*
|
|
4
|
+
* Split from `run.ts` because they are two things. That file decides what an
|
|
5
|
+
* engine is loaded with: the host record, the clock, the settings, the venue.
|
|
6
|
+
* This one decides how the bars are then handed over, and the two differ by
|
|
7
|
+
* exactly one thing, whether frames have to reach the engine between them.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { CompiledProgram } from '../../core/emit/index.js';
|
|
11
|
+
import type { Engine } from '../../core/engine/index.js';
|
|
12
|
+
import { hostBar, stateFor } from './bars.js';
|
|
13
|
+
import type { ChartBar, ChartCalcContext, ChartSettings } from './contract.js';
|
|
14
|
+
import { stopped } from './errors.js';
|
|
15
|
+
import { signatureOf } from './settings.js';
|
|
16
|
+
import { answersFor } from './venue.js';
|
|
17
|
+
import type { HeldVenue } from './venue.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A run in hand, held between a chart's recomputes.
|
|
21
|
+
*
|
|
22
|
+
* The signature and the bar times are what a tail run is checked against before
|
|
23
|
+
* it is trusted: splicing a tail onto a history that has changed underneath it
|
|
24
|
+
* draws a wrong study that looks entirely plausible.
|
|
25
|
+
*/
|
|
26
|
+
export interface Held {
|
|
27
|
+
readonly engine: Engine;
|
|
28
|
+
readonly signature: string;
|
|
29
|
+
count: number;
|
|
30
|
+
firstTime: number;
|
|
31
|
+
lastTime: number;
|
|
32
|
+
/**
|
|
33
|
+
* The destination this run's orders go to, for a strategy.
|
|
34
|
+
*
|
|
35
|
+
* Held with the engine because the two are one run: the venue holds the
|
|
36
|
+
* orders that have not filled yet, and an engine continued onto a new bar
|
|
37
|
+
* against a fresh venue would have its working orders silently forgotten.
|
|
38
|
+
*/
|
|
39
|
+
readonly venue: HeldVenue | null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A study: every bar in one hand-over, which is what `run` is for. */
|
|
43
|
+
export function runWhole(
|
|
44
|
+
engine: Engine,
|
|
45
|
+
bars: readonly ChartBar[],
|
|
46
|
+
ctx: ChartCalcContext | undefined,
|
|
47
|
+
program: CompiledProgram,
|
|
48
|
+
settings: ChartSettings,
|
|
49
|
+
): Held {
|
|
50
|
+
const result = engine.run(
|
|
51
|
+
bars.map(hostBar),
|
|
52
|
+
bars.map((_, index) => stateFor(index, bars, ctx)),
|
|
53
|
+
);
|
|
54
|
+
if (result.diagnostic !== undefined) throw stopped(result.diagnostic);
|
|
55
|
+
return heldFrom(engine, null, bars, program, settings);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A strategy: one bar at a time, with the venue answering between them.
|
|
60
|
+
*
|
|
61
|
+
* The order is deliver, execute, then ask. A driver that asked the venue before
|
|
62
|
+
* executing would price a fill against a bar the strategy had not seen yet, and
|
|
63
|
+
* one that delivered after executing would let a script read a position its own
|
|
64
|
+
* order on this bar had just created.
|
|
65
|
+
*
|
|
66
|
+
* `supplied` is the whole count rather than the index reached, so `bar.isLast`
|
|
67
|
+
* means the same on this path as on the one above it: a strategy written to act
|
|
68
|
+
* on the final bar acts on the final bar, not on every bar in turn.
|
|
69
|
+
*/
|
|
70
|
+
export function walkWith(
|
|
71
|
+
engine: Engine,
|
|
72
|
+
held: HeldVenue,
|
|
73
|
+
bars: readonly ChartBar[],
|
|
74
|
+
ctx: ChartCalcContext | undefined,
|
|
75
|
+
program: CompiledProgram,
|
|
76
|
+
settings: ChartSettings,
|
|
77
|
+
): Held {
|
|
78
|
+
for (let index = 0; index < bars.length; index += 1) {
|
|
79
|
+
const bar = bars[index];
|
|
80
|
+
if (bar === undefined) continue;
|
|
81
|
+
|
|
82
|
+
for (const frame of held.pending) engine.deliver(frame);
|
|
83
|
+
held.pending = [];
|
|
84
|
+
|
|
85
|
+
const result = engine.append(hostBar(bar), stateFor(index, bars, ctx), bars.length);
|
|
86
|
+
if (result.diagnostic !== undefined) throw stopped(result.diagnostic);
|
|
87
|
+
|
|
88
|
+
answersFor(held, index);
|
|
89
|
+
}
|
|
90
|
+
return heldFrom(engine, held, bars, program, settings);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function heldFrom(
|
|
94
|
+
engine: Engine,
|
|
95
|
+
venue: HeldVenue | null,
|
|
96
|
+
bars: readonly ChartBar[],
|
|
97
|
+
program: CompiledProgram,
|
|
98
|
+
settings: ChartSettings,
|
|
99
|
+
): Held {
|
|
100
|
+
return {
|
|
101
|
+
engine,
|
|
102
|
+
venue,
|
|
103
|
+
signature: signatureOf(program, settings),
|
|
104
|
+
count: bars.length,
|
|
105
|
+
firstTime: bars[0]?.time ?? 0,
|
|
106
|
+
lastTime: bars[bars.length - 1]?.time ?? 0,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
@@ -39,8 +39,14 @@ import type { SourceFile } from '../../core/index.js';
|
|
|
39
39
|
import { hostBar, hostNow, stateFor } from './bars.js';
|
|
40
40
|
import type { ChartBar, ChartCalcContext, ChartSettings, ChartStore } from './contract.js';
|
|
41
41
|
import { refused, stopped } from './errors.js';
|
|
42
|
+
import { undrawable } from './undrawable.js';
|
|
42
43
|
import { stationIn, stationOf } from './requests.js';
|
|
43
44
|
import { engineSettings, signatureOf } from './settings.js';
|
|
45
|
+
import { answersFor, needsVenue, routeInto, venueFor } from './venue.js';
|
|
46
|
+
import type { HeldVenue } from './venue.js';
|
|
47
|
+
import { runWhole, walkWith } from './driving.js';
|
|
48
|
+
import type { Held } from './driving.js';
|
|
49
|
+
import type { Simulator } from '../../core/backtest/index.js';
|
|
44
50
|
|
|
45
51
|
/** What a host tells the adapter that neither the chart nor the program says. */
|
|
46
52
|
export interface ChartAdapterOptions {
|
|
@@ -116,6 +122,26 @@ export interface ChartAdapterOptions {
|
|
|
116
122
|
* (`stdlib.md` 17.1), never handed over by the chart.
|
|
117
123
|
*/
|
|
118
124
|
readonly orders?: EffectRoute;
|
|
125
|
+
/**
|
|
126
|
+
* Run a strategy against a simulated destination where the host gives none.
|
|
127
|
+
*
|
|
128
|
+
* **Off by default, because the refusal it replaces is useful.** A strategy
|
|
129
|
+
* with nowhere to send an order is refused at load with OS6006 naming the
|
|
130
|
+
* capability, which is exactly what a host that meant to wire a destination
|
|
131
|
+
* and forgot needs to be told. A chart that quietly filled one in for them
|
|
132
|
+
* would draw a convincing strategy that routed nothing.
|
|
133
|
+
*
|
|
134
|
+
* **On, it is the venue a backtest uses.** A chart drawing a strategy then
|
|
135
|
+
* fills the same orders at the same prices as the report of the same script,
|
|
136
|
+
* so the marks on the price and the trades in the report are one answer
|
|
137
|
+
* rather than two. This is what a host turns on to draw a strategy the way it
|
|
138
|
+
* draws a study: the plots, the legend row and the settings all follow from
|
|
139
|
+
* the program running at all.
|
|
140
|
+
*
|
|
141
|
+
* It does not place an order anywhere. A host that wants that supplies
|
|
142
|
+
* `orders`, which wins over this.
|
|
143
|
+
*/
|
|
144
|
+
readonly simulateOrders?: boolean;
|
|
119
145
|
readonly limits?: Partial<EngineLimits>;
|
|
120
146
|
readonly clock?: Clock;
|
|
121
147
|
/**
|
|
@@ -130,14 +156,6 @@ export interface ChartAdapterOptions {
|
|
|
130
156
|
}
|
|
131
157
|
|
|
132
158
|
/** The engine one chart instance is holding, between recomputes. */
|
|
133
|
-
interface Held {
|
|
134
|
-
readonly engine: Engine;
|
|
135
|
-
readonly signature: string;
|
|
136
|
-
count: number;
|
|
137
|
-
firstTime: number;
|
|
138
|
-
lastTime: number;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
159
|
/** The store key. Namespaced, because the store belongs to the host as well. */
|
|
142
160
|
const HELD = 'openscript';
|
|
143
161
|
|
|
@@ -168,20 +186,20 @@ export function fullRun(
|
|
|
168
186
|
options: ChartAdapterOptions,
|
|
169
187
|
): RunOutput {
|
|
170
188
|
const station = stationIn(store);
|
|
171
|
-
const
|
|
172
|
-
const
|
|
173
|
-
bars.map(hostBar),
|
|
174
|
-
bars.map((_, index) => stateFor(index, bars, ctx)),
|
|
175
|
-
);
|
|
176
|
-
if (result.diagnostic !== undefined) throw stopped(result.diagnostic);
|
|
189
|
+
const started = start(program, settings, ctx, options, station.provider(bars), bars);
|
|
190
|
+
const engine = started.engine;
|
|
177
191
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
192
|
+
// **A strategy is walked bar by bar and a study is handed the lot.** The
|
|
193
|
+
// difference is the venue: its frames reach the engine between bars, which is
|
|
194
|
+
// the only place they can, so a strategy's position is right on the bar after
|
|
195
|
+
// the one it traded on. `run()` has no gap to put them in.
|
|
196
|
+
//
|
|
197
|
+
// It is also the loop the backtest uses, which is the point: a chart drawing a
|
|
198
|
+
// strategy and a report of the same strategy walk the same bars in the same
|
|
199
|
+
// order against the same venue, so they cannot disagree about what filled.
|
|
200
|
+
const held: Held = started.venue === null
|
|
201
|
+
? runWhole(engine, bars, ctx, program, settings)
|
|
202
|
+
: walkWith(engine, started.venue, bars, ctx, program, settings);
|
|
185
203
|
store[HELD] = held;
|
|
186
204
|
station.settle();
|
|
187
205
|
return outputOf(program, engine);
|
|
@@ -218,11 +236,29 @@ export function tailRun(
|
|
|
218
236
|
const bar = bars[index];
|
|
219
237
|
if (bar === undefined) return null;
|
|
220
238
|
const state = stateFor(index, bars, ctx);
|
|
239
|
+
|
|
240
|
+
// The venue's answers about the bar before this one, delivered before this
|
|
241
|
+
// one runs, exactly as the full walk does. A tail that skipped this would
|
|
242
|
+
// draw the first bars of a strategy correctly and then quietly stop folding
|
|
243
|
+
// its fills the moment the chart went live, which is the half of the run
|
|
244
|
+
// nobody re-checks.
|
|
245
|
+
//
|
|
246
|
+
// Only when the bar is new. Re-executing the bar that moved must not
|
|
247
|
+
// deliver again: a frame is cumulative and folding one twice is harmless,
|
|
248
|
+
// but the bar's own orders have been rolled back and answering them a
|
|
249
|
+
// second time would fill an order the engine no longer knows it sent.
|
|
250
|
+
if (held.venue !== null && index !== from) {
|
|
251
|
+
for (const frame of held.venue.pending) held.engine.deliver(frame);
|
|
252
|
+
held.venue.pending = [];
|
|
253
|
+
}
|
|
254
|
+
|
|
221
255
|
const result =
|
|
222
256
|
index === from
|
|
223
257
|
? held.engine.update(hostBar(bar), state)
|
|
224
258
|
: held.engine.append(hostBar(bar), state);
|
|
225
259
|
if (result.diagnostic !== undefined) throw stopped(result.diagnostic);
|
|
260
|
+
|
|
261
|
+
if (held.venue !== null) answersFor(held.venue, index);
|
|
226
262
|
}
|
|
227
263
|
|
|
228
264
|
held.count = bars.length;
|
|
@@ -257,23 +293,70 @@ function outputOf(program: CompiledProgram, engine: Engine): RunOutput {
|
|
|
257
293
|
return { columns, tables: engine.tables(), drawings: engine.drawings() };
|
|
258
294
|
}
|
|
259
295
|
|
|
296
|
+
/** A loaded engine, and the destination its orders go to where it has one. */
|
|
297
|
+
interface Started {
|
|
298
|
+
readonly engine: Engine;
|
|
299
|
+
readonly venue: HeldVenue | null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Load the program, and give a strategy somewhere for its orders to go.
|
|
304
|
+
*
|
|
305
|
+
* **The venue is built after the load and reached through a holder, because
|
|
306
|
+
* neither can come first.** The route has to be handed to `load`, since that is
|
|
307
|
+
* what declares the `orders` capability and a program needing one is otherwise
|
|
308
|
+
* refused. The venue has to be built after it, because the declaration it reads
|
|
309
|
+
* may state its fill rule through an `input()`, and inputs are not resolved
|
|
310
|
+
* until `load` has run. The route is only ever called from inside an execution,
|
|
311
|
+
* which is after both, so the holder is always filled by the time anything
|
|
312
|
+
* reaches it.
|
|
313
|
+
*
|
|
314
|
+
* **A host that supplied its own route keeps it.** Somewhere real to send an
|
|
315
|
+
* order is a better destination than a simulated one, and a host that wired one
|
|
316
|
+
* up meant it.
|
|
317
|
+
*
|
|
318
|
+
* **And simulation is asked for rather than assumed.** Without
|
|
319
|
+
* `simulateOrders` a strategy with nowhere to send an order is still refused at
|
|
320
|
+
* load, which is the answer a host that meant to wire a destination and forgot
|
|
321
|
+
* needs to see. Filling in a venue for them would turn that mistake into a
|
|
322
|
+
* chart that draws convincingly and routes nothing, discovered whenever
|
|
323
|
+
* somebody next looked for the orders.
|
|
324
|
+
*/
|
|
260
325
|
function start(
|
|
261
326
|
program: CompiledProgram,
|
|
262
327
|
settings: ChartSettings,
|
|
263
328
|
ctx: ChartCalcContext | undefined,
|
|
264
329
|
options: ChartAdapterOptions,
|
|
265
330
|
requests: RequestProvider,
|
|
266
|
-
|
|
331
|
+
bars: readonly ChartBar[],
|
|
332
|
+
): Started {
|
|
333
|
+
const holder: { current: Simulator | null } = { current: null };
|
|
334
|
+
const simulate = options.simulateOrders === true && options.orders === undefined && needsVenue(program);
|
|
335
|
+
const withRoute: ChartAdapterOptions = simulate
|
|
336
|
+
? { ...options, orders: routeInto(holder) }
|
|
337
|
+
: options;
|
|
338
|
+
|
|
339
|
+
// What this chart has no room for is refused before the engine is asked,
|
|
340
|
+
// because a study drawn without it is a study that looks broken.
|
|
341
|
+
const narrower = undrawable(program);
|
|
342
|
+
if (narrower !== undefined) throw refused(narrower);
|
|
343
|
+
|
|
267
344
|
const loaded = load(program, {
|
|
268
345
|
settings: engineSettings(program, settings),
|
|
269
|
-
host: hostFor(ctx,
|
|
346
|
+
host: hostFor(ctx, withRoute, requests),
|
|
270
347
|
time: timeFor(options, ctx?.timezone ?? ''),
|
|
271
348
|
...(options.limits === undefined ? {} : { limits: options.limits }),
|
|
272
349
|
...(options.source === undefined ? {} : { source: options.source }),
|
|
273
350
|
...(options.clock === undefined ? {} : { clock: options.clock }),
|
|
274
351
|
});
|
|
275
352
|
if (!loaded.ok) throw refused(loaded.diagnostic);
|
|
276
|
-
|
|
353
|
+
|
|
354
|
+
if (!simulate) return { engine: loaded.engine, venue: null };
|
|
355
|
+
|
|
356
|
+
const venue = venueFor(program, bars, loaded.inputs, instrumentFor(ctx, options));
|
|
357
|
+
if (venue === null) return { engine: loaded.engine, venue: null };
|
|
358
|
+
holder.current = venue;
|
|
359
|
+
return { engine: loaded.engine, venue: { venue, pending: [] } };
|
|
277
360
|
}
|
|
278
361
|
|
|
279
362
|
/**
|
|
@@ -286,12 +369,19 @@ function start(
|
|
|
286
369
|
* the chart's own sentence in `req.error`, instead of being refused at load
|
|
287
370
|
* with OS6006 naming a capability the chart does have.
|
|
288
371
|
*/
|
|
289
|
-
|
|
372
|
+
/**
|
|
373
|
+
* The instrument record, from what the host stated and what the chart knows.
|
|
374
|
+
*
|
|
375
|
+
* Its own function because two callers need it and they must not each build
|
|
376
|
+
* one: the engine is loaded with this record, and the venue prices fills
|
|
377
|
+
* against the tick size in it. Two spellings of the same record is how a fill
|
|
378
|
+
* gets rounded to a tick the strategy was never told about.
|
|
379
|
+
*/
|
|
380
|
+
function instrumentFor(
|
|
290
381
|
ctx: ChartCalcContext | undefined,
|
|
291
382
|
options: ChartAdapterOptions,
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const instrument: Instrument = {
|
|
383
|
+
): Instrument {
|
|
384
|
+
return {
|
|
295
385
|
...(options.instrument ?? {}),
|
|
296
386
|
...(ctx?.symbol === undefined ? {} : { symbol: ctx.symbol }),
|
|
297
387
|
...(ctx?.interval === undefined ? {} : { interval: ctx.interval }),
|
|
@@ -302,6 +392,14 @@ function hostFor(
|
|
|
302
392
|
// the chart it was drawn on.
|
|
303
393
|
...(ctx === undefined || ctx.timezone === '' ? {} : { timezone: ctx.timezone }),
|
|
304
394
|
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function hostFor(
|
|
398
|
+
ctx: ChartCalcContext | undefined,
|
|
399
|
+
options: ChartAdapterOptions,
|
|
400
|
+
requests: RequestProvider,
|
|
401
|
+
): EngineHost {
|
|
402
|
+
const instrument = instrumentFor(ctx, options);
|
|
305
403
|
return {
|
|
306
404
|
instrument,
|
|
307
405
|
requestBars: requests,
|
|
@@ -28,25 +28,12 @@
|
|
|
28
28
|
* direction: a cell nothing was written into is blank, never a zero.
|
|
29
29
|
*
|
|
30
30
|
* **A chart pane holds one grid and the language declares as many as it likes.**
|
|
31
|
-
* The descriptor has one `table` hook,
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* and
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* **What ought to happen is a refusal, and it cannot be written yet.** A study
|
|
38
|
-
* whose second grid never appears is a study with no explanation, so the second
|
|
39
|
-
* `table()` should be refused where it is written, naming the limit. Every
|
|
40
|
-
* diagnostic in this project carries a catalogue code and the catalogue has
|
|
41
|
-
* none for "the chart cannot draw something this study declares"; inventing one
|
|
42
|
-
* here is the one thing that is not allowed, so the gap is reported instead, in
|
|
43
|
-
* `issues/0011-a-second-declared-grid-is-dropped-with-nothing-said.md`, with the
|
|
44
|
-
* sentence it should carry.
|
|
45
|
-
*
|
|
46
|
-
* Until it lands, the drop is at least no longer silent inside this repository:
|
|
47
|
-
* it is recorded in `spec/chart-narrowings.json` with its reason, and
|
|
48
|
-
* `scripts/check-chart-surface.mjs` fails the build if this adapter ever drops
|
|
49
|
-
* anything else a compiled program declares without recording it there.
|
|
31
|
+
* The descriptor has one `table` hook, and merging two grids into it would put
|
|
32
|
+
* cells somewhere the script never asked for. So a program declaring a second
|
|
33
|
+
* grid never reaches this file: `undrawable.ts` refuses it before any bar runs,
|
|
34
|
+
* with OS6024, and this reads the one grid a program that runs can have.
|
|
35
|
+
* `spec/chart-narrowings.json` records the refusal and
|
|
36
|
+
* `scripts/check-chart-surface.mjs` proves it with a study that declares two.
|
|
50
37
|
*/
|
|
51
38
|
import type { CompiledProgram, Grid as DeclaredGrid } from '../../core/emit/index.js';
|
|
52
39
|
import type { Grid, GridCell } from '../../core/engine/index.js';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What this chart cannot draw of what a program declares, refused before a bar.
|
|
3
|
+
*
|
|
4
|
+
* `compiled-program.md` section 11: a host draws what its surface has room for,
|
|
5
|
+
* and refuses what it does not with OS6024 rather than drawing part of a study
|
|
6
|
+
* and saying nothing. Two things a version 1 program can declare have no place
|
|
7
|
+
* on this chart's surface, and both used to be dropped in silence.
|
|
8
|
+
*
|
|
9
|
+
* - **A second grid.** The descriptor has one `table` hook, so one grid reaches
|
|
10
|
+
* a pane. Merging two into it would put cells somewhere the script never
|
|
11
|
+
* asked for, and drawing the first alone is a study whose second panel never
|
|
12
|
+
* appears and whose cells look broken.
|
|
13
|
+
* - **A band colour computed per bar.** The chart's band takes one colour for
|
|
14
|
+
* the whole run and has no channel to point at, so a band whose colour the
|
|
15
|
+
* script computes was drawn in the first plot's colour faded, which is a
|
|
16
|
+
* colour the script did not choose.
|
|
17
|
+
*
|
|
18
|
+
* The compiled program carries no source position for a declaration, so the
|
|
19
|
+
* refusal names the declaration by its title, which is what a reader sees in
|
|
20
|
+
* the legend and the settings dialog, and its span is the load's own.
|
|
21
|
+
*/
|
|
22
|
+
import { diagnosticFor, makeSpan } from '../../core/index.js';
|
|
23
|
+
import type { Diagnostic } from '../../core/index.js';
|
|
24
|
+
import type { CompiledProgram, Field } from '../../core/emit/index.js';
|
|
25
|
+
|
|
26
|
+
/** A load refusal has no line of its own: the program is what was refused. */
|
|
27
|
+
const AT_LOAD = makeSpan(0, 0, 0, 0);
|
|
28
|
+
|
|
29
|
+
export function undrawable(program: CompiledProgram): Diagnostic | undefined {
|
|
30
|
+
const second = program.outputs.tables[1];
|
|
31
|
+
if (second !== undefined) {
|
|
32
|
+
return diagnosticFor('OS6024', AT_LOAD, {
|
|
33
|
+
what: `the table ${titled(second.title, 'after the first')}, the second grid this study declares`,
|
|
34
|
+
limit: 'a chart pane draws one grid',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
for (const band of program.outputs.fills) {
|
|
38
|
+
if (band.colorUpChannel !== null || band.colorDownChannel !== null) {
|
|
39
|
+
return diagnosticFor('OS6024', AT_LOAD, {
|
|
40
|
+
what: "a band's colour that is computed per bar",
|
|
41
|
+
limit: "a chart's band takes one colour for the whole run",
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A title as a reader sees it, or a description where it is a setting's. */
|
|
49
|
+
function titled(title: Field, otherwise: string): string {
|
|
50
|
+
return typeof title === 'string' ? `"${title}"` : otherwise;
|
|
51
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The destination a strategy runs against when a chart draws one.
|
|
3
|
+
*
|
|
4
|
+
* **Why a chart needs one at all.** A strategy asks a destination to do things
|
|
5
|
+
* and folds its position from what comes back. Handed no destination it is
|
|
6
|
+
* refused at load with OS6006, and handed one that answers nothing it runs but
|
|
7
|
+
* never learns it is in a position: every `close()` closes nothing, every entry
|
|
8
|
+
* is allowed again on the next signal, and the study draws a strategy that is
|
|
9
|
+
* wrong about itself while looking entirely normal. Measured on a stop and
|
|
10
|
+
* reverse script, that shape produced five buys and no sells.
|
|
11
|
+
*
|
|
12
|
+
* **It is the backtest's own venue, not a second one.** `Simulator` is what
|
|
13
|
+
* `backtest()` runs against, so a chart running a strategy through it fills the
|
|
14
|
+
* same orders at the same prices as the report the trader ran a moment ago. A
|
|
15
|
+
* venue written here to be simpler would be a second answer to "what would this
|
|
16
|
+
* have filled at", and the two would disagree the first time either changed:
|
|
17
|
+
* the chart would draw one set of trades and the report would list another.
|
|
18
|
+
*
|
|
19
|
+
* **What a chart supplies and what it invents.** The bars, the instrument and
|
|
20
|
+
* the declaration are the chart's own and are passed through. Money is not: a
|
|
21
|
+
* chart draws a strategy, it does not report one, so the currency, the point
|
|
22
|
+
* value and the rounding are stated here as the neutral values that make the
|
|
23
|
+
* accounting a no-op. Nothing a chart draws reads them, and a chart that
|
|
24
|
+
* guessed a point value would put a wrong profit in front of somebody.
|
|
25
|
+
*
|
|
26
|
+
* **Nothing here decides when a fill happens.** The declaration does, through
|
|
27
|
+
* `fillOn`, and the venue reads it. A market order priced at the next bar's
|
|
28
|
+
* open is known at that open; one that rested and traded inside a bar is known
|
|
29
|
+
* once the bar is complete. Both are what a venue could have told anybody at
|
|
30
|
+
* the time, and that is the whole reason the frames arrive between bars rather
|
|
31
|
+
* than inside the execution that sent them.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { DEFAULT_FILL, Simulator, declarationOf } from '../../core/backtest/index.js';
|
|
35
|
+
import type { RecordedBar } from '../../core/backtest/index.js';
|
|
36
|
+
import type { CompiledProgram } from '../../core/emit/index.js';
|
|
37
|
+
import type { Instrument, OrderFrame, ResolvedInput, RoutedEffect } from '../../core/engine/index.js';
|
|
38
|
+
import type { ChartBar } from './contract.js';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whether this program needs a destination before it can run at all.
|
|
42
|
+
*
|
|
43
|
+
* Read from the program's own declared capabilities rather than from whether
|
|
44
|
+
* its source said `strategy()`, because `requires` is the same fact the engine
|
|
45
|
+
* tests at load. A strategy that places no orders needs none, and anything that
|
|
46
|
+
* does need one is refused without it.
|
|
47
|
+
*/
|
|
48
|
+
export function needsVenue(program: CompiledProgram): boolean {
|
|
49
|
+
return program.requires.includes('orders');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** One chart bar as the venue reads it. */
|
|
53
|
+
function recorded(bar: ChartBar): RecordedBar {
|
|
54
|
+
return {
|
|
55
|
+
time: bar.time,
|
|
56
|
+
open: bar.open,
|
|
57
|
+
high: bar.high,
|
|
58
|
+
low: bar.low,
|
|
59
|
+
close: bar.close,
|
|
60
|
+
volume: bar.volume ?? null,
|
|
61
|
+
oi: bar.oi ?? null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The venue for one chart run, or nothing where the program does not need one.
|
|
67
|
+
*
|
|
68
|
+
* `inputs` are the ones the engine resolved at load, because a declaration
|
|
69
|
+
* field may be written as an `input()`: a script whose `fillOn` comes from a
|
|
70
|
+
* setting would otherwise be filled the way its source happened to be written
|
|
71
|
+
* rather than the way the trader is running it.
|
|
72
|
+
*/
|
|
73
|
+
export function venueFor(
|
|
74
|
+
program: CompiledProgram,
|
|
75
|
+
bars: readonly ChartBar[],
|
|
76
|
+
inputs: readonly ResolvedInput[],
|
|
77
|
+
instrument: Instrument | undefined,
|
|
78
|
+
): Simulator | null {
|
|
79
|
+
if (!needsVenue(program)) return null;
|
|
80
|
+
|
|
81
|
+
const declared = declarationOf(program, inputs);
|
|
82
|
+
|
|
83
|
+
return new Simulator({
|
|
84
|
+
bars: bars.map(recorded),
|
|
85
|
+
contract: {
|
|
86
|
+
symbol: instrument?.symbol ?? null,
|
|
87
|
+
exchange: instrument?.exchange ?? null,
|
|
88
|
+
// Neutral money. A chart draws a strategy and does not report one, so
|
|
89
|
+
// nothing it puts on screen reads these; stating a point value a host
|
|
90
|
+
// never gave would be inventing the size of somebody's position.
|
|
91
|
+
currency: declared.currency,
|
|
92
|
+
tickSize: instrument?.tickSize ?? null,
|
|
93
|
+
lotSize: null,
|
|
94
|
+
pointValue: 1,
|
|
95
|
+
digits: 2,
|
|
96
|
+
},
|
|
97
|
+
fill: DEFAULT_FILL,
|
|
98
|
+
// The declaration's own, so slippage is applied the way the backtest
|
|
99
|
+
// applies it. Ticks, against the instrument's tick size.
|
|
100
|
+
slippageTicks: declared.slippage,
|
|
101
|
+
fillOn: declared.fillOn,
|
|
102
|
+
qtyType: declared.qtyType,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A venue that can be asked for frames, held across a chart's recomputes.
|
|
108
|
+
*
|
|
109
|
+
* The bars a `Simulator` prices against are the ones it was built with, so a
|
|
110
|
+
* chart that has grown a bar needs a venue that knows about it. `extend` is how
|
|
111
|
+
* the tail path says so without rebuilding the orders it is already holding.
|
|
112
|
+
*/
|
|
113
|
+
export interface HeldVenue {
|
|
114
|
+
readonly venue: Simulator;
|
|
115
|
+
/** Frames the venue answered for a bar, waiting to be delivered before the next. */
|
|
116
|
+
pending: readonly OrderFrame[];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** What a venue has to say after one bar, which the next bar's engine reads. */
|
|
120
|
+
export function answersFor(held: HeldVenue, barIndex: number): void {
|
|
121
|
+
held.pending = held.venue.framesFor(barIndex);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The route an engine is loaded with, pointing at a venue built after it. */
|
|
125
|
+
export function routeInto(holder: { current: Simulator | null }): (effect: RoutedEffect, bar: number) => void {
|
|
126
|
+
// Late bound on purpose. The route is handed to `load`, and the venue cannot
|
|
127
|
+
// be built until `load` has resolved the inputs the declaration reads. The
|
|
128
|
+
// route is only ever called from inside an execution, which is after both.
|
|
129
|
+
return (effect: RoutedEffect, bar: number) => {
|
|
130
|
+
holder.current?.route(effect, bar);
|
|
131
|
+
};
|
|
132
|
+
}
|