oakscriptjs 0.15.2 → 0.15.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/check/format.d.ts.map +1 -1
- package/dist/cjs/check/format.js +5 -0
- package/dist/cjs/check/format.js.map +2 -2
- package/dist/cjs/check/harness.d.ts +24 -0
- package/dist/cjs/check/harness.d.ts.map +1 -1
- package/dist/cjs/check/harness.js +33 -0
- package/dist/cjs/check/harness.js.map +2 -2
- package/dist/cjs/check/index.d.ts +9 -0
- package/dist/cjs/check/index.d.ts.map +1 -1
- package/dist/cjs/check/index.js +7 -0
- package/dist/cjs/check/index.js.map +2 -2
- package/dist/cjs/color/index.d.ts.map +1 -1
- package/dist/cjs/color/index.js +19 -2
- package/dist/cjs/color/index.js.map +2 -2
- package/dist/cjs/color/parse.d.ts +8 -6
- package/dist/cjs/color/parse.d.ts.map +1 -1
- package/dist/cjs/color/parse.js +15 -2
- package/dist/cjs/color/parse.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +32 -0
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +179 -45
- package/dist/cjs/drawing/registry.js.map +2 -2
- package/dist/cjs/index.d.ts +2 -1
- 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/runtime/series.d.ts +16 -0
- package/dist/cjs/runtime/series.d.ts.map +1 -1
- package/dist/cjs/runtime/series.js +19 -2
- package/dist/cjs/runtime/series.js.map +2 -2
- package/dist/cjs/script/closure-writes.d.ts +16 -0
- package/dist/cjs/script/closure-writes.d.ts.map +1 -0
- package/dist/cjs/script/closure-writes.js +435 -0
- package/dist/cjs/script/closure-writes.js.map +7 -0
- package/dist/cjs/script/globals.d.ts +2 -0
- package/dist/cjs/script/globals.d.ts.map +1 -1
- package/dist/cjs/script/globals.js.map +1 -1
- package/dist/cjs/script/incremental.d.ts +140 -0
- package/dist/cjs/script/incremental.d.ts.map +1 -0
- package/dist/cjs/script/incremental.js +142 -0
- package/dist/cjs/script/incremental.js.map +7 -0
- package/dist/cjs/script/index.d.ts +116 -31
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +471 -25
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/cjs/session/bars.d.ts +3 -0
- package/dist/cjs/session/bars.d.ts.map +1 -1
- package/dist/cjs/session/bars.js +10 -3
- package/dist/cjs/session/bars.js.map +2 -2
- package/dist/cjs/session/calendar.d.ts +19 -6
- package/dist/cjs/session/calendar.d.ts.map +1 -1
- package/dist/cjs/session/calendar.js +57 -18
- package/dist/cjs/session/calendar.js.map +2 -2
- package/dist/cjs/session/context.d.ts.map +1 -1
- package/dist/cjs/session/context.js +25 -8
- package/dist/cjs/session/context.js.map +2 -2
- package/dist/cjs/str/numberformat.d.ts.map +1 -1
- package/dist/cjs/str/numberformat.js +23 -0
- package/dist/cjs/str/numberformat.js.map +2 -2
- package/dist/cjs/strategy/index.d.ts +8 -0
- package/dist/cjs/strategy/index.d.ts.map +1 -1
- package/dist/cjs/strategy/index.js.map +1 -1
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +72 -3
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/ta/step.d.ts +476 -0
- package/dist/cjs/ta/step.d.ts.map +1 -0
- package/dist/cjs/ta/step.js +1000 -0
- package/dist/cjs/ta/step.js.map +7 -0
- package/dist/cjs/ta-series.d.ts +1 -1
- package/dist/cjs/ta-series.js +3 -3
- package/dist/cjs/ta-series.js.map +1 -1
- package/dist/cjs/time/timezone.d.ts +13 -1
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +52 -13
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/esm/check/format.d.ts.map +1 -1
- package/dist/esm/check/format.js +5 -0
- package/dist/esm/check/format.js.map +1 -1
- package/dist/esm/check/harness.d.ts +24 -0
- package/dist/esm/check/harness.d.ts.map +1 -1
- package/dist/esm/check/harness.js +40 -6
- package/dist/esm/check/harness.js.map +1 -1
- package/dist/esm/check/index.d.ts +9 -0
- package/dist/esm/check/index.d.ts.map +1 -1
- package/dist/esm/check/index.js +11 -1
- package/dist/esm/check/index.js.map +1 -1
- package/dist/esm/color/index.d.ts.map +1 -1
- package/dist/esm/color/index.js +23 -3
- package/dist/esm/color/index.js.map +1 -1
- package/dist/esm/color/parse.d.ts +8 -6
- package/dist/esm/color/parse.d.ts.map +1 -1
- package/dist/esm/color/parse.js +19 -2
- package/dist/esm/color/parse.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +32 -0
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +214 -51
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/runtime/series.d.ts +16 -0
- package/dist/esm/runtime/series.d.ts.map +1 -1
- package/dist/esm/runtime/series.js +21 -1
- package/dist/esm/runtime/series.js.map +1 -1
- package/dist/esm/script/closure-writes.d.ts +16 -0
- package/dist/esm/script/closure-writes.d.ts.map +1 -0
- package/dist/esm/script/closure-writes.js +323 -0
- package/dist/esm/script/closure-writes.js.map +1 -0
- package/dist/esm/script/globals.d.ts +2 -0
- package/dist/esm/script/globals.d.ts.map +1 -1
- package/dist/esm/script/globals.js.map +1 -1
- package/dist/esm/script/incremental.d.ts +140 -0
- package/dist/esm/script/incremental.d.ts.map +1 -0
- package/dist/esm/script/incremental.js +173 -0
- package/dist/esm/script/incremental.js.map +1 -0
- package/dist/esm/script/index.d.ts +116 -31
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +561 -24
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/session/bars.d.ts +3 -0
- package/dist/esm/session/bars.d.ts.map +1 -1
- package/dist/esm/session/bars.js +11 -3
- package/dist/esm/session/bars.js.map +1 -1
- package/dist/esm/session/calendar.d.ts +19 -6
- package/dist/esm/session/calendar.d.ts.map +1 -1
- package/dist/esm/session/calendar.js +74 -21
- package/dist/esm/session/calendar.js.map +1 -1
- package/dist/esm/session/context.d.ts.map +1 -1
- package/dist/esm/session/context.js +34 -9
- package/dist/esm/session/context.js.map +1 -1
- package/dist/esm/str/numberformat.d.ts.map +1 -1
- package/dist/esm/str/numberformat.js +30 -0
- package/dist/esm/str/numberformat.js.map +1 -1
- package/dist/esm/strategy/index.d.ts +8 -0
- package/dist/esm/strategy/index.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +79 -4
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/ta/step.d.ts +476 -0
- package/dist/esm/ta/step.d.ts.map +1 -0
- package/dist/esm/ta/step.js +1089 -0
- package/dist/esm/ta/step.js.map +1 -0
- package/dist/esm/ta-series.d.ts +1 -1
- package/dist/esm/ta-series.js +4 -4
- package/dist/esm/time/timezone.d.ts +13 -1
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +78 -17
- package/dist/esm/time/timezone.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/SKILL.md +2 -2
- package/skills/oakscript/reference/check.md +5 -0
- package/skills/oakscript/reference/errors.md +0 -1
- package/skills/oakscript/reference/pine-differences.md +1 -2
- package/skills/oakscript/reference/plotting.md +5 -7
- package/skills/oakscript/reference/script-structure.md +6 -0
- package/src/check/format.ts +7 -0
- package/src/check/harness.ts +57 -0
- package/src/check/index.ts +16 -0
- package/src/color/index.ts +22 -3
- package/src/color/parse.ts +28 -3
- package/src/drawing/registry.ts +233 -50
- package/src/index.ts +4 -1
- package/src/runtime/series.ts +33 -1
- package/src/script/closure-writes.ts +279 -0
- package/src/script/globals.ts +2 -0
- package/src/script/incremental.ts +246 -0
- package/src/script/index.ts +695 -26
- package/src/session/bars.ts +11 -3
- package/src/session/calendar.ts +84 -21
- package/src/session/context.ts +36 -9
- package/src/str/numberformat.ts +29 -0
- package/src/strategy/index.ts +8 -0
- package/src/ta/running-sum.ts +78 -3
- package/src/ta/step.ts +1145 -0
- package/src/ta-series.ts +4 -4
- package/src/time/timezone.ts +76 -15
package/src/script/index.ts
CHANGED
|
@@ -75,11 +75,14 @@ import type {
|
|
|
75
75
|
} from '../runtime/types.js';
|
|
76
76
|
import { BarData, Series, MAX_BARS_BACK, historyError } from '../runtime/series.js';
|
|
77
77
|
import * as taSeries from '../ta-series.js';
|
|
78
|
+
import { taStep, type BarStep } from '../ta/step.js';
|
|
79
|
+
import * as incremental from './incremental.js';
|
|
80
|
+
import { outerWrites } from './closure-writes.js';
|
|
78
81
|
import * as pineConstants from './constants.js';
|
|
79
82
|
import { drawingNamespace, isPlainObject, namedArguments, unknownArgument } from './drawing-args.js';
|
|
80
83
|
import * as colorCore from '../color/index.js';
|
|
81
84
|
import { inputColorDefault } from '../color/parse.js';
|
|
82
|
-
import { fixnan as fixnanValues, isNA, nz } from '../utils/index.js';
|
|
85
|
+
import { fixnan as fixnanValues, isNA, nz as nzValue } from '../utils/index.js';
|
|
83
86
|
import { externalSource } from '../utils/source-resolver.js';
|
|
84
87
|
import * as lineCore from '../line/index.js';
|
|
85
88
|
import * as labelCore from '../label/index.js';
|
|
@@ -93,13 +96,38 @@ import * as mathCore from '../math/index.js';
|
|
|
93
96
|
import {
|
|
94
97
|
createdDrawingKinds,
|
|
95
98
|
resetDrawings,
|
|
99
|
+
restoreDrawingCheckpoint,
|
|
96
100
|
restoreDrawingState,
|
|
101
|
+
saveDrawingCheckpoint,
|
|
97
102
|
saveDrawingState,
|
|
98
103
|
setDrawingBar,
|
|
99
104
|
setDrawingLimits,
|
|
105
|
+
holdDrawing,
|
|
106
|
+
releaseDrawing,
|
|
100
107
|
type DrawingKind,
|
|
108
|
+
type DrawingVariable,
|
|
101
109
|
} from '../drawing/registry.js';
|
|
102
|
-
export { holdDrawing, releaseDrawing,
|
|
110
|
+
export { holdDrawing, releaseDrawing, type DrawingVariable } from '../drawing/registry.js';
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A PineScript `var` variable of a drawing type (`var line l = na`): the object it refers to is not deleted by the
|
|
114
|
+
* maximum count; assigning another object (or `undefined` for `na`) releases the one before. Its value is a
|
|
115
|
+
* {@link persist} box: an incremental run puts it back with the drawings (#196). Declare it in the script body.
|
|
116
|
+
*/
|
|
117
|
+
export function varDrawing<T extends object>(initial?: T): DrawingVariable<T> {
|
|
118
|
+
const box = persist<T | undefined>(initial ? holdDrawing(initial) : undefined);
|
|
119
|
+
return {
|
|
120
|
+
get value() {
|
|
121
|
+
return box.value;
|
|
122
|
+
},
|
|
123
|
+
set value(next: T | undefined) {
|
|
124
|
+
const current = box.value;
|
|
125
|
+
if (next === current) return;
|
|
126
|
+
releaseDrawing(current);
|
|
127
|
+
box.value = next ? holdDrawing(next) : undefined;
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
103
131
|
import { drawingOutputs } from '../drawing/output.js';
|
|
104
132
|
import * as timeframeCore from '../timeframe/index.js';
|
|
105
133
|
import * as timeCore from '../time/index.js';
|
|
@@ -431,9 +459,23 @@ export function executeScript(
|
|
|
431
459
|
inputs: Record<string, unknown> = {},
|
|
432
460
|
chart: ChartContext = {},
|
|
433
461
|
options: ScriptOptions = {}
|
|
462
|
+
): ScriptRunResult {
|
|
463
|
+
return runBody(body, barsInput, inputs, chart, options, null);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** executeScript(), as part of an incremental run (`inc`, see createScriptRun) or alone (null). */
|
|
467
|
+
function runBody(
|
|
468
|
+
body: () => void,
|
|
469
|
+
barsInput: Bar[],
|
|
470
|
+
inputs: Record<string, unknown>,
|
|
471
|
+
chart: ChartContext,
|
|
472
|
+
options: ScriptOptions,
|
|
473
|
+
inc: incremental.ActiveRun | null
|
|
434
474
|
): ScriptRunResult {
|
|
435
475
|
// A run inside another one (an intrabar run of a strategy) puts the outer run back when it ends.
|
|
436
476
|
const outer = ctx ? saveRunState() : null;
|
|
477
|
+
// A plain run inside an incremental one is not part of it.
|
|
478
|
+
const outerInc = inc ? incremental.beginRun(inc) : incremental.suspendRun();
|
|
437
479
|
ctxBars.setAll(barsInput);
|
|
438
480
|
chartCtx = { ...chart };
|
|
439
481
|
runNow = chart.now ?? Date.now();
|
|
@@ -448,6 +490,8 @@ export function executeScript(
|
|
|
448
490
|
try {
|
|
449
491
|
body();
|
|
450
492
|
if (missingSymbols.length) throw new MissingSymbolDataError(missingSymbols);
|
|
493
|
+
// the outputs read the drawings: not a drawing of the script
|
|
494
|
+
if (inc) incremental.bodyDone();
|
|
451
495
|
const c = ctx;
|
|
452
496
|
return {
|
|
453
497
|
metadata: {
|
|
@@ -491,6 +535,7 @@ export function executeScript(
|
|
|
491
535
|
} finally {
|
|
492
536
|
ctx = null;
|
|
493
537
|
if (outer) restoreRunState(outer);
|
|
538
|
+
incremental.endRun(outerInc);
|
|
494
539
|
}
|
|
495
540
|
}
|
|
496
541
|
|
|
@@ -544,6 +589,214 @@ export function executeIntrabar(
|
|
|
544
589
|
);
|
|
545
590
|
}
|
|
546
591
|
|
|
592
|
+
// ── Incremental runs (#196) ──────────────────────────────────────────────────
|
|
593
|
+
|
|
594
|
+
/** The result of {@link ScriptRun.update}. */
|
|
595
|
+
export interface ScriptRunUpdate extends ScriptRunResult {
|
|
596
|
+
/** First bar calculated: the forming bar of the previous update (a resumed run), or 0 (a full run) */
|
|
597
|
+
from: number;
|
|
598
|
+
/** The update was resumed from the state of the last confirmed bar */
|
|
599
|
+
incremental: boolean;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
export interface ScriptRunOptions extends ScriptOptions {
|
|
603
|
+
/**
|
|
604
|
+
* Resumed updates also calculated in full and compared, the first ones of the run (default 2): a difference makes
|
|
605
|
+
* the next updates full runs (a script that keeps state outside persist() boxes)
|
|
606
|
+
*/
|
|
607
|
+
verify?: number;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** A script calculated again on each update of the chart bars (see {@link createScriptRun}). */
|
|
611
|
+
export interface ScriptRun {
|
|
612
|
+
/**
|
|
613
|
+
* The script on `bars`: resumed from the forming bar of the previous update when the bars before it did not change
|
|
614
|
+
* (a live update of the forming bar, or new bars after it), else a full run.
|
|
615
|
+
*/
|
|
616
|
+
update(bars: Bar[], chart?: ChartContext): ScriptRunUpdate;
|
|
617
|
+
/** Why the updates are full runs: a feature that is not resumed, or a verification difference (null: resumed) */
|
|
618
|
+
readonly fullRunReason: string | null;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/** Chart context fields that only change the last bar (barstate of the forming bar). */
|
|
622
|
+
const LAST_BAR_FIELDS = new Set(['lastBarConfirmed', 'lastBarNew', 'realtime', 'now']);
|
|
623
|
+
|
|
624
|
+
function sameChart(a: ChartContext, b: ChartContext): boolean {
|
|
625
|
+
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
626
|
+
for (const k of keys) {
|
|
627
|
+
if (LAST_BAR_FIELDS.has(k)) continue;
|
|
628
|
+
if (!Object.is((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k])) return false;
|
|
629
|
+
}
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/** The bar values an update compares with the next one. */
|
|
634
|
+
interface BarsCopy {
|
|
635
|
+
n: number;
|
|
636
|
+
values: Float64Array;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/** `b` in `copy` from bar `from` on (the bars before are already there); a new copy when `copy` is null. */
|
|
640
|
+
function copyBars(b: readonly Bar[], copy: BarsCopy | null = null, from = 0): BarsCopy {
|
|
641
|
+
let values = copy?.values ?? new Float64Array(Math.max(16, b.length) * 6);
|
|
642
|
+
if (values.length < b.length * 6) {
|
|
643
|
+
const grown = new Float64Array(b.length * 12);
|
|
644
|
+
grown.set(values.subarray(0, from * 6));
|
|
645
|
+
values = grown;
|
|
646
|
+
}
|
|
647
|
+
for (let i = from; i < b.length; i++) {
|
|
648
|
+
const x = b[i]!;
|
|
649
|
+
const o = i * 6;
|
|
650
|
+
values[o] = x.time as number;
|
|
651
|
+
values[o + 1] = x.open;
|
|
652
|
+
values[o + 2] = x.high;
|
|
653
|
+
values[o + 3] = x.low;
|
|
654
|
+
values[o + 4] = x.close;
|
|
655
|
+
values[o + 5] = x.volume ?? NaN;
|
|
656
|
+
}
|
|
657
|
+
return { n: b.length, values };
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** a and b are the same number (NaN equal to NaN, -0 not 0) */
|
|
661
|
+
const sameNumber = (a: number, b: number): boolean => a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b;
|
|
662
|
+
|
|
663
|
+
/** The first `count` bars of `b` have the values of `copy`. */
|
|
664
|
+
function sameBars(copy: BarsCopy, b: readonly Bar[], count: number): boolean {
|
|
665
|
+
const v = copy.values;
|
|
666
|
+
for (let i = 0; i < count; i++) {
|
|
667
|
+
const x = b[i]!;
|
|
668
|
+
const o = i * 6;
|
|
669
|
+
if (
|
|
670
|
+
!sameNumber(v[o]!, x.time as number) || !sameNumber(v[o + 1]!, x.open) || !sameNumber(v[o + 2]!, x.high) ||
|
|
671
|
+
!sameNumber(v[o + 3]!, x.low) || !sameNumber(v[o + 4]!, x.close) || !sameNumber(v[o + 5]!, x.volume ?? NaN)
|
|
672
|
+
) {
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
return true;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** Two run results with the same values (numbers compared with Object.is: NaN equal, -0 not 0). */
|
|
680
|
+
function sameResult(a: unknown, b: unknown): boolean {
|
|
681
|
+
if (Object.is(a, b)) return true;
|
|
682
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
|
|
683
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
684
|
+
const ka = Object.keys(a);
|
|
685
|
+
const kb = Object.keys(b);
|
|
686
|
+
if (ka.length !== kb.length) return false;
|
|
687
|
+
for (const k of ka) {
|
|
688
|
+
if (!sameResult((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k])) return false;
|
|
689
|
+
}
|
|
690
|
+
return true;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* A script that calculates only the newest bars on a live update (#196). The first update is a full run; it keeps,
|
|
695
|
+
* for the next one, what the script created (its Series, ta.* calls, eachBar() blocks, persist() boxes and plots) with
|
|
696
|
+
* their state at the end of the last confirmed bar. An update whose bars are the same up to the forming bar of the
|
|
697
|
+
* previous update (a new price of the forming bar, or new bars after it) runs the script again from that bar only;
|
|
698
|
+
* the result is the same as a full run on the same bars. Other changes (history loaded, inputs, symbol, timeframe or
|
|
699
|
+
* other chart fields) run it in full.
|
|
700
|
+
*
|
|
701
|
+
* The state the script keeps from one bar to the next has to be in `persist()` boxes (not in `let` variables outside the
|
|
702
|
+
* eachBar() callbacks, which a resumed run cannot put back). The first resumed updates are also calculated in full and
|
|
703
|
+
* compared (`verify`): a difference makes the next updates full runs. Not resumed yet, so calculated in full: drawings,
|
|
704
|
+
* request.security, strategies, varip, timenow, last_bar_index, BarContext.n, barstate.islastconfirmedhistory
|
|
705
|
+
* (`fullRunReason` names the first one met).
|
|
706
|
+
*/
|
|
707
|
+
export function createScriptRun(
|
|
708
|
+
body: () => void,
|
|
709
|
+
inputs: Record<string, unknown> = {},
|
|
710
|
+
chart: ChartContext = {},
|
|
711
|
+
options: ScriptRunOptions = {}
|
|
712
|
+
): ScriptRun {
|
|
713
|
+
let memory: incremental.RunMemory | null = null;
|
|
714
|
+
let previous: BarsCopy | null = null;
|
|
715
|
+
let previousChart: ChartContext = chart;
|
|
716
|
+
let reason: string | null = null;
|
|
717
|
+
let verifyLeft = options.verify ?? 2;
|
|
718
|
+
|
|
719
|
+
const newRun = (mode: 'record' | 'resume', b: Bar[], from: number): incremental.ActiveRun => {
|
|
720
|
+
const next = incremental.emptyMemory(b.length);
|
|
721
|
+
// the block that draws stays the one of the previous run (the forming bar may not draw)
|
|
722
|
+
if (mode === 'resume' && memory) next.drawingBlock = memory.drawingBlock;
|
|
723
|
+
return {
|
|
724
|
+
mode,
|
|
725
|
+
from,
|
|
726
|
+
bars: b,
|
|
727
|
+
prev: mode === 'resume' ? memory : null,
|
|
728
|
+
next,
|
|
729
|
+
seriesSlot: 0,
|
|
730
|
+
taSlot: 0,
|
|
731
|
+
blockSlot: 0,
|
|
732
|
+
plotSlot: 0,
|
|
733
|
+
boxSlot: 0,
|
|
734
|
+
drawSlot: 0,
|
|
735
|
+
block: -1,
|
|
736
|
+
boxes: [],
|
|
737
|
+
blocked: null,
|
|
738
|
+
};
|
|
739
|
+
};
|
|
740
|
+
|
|
741
|
+
const full = (b: Bar[], c: ChartContext): ScriptRunUpdate => {
|
|
742
|
+
memory = null;
|
|
743
|
+
if (reason) return { ...runBody(body, b, inputs, c, options, null), from: 0, incremental: false };
|
|
744
|
+
const inc = newRun('record', b, 0);
|
|
745
|
+
const result = runBody(body, b, inputs, c, options, inc);
|
|
746
|
+
if (inc.blocked) reason = inc.blocked;
|
|
747
|
+
else memory = inc.next;
|
|
748
|
+
return { ...result, from: 0, incremental: false };
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
return {
|
|
752
|
+
get fullRunReason() {
|
|
753
|
+
return reason;
|
|
754
|
+
},
|
|
755
|
+
update(b: Bar[], c: ChartContext = previousChart): ScriptRunUpdate {
|
|
756
|
+
const last = previous;
|
|
757
|
+
const m = memory;
|
|
758
|
+
const resumable =
|
|
759
|
+
!reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c) &&
|
|
760
|
+
sameBars(last, b, last.n - 1);
|
|
761
|
+
previousChart = c;
|
|
762
|
+
let out: ScriptRunUpdate;
|
|
763
|
+
if (!resumable) {
|
|
764
|
+
out = full(b, c);
|
|
765
|
+
} else {
|
|
766
|
+
const from = last!.n - 1;
|
|
767
|
+
const inc = newRun('resume', b, from);
|
|
768
|
+
let result: ScriptRunResult | null = null;
|
|
769
|
+
try {
|
|
770
|
+
result = runBody(body, b, inputs, c, options, inc);
|
|
771
|
+
} catch (err) {
|
|
772
|
+
// the memory is changed in place by a resumed run: an error leaves it unusable
|
|
773
|
+
memory = null;
|
|
774
|
+
previous = null;
|
|
775
|
+
if (!(err instanceof incremental.ResumeAborted)) throw err;
|
|
776
|
+
reason = err.reason;
|
|
777
|
+
}
|
|
778
|
+
if (result && verifyLeft > 0) {
|
|
779
|
+
verifyLeft--;
|
|
780
|
+
const check = runBody(body, b, inputs, c, options, null);
|
|
781
|
+
if (!sameResult(result, check)) {
|
|
782
|
+
reason = 'a resumed update differs from a full run (state kept outside persist()?)';
|
|
783
|
+
result = check;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
if (result && !reason) {
|
|
787
|
+
memory = inc.next;
|
|
788
|
+
out = { ...result, from, incremental: true };
|
|
789
|
+
} else {
|
|
790
|
+
out = result && reason ? { ...result, from: 0, incremental: false } : full(b, c);
|
|
791
|
+
memory = null;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
previous = out.incremental && last ? copyBars(b, last, last.n - 1) : copyBars(b);
|
|
795
|
+
return out;
|
|
796
|
+
},
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
547
800
|
// ── varip ────────────────────────────────────────────────────────────────────
|
|
548
801
|
|
|
549
802
|
/** How `varip` boxes read and write in the running code. */
|
|
@@ -570,6 +823,7 @@ export interface VaripVariable<T> {
|
|
|
570
823
|
*/
|
|
571
824
|
export function varip<T>(initial: T): VaripVariable<T> {
|
|
572
825
|
collector();
|
|
826
|
+
incremental.nonIncremental('varip');
|
|
573
827
|
const slot = varipRun.slot++;
|
|
574
828
|
const state = varipRun;
|
|
575
829
|
const rec = state.recorder;
|
|
@@ -596,6 +850,80 @@ export function varip<T>(initial: T): VaripVariable<T> {
|
|
|
596
850
|
};
|
|
597
851
|
}
|
|
598
852
|
|
|
853
|
+
// ── persist ──────────────────────────────────────────────────────────────────
|
|
854
|
+
|
|
855
|
+
/** A `persist` variable: `.value` reads and writes it. */
|
|
856
|
+
export interface PersistVariable<T> {
|
|
857
|
+
value: T;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* PineScript `var` kept by the runtime (#196): a variable that keeps its value from one bar to the next. Declare it in
|
|
862
|
+
* the script body (outside the callbacks), in the same order on each run, and read and write it in one eachBar()
|
|
863
|
+
* callback. In a plain run it is a plain variable. In an incremental run (createScriptRun), the runtime keeps its value
|
|
864
|
+
* at the end of the last confirmed bar and puts it back before the forming bar is calculated again: use it instead of
|
|
865
|
+
* a `let` variable outside the callback, which a resumed run cannot put back.
|
|
866
|
+
*
|
|
867
|
+
* The run is calculated in full (not resumed) when the variable is read or written outside an eachBar() callback, by
|
|
868
|
+
* two eachBar() blocks, or holds something else than plain data (numbers, strings, booleans, arrays, plain objects,
|
|
869
|
+
* Map, Set).
|
|
870
|
+
*
|
|
871
|
+
* ```typescript
|
|
872
|
+
* const dir = persist(0); // var dir = 0
|
|
873
|
+
* const trend = eachBar((c) => {
|
|
874
|
+
* if (c.close > c.get(close, 1)) dir.value = 1;
|
|
875
|
+
* else if (c.close < c.get(close, 1)) dir.value = -1;
|
|
876
|
+
* return dir.value;
|
|
877
|
+
* });
|
|
878
|
+
* ```
|
|
879
|
+
*/
|
|
880
|
+
export function persist<T>(initial: T): PersistVariable<T> {
|
|
881
|
+
collector();
|
|
882
|
+
if (currentBar >= 0) {
|
|
883
|
+
throw new Error('persist() is declared in the script body, outside the eachBar() callback (once per run).');
|
|
884
|
+
}
|
|
885
|
+
let current = initial;
|
|
886
|
+
const run = incremental.active;
|
|
887
|
+
if (!run) {
|
|
888
|
+
return {
|
|
889
|
+
get value() {
|
|
890
|
+
return current;
|
|
891
|
+
},
|
|
892
|
+
set value(v: T) {
|
|
893
|
+
current = v;
|
|
894
|
+
},
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
const slot = run.boxSlot++;
|
|
898
|
+
run.boxes[slot] = {
|
|
899
|
+
get: () => current,
|
|
900
|
+
set: (v) => {
|
|
901
|
+
current = v as T;
|
|
902
|
+
},
|
|
903
|
+
};
|
|
904
|
+
if (run.mode === 'resume') run.next.boxOwner[slot] = run.prev?.boxOwner[slot];
|
|
905
|
+
const touch = (): void => {
|
|
906
|
+
if (incremental.active !== run) return;
|
|
907
|
+
if (run.block < 0) {
|
|
908
|
+
incremental.nonIncremental('persist() used outside eachBar()');
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
const owner = run.next.boxOwner[slot];
|
|
912
|
+
if (owner === undefined) run.next.boxOwner[slot] = run.block;
|
|
913
|
+
else if (owner !== run.block) incremental.nonIncremental('persist() used by two eachBar() blocks');
|
|
914
|
+
};
|
|
915
|
+
return {
|
|
916
|
+
get value() {
|
|
917
|
+
touch();
|
|
918
|
+
return current;
|
|
919
|
+
},
|
|
920
|
+
set value(v: T) {
|
|
921
|
+
touch();
|
|
922
|
+
current = v;
|
|
923
|
+
},
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
|
|
599
927
|
// ── Declarations ─────────────────────────────────────────────────────────────
|
|
600
928
|
|
|
601
929
|
export interface IndicatorOptions {
|
|
@@ -844,7 +1172,11 @@ export const hlcc4 = new Series(ctxBars, (b) => (b.high + b.low + 2 * b.close) /
|
|
|
844
1172
|
export const bar_index = new Series(ctxBars, (_b, i) => i);
|
|
845
1173
|
bar_index.fullHistory = true;
|
|
846
1174
|
/** PineScript `last_bar_index`: the index of the last bar, on every bar. In a bar loop, `c.n - 1`. */
|
|
847
|
-
export const last_bar_index = new Series(ctxBars, (_b, _i, data) =>
|
|
1175
|
+
export const last_bar_index = new Series(ctxBars, (_b, _i, data) => {
|
|
1176
|
+
// the same value on every bar, changed by a new bar: not kept by an incremental run (#196)
|
|
1177
|
+
incremental.nonIncremental('last_bar_index');
|
|
1178
|
+
return data.length - 1;
|
|
1179
|
+
});
|
|
848
1180
|
last_bar_index.fullHistory = true;
|
|
849
1181
|
// built-in price variables keep the whole history (no 5000-bar limit)
|
|
850
1182
|
for (const s of [hl2, hlc3, ohlc4, hlcc4]) s.fullHistory = true;
|
|
@@ -966,18 +1298,148 @@ function outsideBarLoops<T extends Record<string, unknown>>(members: T): T {
|
|
|
966
1298
|
return out as T;
|
|
967
1299
|
}
|
|
968
1300
|
|
|
1301
|
+
/**
|
|
1302
|
+
* A ta.* call of the script that has a {@link taStep} machine (#196). Outside an incremental run, the array function
|
|
1303
|
+
* (`full`). In an incremental run, the machine steps every bar of a recorded run, and only the bars from the forming bar
|
|
1304
|
+
* of the previous run in a resumed one (the machine holds the state of the last confirmed bar): the same doubles
|
|
1305
|
+
* (tests/ta/step.test.ts).
|
|
1306
|
+
*/
|
|
1307
|
+
function stepped<R>(
|
|
1308
|
+
kind: string,
|
|
1309
|
+
make: () => BarStep<never[], unknown>,
|
|
1310
|
+
sources: Series[],
|
|
1311
|
+
args: (values: number[][], i: number) => unknown[],
|
|
1312
|
+
outs: number,
|
|
1313
|
+
full: () => R
|
|
1314
|
+
): R {
|
|
1315
|
+
const run = incremental.active;
|
|
1316
|
+
if (!run) return full();
|
|
1317
|
+
const slot = run.taSlot++;
|
|
1318
|
+
const b = sources[0] ? (sources[0].bars as Bar[]) : bars();
|
|
1319
|
+
const n = b.length;
|
|
1320
|
+
const values = sources.map((s) => s.toArray());
|
|
1321
|
+
let mem: incremental.TaMemory;
|
|
1322
|
+
let from = 0;
|
|
1323
|
+
const prev = run.mode === 'resume' ? run.prev?.ta[slot] : undefined;
|
|
1324
|
+
if (run.mode === 'resume') {
|
|
1325
|
+
if (!prev || prev.kind !== kind) incremental.nonIncremental(`a new ta.${kind}() call`);
|
|
1326
|
+
from = run.from;
|
|
1327
|
+
// the arrays of the previous run are the run's own: their first values are kept in place
|
|
1328
|
+
mem = { kind, machine: prev!.machine, outs: prev!.outs };
|
|
1329
|
+
} else {
|
|
1330
|
+
mem = { kind, machine: make() as BarStep<unknown[], unknown>, outs: Array.from({ length: outs }, () => []) };
|
|
1331
|
+
}
|
|
1332
|
+
for (const o of mem.outs) o.length = n;
|
|
1333
|
+
for (let i = from; i < n; i++) {
|
|
1334
|
+
const r = mem.machine.step(...args(values, i));
|
|
1335
|
+
if (outs === 1) mem.outs[0]![i] = typeof r === 'boolean' ? (r ? 1 : 0) : (r as number);
|
|
1336
|
+
else for (let k = 0; k < outs; k++) mem.outs[k]![i] = (r as number[])[k]!;
|
|
1337
|
+
// the forming bar is not committed: the next update steps it again from the confirmed bars
|
|
1338
|
+
if (i < n - 1) mem.machine.commit();
|
|
1339
|
+
}
|
|
1340
|
+
run.next.ta[slot] = mem;
|
|
1341
|
+
const series = mem.outs.map((o) => Series.fromArray(b, o));
|
|
1342
|
+
return (outs === 1 ? series[0] : series) as R;
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
const at = (v: number[][], k: number, i: number): number => v[k]![i]!;
|
|
1346
|
+
|
|
1347
|
+
/** The ta.* calls computed bar by bar in an incremental run (a length given as a Series uses the array function). */
|
|
1348
|
+
const steppedTa = {
|
|
1349
|
+
sma: (source: Series, length: number | Series): Series =>
|
|
1350
|
+
typeof length === 'number'
|
|
1351
|
+
? stepped('sma', taStep.sma, [source], (v, i) => [at(v, 0, i), length], 1, () => taSeries.sma(source, length))
|
|
1352
|
+
: taSeries.sma(source, length),
|
|
1353
|
+
ema: (source: Series, length: number): Series =>
|
|
1354
|
+
stepped('ema', () => taStep.ema(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.ema(source, length)),
|
|
1355
|
+
rma: (source: Series, length: number): Series =>
|
|
1356
|
+
stepped('rma', () => taStep.rma(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.rma(source, length)),
|
|
1357
|
+
wma: (source: Series, length: number | Series): Series =>
|
|
1358
|
+
typeof length === 'number'
|
|
1359
|
+
? stepped('wma', taStep.wma, [source], (v, i) => [at(v, 0, i), length], 1, () => taSeries.wma(source, length))
|
|
1360
|
+
: taSeries.wma(source, length),
|
|
1361
|
+
rsi: (source: Series, length: number | Series): Series =>
|
|
1362
|
+
typeof length === 'number'
|
|
1363
|
+
? stepped('rsi', () => taStep.rsi(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.rsi(source, length))
|
|
1364
|
+
: taSeries.rsi(source, length),
|
|
1365
|
+
change: (source: Series, length: number = 1): Series =>
|
|
1366
|
+
stepped('change', () => taStep.change(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.change(source, length)),
|
|
1367
|
+
mom: (source: Series, length: number): Series =>
|
|
1368
|
+
stepped('mom', () => taStep.mom(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.mom(source, length)),
|
|
1369
|
+
roc: (source: Series, length: number): Series =>
|
|
1370
|
+
stepped('roc', () => taStep.roc(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.roc(source, length)),
|
|
1371
|
+
cum: (source: Series): Series =>
|
|
1372
|
+
stepped('cum', taStep.cum, [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.cum(source)),
|
|
1373
|
+
stdev: (source: Series, length: number | Series, biased: boolean = true): Series =>
|
|
1374
|
+
typeof length === 'number'
|
|
1375
|
+
? stepped('stdev', taStep.stdev, [source], (v, i) => [at(v, 0, i), length, biased], 1, () => taSeries.stdev(source, length, biased))
|
|
1376
|
+
: taSeries.stdev(source, length, biased),
|
|
1377
|
+
variance: (source: Series, length: number | Series, biased: boolean = true): Series =>
|
|
1378
|
+
typeof length === 'number'
|
|
1379
|
+
? stepped('variance', taStep.variance, [source], (v, i) => [at(v, 0, i), length, biased], 1, () =>
|
|
1380
|
+
taSeries.variance(source, length, biased))
|
|
1381
|
+
: taSeries.variance(source, length, biased),
|
|
1382
|
+
crossover: (a: Series, b: Series | number): Series =>
|
|
1383
|
+
stepped('crossover', taStep.crossover, typeof b === 'number' ? [a] : [a, b],
|
|
1384
|
+
(v, i) => [at(v, 0, i), typeof b === 'number' ? b : at(v, 1, i)], 1, () => taSeries.crossover(a, b)),
|
|
1385
|
+
crossunder: (a: Series, b: Series | number): Series =>
|
|
1386
|
+
stepped('crossunder', taStep.crossunder, typeof b === 'number' ? [a] : [a, b],
|
|
1387
|
+
(v, i) => [at(v, 0, i), typeof b === 'number' ? b : at(v, 1, i)], 1, () => taSeries.crossunder(a, b)),
|
|
1388
|
+
cross: (a: Series, b: Series | number): Series =>
|
|
1389
|
+
stepped('cross', taStep.cross, typeof b === 'number' ? [a] : [a, b],
|
|
1390
|
+
(v, i) => [at(v, 0, i), typeof b === 'number' ? b : at(v, 1, i)], 1, () => taSeries.cross(a, b)),
|
|
1391
|
+
barssince: (condition: Series): Series =>
|
|
1392
|
+
stepped('barssince', taStep.barssince, [condition], (v, i) => [at(v, 0, i) === 1], 1, () => taSeries.barssince(condition)),
|
|
1393
|
+
valuewhen: (condition: Series, source: Series, occurrence: number): Series =>
|
|
1394
|
+
stepped('valuewhen', () => taStep.valuewhen(occurrence), [condition, source], (v, i) => [at(v, 0, i) === 1, at(v, 1, i)], 1,
|
|
1395
|
+
() => taSeries.valuewhen(condition, source, occurrence)),
|
|
1396
|
+
macd: (source: Series, fastLength: number, slowLength: number, signalLength: number): [Series, Series, Series] =>
|
|
1397
|
+
stepped('macd', () => taStep.macd(fastLength, slowLength, signalLength), [source], (v, i) => [at(v, 0, i)], 3, () =>
|
|
1398
|
+
taSeries.macd(source, fastLength, slowLength, signalLength)),
|
|
1399
|
+
bb: (source: Series, length: number, mult: number): [Series, Series, Series] =>
|
|
1400
|
+
stepped('bb', () => taStep.bb(length, mult), [source], (v, i) => [at(v, 0, i)], 3, () => taSeries.bb(source, length, mult)),
|
|
1401
|
+
hma: (source: Series, length: number): Series =>
|
|
1402
|
+
stepped('hma', () => taStep.hma(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.hma(source, length)),
|
|
1403
|
+
dev: (source: Series, length: number): Series =>
|
|
1404
|
+
stepped('dev', () => taStep.dev(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.dev(source, length)),
|
|
1405
|
+
cci: (source: Series, length: number): Series =>
|
|
1406
|
+
stepped('cci', () => taStep.cci(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.cci(source, length)),
|
|
1407
|
+
percentrank: (source: Series, length: number): Series =>
|
|
1408
|
+
stepped('percentrank', () => taStep.percentrank(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.percentrank(source, length)),
|
|
1409
|
+
median: (source: Series, length: number): Series =>
|
|
1410
|
+
stepped('median', () => taStep.median(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.median(source, length)),
|
|
1411
|
+
linreg: (source: Series, length: number, offset: number = 0): Series =>
|
|
1412
|
+
stepped('linreg', () => taStep.linreg(length, offset), [source], (v, i) => [at(v, 0, i)], 1, () =>
|
|
1413
|
+
taSeries.linreg(source, length, offset)),
|
|
1414
|
+
correlation: (a: Series, b: Series, length: number): Series =>
|
|
1415
|
+
stepped('correlation', () => taStep.correlation(length), [a, b], (v, i) => [at(v, 0, i), at(v, 1, i)], 1, () =>
|
|
1416
|
+
taSeries.correlation(a, b, length)),
|
|
1417
|
+
stoch: (source: Series, highSource: Series, lowSource: Series, length: number): Series =>
|
|
1418
|
+
stepped('stoch', () => taStep.stoch(length), [source, highSource, lowSource], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1,
|
|
1419
|
+
() => taSeries.stoch(source, highSource, lowSource, length)),
|
|
1420
|
+
};
|
|
1421
|
+
|
|
969
1422
|
/** ta.* with the chart-implicit (bars-first) functions bound to the context,
|
|
970
1423
|
* so scripts call them like PineScript: `ta.tr(true)`, `ta.atr(14)`. */
|
|
971
1424
|
export const ta = strictNamespace('ta', outsideBarLoops({
|
|
972
1425
|
...taSeries,
|
|
973
|
-
|
|
974
|
-
|
|
1426
|
+
...steppedTa,
|
|
1427
|
+
tr: (handleNa: boolean = false): Series =>
|
|
1428
|
+
stepped('tr', () => taStep.tr(handleNa), [high, low, close], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1, () =>
|
|
1429
|
+
taSeries.tr(bars(), handleNa)),
|
|
1430
|
+
atr: (length: number): Series =>
|
|
1431
|
+
stepped('atr', () => taStep.atr(length), [high, low, close], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1, () =>
|
|
1432
|
+
taSeries.atr(bars(), length)),
|
|
975
1433
|
sar: (start?: number, inc?: number, max?: number): Series => taSeries.sar(bars(), start, inc, max),
|
|
976
|
-
wpr: (length
|
|
1434
|
+
wpr: (length: number = 14): Series =>
|
|
1435
|
+
stepped('wpr', () => taStep.wpr(length), [high, low, close], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1, () =>
|
|
1436
|
+
taSeries.wpr(bars(), length)),
|
|
977
1437
|
supertrend: (factor: number, atrLength: number): [Series, Series] =>
|
|
978
|
-
|
|
1438
|
+
stepped('supertrend', () => taStep.supertrend(factor, atrLength), [high, low, close],
|
|
1439
|
+
(v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 2, () => taSeries.supertrend(bars(), factor, atrLength)),
|
|
979
1440
|
dmi: (diLength: number, adxSmoothing: number): [Series, Series, Series] =>
|
|
980
|
-
|
|
1441
|
+
stepped('dmi', () => taStep.dmi(diLength, adxSmoothing), [high, low, close],
|
|
1442
|
+
(v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 3, () => taSeries.dmi(bars(), diLength, adxSmoothing)),
|
|
981
1443
|
kc: (source: Series, length: number, mult: number, useTrueRange?: boolean): [Series, Series, Series] =>
|
|
982
1444
|
taSeries.kc(bars(), source, length, mult, useTrueRange),
|
|
983
1445
|
kcw: (source: Series, length?: number, mult?: number, useTrueRange?: boolean): Series =>
|
|
@@ -990,11 +1452,21 @@ export const ta = strictNamespace('ta', outsideBarLoops({
|
|
|
990
1452
|
): [Series, Series, Series, Series, Series] =>
|
|
991
1453
|
taSeries.ichimoku(bars(), conversionPeriods, basePeriods, laggingSpan2Periods, displacement),
|
|
992
1454
|
/** `ta.highest(source, length)` or `ta.highest(length)` (source = high). */
|
|
993
|
-
highest: (sourceOrLength: Series | number, length?: number | Series): Series =>
|
|
994
|
-
typeof sourceOrLength === 'number' ?
|
|
1455
|
+
highest: (sourceOrLength: Series | number, length?: number | Series): Series => {
|
|
1456
|
+
const source = typeof sourceOrLength === 'number' ? high : sourceOrLength;
|
|
1457
|
+
const len = typeof sourceOrLength === 'number' ? sourceOrLength : length!;
|
|
1458
|
+
return typeof len === 'number'
|
|
1459
|
+
? stepped('highest', taStep.highest, [source], (v, i) => [at(v, 0, i), len], 1, () => taSeries.highest(source, len))
|
|
1460
|
+
: taSeries.highest(source, len);
|
|
1461
|
+
},
|
|
995
1462
|
/** `ta.lowest(source, length)` or `ta.lowest(length)` (source = low). */
|
|
996
|
-
lowest: (sourceOrLength: Series | number, length?: number | Series): Series =>
|
|
997
|
-
typeof sourceOrLength === 'number' ?
|
|
1463
|
+
lowest: (sourceOrLength: Series | number, length?: number | Series): Series => {
|
|
1464
|
+
const source = typeof sourceOrLength === 'number' ? low : sourceOrLength;
|
|
1465
|
+
const len = typeof sourceOrLength === 'number' ? sourceOrLength : length!;
|
|
1466
|
+
return typeof len === 'number'
|
|
1467
|
+
? stepped('lowest', taStep.lowest, [source], (v, i) => [at(v, 0, i), len], 1, () => taSeries.lowest(source, len))
|
|
1468
|
+
: taSeries.lowest(source, len);
|
|
1469
|
+
},
|
|
998
1470
|
/** `ta.highestbars(source, length)` or `ta.highestbars(length)` (source = high); offsets are 0 or negative. */
|
|
999
1471
|
highestbars: (sourceOrLength: Series | number, length?: number | Series): Series =>
|
|
1000
1472
|
typeof sourceOrLength === 'number'
|
|
@@ -1017,9 +1489,13 @@ export const ta = strictNamespace('ta', outsideBarLoops({
|
|
|
1017
1489
|
(source: Series, anchor: Series | undefined, stdev_mult: number): [Series, Series, Series];
|
|
1018
1490
|
},
|
|
1019
1491
|
/** `ta.vwma(source, length)` with the chart volume, as PineScript; a third argument gives another volume. */
|
|
1020
|
-
vwma: (source: Series, length: number, vol?: Series): Series =>
|
|
1492
|
+
vwma: (source: Series, length: number, vol?: Series): Series =>
|
|
1493
|
+
stepped('vwma', () => taStep.vwma(length), [source, vol ?? volume], (v, i) => [at(v, 0, i), at(v, 1, i)], 1, () =>
|
|
1494
|
+
taSeries.vwma(source, length, vol ?? volume)),
|
|
1021
1495
|
/** `ta.mfi(source, length)` with the chart volume (PineScript takes 2 arguments). */
|
|
1022
|
-
mfi: (source: Series, length: number): Series =>
|
|
1496
|
+
mfi: (source: Series, length: number): Series =>
|
|
1497
|
+
stepped('mfi', () => taStep.mfi(length), [source, volume], (v, i) => [at(v, 0, i), at(v, 1, i)], 1, () =>
|
|
1498
|
+
taSeries.mfi(source, length, volume)),
|
|
1023
1499
|
/** `ta.pivothigh(source, leftbars, rightbars)` or `ta.pivothigh(leftbars, rightbars)` (source = high).
|
|
1024
1500
|
* The value appears `rightbars` bars after the pivot bar, as in PineScript. */
|
|
1025
1501
|
pivothigh: (sourceOrLeft: Series | number, leftOrRight: number | Series, rightbars?: number | Series): Series =>
|
|
@@ -1052,6 +1528,30 @@ export function fixnan(source: Series | Array<string | undefined>): Series | Arr
|
|
|
1052
1528
|
return withPalette(fixnanValues(source), colorPalettes.get(source));
|
|
1053
1529
|
}
|
|
1054
1530
|
|
|
1531
|
+
/** PineScript `na(x)`. On a Series: a Series of 1 / 0, 1 where the value is na (NaN or +/-Infinity, #187). On a
|
|
1532
|
+
* single value (a number of a bar loop, a colour, a drawing): true when it is na. */
|
|
1533
|
+
export function na(x: Series): Series;
|
|
1534
|
+
export function na(x: unknown): boolean;
|
|
1535
|
+
export function na(x: unknown): Series | boolean {
|
|
1536
|
+
if (x instanceof Series) return Series.fromArray(ctxBars, x.toArray().map((v) => (isNA(v) ? 1 : 0)));
|
|
1537
|
+
return isNA(x);
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
/** PineScript `nz(source, replacement)`; replacement defaults to 0. On a Series: each na value (NaN or +/-Infinity)
|
|
1541
|
+
* becomes the replacement of the same bar, a number or a Series (#187). On single values: the value or the
|
|
1542
|
+
* replacement. */
|
|
1543
|
+
export function nz(source: Series, replacement?: number | Series): Series;
|
|
1544
|
+
export function nz<T>(source: T, replacement?: T): T;
|
|
1545
|
+
export function nz(source: unknown, replacement: unknown = 0): unknown {
|
|
1546
|
+
if (source instanceof Series) {
|
|
1547
|
+
const values = source.toArray();
|
|
1548
|
+
const rep = replacement instanceof Series ? replacement.toArray() : undefined;
|
|
1549
|
+
const r = rep ? 0 : Number(replacement);
|
|
1550
|
+
return Series.fromArray(ctxBars, values.map((v, i) => (isNA(v) ? (rep ? rep[i]! : r) : v)));
|
|
1551
|
+
}
|
|
1552
|
+
return nzValue(source, replacement);
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1055
1555
|
// ── Color helpers ────────────────────────────────────────────────────────────
|
|
1056
1556
|
|
|
1057
1557
|
/**
|
|
@@ -1082,10 +1582,50 @@ function colorWhen(cond: Series, colorTrue: string, colorFalse?: string): Array<
|
|
|
1082
1582
|
return withPalette(colors, palette);
|
|
1083
1583
|
}
|
|
1084
1584
|
|
|
1585
|
+
type GradientColor = string | null | undefined;
|
|
1586
|
+
|
|
1587
|
+
/** PineScript `color.from_gradient(value, bottom_value, top_value, bottom_color, top_color)`. With numbers and
|
|
1588
|
+
* colours: one colour. When an argument is a Series (or a colour is a per-bar array, as from `color.when`): the
|
|
1589
|
+
* colour of each bar, the rule of the number form applied to the values of that bar (#187), for
|
|
1590
|
+
* plot()/fill()/bgcolor()/barcolor(). */
|
|
1591
|
+
function fromGradient(
|
|
1592
|
+
value: number,
|
|
1593
|
+
bottom_value: number,
|
|
1594
|
+
top_value: number,
|
|
1595
|
+
bottom_color: GradientColor,
|
|
1596
|
+
top_color: GradientColor
|
|
1597
|
+
): string;
|
|
1598
|
+
function fromGradient(
|
|
1599
|
+
value: number | Series,
|
|
1600
|
+
bottom_value: number | Series,
|
|
1601
|
+
top_value: number | Series,
|
|
1602
|
+
bottom_color: GradientColor | readonly GradientColor[],
|
|
1603
|
+
top_color: GradientColor | readonly GradientColor[]
|
|
1604
|
+
): string | string[];
|
|
1605
|
+
function fromGradient(
|
|
1606
|
+
value: number | Series,
|
|
1607
|
+
bottom_value: number | Series,
|
|
1608
|
+
top_value: number | Series,
|
|
1609
|
+
bottom_color: GradientColor | readonly GradientColor[],
|
|
1610
|
+
top_color: GradientColor | readonly GradientColor[]
|
|
1611
|
+
): string | string[] {
|
|
1612
|
+
const args = [value, bottom_value, top_value];
|
|
1613
|
+
if (!args.some((a) => a instanceof Series) && !Array.isArray(bottom_color) && !Array.isArray(top_color)) {
|
|
1614
|
+
return colorCore.from_gradient(value as number, bottom_value as number, top_value as number, bottom_color as string, top_color as string);
|
|
1615
|
+
}
|
|
1616
|
+
const [v, lo, hi] = args.map((a) => (a instanceof Series ? a.toArray() : undefined));
|
|
1617
|
+
const at = (arr: number[] | undefined, num: number | Series, i: number) => (arr ? arr[i]! : (num as number));
|
|
1618
|
+
const colorAt = (c: GradientColor | readonly GradientColor[], i: number) => (Array.isArray(c) ? c[i] : (c as GradientColor));
|
|
1619
|
+
return Array.from({ length: ctxBars.length }, (_, i) =>
|
|
1620
|
+
colorCore.from_gradient(at(v, value, i), at(lo, bottom_value, i), at(hi, top_value, i), colorAt(bottom_color, i) as string, colorAt(top_color, i) as string)
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1085
1624
|
/** color.* plus `when` — per-bar conditional colors for plot()/fill()/bgcolor()/barcolor(). */
|
|
1086
1625
|
export const color = strictNamespace('color', {
|
|
1087
1626
|
...colorCore,
|
|
1088
1627
|
new: colorCore.new_color,
|
|
1628
|
+
from_gradient: fromGradient,
|
|
1089
1629
|
when: colorWhen,
|
|
1090
1630
|
});
|
|
1091
1631
|
|
|
@@ -1149,16 +1689,38 @@ function plotImpl(series: Series, title: string = DEFAULT_OUTPUT_TITLES.plot, op
|
|
|
1149
1689
|
c.outputs.push({ kind: 'plot', id, title });
|
|
1150
1690
|
const values = series.toArray();
|
|
1151
1691
|
const b = bars();
|
|
1152
|
-
|
|
1692
|
+
let data: TimeValue[] = [];
|
|
1153
1693
|
// offset (PineScript plot(offset = k)): the value and colour of bar i are drawn on bar i + k; bars past the
|
|
1154
1694
|
// chart's first or last bar are dropped
|
|
1155
1695
|
const off = Math.trunc(options.offset ?? 0);
|
|
1156
|
-
|
|
1696
|
+
let j0 = Math.max(0, off);
|
|
1697
|
+
// An incremental run (#196) keeps the points of the bars whose values did not change; pos[j]: points before bar j
|
|
1698
|
+
const run = incremental.active;
|
|
1699
|
+
const slot = run ? run.plotSlot++ : -1;
|
|
1700
|
+
let pos: number[] = [];
|
|
1701
|
+
const prev = run?.mode === 'resume' ? run.prev?.plots[slot] : undefined;
|
|
1702
|
+
const sig = `${off}|${perBar ? 'colors' : ''}`;
|
|
1703
|
+
if (prev && prev.sig === sig) {
|
|
1704
|
+
const from = Math.max(j0, run!.from + Math.min(0, off));
|
|
1705
|
+
if (from < prev.pos.length) {
|
|
1706
|
+
// the points are given to the host: a copy of the kept ones (the positions are the run's own)
|
|
1707
|
+
data = prev.data.slice(0, prev.pos[from]);
|
|
1708
|
+
pos = prev.pos;
|
|
1709
|
+
pos.length = from;
|
|
1710
|
+
j0 = from;
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
for (let j = 0; j < j0; j++) pos[j] ??= 0;
|
|
1714
|
+
let j = j0;
|
|
1715
|
+
for (; j < b.length && j - off < b.length; j++) {
|
|
1716
|
+
pos[j] = data.length;
|
|
1157
1717
|
const i = j - off;
|
|
1158
1718
|
const value = values[i];
|
|
1159
1719
|
if (value === undefined || !Number.isFinite(value)) continue; // PineScript plots +/-Infinity as na
|
|
1160
1720
|
data.push(perBar ? { time: b[j]!.time, value, color: perBar[i] } : { time: b[j]!.time, value });
|
|
1161
1721
|
}
|
|
1722
|
+
for (; j <= b.length; j++) pos[j] = data.length;
|
|
1723
|
+
if (run) run.next.plots[slot] = { data, pos, sig };
|
|
1162
1724
|
c.plots[id] = data;
|
|
1163
1725
|
return { id, kind: 'plot' };
|
|
1164
1726
|
}
|
|
@@ -1805,21 +2367,107 @@ interface HistoryBuffer {
|
|
|
1805
2367
|
barMax: number;
|
|
1806
2368
|
}
|
|
1807
2369
|
|
|
2370
|
+
/** The outer variables an eachBar() callback changes, by callback source (the same callback on every run). */
|
|
2371
|
+
const outerWritesCache = new Map<string, string[]>();
|
|
2372
|
+
function outerWritesOf(fn: (...args: never[]) => unknown): string[] {
|
|
2373
|
+
const src = fn.toString();
|
|
2374
|
+
let found = outerWritesCache.get(src);
|
|
2375
|
+
if (!found) {
|
|
2376
|
+
found = outerWrites(src);
|
|
2377
|
+
if (outerWritesCache.size > 500) outerWritesCache.clear();
|
|
2378
|
+
outerWritesCache.set(src, found);
|
|
2379
|
+
}
|
|
2380
|
+
return found;
|
|
2381
|
+
}
|
|
2382
|
+
|
|
1808
2383
|
export function eachBar(fn: (c: BarContext) => number | boolean | void): Series {
|
|
2384
|
+
return runBlock(fn);
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
/** State an eachBar() block keeps in its incremental checkpoint besides the script's (the strategy engine). */
|
|
2388
|
+
interface BlockExtra {
|
|
2389
|
+
save(): unknown;
|
|
2390
|
+
restore(state: unknown): void;
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
function runBlock(
|
|
2394
|
+
fn: (c: BarContext) => number | boolean | void,
|
|
2395
|
+
extra?: BlockExtra,
|
|
2396
|
+
/** The script's own callback, when fn wraps it (strategy.eachBar) */
|
|
2397
|
+
own: (c: BarContext) => number | boolean | void = fn
|
|
2398
|
+
): Series {
|
|
1809
2399
|
collector(); // same lifecycle guard (and error message) as the declaration calls
|
|
1810
2400
|
const b = bars();
|
|
1811
2401
|
// An intrabar run ends on its bar: the chart's last bar stays the main run's.
|
|
1812
2402
|
const lastIndex = intrabarRun ? intrabarRun.lastBarIndex : b.length - 1;
|
|
1813
|
-
|
|
2403
|
+
let out: number[] = new Array(b.length).fill(NaN);
|
|
1814
2404
|
const cache = new Map<Series, number[]>();
|
|
1815
|
-
|
|
2405
|
+
// history buffers by source: the slot of a Series made by the script (matched across incremental runs), or the Series
|
|
2406
|
+
const buffers = new Map<unknown, HistoryBuffer>();
|
|
2407
|
+
const bufferKey = (src: Series): unknown => incremental.slotOf(src) ?? src;
|
|
1816
2408
|
let pendingError: { bar: number; barsBack: number } | null = null;
|
|
1817
2409
|
let idx = 0;
|
|
2410
|
+
|
|
2411
|
+
// An incremental run (#196): the block is matched by its order with the previous run; a resumed block starts on the
|
|
2412
|
+
// forming bar of the previous run, from its state at the end of the last confirmed bar.
|
|
2413
|
+
const run = incremental.active;
|
|
2414
|
+
const slot = run ? (run.block < 0 ? run.blockSlot++ : -1) : -1;
|
|
2415
|
+
if (run && slot < 0) incremental.nonIncremental('eachBar() inside eachBar()');
|
|
2416
|
+
if (run?.mode === 'record') {
|
|
2417
|
+
// a variable outside the callback that it changes is not put back by a resumed run: use persist()
|
|
2418
|
+
const written = outerWritesOf(own);
|
|
2419
|
+
if (written.length) incremental.nonIncremental(`eachBar() callback changes ${written.join(', ')} outside it (use persist())`);
|
|
2420
|
+
}
|
|
2421
|
+
let start = 0;
|
|
2422
|
+
let checkpoint: incremental.BlockCheckpoint | null = null;
|
|
2423
|
+
if (run?.mode === 'resume') {
|
|
2424
|
+
const prev = run.prev?.blocks[slot];
|
|
2425
|
+
if (!prev) incremental.nonIncremental('a new eachBar() block');
|
|
2426
|
+
start = run.from;
|
|
2427
|
+
// the block's own array of the previous run: its first values are kept in place
|
|
2428
|
+
out = prev!.out;
|
|
2429
|
+
out.length = b.length;
|
|
2430
|
+
for (let i = start; i < b.length; i++) out[i] = NaN;
|
|
2431
|
+
checkpoint = prev!.checkpoint;
|
|
2432
|
+
// the block that draws puts the registry back to the last confirmed bar
|
|
2433
|
+
if (run.prev!.drawingBlock === slot && checkpoint.drawings) restoreDrawingCheckpoint(checkpoint.drawings);
|
|
2434
|
+
if (extra) extra.restore(checkpoint.extra);
|
|
2435
|
+
for (const [key, state] of checkpoint.buffers) buffers.set(key, { ...state });
|
|
2436
|
+
pendingError = checkpoint.pendingError ? { ...checkpoint.pendingError } : null;
|
|
2437
|
+
for (const [box, value] of checkpoint.boxes) {
|
|
2438
|
+
const ref = run.boxes[box];
|
|
2439
|
+
if (!ref) incremental.nonIncremental('a persist() box of the previous run is missing');
|
|
2440
|
+
ref!.set(incremental.copyValue(value));
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2443
|
+
/** State at the end of the bar just run: the boxes this block owns, the history buffers */
|
|
2444
|
+
const takeCheckpoint = (): incremental.BlockCheckpoint => {
|
|
2445
|
+
const boxes = new Map<number, unknown>();
|
|
2446
|
+
run!.boxes.forEach((ref, box) => {
|
|
2447
|
+
if (run!.next.boxOwner[box] === slot) boxes.set(box, incremental.copyValue(ref.get()));
|
|
2448
|
+
});
|
|
2449
|
+
const kept = new Map<unknown, HistoryBuffer>();
|
|
2450
|
+
for (const [key, state] of buffers) kept.set(key, { ...state });
|
|
2451
|
+
return {
|
|
2452
|
+
boxes,
|
|
2453
|
+
buffers: kept,
|
|
2454
|
+
pendingError: pendingError ? { ...pendingError } : null,
|
|
2455
|
+
drawings: saveDrawingCheckpoint(),
|
|
2456
|
+
extra: extra?.save(),
|
|
2457
|
+
};
|
|
2458
|
+
};
|
|
2459
|
+
// the last confirmed bar is before the first bar run (a new run of one bar): its state is the initial one
|
|
2460
|
+
if (run && b.length - 2 < start && !checkpoint) checkpoint = takeCheckpoint();
|
|
2461
|
+
|
|
1818
2462
|
const ctx: BarContext = {
|
|
1819
2463
|
get i() {
|
|
1820
2464
|
return idx;
|
|
1821
2465
|
},
|
|
1822
|
-
n
|
|
2466
|
+
get n() {
|
|
2467
|
+
// the bar count changes with each new bar: a value read from it is not kept by an incremental run
|
|
2468
|
+
if (run) incremental.nonIncremental('BarContext.n');
|
|
2469
|
+
return lastIndex + 1;
|
|
2470
|
+
},
|
|
1823
2471
|
get time() {
|
|
1824
2472
|
return b[idx]!.time as number;
|
|
1825
2473
|
},
|
|
@@ -1843,8 +2491,8 @@ export function eachBar(fn: (c: BarContext) => number | boolean | void): Series
|
|
|
1843
2491
|
if (!src.fullHistory && offset > 0) {
|
|
1844
2492
|
const k = Math.floor(offset);
|
|
1845
2493
|
if (k > MAX_BARS_BACK) throw new Error(historyError(idx, k));
|
|
1846
|
-
let buffer = buffers.get(src);
|
|
1847
|
-
if (!buffer) buffers.set(src, (buffer = { size: HISTORY_START, previous: 0, barMax: 0 }));
|
|
2494
|
+
let buffer = buffers.get(bufferKey(src));
|
|
2495
|
+
if (!buffer) buffers.set(bufferKey(src), (buffer = { size: HISTORY_START, previous: 0, barMax: 0 }));
|
|
1848
2496
|
if (k > buffer.barMax) buffer.barMax = k;
|
|
1849
2497
|
}
|
|
1850
2498
|
let vals = cache.get(src);
|
|
@@ -1860,8 +2508,10 @@ export function eachBar(fn: (c: BarContext) => number | boolean | void): Series
|
|
|
1860
2508
|
return j >= 0 ? out[j]! : NaN;
|
|
1861
2509
|
},
|
|
1862
2510
|
};
|
|
2511
|
+
const outerBlock = run ? run.block : -1;
|
|
2512
|
+
if (run) run.block = slot;
|
|
1863
2513
|
try {
|
|
1864
|
-
for (idx =
|
|
2514
|
+
for (idx = start; idx < b.length; idx++) {
|
|
1865
2515
|
if (pendingError && pendingError.bar === idx) throw new Error(historyError(idx, pendingError.barsBack));
|
|
1866
2516
|
currentBar = idx;
|
|
1867
2517
|
setDrawingBar(idx, idx === lastIndex, lastIndex);
|
|
@@ -1878,11 +2528,15 @@ export function eachBar(fn: (c: BarContext) => number | boolean | void): Series
|
|
|
1878
2528
|
buffer.previous = k;
|
|
1879
2529
|
buffer.barMax = 0;
|
|
1880
2530
|
}
|
|
2531
|
+
// the last confirmed bar (the forming bar is the last one): its state is where the next update starts
|
|
2532
|
+
if (run && idx === b.length - 2) checkpoint = takeCheckpoint();
|
|
1881
2533
|
}
|
|
1882
2534
|
} finally {
|
|
1883
2535
|
currentBar = -1;
|
|
1884
2536
|
setDrawingBar(null);
|
|
2537
|
+
if (run) run.block = outerBlock;
|
|
1885
2538
|
}
|
|
2539
|
+
if (run) run.next.blocks[slot] = { out, checkpoint: checkpoint! };
|
|
1886
2540
|
return Series.fromArray(ctxBars, out);
|
|
1887
2541
|
}
|
|
1888
2542
|
|
|
@@ -2020,7 +2674,14 @@ function strategyEachBar(fn: (c: BarContext) => number | boolean | void): Series
|
|
|
2020
2674
|
});
|
|
2021
2675
|
}
|
|
2022
2676
|
const recorder = scriptOptions.recorder;
|
|
2023
|
-
|
|
2677
|
+
// an incremental run keeps the engine state at the last confirmed bar: the engine has to save and restore it
|
|
2678
|
+
if (incremental.active) {
|
|
2679
|
+
if (recorder) incremental.nonIncremental('a strategy with intrabar runs (recorder)');
|
|
2680
|
+
if (!engine.save || !engine.restore) incremental.nonIncremental('a strategy engine without save() / restore()');
|
|
2681
|
+
}
|
|
2682
|
+
const extra: BlockExtra | undefined =
|
|
2683
|
+
engine.save && engine.restore ? { save: () => engine.save!(), restore: (s) => engine.restore!(s) } : undefined;
|
|
2684
|
+
return runBlock((c) => {
|
|
2024
2685
|
engine.processBar(c.i);
|
|
2025
2686
|
if (recorder) {
|
|
2026
2687
|
recorder.varipStarts[c.i] = [...recorder.varip];
|
|
@@ -2034,7 +2695,7 @@ function strategyEachBar(fn: (c: BarContext) => number | boolean | void): Series
|
|
|
2034
2695
|
run.engine = engine;
|
|
2035
2696
|
engine.processClose(c.i);
|
|
2036
2697
|
}
|
|
2037
|
-
});
|
|
2698
|
+
}, extra, fn);
|
|
2038
2699
|
}
|
|
2039
2700
|
|
|
2040
2701
|
/** The engine of a main run that records its answers (see {@link StrategyRecorder}). */
|
|
@@ -2625,7 +3286,10 @@ function timeCloseFunction(tf?: string, session?: string, timezone?: string): Se
|
|
|
2625
3286
|
let runNow = NaN;
|
|
2626
3287
|
|
|
2627
3288
|
/** PineScript `timenow`: the current time in ms, the same on every bar of a run (`ChartContext.now`). */
|
|
2628
|
-
export const timenow = new Series(ctxBars, () =>
|
|
3289
|
+
export const timenow = new Series(ctxBars, () => {
|
|
3290
|
+
incremental.nonIncremental('timenow');
|
|
3291
|
+
return runNow;
|
|
3292
|
+
});
|
|
2629
3293
|
|
|
2630
3294
|
export const time = callableSeries(new Series(ctxBars, (b) => barTime(b)), timeFunction);
|
|
2631
3295
|
|
|
@@ -2722,6 +3386,8 @@ export const barstate = {
|
|
|
2722
3386
|
get isnew(): Series { return namedFlag('isnew', (i, n) => !lastRealtime(i, n) || chartCtx.lastBarNew !== false); },
|
|
2723
3387
|
get islastconfirmedhistory(): Series {
|
|
2724
3388
|
return namedFlag('islastconfirmedhistory', (i, n) => {
|
|
3389
|
+
// moves to the next bar with each new bar: not kept by an incremental run (#196)
|
|
3390
|
+
incremental.nonIncremental('barstate.islastconfirmedhistory');
|
|
2725
3391
|
const lastConfirmed = chartCtx.lastBarConfirmed === true && chartCtx.realtime !== true;
|
|
2726
3392
|
const count = chartBarCount(n);
|
|
2727
3393
|
return i === (lastConfirmed ? count - 1 : count - 2);
|
|
@@ -2804,6 +3470,8 @@ function security<T extends SecurityValue>(
|
|
|
2804
3470
|
ignore_invalid_symbol: boolean = false
|
|
2805
3471
|
): T extends unknown[] ? Series[] : Series {
|
|
2806
3472
|
collector();
|
|
3473
|
+
// other bars merged on the chart bars: not resumed by an incremental run (#196)
|
|
3474
|
+
incremental.nonIncremental('request.security');
|
|
2807
3475
|
const heikin = symbol.startsWith(HEIKIN_ASHI);
|
|
2808
3476
|
const base = heikin ? symbol.slice(HEIKIN_ASHI.length) : symbol;
|
|
2809
3477
|
const chart = chartTimeframe();
|
|
@@ -2982,6 +3650,7 @@ function security_lower_tf<T extends SecurityValue>(
|
|
|
2982
3650
|
ignore_invalid_symbol: boolean = false
|
|
2983
3651
|
): T extends unknown[] ? number[][][] : number[][] {
|
|
2984
3652
|
collector();
|
|
3653
|
+
incremental.nonIncremental('request.security_lower_tf');
|
|
2985
3654
|
const heikin = symbol.startsWith(HEIKIN_ASHI);
|
|
2986
3655
|
const base = heikin ? symbol.slice(HEIKIN_ASHI.length) : symbol;
|
|
2987
3656
|
const chart = chartTimeframe();
|
|
@@ -3338,7 +4007,7 @@ export const alert = Object.assign(alertImpl, {
|
|
|
3338
4007
|
|
|
3339
4008
|
// ── Convenience re-exports ───────────────────────────────────────────────────
|
|
3340
4009
|
|
|
3341
|
-
export { Series, BarData
|
|
4010
|
+
export { Series, BarData };
|
|
3342
4011
|
export { STRATEGY_DEFAULTS } from '../strategy/index.js';
|
|
3343
4012
|
export type {
|
|
3344
4013
|
StrategyCloseAllOptions,
|