openalgo-script 0.4.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 +929 -0
- package/README.md +69 -16
- 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/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 +66 -8
- package/dist/core/backtest/drive.d.ts.map +1 -1
- package/dist/core/backtest/drive.js +109 -59
- package/dist/core/backtest/drive.js.map +1 -1
- package/dist/core/backtest/index.d.ts +24 -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/catalogue/catalogue.generated.d.ts +1 -1
- package/dist/core/catalogue/catalogue.generated.js +1 -1
- package/dist/core/catalogue/catalogue.generated.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.js +2 -2
- package/dist/core/check/library-prose.generated.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/index.d.ts +1 -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/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 +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/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 +10 -5
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +8 -3
- 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/package.json +14 -2
- package/spec/README.md +2 -1
- package/spec/errors.json +3 -3
- package/src/adapters/charts/driving.ts +109 -0
- package/src/adapters/charts/run.ts +120 -28
- 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 +175 -71
- package/src/core/backtest/index.ts +24 -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/catalogue/catalogue.generated.ts +1 -1
- package/src/core/check/library-orders.ts +2 -2
- package/src/core/check/library-prose.generated.ts +2 -2
- package/src/core/emit/canonical.ts +67 -9
- package/src/core/engine/arithmetic.ts +23 -3
- package/src/core/engine/index.ts +1 -1
- 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 +68 -0
- package/src/core/engine/verify-tables.ts +43 -0
- package/src/core/index.ts +16 -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,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A run record, turned into the files a conformance case is made of.
|
|
3
|
+
*
|
|
4
|
+
* **The suite is harvested rather than written.** A case invented by hand tests
|
|
5
|
+
* what somebody imagined a run does; a case taken from a run tests what a run
|
|
6
|
+
* actually did. `conformance.md` section 2 says a case is one directory of named
|
|
7
|
+
* files, and a record already holds every one of them: the script it ran, the
|
|
8
|
+
* bars it ran over, the settings it ran with, and what came back. So this is a
|
|
9
|
+
* projection and not a computation. Nothing here folds money, re-reads a report
|
|
10
|
+
* or decides anything a run did not already decide.
|
|
11
|
+
*
|
|
12
|
+
* **Text out, and no I/O.** Core writes no files, so this returns the bytes and
|
|
13
|
+
* the caller puts them where it likes. That also makes it testable without a
|
|
14
|
+
* disk and usable from a browser, which is where most runs happen.
|
|
15
|
+
*
|
|
16
|
+
* **What it refuses, it refuses loudly.** A case with a missing file is worse
|
|
17
|
+
* than no case: it fails on somebody else's engine and the blame lands on them.
|
|
18
|
+
* So a record that cannot make a whole case does not make a partial one, and a
|
|
19
|
+
* record that cannot make a faithful file does not guess at one: the script it
|
|
20
|
+
* has no text for and the instrument fact its host never stated are both
|
|
21
|
+
* refusals, never a hole and never a default.
|
|
22
|
+
*
|
|
23
|
+
* **Every setting of the run has a place in the case, and the table below says
|
|
24
|
+
* which.** A run once harvested to a case that said nothing about the digit
|
|
25
|
+
* count its money was rounded to, the charge schedule its host supplied or the
|
|
26
|
+
* window its report was about, so a second engine ran under other values and
|
|
27
|
+
* took the blame. `CARRIED` names the file each field of the settings is
|
|
28
|
+
* carried in, the type refuses to compile when the settings gain a field with
|
|
29
|
+
* no row, and a record whose settings hold a field the table does not know is
|
|
30
|
+
* refused by name rather than written into a case that ran under something it
|
|
31
|
+
* does not state.
|
|
32
|
+
*/
|
|
33
|
+
import { diagnosticFor } from '../diagnostics/index.js';
|
|
34
|
+
import type { Diagnostic } from '../diagnostics/index.js';
|
|
35
|
+
import { canonicalNumber, canonicalise } from '../emit/index.js';
|
|
36
|
+
import type { Instrument } from '../engine/index.js';
|
|
37
|
+
import type { RecordedBar, RecordedFrame, RunRecord } from './record.js';
|
|
38
|
+
import type { BacktestSettings, Tolerance } from './settings.js';
|
|
39
|
+
|
|
40
|
+
/** The files of one case, keyed by the name `conformance.md` section 2 gives them. */
|
|
41
|
+
export type CaseFiles = Readonly<Record<string, string>>;
|
|
42
|
+
|
|
43
|
+
/** Why a record could not become a case. */
|
|
44
|
+
export interface CaseRefusal {
|
|
45
|
+
readonly ok: false;
|
|
46
|
+
readonly reason: string;
|
|
47
|
+
/**
|
|
48
|
+
* The catalogue code the refusal is filed under, or null.
|
|
49
|
+
*
|
|
50
|
+
* A refusal about a setting of the run is the catalogue's, OS6021, because
|
|
51
|
+
* it is the same refusal the run makes of a setting it cannot be carried out
|
|
52
|
+
* under. The others here are about what the record holds, a text it never
|
|
53
|
+
* carried, a bar list it only points at, a fact its host never stated, and
|
|
54
|
+
* no catalogue entry is about those.
|
|
55
|
+
*/
|
|
56
|
+
readonly code: string | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CaseWritten {
|
|
60
|
+
readonly ok: true;
|
|
61
|
+
readonly files: CaseFiles;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CaseResult = CaseWritten | CaseRefusal;
|
|
65
|
+
|
|
66
|
+
/** What the caller has to say, because a record cannot know it. */
|
|
67
|
+
export interface CaseIdentity {
|
|
68
|
+
/**
|
|
69
|
+
* The directory path under the suite root, which is also the case id.
|
|
70
|
+
*
|
|
71
|
+
* The caller's because a record does not know where it will live, and
|
|
72
|
+
* `case.json` repeats it on purpose so a directory that is moved without its
|
|
73
|
+
* id being changed is caught by the suite rather than by a confused reader.
|
|
74
|
+
*/
|
|
75
|
+
readonly id: string;
|
|
76
|
+
/** One sentence. It is the failure message a runner prints. */
|
|
77
|
+
readonly description: string;
|
|
78
|
+
/** Section 7's categories. Defaults to the one a strategy run belongs to. */
|
|
79
|
+
readonly category?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The channels a strategy run can be held to.
|
|
84
|
+
*
|
|
85
|
+
* Only what the record carries folded, and only what another engine could
|
|
86
|
+
* produce independently. A case asserts the channels it names and no others, so
|
|
87
|
+
* a change to drawing output cannot break a case about money.
|
|
88
|
+
*/
|
|
89
|
+
const STRATEGY_ASSERTS = ['diagnostics', 'orders', 'trades', 'performance'] as const;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* `conformance.md` section 6: the loosest bounds a conformance case may declare.
|
|
93
|
+
*
|
|
94
|
+
* Core reads no page, so the two figures are written here, and
|
|
95
|
+
* `tests/backtest/case-settings.test.ts` reads them out of section 6 and holds
|
|
96
|
+
* these to the page, which is the arrangement `stdlib.md` section 20's figures
|
|
97
|
+
* are under. Past either bound a run may still be a useful comparison; what it
|
|
98
|
+
* is not is a case, so the projection makes no file of it.
|
|
99
|
+
*/
|
|
100
|
+
export const TOLERANCE_CAP: { readonly rel: number; readonly abs: number } = {
|
|
101
|
+
rel: 1e-9,
|
|
102
|
+
abs: 1e-12,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Where each setting of a run is carried in a case, sections 2 and 3.
|
|
107
|
+
*
|
|
108
|
+
* Every field of the settings has a row and the type makes a field without one
|
|
109
|
+
* a compile error, so a setting the record gains cannot be left out of a case
|
|
110
|
+
* by forgetting. `fill` is the simulated destination's policy, and what it
|
|
111
|
+
* decided is the frames, which are input: an engine handed `frames.csv` folds
|
|
112
|
+
* them and fills nothing itself.
|
|
113
|
+
*/
|
|
114
|
+
const CARRIED: Readonly<Record<keyof BacktestSettings, string>> = {
|
|
115
|
+
contract: 'instrument.json, and backtest.json for the digit count',
|
|
116
|
+
costs: 'backtest.json',
|
|
117
|
+
range: 'backtest.json',
|
|
118
|
+
inputs: 'settings.json',
|
|
119
|
+
now: 'case.json',
|
|
120
|
+
tolerance: 'case.json',
|
|
121
|
+
fill: 'frames.csv, as the frames it decided',
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Where a refusal about a setting points: nowhere in the script.
|
|
126
|
+
*
|
|
127
|
+
* A run setting is what the host stated before the first bar, and a caret
|
|
128
|
+
* under a line of the strategy would blame the one party that did not choose
|
|
129
|
+
* it. The run's own settings check states the same position for the same reason.
|
|
130
|
+
*/
|
|
131
|
+
const NO_POSITION: Diagnostic['span'] = { offset: 0, length: 0, line: 0, column: 0 };
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The files for one case, or the reason there are none.
|
|
135
|
+
*
|
|
136
|
+
* `now` is written only when the run pinned one. A case that names it when the
|
|
137
|
+
* script never asked pins a clock the run did not depend on, and the next
|
|
138
|
+
* reader has to work out whether it mattered.
|
|
139
|
+
*/
|
|
140
|
+
export function caseFilesFrom(record: RunRecord, identity: CaseIdentity): CaseResult {
|
|
141
|
+
if (record.sourceText === null) {
|
|
142
|
+
return refused(
|
|
143
|
+
'the record carries no source text, so the case would have no script.os: ' +
|
|
144
|
+
'record it with sourceText, or re-run the script to record one that has it',
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
if (record.bars.form !== 'inline') {
|
|
148
|
+
return refused(
|
|
149
|
+
'the record points at its bars instead of holding them, and a case holds every ' +
|
|
150
|
+
'byte of its own input: record it with form "inline"',
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (identity.id.trim() === '') return refused('a case needs an id');
|
|
154
|
+
if (identity.description.trim() === '') {
|
|
155
|
+
return refused('a case needs a one-sentence description, which is its failure message');
|
|
156
|
+
}
|
|
157
|
+
const instrument = instrumentOf(record);
|
|
158
|
+
if (!instrument.ok) return instrument;
|
|
159
|
+
const settings = settingsRefusal(record.settings);
|
|
160
|
+
if (settings !== null) return settings;
|
|
161
|
+
|
|
162
|
+
const files: Record<string, string> = {
|
|
163
|
+
'case.json': json({
|
|
164
|
+
id: identity.id,
|
|
165
|
+
category: identity.category ?? 'strategy',
|
|
166
|
+
profile: 'strategy',
|
|
167
|
+
languageVersion: Number(record.languageVersion),
|
|
168
|
+
description: identity.description,
|
|
169
|
+
asserts: [...STRATEGY_ASSERTS],
|
|
170
|
+
...(record.settings.now === null ? {} : { now: record.settings.now }),
|
|
171
|
+
tolerance: record.settings.tolerance,
|
|
172
|
+
}),
|
|
173
|
+
'script.os': endsWithNewline(record.sourceText),
|
|
174
|
+
'bars.csv': barsCsv(record.bars.rows),
|
|
175
|
+
// `conformance.md` section 4: performance is a list of one flat object
|
|
176
|
+
// holding the summary statistics and nothing nested. The trades are their
|
|
177
|
+
// own channel and are not repeated inside it, because a figure stated
|
|
178
|
+
// twice in one case is a figure that can disagree with itself. The equity
|
|
179
|
+
// curve, the monthly table and the markers are not written at all: the
|
|
180
|
+
// first two are not conformance channels, being derived from fills and
|
|
181
|
+
// closes the case already fixes, and a marker is a chart output the
|
|
182
|
+
// `markers` channel owns, which a case about money does not assert.
|
|
183
|
+
'expected.json': json({
|
|
184
|
+
diagnostics: record.diagnostics,
|
|
185
|
+
orders: record.orders,
|
|
186
|
+
trades: record.report.trades,
|
|
187
|
+
performance: [record.report.summary],
|
|
188
|
+
}),
|
|
189
|
+
// Section 2: the record of `host-interface.md` 4.1, which is what the
|
|
190
|
+
// engine was handed and not the money layer's contract. The suite's
|
|
191
|
+
// defaults for an absent file are boring on purpose and are not this run's
|
|
192
|
+
// instrument, so the file is always written.
|
|
193
|
+
'instrument.json': json(instrument.value),
|
|
194
|
+
// Section 3: what the report was folded under and the script never states.
|
|
195
|
+
// Always written, because every run rounds money to some digit count, and a
|
|
196
|
+
// strategy case that left it out would be run under whatever count a runner
|
|
197
|
+
// assumed. `costs` is null for the declaration's own schedule and `range`
|
|
198
|
+
// carries null for a bound nobody stated, so the file says what the run ran
|
|
199
|
+
// under in every case rather than leaving a default to a runner.
|
|
200
|
+
'backtest.json': json({
|
|
201
|
+
digits: record.settings.contract.digits,
|
|
202
|
+
costs: record.settings.costs,
|
|
203
|
+
range: record.settings.range,
|
|
204
|
+
}),
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Without this the case is unpassable, on every engine including the one that
|
|
208
|
+
// wrote it. `conformance.md` section 3 ends "a case with no `frames.csv` is
|
|
209
|
+
// handed no frames at all", and what `expected.json` asserts through the
|
|
210
|
+
// orders channel is what came of the frames: a status, a cumulative quantity,
|
|
211
|
+
// an average fill price. An engine handed none of them folds nothing and
|
|
212
|
+
// disagrees with every row, and the failure reads as a defect in that engine.
|
|
213
|
+
//
|
|
214
|
+
// Written only when the run had frames, because an empty file and an absent
|
|
215
|
+
// one mean the same thing here and the absent one says it in fewer bytes.
|
|
216
|
+
if (record.frames.length > 0) files['frames.csv'] = framesCsv(record.frames);
|
|
217
|
+
|
|
218
|
+
// Written only when the run had inputs to write. An empty settings.json says
|
|
219
|
+
// "these are the values" about nothing, and section 2 reads an absent one as
|
|
220
|
+
// every input taking its declared default, which is what actually happened.
|
|
221
|
+
if (Object.keys(record.settings.inputs).length > 0) {
|
|
222
|
+
files['settings.json'] = json(record.settings.inputs);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { ok: true, files };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** A refusal about what the record holds, which no catalogue entry is about. */
|
|
229
|
+
function refused(reason: string): CaseRefusal {
|
|
230
|
+
return { ok: false, reason, code: null };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The instrument record a case can state faithfully, or why there is none.
|
|
235
|
+
*
|
|
236
|
+
* Two refusals, and both are the same refusal: the file `conformance.md`
|
|
237
|
+
* section 2 names is the record of `host-interface.md` 4.1, and a record that
|
|
238
|
+
* cannot produce that record cannot produce the file.
|
|
239
|
+
*
|
|
240
|
+
* A record written before version 3 carries none, because the facts beside
|
|
241
|
+
* the contract were handed to the engine and written down nowhere. And 4.1
|
|
242
|
+
* requires one fact of every host, `hasVolume`, which is the one the engine
|
|
243
|
+
* does not refuse a run without: a run whose host never stated it ran with the
|
|
244
|
+
* flag absent, so a file stating it would hand a second engine a different
|
|
245
|
+
* study than the one the expected output came from, and a file omitting it is
|
|
246
|
+
* not a 4.1 record. The other rules that page states about a record, a session
|
|
247
|
+
* with no timezone to read it in, are refused at load, so a run that happened
|
|
248
|
+
* cannot carry one.
|
|
249
|
+
*/
|
|
250
|
+
function instrumentOf(
|
|
251
|
+
record: RunRecord,
|
|
252
|
+
): { readonly ok: true; readonly value: Instrument } | CaseRefusal {
|
|
253
|
+
if (record.instrument === null) {
|
|
254
|
+
return refused(
|
|
255
|
+
'the record carries no instrument record, so the case would have no instrument.json: ' +
|
|
256
|
+
'it was written before record version 3, and re-running the script records one',
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (typeof record.instrument.hasVolume !== 'boolean') {
|
|
260
|
+
return refused(
|
|
261
|
+
'the run was handed no hasVolume, which host-interface.md 4.1 requires of every ' +
|
|
262
|
+
'host, so instrument.json cannot be the record that page defines: run the script ' +
|
|
263
|
+
'with the instrument facts stated',
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
return { ok: true, value: record.instrument };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Whether every setting the run was carried out under has a place in the case,
|
|
271
|
+
* and whether its tolerance is one the suite accepts.
|
|
272
|
+
*
|
|
273
|
+
* The first question is asked of the record rather than of the type, because a
|
|
274
|
+
* record read back from JSON is whatever was written: a field this projection
|
|
275
|
+
* has no file for is refused with its name, never passed over into a case that
|
|
276
|
+
* then ran under a value it does not state.
|
|
277
|
+
*/
|
|
278
|
+
function settingsRefusal(settings: BacktestSettings): CaseRefusal | null {
|
|
279
|
+
for (const key of Object.keys(settings)) {
|
|
280
|
+
if (key in CARRIED) continue;
|
|
281
|
+
return refused(
|
|
282
|
+
`the record's settings carry ${key}, which no file of conformance.md section 2 has a ` +
|
|
283
|
+
'place for, so a case written from it would run under a setting it does not state: ' +
|
|
284
|
+
'give the setting a file on that page and a row in this projection first',
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
return toleranceRefusal(settings.tolerance);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* A tolerance past the cap is a comparison somebody may find useful and is not
|
|
292
|
+
* a case, `conformance.md` section 6.
|
|
293
|
+
*
|
|
294
|
+
* Refused here, where the case is written, and not only where one is read,
|
|
295
|
+
* because a directory the suite will not accept fails every runner it meets
|
|
296
|
+
* and the blame lands on the engine under test. The run's own settings check
|
|
297
|
+
* refuses a bound with no reason and a bound below zero before a record
|
|
298
|
+
* exists; the cap is the one rule about a tolerance that is the suite's rather
|
|
299
|
+
* than the run's, so it is the one asked here.
|
|
300
|
+
*/
|
|
301
|
+
function toleranceRefusal(tolerance: Tolerance): CaseRefusal | null {
|
|
302
|
+
const { abs, rel } = tolerance;
|
|
303
|
+
if (!Number.isFinite(abs) || !Number.isFinite(rel)) {
|
|
304
|
+
return settingRefusal('a bound is not a finite number');
|
|
305
|
+
}
|
|
306
|
+
if (abs <= TOLERANCE_CAP.abs && rel <= TOLERANCE_CAP.rel) return null;
|
|
307
|
+
return settingRefusal(
|
|
308
|
+
`a bound of ${canonicalNumber(abs)} absolute and ${canonicalNumber(rel)} relative is ` +
|
|
309
|
+
'past the cap conformance.md section 6 puts on a conformance case, ' +
|
|
310
|
+
`${canonicalNumber(TOLERANCE_CAP.abs)} absolute and ${canonicalNumber(TOLERANCE_CAP.rel)} ` +
|
|
311
|
+
'relative, so this run is a comparison and not a case',
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** A refusal about the comparison tolerance, filed under the run's own code for a setting. */
|
|
316
|
+
function settingRefusal(problem: string): CaseRefusal {
|
|
317
|
+
const diagnostic = diagnosticFor('OS6021', NO_POSITION, {
|
|
318
|
+
setting: 'The comparison tolerance',
|
|
319
|
+
problem,
|
|
320
|
+
});
|
|
321
|
+
return { ok: false, reason: diagnostic.message, code: diagnostic.code };
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Through the canonical writer, so a case's bytes are the record's bytes.
|
|
326
|
+
*
|
|
327
|
+
* There is one canonical writer in this repository and this is not a second
|
|
328
|
+
* one: key order and number form are fixed for everybody, which is what lets a
|
|
329
|
+
* case be compared as text at all.
|
|
330
|
+
*/
|
|
331
|
+
function json(value: unknown): string {
|
|
332
|
+
return `${canonicalise(value)}\n`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** `conformance.md` section 3: header, one row per bar, oldest first, `none` for absent. */
|
|
336
|
+
function barsCsv(rows: readonly RecordedBar[]): string {
|
|
337
|
+
const lines = ['time,open,high,low,close,volume'];
|
|
338
|
+
for (const bar of rows) {
|
|
339
|
+
lines.push(
|
|
340
|
+
[bar.time, bar.open, bar.high, bar.low, bar.close, bar.volume].map(cell).join(','),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
return `${lines.join('\n')}\n`;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* `conformance.md` section 3: the fields of a frame, in the order that page names.
|
|
348
|
+
*
|
|
349
|
+
* A projection and not a translation. The record already holds an intent as an
|
|
350
|
+
* ordinal rather than as this engine's own id, for the reason the same section
|
|
351
|
+
* gives: a case cannot know the id another engine minted and must not depend on
|
|
352
|
+
* its spelling.
|
|
353
|
+
*
|
|
354
|
+
* The instant is written on every row, `none` where the destination stated
|
|
355
|
+
* none, through the same cell writer as an absent price. A case whose frames
|
|
356
|
+
* carried instants and whose file did not would assert an `updatedAt` that its
|
|
357
|
+
* own input cannot reproduce, which is what the column closes.
|
|
358
|
+
*/
|
|
359
|
+
function framesCsv(frames: readonly RecordedFrame[]): string {
|
|
360
|
+
const lines = ['afterBar,intent,status,filledQty,avgFillPrice,orderRef,text,time'];
|
|
361
|
+
for (const frame of frames) {
|
|
362
|
+
lines.push(
|
|
363
|
+
[
|
|
364
|
+
canonicalNumber(frame.afterBar),
|
|
365
|
+
canonicalNumber(frame.intent),
|
|
366
|
+
frame.status,
|
|
367
|
+
canonicalNumber(frame.filledQty),
|
|
368
|
+
cell(frame.avgFillPrice),
|
|
369
|
+
frame.orderRef ?? '',
|
|
370
|
+
frame.text ?? '',
|
|
371
|
+
cell(frame.time),
|
|
372
|
+
].join(','),
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
return `${lines.join('\n')}\n`;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* One cell, through the one number writer.
|
|
380
|
+
*
|
|
381
|
+
* An absent field is `none` rather than empty, because an empty cell between two
|
|
382
|
+
* commas is indistinguishable from a file somebody's editor trimmed, and a
|
|
383
|
+
* language whose central idea is the absent value cannot be vague about it. A
|
|
384
|
+
* case is compared as text, so the number is written by the rule of
|
|
385
|
+
* `language.md` 5.5 and by the same function that writes every other number in
|
|
386
|
+
* the repository, never by a second spelling that agrees until it does not.
|
|
387
|
+
*/
|
|
388
|
+
function cell(value: number | null): string {
|
|
389
|
+
return value === null || !Number.isFinite(value) ? 'none' : canonicalNumber(value);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** A text file ends with a newline, so appending to it never joins two lines. */
|
|
393
|
+
function endsWithNewline(text: string): string {
|
|
394
|
+
return text.endsWith('\n') ? text : `${text}\n`;
|
|
395
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A case's rows and this engine's frames, and the destination between them.
|
|
3
|
+
*
|
|
4
|
+
* Both directions are here because they are one correspondence: a case names an
|
|
5
|
+
* order by an ordinal and an engine knows it by an id, so `Delivery` reads an
|
|
6
|
+
* ordinal into the id of the intent the run placed, and `framedAs` writes an id
|
|
7
|
+
* back out as the ordinal a case file prints. Split between two files, the two
|
|
8
|
+
* halves of it drift, and a suite whose rows say one thing and whose records
|
|
9
|
+
* say another is one nobody can read.
|
|
10
|
+
*
|
|
11
|
+
* `conformance.md` section 3: `frames.csv` supplies order frames the way
|
|
12
|
+
* `bars.csv` supplies bars, so a strategy case asserts the fold against input
|
|
13
|
+
* the engine did not choose. `simulate.ts` is the other destination this module
|
|
14
|
+
* has, the one that reads a bar and works out what a venue would have said.
|
|
15
|
+
* This one works nothing out. It holds the rows a case supplied, hands over the
|
|
16
|
+
* ones each boundary names, and the whole of its behaviour is the mapping
|
|
17
|
+
* between a row and a frame.
|
|
18
|
+
*
|
|
19
|
+
* **An ordinal is what a case can name, and an id is not.** A row names the nth
|
|
20
|
+
* intent the run placed, because no case can know the id an engine minted, so
|
|
21
|
+
* the intents are counted here in the order the run handed them over and the
|
|
22
|
+
* ordinal is read against that count. A row naming an ordinal the run never
|
|
23
|
+
* placed is delivered all the same, carrying an id no run mints: step 1 of
|
|
24
|
+
* `stdlib.md` 17.8 refuses a frame naming no row of the ledger, and that
|
|
25
|
+
* refusal is the ledger's to make. A destination that dropped the row instead
|
|
26
|
+
* would answer nothing at all where section 3 hands an engine a frame about an
|
|
27
|
+
* order its ledger does not hold, and the case would pass by the frame never
|
|
28
|
+
* having arrived.
|
|
29
|
+
*
|
|
30
|
+
* **What a row does not carry, this does not invent.** `host-interface.md` 7.2
|
|
31
|
+
* lets a frame say which instrument and product the destination booked the
|
|
32
|
+
* order under, and the file has no column for either, so neither is stated and
|
|
33
|
+
* the row keeps what its placement put there. The instant is the one the file
|
|
34
|
+
* does carry: a row states one or states `none`, and one that states none
|
|
35
|
+
* leaves `updatedAt` where the placement put it, which is what the fold does
|
|
36
|
+
* with a frame whose destination stated no instant.
|
|
37
|
+
*/
|
|
38
|
+
import type { OrderFrame, OrderIntent, RoutedEffect } from '../engine/index.js';
|
|
39
|
+
import type { RecordedFrame } from './record.js';
|
|
40
|
+
|
|
41
|
+
/** One frame, the boundary it was handed over at, and the row it came from. */
|
|
42
|
+
export interface Delivered {
|
|
43
|
+
readonly frame: OrderFrame;
|
|
44
|
+
readonly afterBar: number;
|
|
45
|
+
/**
|
|
46
|
+
* The row a case supplied, or null where the run's own destination answered
|
|
47
|
+
* the frame and there is no row until the record is written.
|
|
48
|
+
*/
|
|
49
|
+
readonly row: RecordedFrame | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Where a run's orders go, and what it is told between two of its bars. */
|
|
53
|
+
export interface Destination {
|
|
54
|
+
/** Step 9: what the strategy decided, on the bar it decided it. */
|
|
55
|
+
route(effect: RoutedEffect, barIndex: number): void;
|
|
56
|
+
/** What this destination hands over at the boundary after `barIndex`. */
|
|
57
|
+
answers(barIndex: number): readonly Delivered[];
|
|
58
|
+
/** Every intent it was handed, in the order it was handed them. */
|
|
59
|
+
readonly intents: readonly OrderIntent[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* An id no run mints, which a row naming no intent of this run is delivered
|
|
64
|
+
* under.
|
|
65
|
+
*
|
|
66
|
+
* Ids are counted from one by the ledger that mints them, so nothing below one
|
|
67
|
+
* can name a row and the fold refuses the frame by the rule it refuses every
|
|
68
|
+
* other unknown one by. The alternative was an id of a real row chosen by some
|
|
69
|
+
* rule of this file's own, which would fold a case's frame into whichever order
|
|
70
|
+
* happened to be near it.
|
|
71
|
+
*/
|
|
72
|
+
const NO_INTENT = -1;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A destination holding one case's frames, ready at the boundaries they name.
|
|
76
|
+
*
|
|
77
|
+
* Built before the run rather than during it, because the rows are input: what
|
|
78
|
+
* a boundary hands over was decided by whoever wrote the case and not by
|
|
79
|
+
* anything this run does. The one thing it learns as the run goes is which
|
|
80
|
+
* intent an ordinal names, which only the run can say.
|
|
81
|
+
*/
|
|
82
|
+
export class Delivery implements Destination {
|
|
83
|
+
readonly intents: OrderIntent[] = [];
|
|
84
|
+
/** The rows of each boundary, in file order, which section 3 fixes as the delivery order. */
|
|
85
|
+
private readonly rows = new Map<number, RecordedFrame[]>();
|
|
86
|
+
|
|
87
|
+
constructor(frames: readonly RecordedFrame[]) {
|
|
88
|
+
for (const row of frames) {
|
|
89
|
+
const at = this.rows.get(row.afterBar);
|
|
90
|
+
if (at === undefined) this.rows.set(row.afterBar, [row]);
|
|
91
|
+
else at.push(row);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Nothing is decided by an order arriving here: it is counted, and that is all. */
|
|
96
|
+
route(effect: RoutedEffect): void {
|
|
97
|
+
for (const intent of effect.intents) this.intents.push(intent);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
answers(barIndex: number): readonly Delivered[] {
|
|
101
|
+
const due = this.rows.get(barIndex) ?? [];
|
|
102
|
+
return due.map((row) => ({
|
|
103
|
+
frame: frameOf(row, this.intents[row.intent - 1]),
|
|
104
|
+
afterBar: barIndex,
|
|
105
|
+
row,
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** One row of a case, as the frame `host-interface.md` 7.2 describes. */
|
|
111
|
+
function frameOf(row: RecordedFrame, intent: OrderIntent | undefined): OrderFrame {
|
|
112
|
+
return {
|
|
113
|
+
intentId: intent?.intentId ?? NO_INTENT,
|
|
114
|
+
status: row.status,
|
|
115
|
+
filledQty: row.filledQty,
|
|
116
|
+
avgFillPrice: row.avgFillPrice,
|
|
117
|
+
orderRef: row.orderRef,
|
|
118
|
+
text: row.text,
|
|
119
|
+
time: row.time,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The ordinal of every intent, which is how a case names one.
|
|
125
|
+
*
|
|
126
|
+
* One, two, three in the order the run placed them, because no engine can know
|
|
127
|
+
* the id another minted and a case that named one would only ever be readable
|
|
128
|
+
* by the engine that wrote it.
|
|
129
|
+
*/
|
|
130
|
+
export function ordinalsOf(intents: readonly OrderIntent[]): ReadonlyMap<number, number> {
|
|
131
|
+
const out = new Map<number, number>();
|
|
132
|
+
for (const intent of intents) {
|
|
133
|
+
if (!out.has(intent.intentId)) out.set(intent.intentId, out.size + 1);
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* One delivered frame, in the columns a case file prints.
|
|
140
|
+
*
|
|
141
|
+
* The instant travels with it. `stdlib.md` 17.7 folds `updatedAt` from a
|
|
142
|
+
* frame's `time`, so a driver that dropped the field here wrote a record whose
|
|
143
|
+
* ledger no engine could fold from the record's own frames: it would have
|
|
144
|
+
* nothing to move that field to and would leave it at `placedAt`.
|
|
145
|
+
*/
|
|
146
|
+
export function framedAs(
|
|
147
|
+
frame: OrderFrame,
|
|
148
|
+
afterBar: number,
|
|
149
|
+
ordinals: ReadonlyMap<number, number>,
|
|
150
|
+
): RecordedFrame {
|
|
151
|
+
return {
|
|
152
|
+
afterBar,
|
|
153
|
+
intent: ordinals.get(frame.intentId) ?? 0,
|
|
154
|
+
status: frame.status,
|
|
155
|
+
filledQty: frame.filledQty,
|
|
156
|
+
avgFillPrice: frame.avgFillPrice ?? null,
|
|
157
|
+
orderRef: frame.orderRef ?? null,
|
|
158
|
+
text: frame.text ?? null,
|
|
159
|
+
time: frame.time ?? null,
|
|
160
|
+
};
|
|
161
|
+
}
|