openalgo-script 0.7.2 → 0.8.1

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +316 -379
  3. package/dist/adapters/charts/alerts.d.ts +11 -0
  4. package/dist/adapters/charts/alerts.d.ts.map +1 -1
  5. package/dist/adapters/charts/alerts.js +16 -1
  6. package/dist/adapters/charts/alerts.js.map +1 -1
  7. package/dist/adapters/charts/capabilities.d.ts +48 -0
  8. package/dist/adapters/charts/capabilities.d.ts.map +1 -0
  9. package/dist/adapters/charts/capabilities.js +82 -0
  10. package/dist/adapters/charts/capabilities.js.map +1 -0
  11. package/dist/adapters/charts/columns.d.ts +19 -2
  12. package/dist/adapters/charts/columns.d.ts.map +1 -1
  13. package/dist/adapters/charts/columns.js +15 -3
  14. package/dist/adapters/charts/columns.js.map +1 -1
  15. package/dist/adapters/charts/contract.d.ts +35 -3
  16. package/dist/adapters/charts/contract.d.ts.map +1 -1
  17. package/dist/adapters/charts/descriptor.d.ts +1 -1
  18. package/dist/adapters/charts/descriptor.d.ts.map +1 -1
  19. package/dist/adapters/charts/descriptor.js +17 -5
  20. package/dist/adapters/charts/descriptor.js.map +1 -1
  21. package/dist/adapters/charts/fills.d.ts +26 -5
  22. package/dist/adapters/charts/fills.d.ts.map +1 -1
  23. package/dist/adapters/charts/fills.js +55 -7
  24. package/dist/adapters/charts/fills.js.map +1 -1
  25. package/dist/adapters/charts/index.d.ts +9 -3
  26. package/dist/adapters/charts/index.d.ts.map +1 -1
  27. package/dist/adapters/charts/index.js +7 -1
  28. package/dist/adapters/charts/index.js.map +1 -1
  29. package/dist/adapters/charts/produced.d.ts +3 -2
  30. package/dist/adapters/charts/produced.d.ts.map +1 -1
  31. package/dist/adapters/charts/produced.js +1 -1
  32. package/dist/adapters/charts/produced.js.map +1 -1
  33. package/dist/adapters/charts/run.d.ts +23 -0
  34. package/dist/adapters/charts/run.d.ts.map +1 -1
  35. package/dist/adapters/charts/run.js +2 -1
  36. package/dist/adapters/charts/run.js.map +1 -1
  37. package/dist/adapters/charts/surfaces.d.ts +11 -0
  38. package/dist/adapters/charts/surfaces.d.ts.map +1 -1
  39. package/dist/adapters/charts/tables.d.ts +21 -11
  40. package/dist/adapters/charts/tables.d.ts.map +1 -1
  41. package/dist/adapters/charts/tables.js +17 -6
  42. package/dist/adapters/charts/tables.js.map +1 -1
  43. package/dist/adapters/charts/undrawable.d.ts +2 -1
  44. package/dist/adapters/charts/undrawable.d.ts.map +1 -1
  45. package/dist/adapters/charts/undrawable.js +24 -14
  46. package/dist/adapters/charts/undrawable.js.map +1 -1
  47. package/dist/core/check/call-sites.js +15 -5
  48. package/dist/core/check/call-sites.js.map +1 -1
  49. package/dist/core/version/version.generated.d.ts +1 -1
  50. package/dist/core/version/version.generated.js +1 -1
  51. package/package.json +1 -1
  52. package/spec/errors.json +1 -1
  53. package/src/adapters/charts/alerts.ts +28 -1
  54. package/src/adapters/charts/capabilities.ts +103 -0
  55. package/src/adapters/charts/columns.ts +29 -8
  56. package/src/adapters/charts/contract.ts +36 -2
  57. package/src/adapters/charts/descriptor.ts +21 -6
  58. package/src/adapters/charts/fills.ts +95 -11
  59. package/src/adapters/charts/index.ts +9 -1
  60. package/src/adapters/charts/produced.ts +4 -3
  61. package/src/adapters/charts/run.ts +25 -1
  62. package/src/adapters/charts/surfaces.ts +12 -0
  63. package/src/adapters/charts/tables.ts +31 -14
  64. package/src/adapters/charts/undrawable.ts +24 -14
  65. package/src/core/check/call-sites.ts +15 -5
  66. package/src/core/version/version.generated.ts +1 -1
@@ -32,7 +32,7 @@
32
32
  */
33
33
  import type { Value } from '../../core/engine/index.js';
34
34
  import type { ChartSettings } from './contract.js';
35
- import type { ChartDrawing, ChartGrid, ChartMarker } from './surfaces.js';
35
+ import type { ChartDrawing, ChartMarker, ChartTableSpec } from './surfaces.js';
36
36
 
37
37
  /**
38
38
  * One declared alert's message channel, whole, as the run left it.
@@ -47,13 +47,14 @@ export type MessageColumn = readonly Value[];
47
47
  /** One run's non-numeric output, as the hooks that follow it read it. */
48
48
  export interface Produced {
49
49
  readonly markers: readonly ChartMarker[];
50
- readonly table: ChartGrid | null;
50
+ /** Every declared grid, in declaration order; the single hook reads the first. */
51
+ readonly tables: readonly ChartTableSpec[];
51
52
  readonly drawings: readonly ChartDrawing[];
52
53
  /** One entry per declared alert, in the order `outputs.alerts` declares them. */
53
54
  readonly messages: readonly MessageColumn[];
54
55
  }
55
56
 
56
- const NOTHING: Produced = { markers: [], table: null, drawings: [], messages: [] };
57
+ const NOTHING: Produced = { markers: [], tables: [], drawings: [], messages: [] };
57
58
 
58
59
  const runs = new WeakMap<ChartSettings, Produced>();
59
60
 
@@ -38,6 +38,7 @@ import type { CompiledProgram } from '../../core/emit/index.js';
38
38
  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
+ import { capabilitiesOf } from './capabilities.js';
41
42
  import { refused, stopped } from './errors.js';
42
43
  import { undrawable } from './undrawable.js';
43
44
  import { stationIn, stationOf } from './requests.js';
@@ -60,6 +61,29 @@ export interface ChartAdapterOptions {
60
61
  readonly id?: string;
61
62
  /** The category a picker groups the study under, when `meta.group` is empty. */
62
63
  readonly category?: string;
64
+ /**
65
+ * The chart library's own version: the `VERSION` string it exports, which a
66
+ * host that imports the chart already holds, passed on as
67
+ * `descriptorFor(program, { chartVersion: VERSION })`.
68
+ *
69
+ * It says which of the descriptor's hooks the chart this descriptor is
70
+ * registered with will read. Two arrived after the oldest chart the peer
71
+ * range accepts: a band's per-bar colour and the list of grids. From 2.5.4
72
+ * on, a band whose colour the script computes is drawn bar by bar and every
73
+ * declared grid is drawn. Before it, or when nothing is stated, the adapter
74
+ * cannot tell whether the chart will read either hook, and a chart that
75
+ * ignores one draws a band in its own default colours or the first grid
76
+ * alone, with nothing said. So such a program is refused before any bar runs
77
+ * with OS6024, which is what a host that states nothing got before this
78
+ * option existed.
79
+ *
80
+ * The version rather than a switch per hook, because it is one fact the
81
+ * host already holds, it moves with the chart the host actually installed,
82
+ * and a hook the adapter learns to read later needs nothing new from any
83
+ * host. `capabilities.ts` has the rule, prereleases and unreadable strings
84
+ * included.
85
+ */
86
+ readonly chartVersion?: string;
63
87
  /**
64
88
  * What the host has stored for this study's inputs, for the declared shape.
65
89
  *
@@ -338,7 +362,7 @@ function start(
338
362
 
339
363
  // What this chart has no room for is refused before the engine is asked,
340
364
  // because a study drawn without it is a study that looks broken.
341
- const narrower = undrawable(program);
365
+ const narrower = undrawable(program, capabilitiesOf(options.chartVersion));
342
366
  if (narrower !== undefined) throw refused(narrower);
343
367
 
344
368
  const loaded = load(program, {
@@ -125,6 +125,18 @@ export interface ChartGrid {
125
125
  readonly options?: ChartTableOptions;
126
126
  }
127
127
 
128
+ /**
129
+ * One grid of several, under an identity the chart keeps it by.
130
+ *
131
+ * The chart reuses the grid a recompute names again and removes one it no
132
+ * longer names, so the id is the declaration's key: stable across recomputes,
133
+ * never empty, and unique within a program because the compiler writes one per
134
+ * declaration.
135
+ */
136
+ export interface ChartTableSpec extends ChartGrid {
137
+ readonly id: string;
138
+ }
139
+
128
140
  /** One end of a drawing: a time on the shared axis, a price on the pane's scale. */
129
141
  export interface ChartAnchor {
130
142
  readonly time: number;
@@ -27,13 +27,17 @@
27
27
  * placed into it, which is what `language.md` 6.7 asks for from the other
28
28
  * direction: a cell nothing was written into is blank, never a zero.
29
29
  *
30
- * **A chart pane holds one grid and the language declares as many as it likes.**
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.
30
+ * **The language declares as many grids as it likes, and a chart with the list
31
+ * of grids draws them all** (`capabilities.ts` says which chart has it). The
32
+ * list holds each grid under an id the chart keeps it by, and every declared
33
+ * grid is built for it here under its declaration's key. The single `table`
34
+ * hook stays beside the list with the first grid in it, because a chart
35
+ * without the list reads that one. Merging two grids into the single hook
36
+ * would put cells somewhere the script never asked for, so on a chart without
37
+ * the list a program declaring a second grid never reaches this file:
38
+ * `undrawable.ts` refuses it before any bar runs, with OS6024.
39
+ * `spec/chart-narrowings.json` records both halves and
40
+ * `scripts/check-chart-surface.mjs` proves both with a study that declares two.
37
41
  */
38
42
  import type { CompiledProgram, Grid as DeclaredGrid } from '../../core/emit/index.js';
39
43
  import type { Grid, GridCell } from '../../core/engine/index.js';
@@ -45,6 +49,7 @@ import type {
45
49
  ChartCellAlign,
46
50
  ChartGrid,
47
51
  ChartTablePosition,
52
+ ChartTableSpec,
48
53
  } from './surfaces.js';
49
54
 
50
55
  /** The language's four corners, in the chart's own words. */
@@ -58,22 +63,34 @@ const CORNERS: Readonly<Record<string, ChartTablePosition>> = {
58
63
  const ALIGNMENTS: readonly string[] = ['left', 'center', 'right'];
59
64
 
60
65
  /**
61
- * The grid a chart draws, from the declaration and the engine's buffer.
66
+ * Every grid a chart draws, from the declarations and the engine's buffers.
62
67
  *
63
- * The buffer is paired with the declaration by the key both of them carry
68
+ * Each buffer is paired with its declaration by the key both of them carry
64
69
  * rather than by position. The engine reads its grids in declaration order, so
65
70
  * the two agree today, and a pairing by position is one reordering away from
66
71
  * drawing one grid's cells into another grid's shape, which would be a wrong
67
72
  * table that looks like a right one.
73
+ *
74
+ * The key is the id as well. The compiler writes one per declaration and never
75
+ * an empty one, and it does not move between recomputes, which is what lets the
76
+ * chart keep a grid rather than build it again on every tick.
68
77
  */
69
- export function buildTable(
78
+ export function buildTables(
70
79
  program: CompiledProgram,
71
80
  lookup: InputLookup,
72
81
  written: readonly Grid[],
73
- ): ChartGrid | null {
74
- const declared = program.outputs.tables[0];
75
- if (declared === undefined) return null;
76
- return oneTable(declared, lookup, written.find((one) => one.key === declared.key));
82
+ ): readonly ChartTableSpec[] {
83
+ return program.outputs.tables.map((declared) => ({
84
+ id: declared.key,
85
+ ...oneTable(declared, lookup, written.find((one) => one.key === declared.key)),
86
+ }));
87
+ }
88
+
89
+ /** The first grid alone, for the single hook a chart without the list reads. */
90
+ export function firstGrid(tables: readonly ChartTableSpec[]): ChartGrid | null {
91
+ const first = tables[0];
92
+ if (first === undefined) return null;
93
+ return { rows: first.rows, ...(first.options === undefined ? {} : { options: first.options }) };
77
94
  }
78
95
 
79
96
  function oneTable(
@@ -3,17 +3,24 @@
3
3
  *
4
4
  * `compiled-program.md` section 11: a host draws what its surface has room for,
5
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.
6
+ * and saying nothing. Two things a version 1 program can declare have a place
7
+ * on a newer chart and none on an older one (`capabilities.ts` says which is
8
+ * which), and on an older one both used to be dropped in silence.
8
9
  *
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.
10
+ * - **A second grid.** An older descriptor has one `table` hook, so one grid
11
+ * reaches a pane. Merging two into it would put cells somewhere the script
12
+ * never asked for, and drawing the first alone is a study whose second panel
13
+ * never appears and whose cells look broken. A newer chart's list of grids
14
+ * carries every one (`tables.ts`).
15
+ * - **A band colour computed per bar.** An older chart's band takes one colour
16
+ * for each side for the whole run, and a band whose colour the script
17
+ * computes would be drawn in a colour the script did not choose. A newer
18
+ * chart's band has a per-bar colour callback (`fills.ts`).
19
+ *
20
+ * Which chart the descriptor is registered with is what the host states, and
21
+ * `capabilities.ts` reads it. A host that states nothing is refused both, as
22
+ * before, because a chart that ignores a hook it does not know draws the
23
+ * silent version of each.
17
24
  *
18
25
  * The compiled program carries no source position for a declaration, so the
19
26
  * refusal names the declaration by its title, which is what a reader sees in
@@ -22,23 +29,26 @@
22
29
  import { diagnosticFor, makeSpan } from '../../core/index.js';
23
30
  import type { Diagnostic } from '../../core/index.js';
24
31
  import type { CompiledProgram, Field } from '../../core/emit/index.js';
32
+ import { lacking } from './capabilities.js';
33
+ import type { ChartCapabilities } from './capabilities.js';
25
34
 
26
35
  /** A load refusal has no line of its own: the program is what was refused. */
27
36
  const AT_LOAD = makeSpan(0, 0, 0, 0);
28
37
 
29
- export function undrawable(program: CompiledProgram): Diagnostic | undefined {
38
+ export function undrawable(program: CompiledProgram, chart: ChartCapabilities): Diagnostic | undefined {
30
39
  const second = program.outputs.tables[1];
31
- if (second !== undefined) {
40
+ if (second !== undefined && !chart.grids) {
32
41
  return diagnosticFor('OS6024', AT_LOAD, {
33
42
  what: `the table ${titled(second.title, 'after the first')}, the second grid this study declares`,
34
- limit: 'a chart pane draws one grid',
43
+ limit: lacking(chart, 'grids', 'a chart pane draws one grid'),
35
44
  });
36
45
  }
46
+ if (chart.bandColours) return undefined;
37
47
  for (const band of program.outputs.fills) {
38
48
  if (band.colorUpChannel !== null || band.colorDownChannel !== null) {
39
49
  return diagnosticFor('OS6024', AT_LOAD, {
40
50
  what: "a band's colour that is computed per bar",
41
- limit: "a chart's band takes one colour for the whole run",
51
+ limit: lacking(chart, 'bandColours', "a chart's band takes one colour for the whole run"),
42
52
  });
43
53
  }
44
54
  }
@@ -157,7 +157,7 @@ function checkRequest(
157
157
  checker.report('OS6001', timeframe?.span ?? call.span, { value: written });
158
158
  }
159
159
 
160
- const mode = modeOf(call);
160
+ const mode = modeOf(checked);
161
161
  if (mode === 'lookahead') {
162
162
  checker.report('OS8005', call.span, { mode });
163
163
  checker.repaints = true;
@@ -195,14 +195,24 @@ function timeframeArgument(call: Call, name: string): Argument | undefined {
195
195
  return positional[name === 'req.symbol' ? 1 : 0];
196
196
  }
197
197
 
198
- function modeOf(call: Call): RequestMode {
199
- const labelled = call.args.find((one) => one.label?.text === 'mode');
200
- const written = labelled === undefined ? undefined : stringLiteral(labelled.value);
198
+ /**
199
+ * The mode as the signature bound it, written by its label or in its place.
200
+ *
201
+ * `language.md` 11.2 fills parameters from the left, so `mode` is the third
202
+ * argument of `req.timeframe` and the fifth of `req.symbol` whether or not it
203
+ * carries its label, and the emitter reads every other argument of a read from
204
+ * the same binding. Looking for the label alone read a positional
205
+ * `"lookahead"` as the default: the study ran confirmed, with no OS8005 and no
206
+ * repaint mark, while its source named the mode on the line.
207
+ */
208
+ function modeOf(checked: CheckedCall): RequestMode {
209
+ const bound = argumentOf(checked, 'mode');
210
+ const written = bound === undefined ? undefined : stringLiteral(bound.value);
201
211
  if (written === 'confirmed' || written === 'developing' || written === 'lookahead') {
202
212
  return written;
203
213
  }
204
214
  // The default is the one mode that never repaints (stdlib.md 15.3).
205
- return labelled === undefined ? 'confirmed' : 'unknown';
215
+ return bound === undefined ? 'confirmed' : 'unknown';
206
216
  }
207
217
 
208
218
  function stringLiteral(expression: Expression): string | undefined {
@@ -4,7 +4,7 @@
4
4
  // spec/compiled-program.md, so neither can drift from the thing it names.
5
5
 
6
6
  /** This build's package version. The release refuses to publish unless the tag, the manifest and this agree. */
7
- export const VERSION = "0.7.2";
7
+ export const VERSION = "0.8.1";
8
8
 
9
9
  /**
10
10
  * The compiled program format this compiler emits.