oakscriptjs 0.15.3 → 0.16.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/dist/cjs/check/format.d.ts.map +1 -1
- package/dist/cjs/check/format.js +5 -0
- package/dist/cjs/check/format.js.map +2 -2
- package/dist/cjs/check/harness.d.ts +24 -0
- package/dist/cjs/check/harness.d.ts.map +1 -1
- package/dist/cjs/check/harness.js +33 -0
- package/dist/cjs/check/harness.js.map +2 -2
- package/dist/cjs/check/index.d.ts +9 -0
- package/dist/cjs/check/index.d.ts.map +1 -1
- package/dist/cjs/check/index.js +7 -0
- package/dist/cjs/check/index.js.map +2 -2
- package/dist/cjs/color/index.d.ts.map +1 -1
- package/dist/cjs/color/index.js +19 -2
- package/dist/cjs/color/index.js.map +2 -2
- package/dist/cjs/color/parse.d.ts +8 -6
- package/dist/cjs/color/parse.d.ts.map +1 -1
- package/dist/cjs/color/parse.js +15 -2
- package/dist/cjs/color/parse.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +68 -0
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +99 -9
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/runtime/series.d.ts +16 -0
- package/dist/cjs/runtime/series.d.ts.map +1 -1
- package/dist/cjs/runtime/series.js +19 -2
- package/dist/cjs/runtime/series.js.map +2 -2
- package/dist/cjs/script/closure-writes.d.ts +16 -0
- package/dist/cjs/script/closure-writes.d.ts.map +1 -0
- package/dist/cjs/script/closure-writes.js +435 -0
- package/dist/cjs/script/closure-writes.js.map +7 -0
- package/dist/cjs/script/globals.d.ts +2 -0
- package/dist/cjs/script/globals.d.ts.map +1 -1
- package/dist/cjs/script/globals.js.map +1 -1
- package/dist/cjs/script/incremental.d.ts +140 -0
- package/dist/cjs/script/incremental.d.ts.map +1 -0
- package/dist/cjs/script/incremental.js +142 -0
- package/dist/cjs/script/incremental.js.map +7 -0
- package/dist/cjs/script/index.d.ts +116 -31
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +471 -25
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/cjs/str/numberformat.d.ts.map +1 -1
- package/dist/cjs/str/numberformat.js +23 -0
- package/dist/cjs/str/numberformat.js.map +2 -2
- package/dist/cjs/strategy/index.d.ts +8 -0
- package/dist/cjs/strategy/index.d.ts.map +1 -1
- package/dist/cjs/strategy/index.js.map +1 -1
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +72 -3
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/ta/step.d.ts +476 -0
- package/dist/cjs/ta/step.d.ts.map +1 -0
- package/dist/cjs/ta/step.js +1000 -0
- package/dist/cjs/ta/step.js.map +7 -0
- package/dist/cjs/ta-series.d.ts +1 -1
- package/dist/cjs/ta-series.js +3 -3
- package/dist/cjs/ta-series.js.map +1 -1
- package/dist/esm/check/format.d.ts.map +1 -1
- package/dist/esm/check/format.js +5 -0
- package/dist/esm/check/format.js.map +1 -1
- package/dist/esm/check/harness.d.ts +24 -0
- package/dist/esm/check/harness.d.ts.map +1 -1
- package/dist/esm/check/harness.js +40 -6
- package/dist/esm/check/harness.js.map +1 -1
- package/dist/esm/check/index.d.ts +9 -0
- package/dist/esm/check/index.d.ts.map +1 -1
- package/dist/esm/check/index.js +11 -1
- package/dist/esm/check/index.js.map +1 -1
- package/dist/esm/color/index.d.ts.map +1 -1
- package/dist/esm/color/index.js +23 -3
- package/dist/esm/color/index.js.map +1 -1
- package/dist/esm/color/parse.d.ts +8 -6
- package/dist/esm/color/parse.d.ts.map +1 -1
- package/dist/esm/color/parse.js +19 -2
- package/dist/esm/color/parse.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +68 -0
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +156 -10
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/runtime/series.d.ts +16 -0
- package/dist/esm/runtime/series.d.ts.map +1 -1
- package/dist/esm/runtime/series.js +21 -1
- package/dist/esm/runtime/series.js.map +1 -1
- package/dist/esm/script/closure-writes.d.ts +16 -0
- package/dist/esm/script/closure-writes.d.ts.map +1 -0
- package/dist/esm/script/closure-writes.js +323 -0
- package/dist/esm/script/closure-writes.js.map +1 -0
- package/dist/esm/script/globals.d.ts +2 -0
- package/dist/esm/script/globals.d.ts.map +1 -1
- package/dist/esm/script/globals.js.map +1 -1
- package/dist/esm/script/incremental.d.ts +140 -0
- package/dist/esm/script/incremental.d.ts.map +1 -0
- package/dist/esm/script/incremental.js +173 -0
- package/dist/esm/script/incremental.js.map +1 -0
- package/dist/esm/script/index.d.ts +116 -31
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +561 -24
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/str/numberformat.d.ts.map +1 -1
- package/dist/esm/str/numberformat.js +30 -0
- package/dist/esm/str/numberformat.js.map +1 -1
- package/dist/esm/strategy/index.d.ts +8 -0
- package/dist/esm/strategy/index.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +79 -4
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/ta/step.d.ts +476 -0
- package/dist/esm/ta/step.d.ts.map +1 -0
- package/dist/esm/ta/step.js +1089 -0
- package/dist/esm/ta/step.js.map +1 -0
- package/dist/esm/ta-series.d.ts +1 -1
- package/dist/esm/ta-series.js +4 -4
- package/package.json +1 -1
- package/skills/oakscript/SKILL.md +2 -2
- package/skills/oakscript/reference/check.md +5 -0
- package/skills/oakscript/reference/errors.md +0 -1
- package/skills/oakscript/reference/pine-differences.md +1 -2
- package/skills/oakscript/reference/plotting.md +5 -7
- package/skills/oakscript/reference/script-structure.md +6 -0
- package/src/check/format.ts +7 -0
- package/src/check/harness.ts +57 -0
- package/src/check/index.ts +16 -0
- package/src/color/index.ts +22 -3
- package/src/color/parse.ts +28 -3
- package/src/drawing/registry.ts +172 -10
- package/src/index.ts +9 -2
- package/src/runtime/series.ts +33 -1
- package/src/script/closure-writes.ts +279 -0
- package/src/script/globals.ts +2 -0
- package/src/script/incremental.ts +246 -0
- package/src/script/index.ts +695 -26
- package/src/str/numberformat.ts +29 -0
- package/src/strategy/index.ts +8 -0
- package/src/ta/running-sum.ts +78 -3
- package/src/ta/step.ts +1145 -0
- package/src/ta-series.ts +4 -4
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* State of an incremental script run (#196, createScriptRun).
|
|
3
|
+
*
|
|
4
|
+
* A run executes the script body again on each update, but what the body creates (Series, ta.* calls, eachBar()
|
|
5
|
+
* blocks, plots, persist() boxes) is matched by creation order with the previous run, and keeps the values of the bars
|
|
6
|
+
* before the first bar to calculate (`from`, the forming bar of the previous run). Only the bars from `from` on are
|
|
7
|
+
* calculated:
|
|
8
|
+
* - a Series: its values before `from` are those of the previous run, the others are computed
|
|
9
|
+
* - a ta.* call with a {@link taStep} machine: the machine holds the state of the last confirmed bar and steps the newest
|
|
10
|
+
* bars only
|
|
11
|
+
* - an eachBar() block: its callback runs from `from` on, its persist() boxes and history buffers put back to their
|
|
12
|
+
* state at the end of the last confirmed bar
|
|
13
|
+
* - a plot: the points of the bars before `from` are kept
|
|
14
|
+
*
|
|
15
|
+
* A feature that is not resumed (drawings, other symbols, strategies, varip, values that depend on the bar count)
|
|
16
|
+
* calls {@link nonIncremental}: a recorded (full) run marks the run so that the next updates are full runs, a resumed
|
|
17
|
+
* run stops ({@link ResumeAborted}) and the host runs it in full.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
import { Series, setSeriesRunHooks, type SeriesRunHooks } from '../runtime/series.js';
|
|
22
|
+
import type { Bar } from '../types/index.js';
|
|
23
|
+
import type { BarStep } from '../ta/step.js';
|
|
24
|
+
import { isDrawingObject, setDrawingRunHooks, type DrawingCheckpoint, type DrawingKind, type DrawingRunHooks } from '../drawing/registry.js';
|
|
25
|
+
|
|
26
|
+
/** A resumed run met a feature it does not resume: the update is calculated by a full run. */
|
|
27
|
+
export class ResumeAborted extends Error {
|
|
28
|
+
constructor(readonly reason: string) {
|
|
29
|
+
super(`incremental run stopped: ${reason}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** History buffer state of one source of an eachBar() block (RE10008). */
|
|
34
|
+
export interface BufferState {
|
|
35
|
+
size: number;
|
|
36
|
+
previous: number;
|
|
37
|
+
barMax: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** State of an eachBar() block at the end of the last confirmed bar. */
|
|
41
|
+
export interface BlockCheckpoint {
|
|
42
|
+
/** Values of the persist() boxes the block owns, by box slot */
|
|
43
|
+
boxes: Map<number, unknown>;
|
|
44
|
+
/** History buffers by source key (Series slot, or the Series of the chart) */
|
|
45
|
+
buffers: Map<unknown, BufferState>;
|
|
46
|
+
pendingError: { bar: number; barsBack: number } | null;
|
|
47
|
+
/** The drawing registry and its objects (the block that draws puts it back) */
|
|
48
|
+
drawings: DrawingCheckpoint | null;
|
|
49
|
+
/** State the block keeps besides the script's (the strategy engine of strategy.eachBar()) */
|
|
50
|
+
extra?: unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface BlockMemory {
|
|
54
|
+
out: number[];
|
|
55
|
+
checkpoint: BlockCheckpoint;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A ta.* call with a machine: its machines (state of the last confirmed bar) and output values. */
|
|
59
|
+
export interface TaMemory {
|
|
60
|
+
kind: string;
|
|
61
|
+
machine: BarStep<unknown[], unknown>;
|
|
62
|
+
outs: number[][];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A plot's points and, for each bar j, the number of points before it. */
|
|
66
|
+
export interface PlotMemory {
|
|
67
|
+
data: Array<{ time: number; value: number; color?: string }>;
|
|
68
|
+
pos: number[];
|
|
69
|
+
/** Offset and colour kind of the plot: the points are kept only for the same */
|
|
70
|
+
sig: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What a run keeps for the next update. */
|
|
74
|
+
export interface RunMemory {
|
|
75
|
+
/** Bars of that run */
|
|
76
|
+
n: number;
|
|
77
|
+
/** Values of the Series the script created, by slot */
|
|
78
|
+
series: Array<number[] | undefined>;
|
|
79
|
+
/** Values of the Series that live across runs (the chart's open, close, bar_index...) */
|
|
80
|
+
kept: WeakMap<Series, number[]>;
|
|
81
|
+
ta: Array<TaMemory | undefined>;
|
|
82
|
+
blocks: Array<BlockMemory | undefined>;
|
|
83
|
+
plots: Array<PlotMemory | undefined>;
|
|
84
|
+
/** Block slot that owns each persist() box */
|
|
85
|
+
boxOwner: Array<number | undefined>;
|
|
86
|
+
/** Drawing objects created in the script body (outside the callbacks), by creation order */
|
|
87
|
+
bodyDrawings: Array<{ kind: DrawingKind; obj: object } | undefined>;
|
|
88
|
+
/** The eachBar() block that uses the drawings (one per script), undefined when none */
|
|
89
|
+
drawingBlock: number | undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function emptyMemory(n: number): RunMemory {
|
|
93
|
+
return { n, series: [], kept: new WeakMap(), ta: [], blocks: [], plots: [], boxOwner: [], bodyDrawings: [], drawingBlock: undefined };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface ActiveRun {
|
|
97
|
+
mode: 'record' | 'resume';
|
|
98
|
+
/** First bar calculated (0 when recording) */
|
|
99
|
+
from: number;
|
|
100
|
+
bars: readonly Bar[];
|
|
101
|
+
prev: RunMemory | null;
|
|
102
|
+
next: RunMemory;
|
|
103
|
+
seriesSlot: number;
|
|
104
|
+
taSlot: number;
|
|
105
|
+
blockSlot: number;
|
|
106
|
+
plotSlot: number;
|
|
107
|
+
boxSlot: number;
|
|
108
|
+
drawSlot: number;
|
|
109
|
+
/** The eachBar() block running (its slot), -1 outside */
|
|
110
|
+
block: number;
|
|
111
|
+
/** Getters and setters of the persist() boxes of this run, by slot */
|
|
112
|
+
boxes: Array<{ get(): unknown; set(v: unknown): void }>;
|
|
113
|
+
/** Why the next updates are full runs (null: they can be resumed) */
|
|
114
|
+
blocked: string | null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The incremental run being executed (null outside createScriptRun or in a plain executeScript). */
|
|
118
|
+
export let active: ActiveRun | null = null;
|
|
119
|
+
|
|
120
|
+
const SLOT = Symbol('series slot');
|
|
121
|
+
|
|
122
|
+
/** Slot of a Series created by the script in this run, or undefined (a chart Series, or one made inside eachBar) */
|
|
123
|
+
export function slotOf(series: Series): number | undefined {
|
|
124
|
+
return (series as unknown as Record<symbol, number | undefined>)[SLOT];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const hooks: SeriesRunHooks = {
|
|
128
|
+
created(series) {
|
|
129
|
+
const run = active;
|
|
130
|
+
// A Series made in an eachBar() callback is made again on each bar: it is not matched with the previous run.
|
|
131
|
+
if (!run || run.block >= 0) return;
|
|
132
|
+
(series as unknown as Record<symbol, number>)[SLOT] = run.seriesSlot++;
|
|
133
|
+
},
|
|
134
|
+
prefix(series, bars) {
|
|
135
|
+
const run = active;
|
|
136
|
+
if (!run || run.mode !== 'resume' || bars !== run.bars || !run.prev) return null;
|
|
137
|
+
const slot = slotOf(series);
|
|
138
|
+
const values = slot !== undefined ? run.prev.series[slot] : run.prev.kept.get(series);
|
|
139
|
+
return values && values.length >= run.from ? { values, from: run.from } : null;
|
|
140
|
+
},
|
|
141
|
+
computed(series, values) {
|
|
142
|
+
const run = active;
|
|
143
|
+
if (!run || run.block >= 0) return;
|
|
144
|
+
const slot = slotOf(series);
|
|
145
|
+
if (slot !== undefined) run.next.series[slot] = values;
|
|
146
|
+
else run.next.kept.set(series, values);
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Drawings in an incremental run: one eachBar() block uses them (its checkpoint holds the registry and the state of
|
|
152
|
+
* the objects); the objects created in the script body before the blocks are the same objects on each run (matched by
|
|
153
|
+
* creation order), set again to their new state. Drawings used by two blocks, or in the body after a block, are not
|
|
154
|
+
* resumed.
|
|
155
|
+
*/
|
|
156
|
+
const drawingHooks: DrawingRunHooks = {
|
|
157
|
+
access() {
|
|
158
|
+
const run = active;
|
|
159
|
+
if (!run) return;
|
|
160
|
+
if (run.block < 0) {
|
|
161
|
+
if (run.blockSlot > 0) nonIncremental('drawings in the script body after an eachBar() block');
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (run.next.drawingBlock === undefined) run.next.drawingBlock = run.block;
|
|
165
|
+
else if (run.next.drawingBlock !== run.block) nonIncremental('drawings in two eachBar() blocks');
|
|
166
|
+
},
|
|
167
|
+
reuse(kind, obj) {
|
|
168
|
+
const run = active;
|
|
169
|
+
if (!run || run.block >= 0) return null;
|
|
170
|
+
const slot = run.drawSlot++;
|
|
171
|
+
if (run.mode === 'record') {
|
|
172
|
+
run.next.bodyDrawings[slot] = { kind, obj };
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
const old = run.prev?.bodyDrawings[slot];
|
|
176
|
+
if (!old || old.kind !== kind) nonIncremental('a new drawing in the script body');
|
|
177
|
+
run.next.bodyDrawings[slot] = old;
|
|
178
|
+
return old!.obj;
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/** Starts an incremental run (the script body runs next); returns the run to give to {@link endRun}. */
|
|
183
|
+
export function beginRun(run: ActiveRun): ActiveRun | null {
|
|
184
|
+
const outer = active;
|
|
185
|
+
active = run;
|
|
186
|
+
setSeriesRunHooks(hooks);
|
|
187
|
+
setDrawingRunHooks(drawingHooks);
|
|
188
|
+
return outer;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Ends the run started by {@link beginRun}, putting back the outer one (a nested run) or none. */
|
|
192
|
+
export function endRun(outer: ActiveRun | null): void {
|
|
193
|
+
active = outer;
|
|
194
|
+
setSeriesRunHooks(outer ? hooks : null);
|
|
195
|
+
setDrawingRunHooks(outer ? drawingHooks : null);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The script body has run: the run builds its outputs (they read the drawings without being part of the script). */
|
|
199
|
+
export function bodyDone(): void {
|
|
200
|
+
setDrawingRunHooks(null);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** No incremental run inside (a nested executeScript, e.g. other symbols): the outer one is put back by endRun(). */
|
|
204
|
+
export function suspendRun(): ActiveRun | null {
|
|
205
|
+
const outer = active;
|
|
206
|
+
active = null;
|
|
207
|
+
setSeriesRunHooks(null);
|
|
208
|
+
setDrawingRunHooks(null);
|
|
209
|
+
return outer;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* A copy of a persist() value for a checkpoint: the forming bar may change an array or an object in place after it was
|
|
214
|
+
* taken. Plain data is copied (structuredClone); a value that cannot be copied (a function, a class instance such as
|
|
215
|
+
* a drawing) is kept as it is and marks the run as not resumable.
|
|
216
|
+
*/
|
|
217
|
+
export function copyValue(value: unknown): unknown {
|
|
218
|
+
// a drawing object is shared: the checkpoint of the registry puts its state back
|
|
219
|
+
if (isDrawingObject(value)) return value;
|
|
220
|
+
if (value === null || typeof value !== 'object') {
|
|
221
|
+
if (typeof value === 'function') nonIncremental('a function in persist()');
|
|
222
|
+
return value;
|
|
223
|
+
}
|
|
224
|
+
if (Array.isArray(value)) return value.map(copyValue);
|
|
225
|
+
if (value instanceof Map) return new Map([...value].map(([k, v]) => [copyValue(k), copyValue(v)]));
|
|
226
|
+
if (value instanceof Set) return new Set([...value].map(copyValue));
|
|
227
|
+
const proto = Object.getPrototypeOf(value);
|
|
228
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
229
|
+
nonIncremental('a class instance in persist()');
|
|
230
|
+
return value;
|
|
231
|
+
}
|
|
232
|
+
const out: Record<string, unknown> = {};
|
|
233
|
+
for (const [k, v] of Object.entries(value)) out[k] = copyValue(v);
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* A feature an incremental run does not resume: a recorded run keeps its result (it is a full run) and marks the next
|
|
239
|
+
* updates as full runs; a resumed run stops.
|
|
240
|
+
*/
|
|
241
|
+
export function nonIncremental(reason: string): void {
|
|
242
|
+
const run = active;
|
|
243
|
+
if (!run) return;
|
|
244
|
+
if (run.mode === 'resume') throw new ResumeAborted(reason);
|
|
245
|
+
run.blocked ??= reason;
|
|
246
|
+
}
|