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.
Files changed (178) hide show
  1. package/dist/cjs/check/format.d.ts.map +1 -1
  2. package/dist/cjs/check/format.js +5 -0
  3. package/dist/cjs/check/format.js.map +2 -2
  4. package/dist/cjs/check/harness.d.ts +24 -0
  5. package/dist/cjs/check/harness.d.ts.map +1 -1
  6. package/dist/cjs/check/harness.js +33 -0
  7. package/dist/cjs/check/harness.js.map +2 -2
  8. package/dist/cjs/check/index.d.ts +9 -0
  9. package/dist/cjs/check/index.d.ts.map +1 -1
  10. package/dist/cjs/check/index.js +7 -0
  11. package/dist/cjs/check/index.js.map +2 -2
  12. package/dist/cjs/color/index.d.ts.map +1 -1
  13. package/dist/cjs/color/index.js +19 -2
  14. package/dist/cjs/color/index.js.map +2 -2
  15. package/dist/cjs/color/parse.d.ts +8 -6
  16. package/dist/cjs/color/parse.d.ts.map +1 -1
  17. package/dist/cjs/color/parse.js +15 -2
  18. package/dist/cjs/color/parse.js.map +2 -2
  19. package/dist/cjs/drawing/registry.d.ts +32 -0
  20. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  21. package/dist/cjs/drawing/registry.js +179 -45
  22. package/dist/cjs/drawing/registry.js.map +2 -2
  23. package/dist/cjs/index.d.ts +2 -1
  24. package/dist/cjs/index.d.ts.map +1 -1
  25. package/dist/cjs/index.js +3 -1
  26. package/dist/cjs/index.js.map +2 -2
  27. package/dist/cjs/runtime/series.d.ts +16 -0
  28. package/dist/cjs/runtime/series.d.ts.map +1 -1
  29. package/dist/cjs/runtime/series.js +19 -2
  30. package/dist/cjs/runtime/series.js.map +2 -2
  31. package/dist/cjs/script/closure-writes.d.ts +16 -0
  32. package/dist/cjs/script/closure-writes.d.ts.map +1 -0
  33. package/dist/cjs/script/closure-writes.js +435 -0
  34. package/dist/cjs/script/closure-writes.js.map +7 -0
  35. package/dist/cjs/script/globals.d.ts +2 -0
  36. package/dist/cjs/script/globals.d.ts.map +1 -1
  37. package/dist/cjs/script/globals.js.map +1 -1
  38. package/dist/cjs/script/incremental.d.ts +140 -0
  39. package/dist/cjs/script/incremental.d.ts.map +1 -0
  40. package/dist/cjs/script/incremental.js +142 -0
  41. package/dist/cjs/script/incremental.js.map +7 -0
  42. package/dist/cjs/script/index.d.ts +116 -31
  43. package/dist/cjs/script/index.d.ts.map +1 -1
  44. package/dist/cjs/script/index.js +471 -25
  45. package/dist/cjs/script/index.js.map +3 -3
  46. package/dist/cjs/session/bars.d.ts +3 -0
  47. package/dist/cjs/session/bars.d.ts.map +1 -1
  48. package/dist/cjs/session/bars.js +10 -3
  49. package/dist/cjs/session/bars.js.map +2 -2
  50. package/dist/cjs/session/calendar.d.ts +19 -6
  51. package/dist/cjs/session/calendar.d.ts.map +1 -1
  52. package/dist/cjs/session/calendar.js +57 -18
  53. package/dist/cjs/session/calendar.js.map +2 -2
  54. package/dist/cjs/session/context.d.ts.map +1 -1
  55. package/dist/cjs/session/context.js +25 -8
  56. package/dist/cjs/session/context.js.map +2 -2
  57. package/dist/cjs/str/numberformat.d.ts.map +1 -1
  58. package/dist/cjs/str/numberformat.js +23 -0
  59. package/dist/cjs/str/numberformat.js.map +2 -2
  60. package/dist/cjs/strategy/index.d.ts +8 -0
  61. package/dist/cjs/strategy/index.d.ts.map +1 -1
  62. package/dist/cjs/strategy/index.js.map +1 -1
  63. package/dist/cjs/ta/running-sum.d.ts.map +1 -1
  64. package/dist/cjs/ta/running-sum.js +72 -3
  65. package/dist/cjs/ta/running-sum.js.map +2 -2
  66. package/dist/cjs/ta/step.d.ts +476 -0
  67. package/dist/cjs/ta/step.d.ts.map +1 -0
  68. package/dist/cjs/ta/step.js +1000 -0
  69. package/dist/cjs/ta/step.js.map +7 -0
  70. package/dist/cjs/ta-series.d.ts +1 -1
  71. package/dist/cjs/ta-series.js +3 -3
  72. package/dist/cjs/ta-series.js.map +1 -1
  73. package/dist/cjs/time/timezone.d.ts +13 -1
  74. package/dist/cjs/time/timezone.d.ts.map +1 -1
  75. package/dist/cjs/time/timezone.js +52 -13
  76. package/dist/cjs/time/timezone.js.map +2 -2
  77. package/dist/esm/check/format.d.ts.map +1 -1
  78. package/dist/esm/check/format.js +5 -0
  79. package/dist/esm/check/format.js.map +1 -1
  80. package/dist/esm/check/harness.d.ts +24 -0
  81. package/dist/esm/check/harness.d.ts.map +1 -1
  82. package/dist/esm/check/harness.js +40 -6
  83. package/dist/esm/check/harness.js.map +1 -1
  84. package/dist/esm/check/index.d.ts +9 -0
  85. package/dist/esm/check/index.d.ts.map +1 -1
  86. package/dist/esm/check/index.js +11 -1
  87. package/dist/esm/check/index.js.map +1 -1
  88. package/dist/esm/color/index.d.ts.map +1 -1
  89. package/dist/esm/color/index.js +23 -3
  90. package/dist/esm/color/index.js.map +1 -1
  91. package/dist/esm/color/parse.d.ts +8 -6
  92. package/dist/esm/color/parse.d.ts.map +1 -1
  93. package/dist/esm/color/parse.js +19 -2
  94. package/dist/esm/color/parse.js.map +1 -1
  95. package/dist/esm/drawing/registry.d.ts +32 -0
  96. package/dist/esm/drawing/registry.d.ts.map +1 -1
  97. package/dist/esm/drawing/registry.js +214 -51
  98. package/dist/esm/drawing/registry.js.map +1 -1
  99. package/dist/esm/index.d.ts +2 -1
  100. package/dist/esm/index.d.ts.map +1 -1
  101. package/dist/esm/index.js +3 -1
  102. package/dist/esm/index.js.map +1 -1
  103. package/dist/esm/runtime/series.d.ts +16 -0
  104. package/dist/esm/runtime/series.d.ts.map +1 -1
  105. package/dist/esm/runtime/series.js +21 -1
  106. package/dist/esm/runtime/series.js.map +1 -1
  107. package/dist/esm/script/closure-writes.d.ts +16 -0
  108. package/dist/esm/script/closure-writes.d.ts.map +1 -0
  109. package/dist/esm/script/closure-writes.js +323 -0
  110. package/dist/esm/script/closure-writes.js.map +1 -0
  111. package/dist/esm/script/globals.d.ts +2 -0
  112. package/dist/esm/script/globals.d.ts.map +1 -1
  113. package/dist/esm/script/globals.js.map +1 -1
  114. package/dist/esm/script/incremental.d.ts +140 -0
  115. package/dist/esm/script/incremental.d.ts.map +1 -0
  116. package/dist/esm/script/incremental.js +173 -0
  117. package/dist/esm/script/incremental.js.map +1 -0
  118. package/dist/esm/script/index.d.ts +116 -31
  119. package/dist/esm/script/index.d.ts.map +1 -1
  120. package/dist/esm/script/index.js +561 -24
  121. package/dist/esm/script/index.js.map +1 -1
  122. package/dist/esm/session/bars.d.ts +3 -0
  123. package/dist/esm/session/bars.d.ts.map +1 -1
  124. package/dist/esm/session/bars.js +11 -3
  125. package/dist/esm/session/bars.js.map +1 -1
  126. package/dist/esm/session/calendar.d.ts +19 -6
  127. package/dist/esm/session/calendar.d.ts.map +1 -1
  128. package/dist/esm/session/calendar.js +74 -21
  129. package/dist/esm/session/calendar.js.map +1 -1
  130. package/dist/esm/session/context.d.ts.map +1 -1
  131. package/dist/esm/session/context.js +34 -9
  132. package/dist/esm/session/context.js.map +1 -1
  133. package/dist/esm/str/numberformat.d.ts.map +1 -1
  134. package/dist/esm/str/numberformat.js +30 -0
  135. package/dist/esm/str/numberformat.js.map +1 -1
  136. package/dist/esm/strategy/index.d.ts +8 -0
  137. package/dist/esm/strategy/index.d.ts.map +1 -1
  138. package/dist/esm/ta/running-sum.d.ts.map +1 -1
  139. package/dist/esm/ta/running-sum.js +79 -4
  140. package/dist/esm/ta/running-sum.js.map +1 -1
  141. package/dist/esm/ta/step.d.ts +476 -0
  142. package/dist/esm/ta/step.d.ts.map +1 -0
  143. package/dist/esm/ta/step.js +1089 -0
  144. package/dist/esm/ta/step.js.map +1 -0
  145. package/dist/esm/ta-series.d.ts +1 -1
  146. package/dist/esm/ta-series.js +4 -4
  147. package/dist/esm/time/timezone.d.ts +13 -1
  148. package/dist/esm/time/timezone.d.ts.map +1 -1
  149. package/dist/esm/time/timezone.js +78 -17
  150. package/dist/esm/time/timezone.js.map +1 -1
  151. package/package.json +1 -1
  152. package/skills/oakscript/SKILL.md +2 -2
  153. package/skills/oakscript/reference/check.md +5 -0
  154. package/skills/oakscript/reference/errors.md +0 -1
  155. package/skills/oakscript/reference/pine-differences.md +1 -2
  156. package/skills/oakscript/reference/plotting.md +5 -7
  157. package/skills/oakscript/reference/script-structure.md +6 -0
  158. package/src/check/format.ts +7 -0
  159. package/src/check/harness.ts +57 -0
  160. package/src/check/index.ts +16 -0
  161. package/src/color/index.ts +22 -3
  162. package/src/color/parse.ts +28 -3
  163. package/src/drawing/registry.ts +233 -50
  164. package/src/index.ts +4 -1
  165. package/src/runtime/series.ts +33 -1
  166. package/src/script/closure-writes.ts +279 -0
  167. package/src/script/globals.ts +2 -0
  168. package/src/script/incremental.ts +246 -0
  169. package/src/script/index.ts +695 -26
  170. package/src/session/bars.ts +11 -3
  171. package/src/session/calendar.ts +84 -21
  172. package/src/session/context.ts +36 -9
  173. package/src/str/numberformat.ts +29 -0
  174. package/src/strategy/index.ts +8 -0
  175. package/src/ta/running-sum.ts +78 -3
  176. package/src/ta/step.ts +1145 -0
  177. package/src/ta-series.ts +4 -4
  178. package/src/time/timezone.ts +76 -15
@@ -0,0 +1,140 @@
1
+ /**
2
+ * State of an incremental script run (#196, createScriptRun).
3
+ *
4
+ * A run executes the script body again on each update, but what the body creates (Series, ta.* calls, eachBar()
5
+ * blocks, plots, persist() boxes) is matched by creation order with the previous run, and keeps the values of the bars
6
+ * before the first bar to calculate (`from`, the forming bar of the previous run). Only the bars from `from` on are
7
+ * calculated:
8
+ * - a Series: its values before `from` are those of the previous run, the others are computed
9
+ * - a ta.* call with a {@link taStep} machine: the machine holds the state of the last confirmed bar and steps the newest
10
+ * bars only
11
+ * - an eachBar() block: its callback runs from `from` on, its persist() boxes and history buffers put back to their
12
+ * state at the end of the last confirmed bar
13
+ * - a plot: the points of the bars before `from` are kept
14
+ *
15
+ * A feature that is not resumed (drawings, other symbols, strategies, varip, values that depend on the bar count)
16
+ * calls {@link nonIncremental}: a recorded (full) run marks the run so that the next updates are full runs, a resumed
17
+ * run stops ({@link ResumeAborted}) and the host runs it in full.
18
+ *
19
+ * @internal
20
+ */
21
+ import { Series } from '../runtime/series.js';
22
+ import type { Bar } from '../types/index.js';
23
+ import type { BarStep } from '../ta/step.js';
24
+ import { type DrawingCheckpoint, type DrawingKind } from '../drawing/registry.js';
25
+ /** A resumed run met a feature it does not resume: the update is calculated by a full run. */
26
+ export declare class ResumeAborted extends Error {
27
+ readonly reason: string;
28
+ constructor(reason: string);
29
+ }
30
+ /** History buffer state of one source of an eachBar() block (RE10008). */
31
+ export interface BufferState {
32
+ size: number;
33
+ previous: number;
34
+ barMax: number;
35
+ }
36
+ /** State of an eachBar() block at the end of the last confirmed bar. */
37
+ export interface BlockCheckpoint {
38
+ /** Values of the persist() boxes the block owns, by box slot */
39
+ boxes: Map<number, unknown>;
40
+ /** History buffers by source key (Series slot, or the Series of the chart) */
41
+ buffers: Map<unknown, BufferState>;
42
+ pendingError: {
43
+ bar: number;
44
+ barsBack: number;
45
+ } | null;
46
+ /** The drawing registry and its objects (the block that draws puts it back) */
47
+ drawings: DrawingCheckpoint | null;
48
+ /** State the block keeps besides the script's (the strategy engine of strategy.eachBar()) */
49
+ extra?: unknown;
50
+ }
51
+ export interface BlockMemory {
52
+ out: number[];
53
+ checkpoint: BlockCheckpoint;
54
+ }
55
+ /** A ta.* call with a machine: its machines (state of the last confirmed bar) and output values. */
56
+ export interface TaMemory {
57
+ kind: string;
58
+ machine: BarStep<unknown[], unknown>;
59
+ outs: number[][];
60
+ }
61
+ /** A plot's points and, for each bar j, the number of points before it. */
62
+ export interface PlotMemory {
63
+ data: Array<{
64
+ time: number;
65
+ value: number;
66
+ color?: string;
67
+ }>;
68
+ pos: number[];
69
+ /** Offset and colour kind of the plot: the points are kept only for the same */
70
+ sig: string;
71
+ }
72
+ /** What a run keeps for the next update. */
73
+ export interface RunMemory {
74
+ /** Bars of that run */
75
+ n: number;
76
+ /** Values of the Series the script created, by slot */
77
+ series: Array<number[] | undefined>;
78
+ /** Values of the Series that live across runs (the chart's open, close, bar_index...) */
79
+ kept: WeakMap<Series, number[]>;
80
+ ta: Array<TaMemory | undefined>;
81
+ blocks: Array<BlockMemory | undefined>;
82
+ plots: Array<PlotMemory | undefined>;
83
+ /** Block slot that owns each persist() box */
84
+ boxOwner: Array<number | undefined>;
85
+ /** Drawing objects created in the script body (outside the callbacks), by creation order */
86
+ bodyDrawings: Array<{
87
+ kind: DrawingKind;
88
+ obj: object;
89
+ } | undefined>;
90
+ /** The eachBar() block that uses the drawings (one per script), undefined when none */
91
+ drawingBlock: number | undefined;
92
+ }
93
+ export declare function emptyMemory(n: number): RunMemory;
94
+ export interface ActiveRun {
95
+ mode: 'record' | 'resume';
96
+ /** First bar calculated (0 when recording) */
97
+ from: number;
98
+ bars: readonly Bar[];
99
+ prev: RunMemory | null;
100
+ next: RunMemory;
101
+ seriesSlot: number;
102
+ taSlot: number;
103
+ blockSlot: number;
104
+ plotSlot: number;
105
+ boxSlot: number;
106
+ drawSlot: number;
107
+ /** The eachBar() block running (its slot), -1 outside */
108
+ block: number;
109
+ /** Getters and setters of the persist() boxes of this run, by slot */
110
+ boxes: Array<{
111
+ get(): unknown;
112
+ set(v: unknown): void;
113
+ }>;
114
+ /** Why the next updates are full runs (null: they can be resumed) */
115
+ blocked: string | null;
116
+ }
117
+ /** The incremental run being executed (null outside createScriptRun or in a plain executeScript). */
118
+ export declare let active: ActiveRun | null;
119
+ /** Slot of a Series created by the script in this run, or undefined (a chart Series, or one made inside eachBar) */
120
+ export declare function slotOf(series: Series): number | undefined;
121
+ /** Starts an incremental run (the script body runs next); returns the run to give to {@link endRun}. */
122
+ export declare function beginRun(run: ActiveRun): ActiveRun | null;
123
+ /** Ends the run started by {@link beginRun}, putting back the outer one (a nested run) or none. */
124
+ export declare function endRun(outer: ActiveRun | null): void;
125
+ /** The script body has run: the run builds its outputs (they read the drawings without being part of the script). */
126
+ export declare function bodyDone(): void;
127
+ /** No incremental run inside (a nested executeScript, e.g. other symbols): the outer one is put back by endRun(). */
128
+ export declare function suspendRun(): ActiveRun | null;
129
+ /**
130
+ * A copy of a persist() value for a checkpoint: the forming bar may change an array or an object in place after it was
131
+ * taken. Plain data is copied (structuredClone); a value that cannot be copied (a function, a class instance such as
132
+ * a drawing) is kept as it is and marks the run as not resumable.
133
+ */
134
+ export declare function copyValue(value: unknown): unknown;
135
+ /**
136
+ * A feature an incremental run does not resume: a recorded run keeps its result (it is a full run) and marks the next
137
+ * updates as full runs; a resumed run stops.
138
+ */
139
+ export declare function nonIncremental(reason: string): void;
140
+ //# sourceMappingURL=incremental.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"incremental.d.ts","sourceRoot":"","sources":["../../../src/script/incremental.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,MAAM,EAA0C,MAAM,sBAAsB,CAAC;AACtF,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAuC,KAAK,iBAAiB,EAAE,KAAK,WAAW,EAAwB,MAAM,wBAAwB,CAAC;AAE7I,8FAA8F;AAC9F,qBAAa,aAAc,SAAQ,KAAK;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;CAGpC;AAED,0EAA0E;AAC1E,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,wEAAwE;AACxE,MAAM,WAAW,eAAe;IAC9B,gEAAgE;IAChE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC5B,8EAA8E;IAC9E,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACnC,YAAY,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ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AI,CAIpD;AAED,qHAAqH;AACrH,wBAAgB,QAAQ,IAAI,IAAI,CAE/B;AAED,qHAAqH;AACrH,wBAAgB,UAAU,IAAI,SAAS,GAAG,IAAI,CAM7C;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAkBjD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAKnD"}
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var incremental_exports = {};
20
+ __export(incremental_exports, {
21
+ ResumeAborted: () => ResumeAborted,
22
+ active: () => active,
23
+ beginRun: () => beginRun,
24
+ bodyDone: () => bodyDone,
25
+ copyValue: () => copyValue,
26
+ emptyMemory: () => emptyMemory,
27
+ endRun: () => endRun,
28
+ nonIncremental: () => nonIncremental,
29
+ slotOf: () => slotOf,
30
+ suspendRun: () => suspendRun
31
+ });
32
+ module.exports = __toCommonJS(incremental_exports);
33
+ var import_series = require("../runtime/series.js");
34
+ var import_registry = require("../drawing/registry.js");
35
+ class ResumeAborted extends Error {
36
+ constructor(reason) {
37
+ super(`incremental run stopped: ${reason}`);
38
+ this.reason = reason;
39
+ }
40
+ reason;
41
+ }
42
+ function emptyMemory(n) {
43
+ return { n, series: [], kept: /* @__PURE__ */ new WeakMap(), ta: [], blocks: [], plots: [], boxOwner: [], bodyDrawings: [], drawingBlock: void 0 };
44
+ }
45
+ let active = null;
46
+ const SLOT = /* @__PURE__ */ Symbol("series slot");
47
+ function slotOf(series) {
48
+ return series[SLOT];
49
+ }
50
+ const hooks = {
51
+ created(series) {
52
+ const run = active;
53
+ if (!run || run.block >= 0) return;
54
+ series[SLOT] = run.seriesSlot++;
55
+ },
56
+ prefix(series, bars) {
57
+ const run = active;
58
+ if (!run || run.mode !== "resume" || bars !== run.bars || !run.prev) return null;
59
+ const slot = slotOf(series);
60
+ const values = slot !== void 0 ? run.prev.series[slot] : run.prev.kept.get(series);
61
+ return values && values.length >= run.from ? { values, from: run.from } : null;
62
+ },
63
+ computed(series, values) {
64
+ const run = active;
65
+ if (!run || run.block >= 0) return;
66
+ const slot = slotOf(series);
67
+ if (slot !== void 0) run.next.series[slot] = values;
68
+ else run.next.kept.set(series, values);
69
+ }
70
+ };
71
+ const drawingHooks = {
72
+ access() {
73
+ const run = active;
74
+ if (!run) return;
75
+ if (run.block < 0) {
76
+ if (run.blockSlot > 0) nonIncremental("drawings in the script body after an eachBar() block");
77
+ return;
78
+ }
79
+ if (run.next.drawingBlock === void 0) run.next.drawingBlock = run.block;
80
+ else if (run.next.drawingBlock !== run.block) nonIncremental("drawings in two eachBar() blocks");
81
+ },
82
+ reuse(kind, obj) {
83
+ const run = active;
84
+ if (!run || run.block >= 0) return null;
85
+ const slot = run.drawSlot++;
86
+ if (run.mode === "record") {
87
+ run.next.bodyDrawings[slot] = { kind, obj };
88
+ return null;
89
+ }
90
+ const old = run.prev?.bodyDrawings[slot];
91
+ if (!old || old.kind !== kind) nonIncremental("a new drawing in the script body");
92
+ run.next.bodyDrawings[slot] = old;
93
+ return old.obj;
94
+ }
95
+ };
96
+ function beginRun(run) {
97
+ const outer = active;
98
+ active = run;
99
+ (0, import_series.setSeriesRunHooks)(hooks);
100
+ (0, import_registry.setDrawingRunHooks)(drawingHooks);
101
+ return outer;
102
+ }
103
+ function endRun(outer) {
104
+ active = outer;
105
+ (0, import_series.setSeriesRunHooks)(outer ? hooks : null);
106
+ (0, import_registry.setDrawingRunHooks)(outer ? drawingHooks : null);
107
+ }
108
+ function bodyDone() {
109
+ (0, import_registry.setDrawingRunHooks)(null);
110
+ }
111
+ function suspendRun() {
112
+ const outer = active;
113
+ active = null;
114
+ (0, import_series.setSeriesRunHooks)(null);
115
+ (0, import_registry.setDrawingRunHooks)(null);
116
+ return outer;
117
+ }
118
+ function copyValue(value) {
119
+ if ((0, import_registry.isDrawingObject)(value)) return value;
120
+ if (value === null || typeof value !== "object") {
121
+ if (typeof value === "function") nonIncremental("a function in persist()");
122
+ return value;
123
+ }
124
+ if (Array.isArray(value)) return value.map(copyValue);
125
+ if (value instanceof Map) return new Map([...value].map(([k, v]) => [copyValue(k), copyValue(v)]));
126
+ if (value instanceof Set) return new Set([...value].map(copyValue));
127
+ const proto = Object.getPrototypeOf(value);
128
+ if (proto !== Object.prototype && proto !== null) {
129
+ nonIncremental("a class instance in persist()");
130
+ return value;
131
+ }
132
+ const out = {};
133
+ for (const [k, v] of Object.entries(value)) out[k] = copyValue(v);
134
+ return out;
135
+ }
136
+ function nonIncremental(reason) {
137
+ const run = active;
138
+ if (!run) return;
139
+ if (run.mode === "resume") throw new ResumeAborted(reason);
140
+ run.blocked ??= reason;
141
+ }
142
+ //# sourceMappingURL=incremental.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/script/incremental.ts"],
4
+ "sourcesContent": ["/**\n * State of an incremental script run (#196, createScriptRun).\n *\n * A run executes the script body again on each update, but what the body creates (Series, ta.* calls, eachBar()\n * blocks, plots, persist() boxes) is matched by creation order with the previous run, and keeps the values of the bars\n * before the first bar to calculate (`from`, the forming bar of the previous run). Only the bars from `from` on are\n * calculated:\n * - a Series: its values before `from` are those of the previous run, the others are computed\n * - a ta.* call with a {@link taStep} machine: the machine holds the state of the last confirmed bar and steps the newest\n * bars only\n * - an eachBar() block: its callback runs from `from` on, its persist() boxes and history buffers put back to their\n * state at the end of the last confirmed bar\n * - a plot: the points of the bars before `from` are kept\n *\n * A feature that is not resumed (drawings, other symbols, strategies, varip, values that depend on the bar count)\n * calls {@link nonIncremental}: a recorded (full) run marks the run so that the next updates are full runs, a resumed\n * run stops ({@link ResumeAborted}) and the host runs it in full.\n *\n * @internal\n */\nimport { Series, setSeriesRunHooks, type SeriesRunHooks } from '../runtime/series.js';\nimport type { Bar } from '../types/index.js';\nimport type { BarStep } from '../ta/step.js';\nimport { isDrawingObject, setDrawingRunHooks, type DrawingCheckpoint, type DrawingKind, type DrawingRunHooks } from '../drawing/registry.js';\n\n/** A resumed run met a feature it does not resume: the update is calculated by a full run. */\nexport class ResumeAborted extends Error {\n constructor(readonly reason: string) {\n super(`incremental run stopped: ${reason}`);\n }\n}\n\n/** History buffer state of one source of an eachBar() block (RE10008). */\nexport interface BufferState {\n size: number;\n previous: number;\n barMax: number;\n}\n\n/** State of an eachBar() block at the end of the last confirmed bar. */\nexport interface BlockCheckpoint {\n /** Values of the persist() boxes the block owns, by box slot */\n boxes: Map<number, unknown>;\n /** History buffers by source key (Series slot, or the Series of the chart) */\n buffers: Map<unknown, BufferState>;\n pendingError: { bar: number; barsBack: number } | null;\n /** The drawing registry and its objects (the block that draws puts it back) */\n drawings: DrawingCheckpoint | null;\n /** State the block keeps besides the script's (the strategy engine of strategy.eachBar()) */\n extra?: unknown;\n}\n\nexport interface BlockMemory {\n out: number[];\n checkpoint: BlockCheckpoint;\n}\n\n/** A ta.* call with a machine: its machines (state of the last confirmed bar) and output values. */\nexport interface TaMemory {\n kind: string;\n machine: BarStep<unknown[], unknown>;\n outs: number[][];\n}\n\n/** A plot's points and, for each bar j, the number of points before it. */\nexport interface PlotMemory {\n data: Array<{ time: number; value: number; color?: string }>;\n pos: number[];\n /** Offset and colour kind of the plot: the points are kept only for the same */\n sig: string;\n}\n\n/** What a run keeps for the next update. */\nexport interface RunMemory {\n /** Bars of that run */\n n: number;\n /** Values of the Series the script created, by slot */\n series: Array<number[] | undefined>;\n /** Values of the Series that live across runs (the chart's open, close, bar_index...) */\n kept: WeakMap<Series, number[]>;\n ta: Array<TaMemory | undefined>;\n blocks: Array<BlockMemory | undefined>;\n plots: Array<PlotMemory | undefined>;\n /** Block slot that owns each persist() box */\n boxOwner: Array<number | undefined>;\n /** Drawing objects created in the script body (outside the callbacks), by creation order */\n bodyDrawings: Array<{ kind: DrawingKind; obj: object } | undefined>;\n /** The eachBar() block that uses the drawings (one per script), undefined when none */\n drawingBlock: number | undefined;\n}\n\nexport function emptyMemory(n: number): RunMemory {\n return { n, series: [], kept: new WeakMap(), ta: [], blocks: [], plots: [], boxOwner: [], bodyDrawings: [], drawingBlock: undefined };\n}\n\nexport interface ActiveRun {\n mode: 'record' | 'resume';\n /** First bar calculated (0 when recording) */\n from: number;\n bars: readonly Bar[];\n prev: RunMemory | null;\n next: RunMemory;\n seriesSlot: number;\n taSlot: number;\n blockSlot: number;\n plotSlot: number;\n boxSlot: number;\n drawSlot: number;\n /** The eachBar() block running (its slot), -1 outside */\n block: number;\n /** Getters and setters of the persist() boxes of this run, by slot */\n boxes: Array<{ get(): unknown; set(v: unknown): void }>;\n /** Why the next updates are full runs (null: they can be resumed) */\n blocked: string | null;\n}\n\n/** The incremental run being executed (null outside createScriptRun or in a plain executeScript). */\nexport let active: ActiveRun | null = null;\n\nconst SLOT = Symbol('series slot');\n\n/** Slot of a Series created by the script in this run, or undefined (a chart Series, or one made inside eachBar) */\nexport function slotOf(series: Series): number | undefined {\n return (series as unknown as Record<symbol, number | undefined>)[SLOT];\n}\n\nconst hooks: SeriesRunHooks = {\n created(series) {\n const run = active;\n // A Series made in an eachBar() callback is made again on each bar: it is not matched with the previous run.\n if (!run || run.block >= 0) return;\n (series as unknown as Record<symbol, number>)[SLOT] = run.seriesSlot++;\n },\n prefix(series, bars) {\n const run = active;\n if (!run || run.mode !== 'resume' || bars !== run.bars || !run.prev) return null;\n const slot = slotOf(series);\n const values = slot !== undefined ? run.prev.series[slot] : run.prev.kept.get(series);\n return values && values.length >= run.from ? { values, from: run.from } : null;\n },\n computed(series, values) {\n const run = active;\n if (!run || run.block >= 0) return;\n const slot = slotOf(series);\n if (slot !== undefined) run.next.series[slot] = values;\n else run.next.kept.set(series, values);\n },\n};\n\n/**\n * Drawings in an incremental run: one eachBar() block uses them (its checkpoint holds the registry and the state of\n * the objects); the objects created in the script body before the blocks are the same objects on each run (matched by\n * creation order), set again to their new state. Drawings used by two blocks, or in the body after a block, are not\n * resumed.\n */\nconst drawingHooks: DrawingRunHooks = {\n access() {\n const run = active;\n if (!run) return;\n if (run.block < 0) {\n if (run.blockSlot > 0) nonIncremental('drawings in the script body after an eachBar() block');\n return;\n }\n if (run.next.drawingBlock === undefined) run.next.drawingBlock = run.block;\n else if (run.next.drawingBlock !== run.block) nonIncremental('drawings in two eachBar() blocks');\n },\n reuse(kind, obj) {\n const run = active;\n if (!run || run.block >= 0) return null;\n const slot = run.drawSlot++;\n if (run.mode === 'record') {\n run.next.bodyDrawings[slot] = { kind, obj };\n return null;\n }\n const old = run.prev?.bodyDrawings[slot];\n if (!old || old.kind !== kind) nonIncremental('a new drawing in the script body');\n run.next.bodyDrawings[slot] = old;\n return old!.obj;\n },\n};\n\n/** Starts an incremental run (the script body runs next); returns the run to give to {@link endRun}. */\nexport function beginRun(run: ActiveRun): ActiveRun | null {\n const outer = active;\n active = run;\n setSeriesRunHooks(hooks);\n setDrawingRunHooks(drawingHooks);\n return outer;\n}\n\n/** Ends the run started by {@link beginRun}, putting back the outer one (a nested run) or none. */\nexport function endRun(outer: ActiveRun | null): void {\n active = outer;\n setSeriesRunHooks(outer ? hooks : null);\n setDrawingRunHooks(outer ? drawingHooks : null);\n}\n\n/** The script body has run: the run builds its outputs (they read the drawings without being part of the script). */\nexport function bodyDone(): void {\n setDrawingRunHooks(null);\n}\n\n/** No incremental run inside (a nested executeScript, e.g. other symbols): the outer one is put back by endRun(). */\nexport function suspendRun(): ActiveRun | null {\n const outer = active;\n active = null;\n setSeriesRunHooks(null);\n setDrawingRunHooks(null);\n return outer;\n}\n\n/**\n * A copy of a persist() value for a checkpoint: the forming bar may change an array or an object in place after it was\n * taken. Plain data is copied (structuredClone); a value that cannot be copied (a function, a class instance such as\n * a drawing) is kept as it is and marks the run as not resumable.\n */\nexport function copyValue(value: unknown): unknown {\n // a drawing object is shared: the checkpoint of the registry puts its state back\n if (isDrawingObject(value)) return value;\n if (value === null || typeof value !== 'object') {\n if (typeof value === 'function') nonIncremental('a function in persist()');\n return value;\n }\n if (Array.isArray(value)) return value.map(copyValue);\n if (value instanceof Map) return new Map([...value].map(([k, v]) => [copyValue(k), copyValue(v)]));\n if (value instanceof Set) return new Set([...value].map(copyValue));\n const proto = Object.getPrototypeOf(value);\n if (proto !== Object.prototype && proto !== null) {\n nonIncremental('a class instance in persist()');\n return value;\n }\n const out: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(value)) out[k] = copyValue(v);\n return out;\n}\n\n/**\n * A feature an incremental run does not resume: a recorded run keeps its result (it is a full run) and marks the next\n * updates as full runs; a resumed run stops.\n */\nexport function nonIncremental(reason: string): void {\n const run = active;\n if (!run) return;\n if (run.mode === 'resume') throw new ResumeAborted(reason);\n run.blocked ??= reason;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAoBA,oBAA+D;AAG/D,sBAAoH;AAG7G,MAAM,sBAAsB,MAAM;AAAA,EACvC,YAAqB,QAAgB;AACnC,UAAM,4BAA4B,MAAM,EAAE;AADvB;AAAA,EAErB;AAAA,EAFqB;AAGvB;AA6DO,SAAS,YAAY,GAAsB;AAChD,SAAO,EAAE,GAAG,QAAQ,CAAC,GAAG,MAAM,oBA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AAC;AACjG,MAAI,iBAAiB,IAAK,QAAO,IAAI,IAAI,CAAC,GAAG,KAAK,EAAE,IAAI,SAAS,CAAC;AAClE,QAAM,QAAQ,OAAO,eAAe,KAAK;AACzC,MAAI,UAAU,OAAO,aAAa,UAAU,MAAM;AAChD,mBAAe,+BAA+B;AAC9C,WAAO;AAAA,EACT;AACA,QAAM,MAA+B,CAAC;AACtC,aAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,KAAK,EAAG,KAAI,CAAC,IAAI,UAAU,CAAC;AAChE,SAAO;AACT;AAMO,SAAS,eAAe,QAAsB;AACnD,QAAM,MAAM;AACZ,MAAI,CAAC,IAAK;AACV,MAAI,IAAI,SAAS,SAAU,OAAM,IAAI,cAAc,MAAM;AACzD,MAAI,YAAY;AAClB;",
6
+ "names": []
7
+ }
@@ -38,7 +38,6 @@ import type { ArrowConfig, BarColorConfig, CandleConfig, FillConfig, HLineConfig
38
38
  import { BarData, Series } from '../runtime/series.js';
39
39
  import * as taSeries from '../ta-series.js';
40
40
  import * as colorCore from '../color/index.js';
41
- import { isNA, nz } from '../utils/index.js';
42
41
  import * as lineCore from '../line/index.js';
43
42
  import * as labelCore from '../label/index.js';
44
43
  import * as boxCore from '../box/index.js';
@@ -47,8 +46,14 @@ import * as polylineCore from '../polyline/index.js';
47
46
  import * as tableCore from '../table/index.js';
48
47
  import * as chartPointCore from '../chartpoint/index.js';
49
48
  import * as mathCore from '../math/index.js';
50
- import { type DrawingKind } from '../drawing/registry.js';
51
- export { holdDrawing, releaseDrawing, varDrawing, type DrawingVariable } from '../drawing/registry.js';
49
+ import { type DrawingKind, type DrawingVariable } from '../drawing/registry.js';
50
+ export { holdDrawing, releaseDrawing, type DrawingVariable } from '../drawing/registry.js';
51
+ /**
52
+ * A PineScript `var` variable of a drawing type (`var line l = na`): the object it refers to is not deleted by the
53
+ * maximum count; assigning another object (or `undefined` for `na`) releases the one before. Its value is a
54
+ * {@link persist} box: an incremental run puts it back with the drawings (#196). Declare it in the script body.
55
+ */
56
+ export declare function varDrawing<T extends object>(initial?: T): DrawingVariable<T>;
52
57
  import * as timeframeCore from '../timeframe/index.js';
53
58
  import * as strCore from '../str/index.js';
54
59
  import { type SessionSpec } from '../session/calendar.js';
@@ -196,6 +201,45 @@ export interface IntrabarOptions {
196
201
  * drawings) are dropped. Re-entrant: the host calls it inside the bar loop of the main run.
197
202
  */
198
203
  export declare function executeIntrabar(body: () => void, bars: Bar[], inputs: Record<string, unknown>, chart: ChartContext, options: IntrabarOptions): void;
204
+ /** The result of {@link ScriptRun.update}. */
205
+ export interface ScriptRunUpdate extends ScriptRunResult {
206
+ /** First bar calculated: the forming bar of the previous update (a resumed run), or 0 (a full run) */
207
+ from: number;
208
+ /** The update was resumed from the state of the last confirmed bar */
209
+ incremental: boolean;
210
+ }
211
+ export interface ScriptRunOptions extends ScriptOptions {
212
+ /**
213
+ * Resumed updates also calculated in full and compared, the first ones of the run (default 2): a difference makes
214
+ * the next updates full runs (a script that keeps state outside persist() boxes)
215
+ */
216
+ verify?: number;
217
+ }
218
+ /** A script calculated again on each update of the chart bars (see {@link createScriptRun}). */
219
+ export interface ScriptRun {
220
+ /**
221
+ * The script on `bars`: resumed from the forming bar of the previous update when the bars before it did not change
222
+ * (a live update of the forming bar, or new bars after it), else a full run.
223
+ */
224
+ update(bars: Bar[], chart?: ChartContext): ScriptRunUpdate;
225
+ /** Why the updates are full runs: a feature that is not resumed, or a verification difference (null: resumed) */
226
+ readonly fullRunReason: string | null;
227
+ }
228
+ /**
229
+ * A script that calculates only the newest bars on a live update (#196). The first update is a full run; it keeps,
230
+ * for the next one, what the script created (its Series, ta.* calls, eachBar() blocks, persist() boxes and plots) with
231
+ * their state at the end of the last confirmed bar. An update whose bars are the same up to the forming bar of the
232
+ * previous update (a new price of the forming bar, or new bars after it) runs the script again from that bar only;
233
+ * the result is the same as a full run on the same bars. Other changes (history loaded, inputs, symbol, timeframe or
234
+ * other chart fields) run it in full.
235
+ *
236
+ * The state the script keeps from one bar to the next has to be in `persist()` boxes (not in `let` variables outside the
237
+ * eachBar() callbacks, which a resumed run cannot put back). The first resumed updates are also calculated in full and
238
+ * compared (`verify`): a difference makes the next updates full runs. Not resumed yet, so calculated in full: drawings,
239
+ * request.security, strategies, varip, timenow, last_bar_index, BarContext.n, barstate.islastconfirmedhistory
240
+ * (`fullRunReason` names the first one met).
241
+ */
242
+ export declare function createScriptRun(body: () => void, inputs?: Record<string, unknown>, chart?: ChartContext, options?: ScriptRunOptions): ScriptRun;
199
243
  /** A `varip` variable: `.value` reads and writes it. */
200
244
  export interface VaripVariable<T> {
201
245
  value: T;
@@ -207,6 +251,31 @@ export interface VaripVariable<T> {
207
251
  * same order on each run; read and write it in strategy.eachBar(). Without intrabar runs it is a plain variable.
208
252
  */
209
253
  export declare function varip<T>(initial: T): VaripVariable<T>;
254
+ /** A `persist` variable: `.value` reads and writes it. */
255
+ export interface PersistVariable<T> {
256
+ value: T;
257
+ }
258
+ /**
259
+ * PineScript `var` kept by the runtime (#196): a variable that keeps its value from one bar to the next. Declare it in
260
+ * the script body (outside the callbacks), in the same order on each run, and read and write it in one eachBar()
261
+ * callback. In a plain run it is a plain variable. In an incremental run (createScriptRun), the runtime keeps its value
262
+ * at the end of the last confirmed bar and puts it back before the forming bar is calculated again: use it instead of
263
+ * a `let` variable outside the callback, which a resumed run cannot put back.
264
+ *
265
+ * The run is calculated in full (not resumed) when the variable is read or written outside an eachBar() callback, by
266
+ * two eachBar() blocks, or holds something else than plain data (numbers, strings, booleans, arrays, plain objects,
267
+ * Map, Set).
268
+ *
269
+ * ```typescript
270
+ * const dir = persist(0); // var dir = 0
271
+ * const trend = eachBar((c) => {
272
+ * if (c.close > c.get(close, 1)) dir.value = 1;
273
+ * else if (c.close < c.get(close, 1)) dir.value = -1;
274
+ * return dir.value;
275
+ * });
276
+ * ```
277
+ */
278
+ export declare function persist<T>(initial: T): PersistVariable<T>;
210
279
  export interface IndicatorOptions {
211
280
  shorttitle?: string;
212
281
  /** true: drawn on the price pane; false (default, as PineScript): in its own pane. */
@@ -344,38 +413,38 @@ export declare const ta: {
344
413
  iii: Series;
345
414
  wad: Series;
346
415
  wvad: Series;
347
- sma(source: Series, length: number | Series): Series;
348
- ema(source: Series, length: number): Series;
349
- wma(source: Series, length: number | Series): Series;
350
- rma(source: Series, length: number): Series;
351
- rsi(source: Series, length: number | Series): Series;
352
- macd(source: Series, fastLength: number, slowLength: number, signalLength: number): [Series, Series, Series];
353
- bb(source: Series, length: number, mult: number): [Series, Series, Series];
354
- stdev(source: Series, length: number | Series, biased?: boolean): Series;
355
- stoch(source: Series, high: Series, low: Series, length: number): Series;
356
- crossover(source1: Series, source2: Series | number): Series;
357
- crossunder(source1: Series, source2: Series | number): Series;
358
- cross(source1: Series, source2: Series | number): Series;
359
- change(source: Series, length?: number): Series;
360
- mom(source: Series, length: number): Series;
361
- roc(source: Series, length: number): Series;
416
+ sma: (source: Series, length: number | Series) => Series;
417
+ ema: (source: Series, length: number) => Series;
418
+ rma: (source: Series, length: number) => Series;
419
+ wma: (source: Series, length: number | Series) => Series;
420
+ rsi: (source: Series, length: number | Series) => Series;
421
+ change: (source: Series, length?: number) => Series;
422
+ mom: (source: Series, length: number) => Series;
423
+ roc: (source: Series, length: number) => Series;
424
+ cum: (source: Series) => Series;
425
+ stdev: (source: Series, length: number | Series, biased?: boolean) => Series;
426
+ variance: (source: Series, length: number | Series, biased?: boolean) => Series;
427
+ crossover: (a: Series, b: Series | number) => Series;
428
+ crossunder: (a: Series, b: Series | number) => Series;
429
+ cross: (a: Series, b: Series | number) => Series;
430
+ barssince: (condition: Series) => Series;
431
+ valuewhen: (condition: Series, source: Series, occurrence: number) => Series;
432
+ macd: (source: Series, fastLength: number, slowLength: number, signalLength: number) => [Series, Series, Series];
433
+ bb: (source: Series, length: number, mult: number) => [Series, Series, Series];
434
+ hma: (source: Series, length: number) => Series;
435
+ dev: (source: Series, length: number) => Series;
436
+ cci: (source: Series, length: number) => Series;
437
+ percentrank: (source: Series, length: number) => Series;
438
+ median: (source: Series, length: number) => Series;
439
+ linreg: (source: Series, length: number, offset?: number) => Series;
440
+ correlation: (a: Series, b: Series, length: number) => Series;
441
+ stoch: (source: Series, highSource: Series, lowSource: Series, length: number) => Series;
362
442
  rising(source: Series, length: number): Series;
363
443
  falling(source: Series, length: number): Series;
364
- cum(source: Series): Series;
365
444
  vwapBands(source: Series, volume: Series, anchor: Series | undefined, stdev_mult: number): [Series, Series, Series];
366
- linreg(source: Series, length: number, offset?: number): Series;
367
445
  alma(source: Series, length?: number, offset?: number, sigma?: number, floor?: boolean): Series;
368
446
  zigzag(bars: Bar[], deviation?: number, depth?: number, backstep?: number): [Series, Series, Series];
369
- cci(source: Series, length: number): Series;
370
- dev(source: Series, length: number): Series;
371
- variance(source: Series, length: number | Series, biased?: boolean): Series;
372
- median(source: Series, length: number): Series;
373
447
  swma(source: Series): Series;
374
- correlation(source1: Series, source2: Series, length: number): Series;
375
- percentrank(source: Series, length: number): Series;
376
- hma(source: Series, length: number): Series;
377
- barssince(condition: Series): Series;
378
- valuewhen(condition: Series, source: Series, occurrence: number): Series;
379
448
  tsi(source: Series, shortLength: number, longLength: number): Series;
380
449
  cmo(source: Series, length: number): Series;
381
450
  bbw(source: Series, length: number, mult: number): Series;
@@ -463,14 +532,31 @@ export declare const ta: {
463
532
  * Accepts a Series or a per-bar color array (`undefined` = `na`, as returned by `color.when`). */
464
533
  export declare function fixnan(source: Series): Series;
465
534
  export declare function fixnan(source: Array<string | undefined>): Array<string | undefined>;
535
+ /** PineScript `na(x)`. On a Series: a Series of 1 / 0, 1 where the value is na (NaN or +/-Infinity, #187). On a
536
+ * single value (a number of a bar loop, a colour, a drawing): true when it is na. */
537
+ export declare function na(x: Series): Series;
538
+ export declare function na(x: unknown): boolean;
539
+ /** PineScript `nz(source, replacement)`; replacement defaults to 0. On a Series: each na value (NaN or +/-Infinity)
540
+ * becomes the replacement of the same bar, a number or a Series (#187). On single values: the value or the
541
+ * replacement. */
542
+ export declare function nz(source: Series, replacement?: number | Series): Series;
543
+ export declare function nz<T>(source: T, replacement?: T): T;
466
544
  /** Per-bar color selection: truthy → colorTrue, else colorFalse.
467
545
  * colorFalse may be omitted (PineScript `na`): those bars get no color,
468
546
  * which bgcolor()/barcolor() skip. */
469
547
  declare function colorWhen(cond: Series, colorTrue: string, colorFalse: string): string[];
470
548
  declare function colorWhen(cond: Series, colorTrue: string, colorFalse?: string): Array<string | undefined>;
549
+ type GradientColor = string | null | undefined;
550
+ /** PineScript `color.from_gradient(value, bottom_value, top_value, bottom_color, top_color)`. With numbers and
551
+ * colours: one colour. When an argument is a Series (or a colour is a per-bar array, as from `color.when`): the
552
+ * colour of each bar, the rule of the number form applied to the values of that bar (#187), for
553
+ * plot()/fill()/bgcolor()/barcolor(). */
554
+ declare function fromGradient(value: number, bottom_value: number, top_value: number, bottom_color: GradientColor, top_color: GradientColor): string;
555
+ declare function fromGradient(value: number | Series, bottom_value: number | Series, top_value: number | Series, bottom_color: GradientColor | readonly GradientColor[], top_color: GradientColor | readonly GradientColor[]): string | string[];
471
556
  /** color.* plus `when` — per-bar conditional colors for plot()/fill()/bgcolor()/barcolor(). */
472
557
  export declare const color: {
473
558
  new: typeof colorCore.new_color;
559
+ from_gradient: typeof fromGradient;
474
560
  when: typeof colorWhen;
475
561
  rgb(red: import("../index.js").simple_int, green: import("../index.js").simple_int, blue: import("../index.js").simple_int, transp?: import("../index.js").simple_float): string;
476
562
  from_hex(hex: string, transp?: import("../index.js").simple_float): string;
@@ -479,7 +565,6 @@ export declare const color: {
479
565
  g(clr: import("../types/index.js").color): import("../index.js").int;
480
566
  b(clr: import("../types/index.js").color): import("../index.js").int;
481
567
  t(clr: import("../types/index.js").color | null | undefined): import("../index.js").float;
482
- from_gradient(value: import("../index.js").float, bottom_value: import("../index.js").float, top_value: import("../index.js").float, bottom_color: import("../types/index.js").color, top_color: import("../types/index.js").color): string;
483
568
  aqua: "#00BCD4";
484
569
  black: "#363A45";
485
570
  blue: "#2962FF";
@@ -1563,7 +1648,7 @@ export declare const alert: typeof alertImpl & {
1563
1648
  readonly freq_once_per_bar: "once_per_bar";
1564
1649
  readonly freq_once_per_bar_close: "once_per_bar_close";
1565
1650
  };
1566
- export { Series, BarData, isNA as na, nz };
1651
+ export { Series, BarData };
1567
1652
  export { STRATEGY_DEFAULTS } from '../strategy/index.js';
1568
1653
  export type { StrategyCloseAllOptions, StrategyCloseOptions, StrategyCommissionType, StrategyDirection, StrategyEngine, StrategyEntryOptions, StrategyExitOptions, StrategyNumberVariable, StrategyOcaType, StrategyOptions, StrategyProperties, StrategyQtyType, StrategyRiskDirection, StrategyRiskRule, StrategyRiskValueType, StrategyTextVariable, StrategyTrade, StrategyVariable, } from '../strategy/index.js';
1569
1654
  export declare const math: {