@tradecanvas/script 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +120 -3
- package/dist/Column.d.ts +21 -0
- package/dist/Column.d.ts.map +1 -0
- package/dist/Ring.d.ts +28 -0
- package/dist/Ring.d.ts.map +1 -0
- package/dist/ScriptRun.d.ts +75 -0
- package/dist/ScriptRun.d.ts.map +1 -0
- package/dist/defineScript.d.ts +203 -0
- package/dist/defineScript.d.ts.map +1 -0
- package/dist/drawings.d.ts +46 -0
- package/dist/drawings.d.ts.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1522 -0
- package/dist/index.js.map +1 -0
- package/dist/limits.d.ts +30 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/outputs.d.ts +129 -0
- package/dist/outputs.d.ts.map +1 -0
- package/dist/points.d.ts +13 -0
- package/dist/points.d.ts.map +1 -0
- package/dist/scope.d.ts +33 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scriptIndicator.d.ts +25 -0
- package/dist/scriptIndicator.d.ts.map +1 -0
- package/dist/security.d.ts +52 -0
- package/dist/security.d.ts.map +1 -0
- package/dist/state.d.ts +52 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/ta/averages.d.ts +19 -0
- package/dist/ta/averages.d.ts.map +1 -0
- package/dist/ta/common.d.ts +24 -0
- package/dist/ta/common.d.ts.map +1 -0
- package/dist/ta/events.d.ts +16 -0
- package/dist/ta/events.d.ts.map +1 -0
- package/dist/ta/index.d.ts +65 -0
- package/dist/ta/index.d.ts.map +1 -0
- package/dist/ta/moves.d.ts +26 -0
- package/dist/ta/moves.d.ts.map +1 -0
- package/dist/ta/oscillators.d.ts +29 -0
- package/dist/ta/oscillators.d.ts.map +1 -0
- package/dist/ta/ranges.d.ts +18 -0
- package/dist/ta/ranges.d.ts.map +1 -0
- package/dist/ta/statistics.d.ts +26 -0
- package/dist/ta/statistics.d.ts.map +1 -0
- package/dist/worker/messages.d.ts +49 -0
- package/dist/worker/messages.d.ts.map +1 -0
- package/dist/worker/scriptWorkerIndicator.d.ts +40 -0
- package/dist/worker/scriptWorkerIndicator.d.ts.map +1 -0
- package/dist/worker/serveScripts.d.ts +28 -0
- package/dist/worker/serveScripts.d.ts.map +1 -0
- 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,120 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @tradecanvas/script
|
|
2
|
+
|
|
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
|
+
Not published yet: the package is being built in steps, and its API can still change.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { defineScript, scriptIndicator, ta } from '@tradecanvas/script';
|
|
9
|
+
|
|
10
|
+
const emaCross = defineScript({
|
|
11
|
+
id: 'emaCross',
|
|
12
|
+
name: 'EMA Cross',
|
|
13
|
+
overlay: true,
|
|
14
|
+
inputs: { fast: { default: 9, kind: 'int', min: 1 }, slow: { default: 21, kind: 'int', min: 1 } },
|
|
15
|
+
setup({ inputs, plot, shape }) {
|
|
16
|
+
const fast = ta.ema(inputs.fast);
|
|
17
|
+
const slow = ta.ema(inputs.slow);
|
|
18
|
+
const up = ta.crossover();
|
|
19
|
+
const fastLine = plot('Fast', { color: '#f5c542' });
|
|
20
|
+
const slowLine = plot('Slow', { color: '#3b82f6' });
|
|
21
|
+
const mark = shape('Cross up', { shape: 'triangleUp', position: 'belowBar', color: '#22c55e' });
|
|
22
|
+
return (bar) => {
|
|
23
|
+
const f = fastLine.set(fast(bar.close));
|
|
24
|
+
const s = slowLine.set(slow(bar.close));
|
|
25
|
+
mark.set(up(f, s));
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
chart.registerIndicator(scriptIndicator(emaCross));
|
|
31
|
+
chart.addIndicator('emaCross');
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## How it runs
|
|
35
|
+
|
|
36
|
+
- `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.
|
|
37
|
+
- 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).
|
|
38
|
+
- 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.
|
|
39
|
+
- `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.
|
|
40
|
+
- Only what the script keeps through `state` and the `ta` sums is put back before a tick. A plain `let` in `setup` is not: keep values in `state.var`.
|
|
41
|
+
- 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.
|
|
42
|
+
- 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.
|
|
43
|
+
- `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.
|
|
44
|
+
- 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.
|
|
45
|
+
|
|
46
|
+
## The ta sums
|
|
47
|
+
|
|
48
|
+
Each is made in `setup` with its settings, then given a value each bar.
|
|
49
|
+
|
|
50
|
+
- Averages: `sma`, `ema`, `rma`, `wma`, `vwma` (value, volume), `hma`, `alma(period, offset, sigma)`, `swma` (four values, weighted 1, 2, 2, 1), `linreg(period, offset)`.
|
|
51
|
+
- 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).
|
|
52
|
+
- Running: `cum` (the total so far), `change`, `mom`, `roc`, `barssince`, `valuewhen`.
|
|
53
|
+
- Oscillators: `rsi`, `macd`, `bb`, `stoch` (value, high, low: the raw %K), `mfi` (value, volume).
|
|
54
|
+
- Of the bars: `tr`, `atr`, `supertrend`, `sar(start, increment, max)`, `obv` (close, volume).
|
|
55
|
+
- Events: `crossover`, `crossunder`, `cross`, `rising(period)` and `falling(period)` (over, or under, every one of the bars before), `pivothigh`, `pivotlow`.
|
|
56
|
+
|
|
57
|
+
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.
|
|
58
|
+
|
|
59
|
+
`bar.live` is true on the live bar, the last one, which a tick runs again.
|
|
60
|
+
|
|
61
|
+
## Colours, fills, backgrounds
|
|
62
|
+
|
|
63
|
+
- `plot.set(value, color)` gives a bar its own colour; the line's stretch to that bar takes it.
|
|
64
|
+
- `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.
|
|
65
|
+
- `bgcolor()` returns a background: `.set(color)` shades the pane behind that bar, behind the candles and the levels.
|
|
66
|
+
- `plot(title, { kind: 'area' })` shades the space down to zero under the line.
|
|
67
|
+
- `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.
|
|
68
|
+
- `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.
|
|
69
|
+
|
|
70
|
+
## Labels, lines and boxes
|
|
71
|
+
|
|
72
|
+
`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.
|
|
73
|
+
|
|
74
|
+
## Alerts
|
|
75
|
+
|
|
76
|
+
`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.
|
|
77
|
+
|
|
78
|
+
## Higher intervals
|
|
79
|
+
|
|
80
|
+
`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.
|
|
81
|
+
|
|
82
|
+
## Levels
|
|
83
|
+
|
|
84
|
+
`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.
|
|
85
|
+
|
|
86
|
+
## Limits
|
|
87
|
+
|
|
88
|
+
A run stops a script at the bar that goes past one of its limits, and says which in `errors` (`limit: 'loop' | 'array' | 'time'`):
|
|
89
|
+
|
|
90
|
+
- **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.
|
|
91
|
+
- **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.
|
|
92
|
+
- **Time**: with `runMs` set, a `load`, `tick` or `next` that is past that many ms after a bar stops there.
|
|
93
|
+
|
|
94
|
+
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.
|
|
95
|
+
|
|
96
|
+
## In a worker
|
|
97
|
+
|
|
98
|
+
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`:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
// scripts.worker.ts
|
|
102
|
+
import { serveScripts } from '@tradecanvas/script'
|
|
103
|
+
import { emaCross } from './emaCross'
|
|
104
|
+
serveScripts([emaCross])
|
|
105
|
+
|
|
106
|
+
// the page
|
|
107
|
+
chart.registerIndicator(scriptWorkerIndicator(emaCross, {
|
|
108
|
+
spawn: () => new Worker(new URL('./scripts.worker.ts', import.meta.url), { type: 'module' }),
|
|
109
|
+
timeoutMs: 5000,
|
|
110
|
+
}))
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
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.
|
|
114
|
+
|
|
115
|
+
## Not there yet
|
|
116
|
+
|
|
117
|
+
- Other symbols in `request.security`.
|
|
118
|
+
- Pine Script source: a compiler to these scripts is planned.
|
|
119
|
+
|
|
120
|
+
The guide: [tradecanvas.dev/docs/scripts](https://tradecanvas.dev/docs/scripts).
|
package/dist/Column.d.ts
ADDED
|
@@ -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,75 @@
|
|
|
1
|
+
import { OHLCBar } 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
|
+
/** How far the run may go: loop steps a bar, numbers a list, ms a run. */
|
|
12
|
+
limits?: ScriptLimits;
|
|
13
|
+
}
|
|
14
|
+
/** What `update` did with a bar: ran the live bar again, ran a new bar, or nothing (an older bar: load the bars again). */
|
|
15
|
+
export type UpdateKind = 'tick' | 'bar' | 'reload';
|
|
16
|
+
/**
|
|
17
|
+
* A script run over bars. `load` runs every bar; the last one stays live:
|
|
18
|
+
* `tick` runs it again from the state the bar before left, `next` closes it
|
|
19
|
+
* and runs a new bar. A tick costs one bar, whatever the bars before.
|
|
20
|
+
*/
|
|
21
|
+
export declare class ScriptRun {
|
|
22
|
+
private readonly script;
|
|
23
|
+
private readonly params;
|
|
24
|
+
private readonly options;
|
|
25
|
+
private scope;
|
|
26
|
+
private outputs;
|
|
27
|
+
private onBar;
|
|
28
|
+
private prices;
|
|
29
|
+
private count;
|
|
30
|
+
private failures;
|
|
31
|
+
/** The bar the script threw on, after which no bar runs; -1: none. */
|
|
32
|
+
private haltedAt;
|
|
33
|
+
private declared;
|
|
34
|
+
/** Whether no bar has run since the setup: a load needs no setup of its own. */
|
|
35
|
+
private fresh;
|
|
36
|
+
/** What it draws, known from its setup. */
|
|
37
|
+
get declarations(): ScriptDeclarations;
|
|
38
|
+
/** The bars loaded last: what a higher interval reads the bars' clock from. */
|
|
39
|
+
private loaded;
|
|
40
|
+
private readonly limits;
|
|
41
|
+
/** When the `load`, `tick` or `next` running began, by `performance.now()`. */
|
|
42
|
+
private began;
|
|
43
|
+
constructor(script: Script<ScriptInputs>, params?: Readonly<Record<string, unknown>>, options?: ScriptRunOptions);
|
|
44
|
+
/** Where the script stopped, and why: none while it runs. */
|
|
45
|
+
get errors(): readonly ScriptError[];
|
|
46
|
+
/** How many bars it has run. */
|
|
47
|
+
get length(): number;
|
|
48
|
+
get output(): ScriptOutput;
|
|
49
|
+
/** Run all of `bars` from the start; the last one stays live. */
|
|
50
|
+
load(bars: readonly OHLCBar[]): void;
|
|
51
|
+
/** The live bar as it now is: run it again from the state the bar before left. */
|
|
52
|
+
tick(bar: OHLCBar): void;
|
|
53
|
+
/**
|
|
54
|
+
* Close the live bar as it last ran, and run `bar` as the new live one. A
|
|
55
|
+
* script that threw on the live bar stays stopped once it closes, as on
|
|
56
|
+
* any closed bar: load the bars again to run it anew.
|
|
57
|
+
*/
|
|
58
|
+
next(bar: OHLCBar): void;
|
|
59
|
+
/** 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. */
|
|
60
|
+
update(bar: OHLCBar): UpdateKind;
|
|
61
|
+
/** Begin again from no bars: a new setup, new state. */
|
|
62
|
+
private start;
|
|
63
|
+
/** From here on the bar run is the live one: keep the state the bars before left. */
|
|
64
|
+
private goLive;
|
|
65
|
+
private priceSeries;
|
|
66
|
+
private write;
|
|
67
|
+
/** Bar `i` as its prices now stand. */
|
|
68
|
+
private barAt;
|
|
69
|
+
private runBar;
|
|
70
|
+
/** Start the clock of a `load`, `tick` or `next`, when the run has a time. */
|
|
71
|
+
private clockIn;
|
|
72
|
+
/** The time gone past, once a bar is done; null while within it. */
|
|
73
|
+
private overTime;
|
|
74
|
+
}
|
|
75
|
+
//# 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,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAA4C,KAAK,MAAM,EAAkB,KAAK,YAAY,EAAqB,MAAM,mBAAmB,CAAC;AAChJ,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,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;IAyBlB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IA1B1B,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,OAAO,CAAC,QAAQ,CAAqB;IACrC,sEAAsE;IACtE,OAAO,CAAC,QAAQ,CAAM;IACtB,OA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riptSeries, 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 before it.
|
|
29
|
+
*/
|
|
30
|
+
readonly live: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** A price of the bars, read back from the bar being run. */
|
|
33
|
+
export interface PriceSeries {
|
|
34
|
+
/** Its value `back` bars ago (0: this bar's); NaN before the first bar. */
|
|
35
|
+
at(back?: number): number;
|
|
36
|
+
}
|
|
37
|
+
export interface ScriptPrices {
|
|
38
|
+
readonly time: PriceSeries;
|
|
39
|
+
readonly open: PriceSeries;
|
|
40
|
+
readonly high: PriceSeries;
|
|
41
|
+
readonly low: PriceSeries;
|
|
42
|
+
readonly close: PriceSeries;
|
|
43
|
+
readonly volume: PriceSeries;
|
|
44
|
+
}
|
|
45
|
+
/** How a plot is drawn: `'area'` a line with the space down to zero shaded under it. */
|
|
46
|
+
export type ScriptPlotKind = 'line' | 'histogram' | 'dots' | 'step' | 'area';
|
|
47
|
+
export interface PlotOptions {
|
|
48
|
+
/** Its colour (`'#rrggbb'`, `'#rrggbbaa'`); unset: the theme's next series colour. */
|
|
49
|
+
color?: string;
|
|
50
|
+
kind?: ScriptPlotKind;
|
|
51
|
+
/** Its line width in px. */
|
|
52
|
+
width?: number;
|
|
53
|
+
}
|
|
54
|
+
export interface PlotHandle {
|
|
55
|
+
/** This bar's value, and a colour for this bar only; returns the value. */
|
|
56
|
+
set(value: number, color?: string): number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Where a mark goes: over or under its bar, or at the top or the bottom of
|
|
60
|
+
* the pane. In a pane of its own, over the bar is the top and under it the
|
|
61
|
+
* bottom.
|
|
62
|
+
*/
|
|
63
|
+
export type ShapePosition = 'aboveBar' | 'belowBar' | 'top' | 'bottom';
|
|
64
|
+
export interface ShapeOptions {
|
|
65
|
+
shape?: IndicatorMarkerShape;
|
|
66
|
+
/** Default under the bar. */
|
|
67
|
+
position?: ShapePosition;
|
|
68
|
+
color?: string;
|
|
69
|
+
}
|
|
70
|
+
export interface ShapeHandle {
|
|
71
|
+
/** Whether this bar is marked; returns it. */
|
|
72
|
+
set(on: boolean): boolean;
|
|
73
|
+
}
|
|
74
|
+
export interface FillOptions {
|
|
75
|
+
/** 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. */
|
|
76
|
+
color?: string;
|
|
77
|
+
}
|
|
78
|
+
export interface FillHandle {
|
|
79
|
+
/** This bar's colour (from the bar before to this one); unset: the fill's own. */
|
|
80
|
+
set(color?: string): void;
|
|
81
|
+
}
|
|
82
|
+
export interface BackgroundOptions {
|
|
83
|
+
/** Its name, for the settings. */
|
|
84
|
+
title?: string;
|
|
85
|
+
}
|
|
86
|
+
export interface BackgroundHandle {
|
|
87
|
+
/** The colour behind this bar, the pane's whole height; unset: none. */
|
|
88
|
+
set(color?: string): void;
|
|
89
|
+
}
|
|
90
|
+
export interface AlertConditionOptions {
|
|
91
|
+
/** What the alert says when it fires, unless the viewer writes their own. */
|
|
92
|
+
message?: string;
|
|
93
|
+
}
|
|
94
|
+
export interface AlertConditionHandle {
|
|
95
|
+
/** Whether the condition holds on this bar; returns it. */
|
|
96
|
+
set(on: boolean): boolean;
|
|
97
|
+
}
|
|
98
|
+
export interface BarColorHandle {
|
|
99
|
+
/** The colour of this bar's candle (or OHLC bar); unset: the chart's own. */
|
|
100
|
+
set(color?: string): void;
|
|
101
|
+
}
|
|
102
|
+
export interface HlineOptions {
|
|
103
|
+
/** Drawn whole in it; unset: faint, in the axis colour. */
|
|
104
|
+
color?: string;
|
|
105
|
+
/** Default `'dashed'`. Without a colour, in the axis colour, faint. */
|
|
106
|
+
lineStyle?: LineStyle;
|
|
107
|
+
/** Its name: not drawn, and not shown in the indicator settings yet. */
|
|
108
|
+
title?: string;
|
|
109
|
+
}
|
|
110
|
+
/** What a script's setup is given to declare what it draws and keeps. */
|
|
111
|
+
export interface ScriptSetup<V> {
|
|
112
|
+
readonly inputs: V;
|
|
113
|
+
/** The bars' prices, read back from the bar being run (`prices.close.at(1)`). */
|
|
114
|
+
readonly prices: ScriptPrices;
|
|
115
|
+
/** A value a bar drawn as a line, histogram, dots or steps. */
|
|
116
|
+
plot(title: string, options?: PlotOptions): PlotHandle;
|
|
117
|
+
/** A mark over or under the bars it is set on, or at the top or the bottom of the pane. */
|
|
118
|
+
shape(title: string, options?: ShapeOptions): ShapeHandle;
|
|
119
|
+
/** The area between two plots, in a colour a bar if need be. */
|
|
120
|
+
fill(a: PlotHandle, b: PlotHandle, options?: FillOptions): FillHandle;
|
|
121
|
+
/** A colour behind the bars it is set on, the pane's whole height. */
|
|
122
|
+
bgcolor(options?: BackgroundOptions): BackgroundHandle;
|
|
123
|
+
/**
|
|
124
|
+
* The price bars painted in a colour a bar, for a script drawn over the
|
|
125
|
+
* prices (`overlay: true`), on a chart of candles or OHLC bars (another
|
|
126
|
+
* chart type, Renko or a line, shows them as they are).
|
|
127
|
+
*/
|
|
128
|
+
barcolor(): BarColorHandle;
|
|
129
|
+
/**
|
|
130
|
+
* A moment the script says, which a viewer can set an alert on: the
|
|
131
|
+
* widget's alert form lists it as "<name>: <title>", and the alert fires
|
|
132
|
+
* as it turns true. `.set(true)` on the bars it holds.
|
|
133
|
+
*/
|
|
134
|
+
alertCondition(title: string, options?: AlertConditionOptions): AlertConditionHandle;
|
|
135
|
+
/**
|
|
136
|
+
* Labels, lines and boxes the script draws on its bars (`label()`,
|
|
137
|
+
* `line()`, `box()`), each a handle to change or take away later; past
|
|
138
|
+
* the limit of a kind, the oldest goes. Keep a handle across bars in
|
|
139
|
+
* `state.var`: a plain variable is not put back on a tick, and may then
|
|
140
|
+
* reach a shape made since in its place.
|
|
141
|
+
*/
|
|
142
|
+
drawings(limits?: DrawingLimits): ScriptDrawings;
|
|
143
|
+
/** Data the chart's bars do not hold as they are. */
|
|
144
|
+
readonly request: {
|
|
145
|
+
/**
|
|
146
|
+
* A value of a higher interval of the same symbol (`'1h'` on a 5-minute
|
|
147
|
+
* chart), a chart bar at a time: `setup` makes the sums of that
|
|
148
|
+
* interval's bars and returns their value a bar. Each chart bar reads
|
|
149
|
+
* the value of the last bar of the higher interval that closed before
|
|
150
|
+
* its own; with `{ live: true }`, the forming one's as it was then (it
|
|
151
|
+
* repaints). Neither reads a bar after the chart's own. Made in setup,
|
|
152
|
+
* called on every bar.
|
|
153
|
+
*/
|
|
154
|
+
security(interval: TimeFrame, setup: SecuritySetup, options?: SecurityOptions): (bar: ScriptBar) => number;
|
|
155
|
+
};
|
|
156
|
+
/** A level drawn across the pane. */
|
|
157
|
+
hline(price: number, options?: HlineOptions): void;
|
|
158
|
+
/**
|
|
159
|
+
* Count one step of a loop, on a bar: call it once each time round. Past
|
|
160
|
+
* the run's `loopSteps` (a million unless set) the script stops at that
|
|
161
|
+
* bar, so a loop that never ends is stopped.
|
|
162
|
+
*/
|
|
163
|
+
loop(): void;
|
|
164
|
+
readonly state: {
|
|
165
|
+
/** 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. */
|
|
166
|
+
var<T>(init: T): Var<T>;
|
|
167
|
+
/** A list of numbers kept from bar to bar. */
|
|
168
|
+
array(): ScriptArray;
|
|
169
|
+
/** A value a bar of the script's own, read back with `at(n)`. */
|
|
170
|
+
series(): ScriptSeries;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/** What a script's setup returns: what to do on each bar. */
|
|
174
|
+
export type OnBar = (bar: ScriptBar) => void;
|
|
175
|
+
export interface ScriptDefinition<I extends ScriptInputs> {
|
|
176
|
+
/** Its id as an indicator: unique among the chart's indicators. */
|
|
177
|
+
id: string;
|
|
178
|
+
name: string;
|
|
179
|
+
/** Its name in the legend; unset: its name. */
|
|
180
|
+
shortName?: string;
|
|
181
|
+
/** Drawn over the prices; unset: in a pane of its own. */
|
|
182
|
+
overlay?: boolean;
|
|
183
|
+
inputs?: I;
|
|
184
|
+
/**
|
|
185
|
+
* Run once, before the first bar: declares the plots, makes the `ta`
|
|
186
|
+
* sums and the kept state, and returns what each bar runs.
|
|
187
|
+
*/
|
|
188
|
+
setup(api: ScriptSetup<InputValues<I>>): OnBar;
|
|
189
|
+
}
|
|
190
|
+
/** A script, ready to run (`new ScriptRun`) or to draw (`scriptIndicator`). */
|
|
191
|
+
export interface Script<I extends ScriptInputs = ScriptInputs> extends Readonly<ScriptDefinition<I>> {
|
|
192
|
+
readonly kind: 'script';
|
|
193
|
+
}
|
|
194
|
+
/** A script written bar by bar. */
|
|
195
|
+
export declare function defineScript<I extends ScriptInputs = Record<string, never>>(definition: ScriptDefinition<I>): Script<I>;
|
|
196
|
+
/**
|
|
197
|
+
* An input's value as a bar sees it: what was set when it is of the
|
|
198
|
+
* default's type, else the default; a number within `min` and `max`, whole
|
|
199
|
+
* for an `'int'`; a choice one of the `options`. The rules
|
|
200
|
+
* `defineIndicator` reads its inputs by.
|
|
201
|
+
*/
|
|
202
|
+
export declare function inputValue(set: unknown, input: ScriptInput): ScriptInputValue;
|
|
203
|
+
//# 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,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,OAAO,GAAG,OAAO,GAAG,MAAM;CACtH,CAAC;AAEF,yBAAyB;AACzB,MAAM,WAAW,SAAS;IACxB,wCAAwC;IACxC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,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ACjE,MAAM,IAAI,YAAY,CAAC;KACxB,CAAC;CACH;AAED,6DAA6D;AAC7D,MAAM,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,SAAS,KAAK,IAAI,CAAC;AAE7C,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,YAAY;IACtD,mEAAmE;IACnE,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,+CAA+C;IAC/C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,0DAA0D;IAC1D,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,CAAC,CAAC;IACX;;;OAGG;IACH,KAAK,CAAC,GAAG,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;CAChD;AAED,+EAA+E;AAC/E,MAAM,WAAW,MAAM,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,CAAE,SAAQ,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;IAClG,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;CACzB;AAED,mCAAmC;AACnC,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 @@
|
|
|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