oakscriptjs 0.15.4 → 0.17.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/drawing/registry.d.ts +44 -5
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +18 -9
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/script/index.d.ts +20 -0
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +11 -1
- package/dist/cjs/script/index.js.map +2 -2
- package/dist/esm/drawing/registry.d.ts +44 -5
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +58 -11
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +4 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/script/index.d.ts +20 -0
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +18 -2
- package/dist/esm/script/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/pine-differences.md +3 -3
- package/skills/oakscript/reference/timeframes-symbols.md +10 -0
- package/src/drawing/registry.ts +64 -14
- package/src/index.ts +6 -2
- package/src/script/index.ts +44 -2
|
@@ -77,6 +77,16 @@ const upVolume = eachBar((c) => vols[c.i].reduce((sum, v) => sum + v, 0));
|
|
|
77
77
|
plot(upVolume, "Up volume", { style: plot.style_columns, color: color.teal });
|
|
78
78
|
```
|
|
79
79
|
|
|
80
|
+
### Seed data
|
|
81
|
+
|
|
82
|
+
`request.seed(source, symbol, expression)` reads a seed data series on the chart timeframe: it is
|
|
83
|
+
`request.security` of the symbol the host gives that series (by default `"<SOURCE>:<SYMBOL>"` upper-cased).
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
indicator("Seed value", { overlay: false });
|
|
87
|
+
plot(request.seed("seed_crypto_santiment", "BTC_DEV_ACTIVITY", () => close), "Dev activity");
|
|
88
|
+
```
|
|
89
|
+
|
|
80
90
|
## Timeframe values
|
|
81
91
|
|
|
82
92
|
- `timeframe.period` (`"60"`, `"1D"`...), `timeframe.multiplier`, `timeframe.isintraday`, `timeframe.isdaily`,
|
package/src/drawing/registry.ts
CHANGED
|
@@ -48,6 +48,9 @@
|
|
|
48
48
|
* The script API (`executeScript`) resets the registry at the start of every run and applies the
|
|
49
49
|
* `max_*_count` values of `indicator()`. Outside a script run the registry is global to the module.
|
|
50
50
|
*
|
|
51
|
+
* A port with its own bar loop resumes a live update with {@link saveDrawingCheckpoint} (at the end of the last
|
|
52
|
+
* confirmed bar) and {@link restoreDrawingCheckpoint} (before the forming bar is calculated again).
|
|
53
|
+
*
|
|
51
54
|
* @module drawing/registry
|
|
52
55
|
*/
|
|
53
56
|
|
|
@@ -216,6 +219,8 @@ interface Holds {
|
|
|
216
219
|
/** Records made in the run. None: no object has one, nothing to look up for each object the maximum count may
|
|
217
220
|
* delete */
|
|
218
221
|
records: number;
|
|
222
|
+
/** The {@link varDrawing} variables made in the run: a checkpoint puts their objects back with the records */
|
|
223
|
+
readonly variables: Array<{ current: object | undefined }>;
|
|
219
224
|
}
|
|
220
225
|
|
|
221
226
|
/** What the `var` variables of a run did with an object. */
|
|
@@ -229,7 +234,7 @@ interface HoldRecord {
|
|
|
229
234
|
/** Bar on which the last `var` variable released the object (linefill rule) */
|
|
230
235
|
releasedAt: number | undefined;
|
|
231
236
|
}
|
|
232
|
-
let holds: Holds = { others: new WeakMap(), records: 0 };
|
|
237
|
+
let holds: Holds = { others: new WeakMap(), records: 0, variables: [] };
|
|
233
238
|
|
|
234
239
|
/**
|
|
235
240
|
* The record of the first run that holds an object, on the object itself (a study holding a new object on every bar
|
|
@@ -335,15 +340,17 @@ export interface DrawingVariable<T extends object> {
|
|
|
335
340
|
* ```
|
|
336
341
|
*/
|
|
337
342
|
export function varDrawing<T extends object>(initial?: T): DrawingVariable<T> {
|
|
338
|
-
|
|
343
|
+
const slot: { current: T | undefined } = { current: initial ? holdDrawing(initial) : undefined };
|
|
344
|
+
// kept by the run: a drawing checkpoint puts the variable back without holding or releasing again
|
|
345
|
+
holds.variables.push(slot);
|
|
339
346
|
return {
|
|
340
347
|
get value() {
|
|
341
|
-
return current;
|
|
348
|
+
return slot.current;
|
|
342
349
|
},
|
|
343
350
|
set value(next: T | undefined) {
|
|
344
|
-
if (next === current) return;
|
|
345
|
-
releaseDrawing(current);
|
|
346
|
-
current = next ? holdDrawing(next) : undefined;
|
|
351
|
+
if (next === slot.current) return;
|
|
352
|
+
releaseDrawing(slot.current);
|
|
353
|
+
slot.current = next ? holdDrawing(next) : undefined;
|
|
347
354
|
},
|
|
348
355
|
};
|
|
349
356
|
}
|
|
@@ -619,7 +626,7 @@ export function resetDrawings(newLimits: DrawingLimits = {}): void {
|
|
|
619
626
|
for (const kind of Object.keys(live) as DrawingKind[]) live[kind].replace([]);
|
|
620
627
|
fillsOfLine = new Map();
|
|
621
628
|
created.clear();
|
|
622
|
-
holds = { others: new WeakMap(), records: 0 };
|
|
629
|
+
holds = { others: new WeakMap(), records: 0, variables: [] };
|
|
623
630
|
pending = { line: 0, label: 0, box: 0, polyline: 0 };
|
|
624
631
|
currentBar = null;
|
|
625
632
|
lastBarIndex = null;
|
|
@@ -671,14 +678,16 @@ export function saveDrawingState(): DrawingState {
|
|
|
671
678
|
}
|
|
672
679
|
|
|
673
680
|
/**
|
|
674
|
-
* The registry and the state of its objects at the end of a bar
|
|
675
|
-
* fields of each one (arrays, maps and plain objects copied; other drawing objects kept as references), their `var`
|
|
676
|
-
* records,
|
|
681
|
+
* The registry and the state of its objects at the end of a bar (see {@link saveDrawingCheckpoint}): the live objects,
|
|
682
|
+
* the fields of each one (arrays, maps and plain objects copied; other drawing objects kept as references), their `var`
|
|
683
|
+
* records, the objects of the {@link varDrawing} variables, and the counters. Its fields are internal.
|
|
677
684
|
*/
|
|
678
685
|
export interface DrawingCheckpoint {
|
|
679
686
|
state: DrawingState;
|
|
680
687
|
fields: Map<object, Record<string, unknown>>;
|
|
681
688
|
records: Map<object, { count: number; heldAt: number | undefined; releasedAt: number | undefined } | null>;
|
|
689
|
+
/** The object of each variable of the run, in the order they were made */
|
|
690
|
+
variables: Array<object | undefined>;
|
|
682
691
|
}
|
|
683
692
|
|
|
684
693
|
/** A copy of a field value of a drawing object: plain data copied, drawing objects (and other instances) shared. */
|
|
@@ -700,9 +709,37 @@ function copyFields(obj: object): Record<string, unknown> {
|
|
|
700
709
|
return out;
|
|
701
710
|
}
|
|
702
711
|
|
|
703
|
-
/**
|
|
712
|
+
/**
|
|
713
|
+
* The drawings at the end of a bar, for a live update of a study with its own bar loop: save it at the end of the last
|
|
714
|
+
* confirmed bar, and put it back with {@link restoreDrawingCheckpoint} before the forming bar is calculated again. The
|
|
715
|
+
* drawings then are those of a full run on the same bars: the objects created, changed or deleted on the forming bar
|
|
716
|
+
* are undone, the objects keep their identity and creation order, and the maximum counts, the `var` holds (also of
|
|
717
|
+
* {@link varDrawing} variables), the linefills and the rules of the last bar ({@link setDrawingBar}) go on as in a full
|
|
718
|
+
* run. The incremental script runs use it too (#196, #203).
|
|
719
|
+
*
|
|
720
|
+
* The checkpoint holds the whole registry of the study: other studies on the same chart may reset and use the registry
|
|
721
|
+
* between two updates, putting it back makes it the study's again (call it where a full run calls
|
|
722
|
+
* {@link resetDrawings}). The study keeps its own variables (the objects it refers to, its arrays of objects) with the
|
|
723
|
+
* checkpoint and puts them back as plain values.
|
|
724
|
+
*
|
|
725
|
+
* @example
|
|
726
|
+
* ```typescript
|
|
727
|
+
* // full run: resetDrawings({ line: 500 }) ... for each bar i: setDrawingBar(i, i === n - 1); draw(i);
|
|
728
|
+
* // and after the bar n - 2: checkpoint = saveDrawingCheckpoint()
|
|
729
|
+
* // live update of the forming bar n - 1 (or new bars after it):
|
|
730
|
+
* restoreDrawingCheckpoint(checkpoint);
|
|
731
|
+
* for (let i = n - 1; i < bars.length; i++) {
|
|
732
|
+
* setDrawingBar(i, i === bars.length - 1);
|
|
733
|
+
* draw(i);
|
|
734
|
+
* if (i === bars.length - 2) checkpoint = saveDrawingCheckpoint();
|
|
735
|
+
* }
|
|
736
|
+
* setDrawingBar(null);
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
704
739
|
export function saveDrawingCheckpoint(): DrawingCheckpoint {
|
|
705
740
|
const state = saveDrawingState();
|
|
741
|
+
// the state of the last bar changes during the bar: a checkpoint taken on it keeps a copy
|
|
742
|
+
if (state.lastBar) state.lastBar = copyLastBar(state.lastBar);
|
|
706
743
|
const fields = new Map<object, Record<string, unknown>>();
|
|
707
744
|
const records: DrawingCheckpoint['records'] = new Map();
|
|
708
745
|
for (const kind of Object.keys(state.live) as DrawingKind[]) {
|
|
@@ -712,13 +749,21 @@ export function saveDrawingCheckpoint(): DrawingCheckpoint {
|
|
|
712
749
|
records.set(obj, r ? { count: r.count, heldAt: r.heldAt, releasedAt: r.releasedAt } : null);
|
|
713
750
|
}
|
|
714
751
|
}
|
|
715
|
-
return { state, fields, records };
|
|
752
|
+
return { state, fields, records, variables: holds.variables.map((v) => v.current) };
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
function copyLastBar(bar: LastBar): LastBar {
|
|
756
|
+
return { ...bar, old: new Set(bar.old), allowance: { ...bar.allowance } };
|
|
716
757
|
}
|
|
717
758
|
|
|
718
|
-
/**
|
|
759
|
+
/**
|
|
760
|
+
* Puts back a {@link saveDrawingCheckpoint}: the registry becomes the one of the checkpoint, the objects alive then get
|
|
761
|
+
* their fields and `var` records back, the variables their objects. It can be put back again later (on each update of
|
|
762
|
+
* the forming bar).
|
|
763
|
+
*/
|
|
719
764
|
export function restoreDrawingCheckpoint(cp: DrawingCheckpoint): void {
|
|
720
765
|
restoreDrawingState(cp.state);
|
|
721
|
-
|
|
766
|
+
if (cp.state.lastBar) lastBar = copyLastBar(cp.state.lastBar);
|
|
722
767
|
for (const [obj, saved] of cp.fields) {
|
|
723
768
|
for (const key of Object.keys(obj)) if (!(key in saved)) delete (obj as Record<string, unknown>)[key];
|
|
724
769
|
Object.assign(obj, copyFields(saved));
|
|
@@ -737,6 +782,11 @@ export function restoreDrawingCheckpoint(cp: DrawingCheckpoint): void {
|
|
|
737
782
|
r.releasedAt = undefined;
|
|
738
783
|
}
|
|
739
784
|
}
|
|
785
|
+
// the variables made after the checkpoint belong to the calculation being undone
|
|
786
|
+
holds.variables.length = cp.variables.length;
|
|
787
|
+
cp.variables.forEach((obj, k) => {
|
|
788
|
+
holds.variables[k]!.current = obj;
|
|
789
|
+
});
|
|
740
790
|
}
|
|
741
791
|
|
|
742
792
|
/** Puts back the registry saved by {@link saveDrawingState} after the nested run. */
|
package/src/index.ts
CHANGED
|
@@ -46,7 +46,8 @@ export { taStep, type BarStep } from './ta/step.js';
|
|
|
46
46
|
export { drawingOutputs, type DrawingOutputs } from './drawing/output.js';
|
|
47
47
|
export { barInterval, barTime } from './drawing/bar-time.js';
|
|
48
48
|
|
|
49
|
-
// Drawing registry limits (PineScript indicator(max_lines_count / max_labels_count / max_boxes_count / max_polylines_count))
|
|
49
|
+
// Drawing registry limits (PineScript indicator(max_lines_count / max_labels_count / max_boxes_count / max_polylines_count)),
|
|
50
|
+
// and the drawing checkpoint of a port with its own bar loop (live updates of the forming bar, #203)
|
|
50
51
|
export {
|
|
51
52
|
resetDrawings,
|
|
52
53
|
setDrawingLimits,
|
|
@@ -55,6 +56,9 @@ export {
|
|
|
55
56
|
releaseDrawing,
|
|
56
57
|
varDrawing,
|
|
57
58
|
createdDrawingKinds,
|
|
59
|
+
saveDrawingCheckpoint,
|
|
60
|
+
restoreDrawingCheckpoint,
|
|
61
|
+
type DrawingCheckpoint,
|
|
58
62
|
type DrawingKind,
|
|
59
63
|
type DrawingLimits,
|
|
60
64
|
type DrawingVariable,
|
|
@@ -228,7 +232,7 @@ export type { LightweightSeriesDefinitions } from './runtime/adapters/Lightweigh
|
|
|
228
232
|
export { SimpleInputAdapter } from './runtime/adapters/SimpleInputAdapter.js';
|
|
229
233
|
|
|
230
234
|
// Version
|
|
231
|
-
export const VERSION = '0.
|
|
235
|
+
export const VERSION = '0.16.0';
|
|
232
236
|
|
|
233
237
|
/**
|
|
234
238
|
* Library information
|
package/src/script/index.ts
CHANGED
|
@@ -2944,6 +2944,11 @@ export interface ChartContext {
|
|
|
2944
2944
|
* `request.security` merges on (#179), and the other fields its `syminfo.*` inside the expression.
|
|
2945
2945
|
*/
|
|
2946
2946
|
symbolInfo?: (symbol: string) => SymbolInfo | undefined;
|
|
2947
|
+
/**
|
|
2948
|
+
* Ticker of a seed data series for `request.seed(source, symbol, ...)` (#205), whose bars then come from
|
|
2949
|
+
* {@link bars} like any other symbol. Default: `"<SOURCE>:<SYMBOL>"` upper-cased.
|
|
2950
|
+
*/
|
|
2951
|
+
seedSymbol?: (source: string, symbol: string) => string;
|
|
2947
2952
|
}
|
|
2948
2953
|
|
|
2949
2954
|
/** Calendar and properties of a symbol, as in {@link ChartContext}. */
|
|
@@ -3666,10 +3671,26 @@ function security_lower_tf<T extends SecurityValue>(
|
|
|
3666
3671
|
return (Array.isArray(result) ? mapped : mapped[0]) as T extends unknown[] ? number[][][] : number[][];
|
|
3667
3672
|
}
|
|
3668
3673
|
|
|
3669
|
-
/**
|
|
3674
|
+
/**
|
|
3675
|
+
* PineScript `request.seed(source, symbol, expression, ignore_invalid_symbol)` (#205): `expression` on a seed data
|
|
3676
|
+
* series, on the chart timeframe. The series is the symbol `ChartContext.seedSymbol(source, symbol)` (default
|
|
3677
|
+
* `"<SOURCE>:<SYMBOL>"` upper-cased), read as `request.security(<that symbol>, "", expression)`.
|
|
3678
|
+
*/
|
|
3679
|
+
function seed<T extends SecurityValue>(
|
|
3680
|
+
source: string,
|
|
3681
|
+
symbol: string,
|
|
3682
|
+
expression: () => T,
|
|
3683
|
+
ignore_invalid_symbol: boolean = false
|
|
3684
|
+
): T extends unknown[] ? Series[] : Series {
|
|
3685
|
+
const ticker = chartCtx.seedSymbol?.(source, symbol) ?? `${source.toUpperCase()}:${symbol.toUpperCase()}`;
|
|
3686
|
+
return security(ticker, '', expression, barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol);
|
|
3687
|
+
}
|
|
3688
|
+
|
|
3689
|
+
/** PineScript `request.*`: `security` (issue #101, other symbols #179, lower timeframes #181), `security_lower_tf` and `seed` (#205). */
|
|
3670
3690
|
/** PineScript parameters of `request.security` / `request.security_lower_tf`, in their positional order. */
|
|
3671
3691
|
const SECURITY_PARAMETERS = ['symbol', 'timeframe', 'expression', 'gaps', 'lookahead', 'ignore_invalid_symbol', 'currency', 'calc_bars_count'] as const;
|
|
3672
3692
|
const SECURITY_LOWER_TF_PARAMETERS = ['symbol', 'timeframe', 'expression', 'ignore_invalid_symbol', 'currency', 'ignore_invalid_timeframe', 'calc_bars_count'] as const;
|
|
3693
|
+
const SEED_PARAMETERS = ['source', 'symbol', 'expression', 'ignore_invalid_symbol', 'calc_bars_count'] as const;
|
|
3673
3694
|
|
|
3674
3695
|
/** Named arguments of `request.security` (PineScript names). */
|
|
3675
3696
|
export interface SecurityArgs<T extends SecurityValue = SecurityValue> {
|
|
@@ -3727,7 +3748,28 @@ function securityLowerTfCall<T extends SecurityValue>(
|
|
|
3727
3748
|
return security_lower_tf(a[0] as string, a[1] as string, a[2] as () => T, a[3] as boolean | undefined);
|
|
3728
3749
|
}
|
|
3729
3750
|
|
|
3730
|
-
|
|
3751
|
+
/** Named arguments of `request.seed` (PineScript names). */
|
|
3752
|
+
export interface SeedArgs<T extends SecurityValue = SecurityValue> {
|
|
3753
|
+
source?: string;
|
|
3754
|
+
symbol?: string;
|
|
3755
|
+
expression?: () => T;
|
|
3756
|
+
ignore_invalid_symbol?: boolean;
|
|
3757
|
+
calc_bars_count?: number;
|
|
3758
|
+
}
|
|
3759
|
+
|
|
3760
|
+
/** `request.seed` with PineScript's named arguments. */
|
|
3761
|
+
function seedCall<T extends SecurityValue>(
|
|
3762
|
+
...args:
|
|
3763
|
+
| [source: string, symbol: string, expression: () => T, ignore_invalid_symbol?: boolean]
|
|
3764
|
+
| [source: string, symbol: string, expression: () => T, args: SeedArgs<T>]
|
|
3765
|
+
| [args: SeedArgs<T> & { source: string; symbol: string; expression: () => T }]
|
|
3766
|
+
): T extends unknown[] ? Series[] : Series {
|
|
3767
|
+
const a = namedArguments('request.seed', SEED_PARAMETERS, args);
|
|
3768
|
+
unsupportedSecurityArgs('request.seed', undefined, a[4]);
|
|
3769
|
+
return seed(a[0] as string, a[1] as string, a[2] as () => T, a[3] as boolean | undefined);
|
|
3770
|
+
}
|
|
3771
|
+
|
|
3772
|
+
export const request = strictNamespace('request', { security: securityCall, security_lower_tf: securityLowerTfCall, seed: seedCall });
|
|
3731
3773
|
|
|
3732
3774
|
// ── Drawing objects ──────────────────────────────────────────────────────────
|
|
3733
3775
|
// The drawing namespaces come from this bundle so that they share the registry that
|