@tradecanvas/script 0.0.0-stage → 0.2.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +125 -2
  3. package/dist/Column.d.ts +21 -0
  4. package/dist/Column.d.ts.map +1 -0
  5. package/dist/Ring.d.ts +28 -0
  6. package/dist/Ring.d.ts.map +1 -0
  7. package/dist/ScriptRun.d.ts +85 -0
  8. package/dist/ScriptRun.d.ts.map +1 -0
  9. package/dist/defineScript.d.ts +216 -0
  10. package/dist/defineScript.d.ts.map +1 -0
  11. package/dist/drawings.d.ts +46 -0
  12. package/dist/drawings.d.ts.map +1 -0
  13. package/dist/index.cjs +2 -0
  14. package/dist/index.cjs.map +1 -0
  15. package/dist/index.d.ts +20 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +1726 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/limits.d.ts +30 -0
  20. package/dist/limits.d.ts.map +1 -0
  21. package/dist/outputs.d.ts +129 -0
  22. package/dist/outputs.d.ts.map +1 -0
  23. package/dist/points.d.ts +13 -0
  24. package/dist/points.d.ts.map +1 -0
  25. package/dist/scope.d.ts +33 -0
  26. package/dist/scope.d.ts.map +1 -0
  27. package/dist/scriptIndicator.d.ts +25 -0
  28. package/dist/scriptIndicator.d.ts.map +1 -0
  29. package/dist/security.d.ts +52 -0
  30. package/dist/security.d.ts.map +1 -0
  31. package/dist/state.d.ts +52 -0
  32. package/dist/state.d.ts.map +1 -0
  33. package/dist/ta/averages.d.ts +19 -0
  34. package/dist/ta/averages.d.ts.map +1 -0
  35. package/dist/ta/common.d.ts +24 -0
  36. package/dist/ta/common.d.ts.map +1 -0
  37. package/dist/ta/events.d.ts +16 -0
  38. package/dist/ta/events.d.ts.map +1 -0
  39. package/dist/ta/index.d.ts +65 -0
  40. package/dist/ta/index.d.ts.map +1 -0
  41. package/dist/ta/moves.d.ts +26 -0
  42. package/dist/ta/moves.d.ts.map +1 -0
  43. package/dist/ta/oscillators.d.ts +29 -0
  44. package/dist/ta/oscillators.d.ts.map +1 -0
  45. package/dist/ta/ranges.d.ts +18 -0
  46. package/dist/ta/ranges.d.ts.map +1 -0
  47. package/dist/ta/statistics.d.ts +26 -0
  48. package/dist/ta/statistics.d.ts.map +1 -0
  49. package/dist/worker/messages.d.ts +49 -0
  50. package/dist/worker/messages.d.ts.map +1 -0
  51. package/dist/worker/scriptWorkerIndicator.d.ts +40 -0
  52. package/dist/worker/scriptWorkerIndicator.d.ts.map +1 -0
  53. package/dist/worker/serveScripts.d.ts +28 -0
  54. package/dist/worker/serveScripts.d.ts.map +1 -0
  55. package/package.json +39 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 TradeCanvas Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,126 @@
1
- # Temporary Holding Version
1
+ # @tradecanvas/script
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Indicators written bar by bar for [TradeCanvas](https://tradecanvas.dev/): state kept from bar to bar, past values read back, `ta` sums that run one bar at a time, and a live bar run again on each tick.
4
+
5
+ ```bash
6
+ npm install @tradecanvas/script @tradecanvas/chart
7
+ ```
8
+
9
+ Before 1.0, its API can still change between minor versions.
10
+
11
+ ```ts
12
+ import { defineScript, scriptIndicator, ta } from '@tradecanvas/script';
13
+
14
+ const emaCross = defineScript({
15
+ id: 'emaCross',
16
+ name: 'EMA Cross',
17
+ overlay: true,
18
+ inputs: { fast: { default: 9, kind: 'int', min: 1 }, slow: { default: 21, kind: 'int', min: 1 } },
19
+ setup({ inputs, plot, shape }) {
20
+ const fast = ta.ema(inputs.fast);
21
+ const slow = ta.ema(inputs.slow);
22
+ const up = ta.crossover();
23
+ const fastLine = plot('Fast', { color: '#f5c542' });
24
+ const slowLine = plot('Slow', { color: '#3b82f6' });
25
+ const mark = shape('Cross up', { shape: 'triangleUp', position: 'belowBar', color: '#22c55e' });
26
+ return (bar) => {
27
+ const f = fastLine.set(fast(bar.close));
28
+ const s = slowLine.set(slow(bar.close));
29
+ mark.set(up(f, s));
30
+ };
31
+ },
32
+ });
33
+
34
+ chart.registerIndicator(scriptIndicator(emaCross));
35
+ chart.addIndicator('emaCross');
36
+ ```
37
+
38
+ ## How it runs
39
+
40
+ - `setup` runs once: it declares the plots, marks and levels, makes the `ta` sums and the kept state (`state.var`, `state.array`, `state.series`), and returns what each bar runs.
41
+ - Each `ta` sum keeps its own state: two calls of `ta.ema(9)` are two averages. Make them in `setup`, never on a bar. Their values are the chart's built-in indicators' (the MACD's averages start as simple ones).
42
+ - The closed bars run once. The last bar stays live: a tick runs it again from the state the bar before left, so a tick costs one bar. When a new bar comes, the live one runs once more as a closed bar (`bar.live` false), so a feed ends where loading its bars does.
43
+ - `ScriptRun` runs a script without a chart (`load`, `tick`, `next`, `update`); its output is a typed array a plot. `new ScriptRun(script, params, { exchangeTimeZone, sessionStart })` counts days on the exchange's clock.
44
+ - Only what the script keeps through `state` and the `ta` sums is put back before a bar runs again (a tick, or the bar closing). A plain `let` in `setup` is not: keep values in `state.var`.
45
+ - Feed each `ta` sum on every bar for the values the chart's indicators have: a sum fed on some bars only averages those bars.
46
+ - A missing value (NaN) is handled as `@tradecanvas/core`'s `ta` handles it: a simple or weighted average needs its period of numbers in a row, a smoothing skips it, and RSI, MACD, the bands, HMA, ROC and momentum compute over it as the last value and give none there.
47
+ - `ta.macd`'s averages start as simple ones, like every EMA here; the chart's built-in MACD starts its own at the first close, so their first hundred bars or so differ by a hair.
48
+ - A bar that throws stops the script there; `output.errors` (`meta.errors` of the indicator's output) says which bar and why. A tick on the live bar runs it again; once that bar closes, the script stays stopped until the bars are loaded again.
49
+
50
+ ## The ta sums
51
+
52
+ Each is made in `setup` with its settings, then given a value each bar.
53
+
54
+ - Averages: `sma`, `ema`, `rma`, `wma`, `vwma` (value, volume), `hma`, `alma(period, offset, sigma)`, `swma` (four values, weighted 1, 2, 2, 1), `linreg(period, offset)`.
55
+ - Over a window: `sum`, `stdev`, `variance`, `dev` (mean distance from the mean), `median`, `percentileNearestRank(period, percentage)`, `percentileLinearInterpolation(period, percentage)`, `percentrank` (the share of the bars before at or under this one), `correlation` (two values), `highest`, `lowest`, `highestbars` and `lowestbars` (how many bars back the extreme is, 0 or less).
56
+ - Running: `cum` (the total so far), `change`, `mom`, `roc`, `barssince`, `valuewhen`.
57
+ - Oscillators: `rsi`, `macd`, `bb`, `stoch` (value, high, low: the raw %K), `mfi` (value, volume).
58
+ - Of the bars: `tr`, `atr`, `supertrend`, `sar(start, increment, max)`, `obv` (close, volume).
59
+ - Events: `crossover`, `crossunder`, `cross`, `rising(period)` and `falling(period)` (over, or under, every one of the bars before), `pivothigh`, `pivotlow`.
60
+
61
+ ALMA, `linreg`, the stochastic, MFI, OBV and `sar` give the values of the built-in indicators (ALMA, LSMA, Stochastic, MFI, OBV, Parabolic SAR); `sar` starts on the second bar, its first trend read from the first two closes. A window with a missing value in it gives none; `cum` passes over one.
62
+
63
+ `bar.live` is true on the live bar, the last one, which a tick runs again, and false once it closes.
64
+
65
+ `chart.timeZone` in `setup` is the exchange's zone (`'UTC'` when the chart names none) and `chart.interval` its interval (`'5m'`, null when not known): a script that reads the interval in `setup` is run again when it changes (what it declares, its plots and levels, may not depend on it), and any script is when the zone does. A run without a chart is told by its options, `new ScriptRun(script, params, { exchangeTimeZone, interval })`.
66
+
67
+ ## Colours, fills, backgrounds
68
+
69
+ - `plot.set(value, color)` gives a bar its own colour; the line's stretch to that bar takes it.
70
+ - `fill(plotA, plotB, { color })` shades between two plots; `.set(color)` gives a bar its own colour. A colour with an alpha of its own (`'#22c55e33'`) keeps it.
71
+ - `bgcolor()` returns a background: `.set(color)` shades the pane behind that bar, behind the candles and the levels.
72
+ - `plot(title, { kind: 'area' })` shades the space down to zero under the line.
73
+ - `barcolor()` returns a handle: `.set(color)` paints that bar's candle, in a script drawn over the prices (`overlay: true`), on a chart of candles or OHLC bars.
74
+ - `shape(title, { position: 'top' })` marks the top or the bottom of the pane; in a pane of its own, `'aboveBar'` and `'belowBar'` are the top and the bottom.
75
+
76
+ ## Labels, lines and boxes
77
+
78
+ `drawings({ maxLabels, maxLines, maxBoxes })` in `setup` gives a list to draw on: on a bar, `label({ at, text })`, `line({ from, to, extend })` and `box({ from, to, fill })` each return a handle to `set()` or `remove()` later. A point is `{ bar, value }`, `bar` being a bar's index (`bar.index - 5`). Past a kind's limit (50 unless set, 500 at most) the oldest goes. Keep a handle across bars in `state.var`, so a tick puts it back with the shapes.
79
+
80
+ ## Alerts
81
+
82
+ `alertCondition(title, { message })` in `setup` says a moment (`.set(true)` on the bars it holds). The widget's alert form lists it as "<name>: <title>", and an alert set on it fires as it turns true.
83
+
84
+ ## Higher intervals
85
+
86
+ `request.security('1h', () => { const avg = ta.sma(20); return (bar) => avg(bar.close); })` in `setup` gives a function to call on every bar: the value of that interval's bars, made of the chart's own (a resample of them, days cut on the exchange's clock). A bar reads the value of the last bar of the higher interval that closed before its own, which never changes after; with `{ live: true }`, the forming bar's as it was then, which repaints. Neither reads a bar after the chart's own. Same symbol only, for now.
87
+
88
+ ## Levels
89
+
90
+ `hline(70, { color: '#ef4444', lineStyle: 'solid', title: 'Overbought' })` in `setup` draws a level across the pane in a colour and dash of its own; without a colour it is faint and dashed, as the chart's levels are. The title is not drawn.
91
+
92
+ ## Limits
93
+
94
+ A run stops a script at the bar that goes past one of its limits, and says which in `errors` (`limit: 'loop' | 'array' | 'time'`):
95
+
96
+ - **Loop steps**: `loop()`, called once each time round a loop, counts the bar's steps; past a million (`loopSteps`) the bar stops, so a loop that never ends does not hang the chart.
97
+ - **Lists**: a `state.array` holds at most 100,000 numbers (`arraySize`). A list that takes a number every bar and is never cut needs a larger `arraySize` on a longer chart, or a `shift()` to keep it short.
98
+ - **Time**: with `runMs` set, a `load`, `tick` or `next` that is past that many ms after a bar stops there.
99
+
100
+ Set them with `new ScriptRun(script, params, { limits: { loopSteps: 10_000, runMs: 200 } })`, or `scriptIndicator(script, { limits })`. A higher interval's run keeps to the same loop steps and list sizes; its time counts in the script's. A script that catches the error and goes on is stopped all the same. A bar that never returns, a loop without `loop()`, is not stopped on the page's thread: that needs a worker.
101
+
102
+ ## In a worker
103
+
104
+ A script can run in a worker, so a bar that never returns cannot hang the page. The worker's module serves the scripts, and the page registers each through `scriptWorkerIndicator`:
105
+
106
+ ```ts
107
+ // scripts.worker.ts
108
+ import { serveScripts } from '@tradecanvas/script'
109
+ import { emaCross } from './emaCross'
110
+ serveScripts([emaCross])
111
+
112
+ // the page
113
+ chart.registerIndicator(scriptWorkerIndicator(emaCross, {
114
+ spawn: () => new Worker(new URL('./scripts.worker.ts', import.meta.url), { type: 'module' }),
115
+ timeoutMs: 5000,
116
+ }))
117
+ ```
118
+
119
+ The page sends the bars once, then one bar a tick, and draws the points the worker sends back from that bar on; until the first answer the indicator draws nothing. A worker that does not answer one request within `timeoutMs` (5 seconds unless set, the first load of the bars included) is stopped: the script it hung on says so (`limit: 'time'`) and loads again only for other inputs or other bars; the others load again in a new worker. A removed indicator's run goes with it, and the worker once it runs nothing. The setup also runs once on the page, to know what the script draws. `limits` are passed on to the worker's runs.
120
+
121
+ ## Not there yet
122
+
123
+ - Other symbols in `request.security`.
124
+ - Pine Script source: a compiler to these scripts is planned.
125
+
126
+ The guide: [tradecanvas.dev/docs/scripts](https://tradecanvas.dev/docs/scripts).
@@ -0,0 +1,21 @@
1
+ type TypedArray = Float64Array | Uint16Array | Uint8Array;
2
+ /** A value a bar that grows with the bars, in a typed array. */
3
+ export declare class Column<T extends TypedArray> {
4
+ private readonly make;
5
+ private readonly empty;
6
+ private data;
7
+ private filled;
8
+ constructor(make: (capacity: number) => T, empty: number);
9
+ /** Set bar `index`, growing to it. */
10
+ set(index: number, value: number): void;
11
+ get(index: number): number;
12
+ /** Make room for `length` bars, the new ones empty. */
13
+ reach(length: number): void;
14
+ /** Its first `length` bars, without copying them: good until a bar is set past what it holds. */
15
+ view(length: number): T;
16
+ }
17
+ export declare const floatColumn: () => Column<Float64Array>;
18
+ export declare const colorColumn: () => Column<Uint16Array>;
19
+ export declare const flagColumn: () => Column<Uint8Array>;
20
+ export {};
21
+ //# sourceMappingURL=Column.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Column.d.ts","sourceRoot":"","sources":["../src/Column.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GAAG,YAAY,GAAG,WAAW,GAAG,UAAU,CAAC;AAI1D,gEAAgE;AAChE,qBAAa,MAAM,CAAC,CAAC,SAAS,UAAU;IAKpC,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,KAAK;IALxB,OAAO,CAAC,IAAI,CAAI;IAChB,OAAO,CAAC,MAAM,CAAK;gBAGA,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,CAAC,EAC7B,KAAK,EAAE,MAAM;IAMhC,sCAAsC;IACtC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAKvC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI1B,uDAAuD;IACvD,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAY3B,iGAAiG;IACjG,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC;CAIxB;AAED,eAAO,MAAM,WAAW,QAAO,MAAM,CAAC,YAAY,CAAuD,CAAC;AAC1G,eAAO,MAAM,WAAW,QAAO,MAAM,CAAC,WAAW,CAA6C,CAAC;AAC/F,eAAO,MAAM,UAAU,QAAO,MAAM,CAAC,UAAU,CAA4C,CAAC"}
package/dist/Ring.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The last values a sum was given, a missing one (NaN) included, so that
3
+ * "the value `n` calls back" is where the whole series would have it.
4
+ *
5
+ * On the live bar it records each slot it writes over and puts it back on a
6
+ * rollback: a tick that pushes and a later tick that does not (a condition
7
+ * true, then false) still read what the bars before left.
8
+ */
9
+ export declare class Ring {
10
+ readonly size: number;
11
+ private readonly slots;
12
+ private count;
13
+ private run;
14
+ private kept;
15
+ /** The slots written over on the live bar, as [slot, value before], oldest first. */
16
+ private overwritten;
17
+ constructor(size: number);
18
+ private readonly live;
19
+ /** Take the next value. */
20
+ push(value: number): void;
21
+ /** The value `back` values before the last one given (0: the last); NaN before the first. */
22
+ at(back: number): number;
23
+ /** How many values were given. */
24
+ get given(): number;
25
+ /** How many values in a row, up to the last, were numbers. */
26
+ get numbersInRow(): number;
27
+ }
28
+ //# sourceMappingURL=Ring.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Ring.d.ts","sourceRoot":"","sources":["../src/Ring.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,qBAAa,IAAI;IAQH,QAAQ,CAAC,IAAI,EAAE,MAAM;IAPjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IACrC,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,GAAG,CAAK;IAChB,OAAO,CAAC,IAAI,CAAwB;IACpC,qFAAqF;IACrF,OAAO,CAAC,WAAW,CAAgB;gBAEd,IAAI,EAAE,MAAM;IAkBjC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgB;IAErC,2BAA2B;IAC3B,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAQzB,6FAA6F;IAC7F,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAMxB,kCAAkC;IAClC,IAAI,KAAK,IAAI,MAAM,CAElB;IAED,8DAA8D;IAC9D,IAAI,YAAY,IAAI,MAAM,CAEzB;CACF"}
@@ -0,0 +1,85 @@
1
+ import { OHLCBar, TimeFrame } from '@tradecanvas/commons';
2
+ import { Script, ScriptInputs } from './defineScript.js';
3
+ import { ScriptDeclarations, ScriptError, ScriptOutput } from './outputs.js';
4
+ import { ScriptLimits } from './limits.js';
5
+ /** Where a run's days are counted, for a higher interval of days or longer. */
6
+ export interface ScriptRunOptions {
7
+ /** The exchange's IANA zone (`'Asia/Ho_Chi_Minh'`); unset: UTC. */
8
+ exchangeTimeZone?: string;
9
+ /** Minutes after midnight the trading day starts at (a 17:00 open: 1020); unset: midnight. */
10
+ sessionStart?: number;
11
+ /** The chart's interval (`'5m'`), which the script may read; unset: not known. */
12
+ interval?: TimeFrame;
13
+ /** How far the run may go: loop steps a bar, numbers a list, ms a run. */
14
+ limits?: ScriptLimits;
15
+ }
16
+ /** What `update` did with a bar: ran the live bar again, ran a new bar, or nothing (an older bar: load the bars again). */
17
+ export type UpdateKind = 'tick' | 'bar' | 'reload';
18
+ /**
19
+ * A script run over bars. `load` runs every bar; the last one stays live:
20
+ * `tick` runs it again from the state the bar before left, `next` closes it
21
+ * and runs a new bar. A tick costs one bar, whatever the bars before.
22
+ */
23
+ export declare class ScriptRun {
24
+ private readonly script;
25
+ private readonly params;
26
+ private readonly options;
27
+ private scope;
28
+ private outputs;
29
+ private onBar;
30
+ private prices;
31
+ private count;
32
+ private failures;
33
+ /** The bar the script threw on, after which no bar runs; -1: none. */
34
+ private haltedAt;
35
+ private declared;
36
+ /** Whether no bar has run since the setup: a load needs no setup of its own. */
37
+ private fresh;
38
+ /** What it draws, known from its setup. */
39
+ get declarations(): ScriptDeclarations;
40
+ /** The bars loaded last: what a higher interval reads the bars' clock from. */
41
+ private loaded;
42
+ private readonly limits;
43
+ /** When the `load`, `tick` or `next` running began, by `performance.now()`. */
44
+ private began;
45
+ private intervalRead;
46
+ constructor(script: Script<ScriptInputs>, params?: Readonly<Record<string, unknown>>, options?: ScriptRunOptions);
47
+ /** Where the script stopped, and why: none while it runs. */
48
+ get errors(): readonly ScriptError[];
49
+ /** How many bars it has run. */
50
+ get length(): number;
51
+ get output(): ScriptOutput;
52
+ /** Run all of `bars` from the start; the last one stays live. */
53
+ load(bars: readonly OHLCBar[]): void;
54
+ /** The live bar as it now is: run it again from the state the bar before left. */
55
+ tick(bar: OHLCBar): void;
56
+ /**
57
+ * Close the live bar, run once more as a closed one (`bar.live` false) so
58
+ * a feed ends where loading its bars does, and run `bar` as the new live
59
+ * one. A script that threw on the live bar stays stopped once it closes,
60
+ * as on any closed bar: load the bars again to run it anew.
61
+ */
62
+ next(bar: OHLCBar): void;
63
+ /** A bar from a feed: a tick when it has the live bar's time, a new bar when later. An earlier one is not run: load the bars again. */
64
+ update(bar: OHLCBar): UpdateKind;
65
+ /** Begin again from no bars: a new setup, new state. */
66
+ private start;
67
+ /** The chart as the script reads it, noting that the interval was. */
68
+ private chart;
69
+ /** Whether the script has read the chart's interval: in its setup, for one that has run no bar. */
70
+ get readsInterval(): boolean;
71
+ /** From here on the bar run is the live one: keep the state the bars before left. */
72
+ private goLive;
73
+ /** The live bar as it closed: run from the state the bar before left, as the closed bar it now is. */
74
+ private closeLive;
75
+ private priceSeries;
76
+ private write;
77
+ /** Bar `i` as its prices now stand. */
78
+ private barAt;
79
+ private runBar;
80
+ /** Start the clock of a `load`, `tick` or `next`, when the run has a time. */
81
+ private clockIn;
82
+ /** The time gone past, once a bar is done; null while within it. */
83
+ private overTime;
84
+ }
85
+ //# sourceMappingURL=ScriptRun.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ScriptRun.d.ts","sourceRoot":"","sources":["../src/ScriptRun.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,EAA4C,KAAK,MAAM,EAAoC,KAAK,YAAY,EAAqB,MAAM,mBAAmB,CAAC;AAClK,OAAO,EAAW,KAAK,kBAAkB,EAAE,KAAK,WAAW,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAKrG,OAAO,EAAyC,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAKvF,+EAA+E;AAC/E,MAAM,WAAW,gBAAgB;IAC/B,mEAAmE;IACnE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,8FAA8F;IAC9F,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kFAAkF;IAClF,QAAQ,CAAC,EAAE,SAAS,CAAC;IACrB,0EAA0E;IAC1E,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB;AAED,2HAA2H;AAC3H,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEnD;;;;GAIG;AACH,qBAAa,SAAS;IA0BlB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IA3B1B,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,OAAO,CAAW;IAC1B,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,KAAK,CAAK;IAClB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;IAcb,OAAO,CAAC,MAAM;IAuBd,8EAA8E;IAC9E,OAAO,CAAC,OAAO;IAIf,oEAAoE;IACpE,OAAO,CAAC,QAAQ;CAKjB"}
@@ -0,0 +1,216 @@
1
+ import { IndicatorInputSpec, IndicatorMarkerShape, LineStyle, TimeFrame } from '@tradecanvas/commons';
2
+ import { ScriptArray, ScriptSeries, Var } from './state.js';
3
+ import { DrawingLimits, ScriptDrawings } from './drawings.js';
4
+ import { SecurityOptions, SecuritySetup } from './security.js';
5
+ export type ScriptInputValue = number | string | boolean;
6
+ /** An input of a script: its default, and how the settings dialog shows it. */
7
+ export interface ScriptInput extends IndicatorInputSpec {
8
+ default: ScriptInputValue;
9
+ }
10
+ export type ScriptInputs = Record<string, ScriptInput>;
11
+ /** The values of a script's inputs, by name. */
12
+ export type InputValues<I extends ScriptInputs> = {
13
+ readonly [K in keyof I]: I[K]['default'] extends number ? number : I[K]['default'] extends boolean ? boolean : string;
14
+ };
15
+ /** The bar being run. */
16
+ export interface ScriptBar {
17
+ /** Its place among the bars, from 0. */
18
+ readonly index: number;
19
+ /** Its time, ms since the epoch. */
20
+ readonly time: number;
21
+ readonly open: number;
22
+ readonly high: number;
23
+ readonly low: number;
24
+ readonly close: number;
25
+ readonly volume: number;
26
+ /**
27
+ * The live bar: the last, run again on each tick until the next bar
28
+ * comes. False on the bars that closed, the live one too when the next
29
+ * bar comes: it is run once more as it closes.
30
+ */
31
+ readonly live: boolean;
32
+ }
33
+ /** A price of the bars, read back from the bar being run. */
34
+ export interface PriceSeries {
35
+ /** Its value `back` bars ago (0: this bar's); NaN before the first bar. */
36
+ at(back?: number): number;
37
+ }
38
+ export interface ScriptPrices {
39
+ readonly time: PriceSeries;
40
+ readonly open: PriceSeries;
41
+ readonly high: PriceSeries;
42
+ readonly low: PriceSeries;
43
+ readonly close: PriceSeries;
44
+ readonly volume: PriceSeries;
45
+ }
46
+ /** How a plot is drawn: `'area'` a line with the space down to zero shaded under it. */
47
+ export type ScriptPlotKind = 'line' | 'histogram' | 'dots' | 'step' | 'area';
48
+ export interface PlotOptions {
49
+ /** Its colour (`'#rrggbb'`, `'#rrggbbaa'`); unset: the theme's next series colour. */
50
+ color?: string;
51
+ kind?: ScriptPlotKind;
52
+ /** Its line width in px. */
53
+ width?: number;
54
+ }
55
+ export interface PlotHandle {
56
+ /** This bar's value, and a colour for this bar only; returns the value. */
57
+ set(value: number, color?: string): number;
58
+ }
59
+ /**
60
+ * Where a mark goes: over or under its bar, or at the top or the bottom of
61
+ * the pane. In a pane of its own, over the bar is the top and under it the
62
+ * bottom.
63
+ */
64
+ export type ShapePosition = 'aboveBar' | 'belowBar' | 'top' | 'bottom';
65
+ export interface ShapeOptions {
66
+ shape?: IndicatorMarkerShape;
67
+ /** Default under the bar. */
68
+ position?: ShapePosition;
69
+ color?: string;
70
+ }
71
+ export interface ShapeHandle {
72
+ /** Whether this bar is marked; returns it. */
73
+ set(on: boolean): boolean;
74
+ }
75
+ export interface FillOptions {
76
+ /** Its colour, for the bars given none; a colour with an alpha of its own (`'#3b82f633'`) is shaded at that alpha. Unset: the first plot's, faint. */
77
+ color?: string;
78
+ }
79
+ export interface FillHandle {
80
+ /** This bar's colour (from the bar before to this one); unset: the fill's own. */
81
+ set(color?: string): void;
82
+ }
83
+ export interface BackgroundOptions {
84
+ /** Its name, for the settings. */
85
+ title?: string;
86
+ }
87
+ export interface BackgroundHandle {
88
+ /** The colour behind this bar, the pane's whole height; unset: none. */
89
+ set(color?: string): void;
90
+ }
91
+ export interface AlertConditionOptions {
92
+ /** What the alert says when it fires, unless the viewer writes their own. */
93
+ message?: string;
94
+ }
95
+ export interface AlertConditionHandle {
96
+ /** Whether the condition holds on this bar; returns it. */
97
+ set(on: boolean): boolean;
98
+ }
99
+ export interface BarColorHandle {
100
+ /** The colour of this bar's candle (or OHLC bar); unset: the chart's own. */
101
+ set(color?: string): void;
102
+ }
103
+ export interface HlineOptions {
104
+ /** Drawn whole in it; unset: faint, in the axis colour. */
105
+ color?: string;
106
+ /** Default `'dashed'`. Without a colour, in the axis colour, faint. */
107
+ lineStyle?: LineStyle;
108
+ /** Its name: not drawn, and not shown in the indicator settings yet. */
109
+ title?: string;
110
+ }
111
+ /** The chart a script runs on. */
112
+ export interface ScriptChart {
113
+ /** The exchange's IANA zone (`'Asia/Ho_Chi_Minh'`); `'UTC'` when the chart names none. */
114
+ readonly timeZone: string;
115
+ /**
116
+ * The chart's interval (`'5m'`, `'1d'`), null when it is not known. Read
117
+ * it in setup: the script is then run again when it changes. What a
118
+ * script declares (its plots, inputs, levels) may not depend on it.
119
+ */
120
+ readonly interval: TimeFrame | null;
121
+ }
122
+ /** What a script's setup is given to declare what it draws and keeps. */
123
+ export interface ScriptSetup<V> {
124
+ readonly inputs: V;
125
+ readonly chart: ScriptChart;
126
+ /** The bars' prices, read back from the bar being run (`prices.close.at(1)`). */
127
+ readonly prices: ScriptPrices;
128
+ /** A value a bar drawn as a line, histogram, dots or steps. */
129
+ plot(title: string, options?: PlotOptions): PlotHandle;
130
+ /** A mark over or under the bars it is set on, or at the top or the bottom of the pane. */
131
+ shape(title: string, options?: ShapeOptions): ShapeHandle;
132
+ /** The area between two plots, in a colour a bar if need be. */
133
+ fill(a: PlotHandle, b: PlotHandle, options?: FillOptions): FillHandle;
134
+ /** A colour behind the bars it is set on, the pane's whole height. */
135
+ bgcolor(options?: BackgroundOptions): BackgroundHandle;
136
+ /**
137
+ * The price bars painted in a colour a bar, for a script drawn over the
138
+ * prices (`overlay: true`), on a chart of candles or OHLC bars (another
139
+ * chart type, Renko or a line, shows them as they are).
140
+ */
141
+ barcolor(): BarColorHandle;
142
+ /**
143
+ * A moment the script says, which a viewer can set an alert on: the
144
+ * widget's alert form lists it as "<name>: <title>", and the alert fires
145
+ * as it turns true. `.set(true)` on the bars it holds.
146
+ */
147
+ alertCondition(title: string, options?: AlertConditionOptions): AlertConditionHandle;
148
+ /**
149
+ * Labels, lines and boxes the script draws on its bars (`label()`,
150
+ * `line()`, `box()`), each a handle to change or take away later; past
151
+ * the limit of a kind, the oldest goes. Keep a handle across bars in
152
+ * `state.var`: a plain variable is not put back on a tick, and may then
153
+ * reach a shape made since in its place.
154
+ */
155
+ drawings(limits?: DrawingLimits): ScriptDrawings;
156
+ /** Data the chart's bars do not hold as they are. */
157
+ readonly request: {
158
+ /**
159
+ * A value of a higher interval of the same symbol (`'1h'` on a 5-minute
160
+ * chart), a chart bar at a time: `setup` makes the sums of that
161
+ * interval's bars and returns their value a bar. Each chart bar reads
162
+ * the value of the last bar of the higher interval that closed before
163
+ * its own; with `{ live: true }`, the forming one's as it was then (it
164
+ * repaints). Neither reads a bar after the chart's own. Made in setup,
165
+ * called on every bar.
166
+ */
167
+ security(interval: TimeFrame, setup: SecuritySetup, options?: SecurityOptions): (bar: ScriptBar) => number;
168
+ };
169
+ /** A level drawn across the pane. */
170
+ hline(price: number, options?: HlineOptions): void;
171
+ /**
172
+ * Count one step of a loop, on a bar: call it once each time round. Past
173
+ * the run's `loopSteps` (a million unless set) the script stops at that
174
+ * bar, so a loop that never ends is stopped.
175
+ */
176
+ loop(): void;
177
+ readonly state: {
178
+ /** A value kept from bar to bar: a number, a text, or a handle (a drawing's), put back as it was on a tick. */
179
+ var<T>(init: T): Var<T>;
180
+ /** A list of numbers kept from bar to bar. */
181
+ array(): ScriptArray;
182
+ /** A value a bar of the script's own, read back with `at(n)`. */
183
+ series(): ScriptSeries;
184
+ };
185
+ }
186
+ /** What a script's setup returns: what to do on each bar. */
187
+ export type OnBar = (bar: ScriptBar) => void;
188
+ export interface ScriptDefinition<I extends ScriptInputs> {
189
+ /** Its id as an indicator: unique among the chart's indicators. */
190
+ id: string;
191
+ name: string;
192
+ /** Its name in the legend; unset: its name. */
193
+ shortName?: string;
194
+ /** Drawn over the prices; unset: in a pane of its own. */
195
+ overlay?: boolean;
196
+ inputs?: I;
197
+ /**
198
+ * Run once, before the first bar: declares the plots, makes the `ta`
199
+ * sums and the kept state, and returns what each bar runs.
200
+ */
201
+ setup(api: ScriptSetup<InputValues<I>>): OnBar;
202
+ }
203
+ /** A script, ready to run (`new ScriptRun`) or to draw (`scriptIndicator`). */
204
+ export interface Script<I extends ScriptInputs = ScriptInputs> extends Readonly<ScriptDefinition<I>> {
205
+ readonly kind: 'script';
206
+ }
207
+ /** A script written bar by bar. */
208
+ export declare function defineScript<I extends ScriptInputs = Record<string, never>>(definition: ScriptDefinition<I>): Script<I>;
209
+ /**
210
+ * An input's value as a bar sees it: what was set when it is of the
211
+ * default's type, else the default; a number within `min` and `max`, whole
212
+ * for an `'int'`; a choice one of the `options`. The rules
213
+ * `defineIndicator` reads its inputs by.
214
+ */
215
+ export declare function inputValue(set: unknown, input: ScriptInput): ScriptInputValue;
216
+ //# sourceMappingURL=defineScript.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defineScript.d.ts","sourceRoot":"","sources":["../src/defineScript.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjE,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAEzD,+EAA+E;AAC/E,MAAM,WAAW,WAAY,SAAQ,kBAAkB;IACrD,OAAO,EAAE,gBAAgB,CAAC;CAC3B;AAED,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAEvD,gDAAgD;AAChD,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,YAAY,IAAI;IAChD,QAAQ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wBAAgB,YAAY,CAAC,CAAC,SAAS,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,UAAU,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAIvH;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,GAAG,gBAAgB,CAW7E"}
@@ -0,0 +1,46 @@
1
+ import { IndicatorBoxDrawing, IndicatorDrawing, IndicatorLabelDrawing, IndicatorLineDrawing } from '@tradecanvas/commons';
2
+ import { Scope } from './scope.js';
3
+ export type LabelProps = Omit<IndicatorLabelDrawing, 'kind'>;
4
+ export type LineProps = Omit<IndicatorLineDrawing, 'kind'>;
5
+ export type BoxProps = Omit<IndicatorBoxDrawing, 'kind'>;
6
+ /** A shape a script drew: changed or taken away on a later bar. */
7
+ export interface DrawingHandle<P> {
8
+ /** Change some of it; a shape taken away (by the script, or as the oldest past the limit) is left as it is. */
9
+ set(changes: Partial<P>): void;
10
+ /** Take it away. */
11
+ remove(): void;
12
+ }
13
+ /** The most of each kind a script keeps; past it, the oldest goes. */
14
+ export interface DrawingLimits {
15
+ /** Labels kept: default 50, at most 500. */
16
+ maxLabels?: number;
17
+ /** Lines kept: default 50, at most 500. */
18
+ maxLines?: number;
19
+ /** Boxes kept: default 50, at most 500. */
20
+ maxBoxes?: number;
21
+ }
22
+ /**
23
+ * The labels, lines and boxes a script draws, kept from bar to bar. On the
24
+ * live bar it records how to undo each change, so a tick that runs the bar
25
+ * again starts from the shapes as the bar before left them.
26
+ */
27
+ export declare class ScriptDrawings {
28
+ private readonly scope;
29
+ /** By id: an id is the order made in. */
30
+ private readonly items;
31
+ private readonly order;
32
+ private readonly limits;
33
+ private nextId;
34
+ private undo;
35
+ constructor(scope: Scope, limits?: DrawingLimits);
36
+ label(props: LabelProps): DrawingHandle<LabelProps>;
37
+ line(props: LineProps): DrawingHandle<LineProps>;
38
+ box(props: BoxProps): DrawingHandle<BoxProps>;
39
+ /** The shapes kept, in the order made. */
40
+ list(): IndicatorDrawing[];
41
+ private record;
42
+ private add;
43
+ private change;
44
+ private remove;
45
+ }
46
+ //# sourceMappingURL=drawings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drawings.d.ts","sourceRoot":"","sources":["../src/drawings.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,gBAAgB,EAChB,qBAAqB,EACrB,oBAAoB,EAErB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,MAAM,UAAU,GAAG,IAAI,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,IAAI,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC;AAC3D,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAC,mBAAmB,EAAE,MAAM,CAAC,CAAC;AAEzD,mEAAmE;AACnE,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,+GAA+G;IAC/G,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAC/B,oBAAoB;IACpB,MAAM,IAAI,IAAI,CAAC;CAChB;AAED,sEAAsE;AACtE,MAAM,WAAW,aAAa;IAC5B,4CAA4C;IAC5C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAwCD;;;;GAIG;AACH,qBAAa,cAAc;IAQb,OAAO,CAAC,QAAQ,CAAC,KAAK;IAPlC,yCAAyC;IACzC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAuC;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA4D;IAClF,OAAO,CAAC,QAAQ,CAAC,M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