oakscriptjs 0.15.3 → 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 (144) 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 +29 -0
  20. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  21. package/dist/cjs/drawing/registry.js +83 -2
  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/str/numberformat.d.ts.map +1 -1
  47. package/dist/cjs/str/numberformat.js +23 -0
  48. package/dist/cjs/str/numberformat.js.map +2 -2
  49. package/dist/cjs/strategy/index.d.ts +8 -0
  50. package/dist/cjs/strategy/index.d.ts.map +1 -1
  51. package/dist/cjs/strategy/index.js.map +1 -1
  52. package/dist/cjs/ta/running-sum.d.ts.map +1 -1
  53. package/dist/cjs/ta/running-sum.js +72 -3
  54. package/dist/cjs/ta/running-sum.js.map +2 -2
  55. package/dist/cjs/ta/step.d.ts +476 -0
  56. package/dist/cjs/ta/step.d.ts.map +1 -0
  57. package/dist/cjs/ta/step.js +1000 -0
  58. package/dist/cjs/ta/step.js.map +7 -0
  59. package/dist/cjs/ta-series.d.ts +1 -1
  60. package/dist/cjs/ta-series.js +3 -3
  61. package/dist/cjs/ta-series.js.map +1 -1
  62. package/dist/esm/check/format.d.ts.map +1 -1
  63. package/dist/esm/check/format.js +5 -0
  64. package/dist/esm/check/format.js.map +1 -1
  65. package/dist/esm/check/harness.d.ts +24 -0
  66. package/dist/esm/check/harness.d.ts.map +1 -1
  67. package/dist/esm/check/harness.js +40 -6
  68. package/dist/esm/check/harness.js.map +1 -1
  69. package/dist/esm/check/index.d.ts +9 -0
  70. package/dist/esm/check/index.d.ts.map +1 -1
  71. package/dist/esm/check/index.js +11 -1
  72. package/dist/esm/check/index.js.map +1 -1
  73. package/dist/esm/color/index.d.ts.map +1 -1
  74. package/dist/esm/color/index.js +23 -3
  75. package/dist/esm/color/index.js.map +1 -1
  76. package/dist/esm/color/parse.d.ts +8 -6
  77. package/dist/esm/color/parse.d.ts.map +1 -1
  78. package/dist/esm/color/parse.js +19 -2
  79. package/dist/esm/color/parse.js.map +1 -1
  80. package/dist/esm/drawing/registry.d.ts +29 -0
  81. package/dist/esm/drawing/registry.d.ts.map +1 -1
  82. package/dist/esm/drawing/registry.js +102 -3
  83. package/dist/esm/drawing/registry.js.map +1 -1
  84. package/dist/esm/index.d.ts +2 -1
  85. package/dist/esm/index.d.ts.map +1 -1
  86. package/dist/esm/index.js +3 -1
  87. package/dist/esm/index.js.map +1 -1
  88. package/dist/esm/runtime/series.d.ts +16 -0
  89. package/dist/esm/runtime/series.d.ts.map +1 -1
  90. package/dist/esm/runtime/series.js +21 -1
  91. package/dist/esm/runtime/series.js.map +1 -1
  92. package/dist/esm/script/closure-writes.d.ts +16 -0
  93. package/dist/esm/script/closure-writes.d.ts.map +1 -0
  94. package/dist/esm/script/closure-writes.js +323 -0
  95. package/dist/esm/script/closure-writes.js.map +1 -0
  96. package/dist/esm/script/globals.d.ts +2 -0
  97. package/dist/esm/script/globals.d.ts.map +1 -1
  98. package/dist/esm/script/globals.js.map +1 -1
  99. package/dist/esm/script/incremental.d.ts +140 -0
  100. package/dist/esm/script/incremental.d.ts.map +1 -0
  101. package/dist/esm/script/incremental.js +173 -0
  102. package/dist/esm/script/incremental.js.map +1 -0
  103. package/dist/esm/script/index.d.ts +116 -31
  104. package/dist/esm/script/index.d.ts.map +1 -1
  105. package/dist/esm/script/index.js +561 -24
  106. package/dist/esm/script/index.js.map +1 -1
  107. package/dist/esm/str/numberformat.d.ts.map +1 -1
  108. package/dist/esm/str/numberformat.js +30 -0
  109. package/dist/esm/str/numberformat.js.map +1 -1
  110. package/dist/esm/strategy/index.d.ts +8 -0
  111. package/dist/esm/strategy/index.d.ts.map +1 -1
  112. package/dist/esm/ta/running-sum.d.ts.map +1 -1
  113. package/dist/esm/ta/running-sum.js +79 -4
  114. package/dist/esm/ta/running-sum.js.map +1 -1
  115. package/dist/esm/ta/step.d.ts +476 -0
  116. package/dist/esm/ta/step.d.ts.map +1 -0
  117. package/dist/esm/ta/step.js +1089 -0
  118. package/dist/esm/ta/step.js.map +1 -0
  119. package/dist/esm/ta-series.d.ts +1 -1
  120. package/dist/esm/ta-series.js +4 -4
  121. package/package.json +1 -1
  122. package/skills/oakscript/SKILL.md +2 -2
  123. package/skills/oakscript/reference/check.md +5 -0
  124. package/skills/oakscript/reference/errors.md +0 -1
  125. package/skills/oakscript/reference/pine-differences.md +1 -2
  126. package/skills/oakscript/reference/plotting.md +5 -7
  127. package/skills/oakscript/reference/script-structure.md +6 -0
  128. package/src/check/format.ts +7 -0
  129. package/src/check/harness.ts +57 -0
  130. package/src/check/index.ts +16 -0
  131. package/src/color/index.ts +22 -3
  132. package/src/color/parse.ts +28 -3
  133. package/src/drawing/registry.ts +115 -3
  134. package/src/index.ts +4 -1
  135. package/src/runtime/series.ts +33 -1
  136. package/src/script/closure-writes.ts +279 -0
  137. package/src/script/globals.ts +2 -0
  138. package/src/script/incremental.ts +246 -0
  139. package/src/script/index.ts +695 -26
  140. package/src/str/numberformat.ts +29 -0
  141. package/src/strategy/index.ts +8 -0
  142. package/src/ta/running-sum.ts +78 -3
  143. package/src/ta/step.ts +1145 -0
  144. package/src/ta-series.ts +4 -4
@@ -48,6 +48,7 @@ __export(script_exports, {
48
48
  close: () => close,
49
49
  color: () => color,
50
50
  compare: () => compare,
51
+ createScriptRun: () => createScriptRun,
51
52
  currency: () => currency,
52
53
  dayofmonth: () => dayofmonth,
53
54
  dayofweek: () => dayofweek,
@@ -80,11 +81,12 @@ __export(script_exports, {
80
81
  math: () => math,
81
82
  minute: () => minute,
82
83
  month: () => month,
83
- na: () => import_utils.isNA,
84
- nz: () => import_utils.nz,
84
+ na: () => na,
85
+ nz: () => nz,
85
86
  ohlc4: () => ohlc4,
86
87
  open: () => open,
87
88
  order: () => order,
89
+ persist: () => persist,
88
90
  plot: () => plot,
89
91
  plotarrow: () => plotarrow,
90
92
  plotbar: () => plotbar,
@@ -114,7 +116,7 @@ __export(script_exports, {
114
116
  timeframe: () => timeframe,
115
117
  timenow: () => timenow,
116
118
  timestamp: () => timestamp,
117
- varDrawing: () => import_registry2.varDrawing,
119
+ varDrawing: () => varDrawing,
118
120
  varip: () => varip,
119
121
  volume: () => volume,
120
122
  weekofyear: () => weekofyear,
@@ -125,6 +127,9 @@ __export(script_exports, {
125
127
  module.exports = __toCommonJS(script_exports);
126
128
  var import_series = require("../runtime/series.js");
127
129
  var taSeries = __toESM(require("../ta-series.js"), 1);
130
+ var import_step = require("../ta/step.js");
131
+ var incremental = __toESM(require("./incremental.js"), 1);
132
+ var import_closure_writes = require("./closure-writes.js");
128
133
  var pineConstants = __toESM(require("./constants.js"), 1);
129
134
  var import_drawing_args = require("./drawing-args.js");
130
135
  var colorCore = __toESM(require("../color/index.js"), 1);
@@ -158,6 +163,20 @@ var import_context = require("../session/context.js");
158
163
  var import_strategy2 = require("../strategy/index.js");
159
164
  var compare = __toESM(require("../compare/index.js"), 1);
160
165
  var import_input_active = require("../runtime/input-active.js");
166
+ function varDrawing(initial) {
167
+ const box2 = persist(initial ? (0, import_registry.holdDrawing)(initial) : void 0);
168
+ return {
169
+ get value() {
170
+ return box2.value;
171
+ },
172
+ set value(next) {
173
+ const current = box2.value;
174
+ if (next === current) return;
175
+ (0, import_registry.releaseDrawing)(current);
176
+ box2.value = next ? (0, import_registry.holdDrawing)(next) : void 0;
177
+ }
178
+ };
179
+ }
161
180
  const ctxBars = new import_series.BarData([]);
162
181
  let ctx = null;
163
182
  function collector() {
@@ -248,7 +267,11 @@ function restoreRunState(st) {
248
267
  mathCore.restoreRandom(st.random);
249
268
  }
250
269
  function executeScript(body, barsInput, inputs = {}, chart2 = {}, options = {}) {
270
+ return runBody(body, barsInput, inputs, chart2, options, null);
271
+ }
272
+ function runBody(body, barsInput, inputs, chart2, options, inc) {
251
273
  const outer = ctx ? saveRunState() : null;
274
+ const outerInc = inc ? incremental.beginRun(inc) : incremental.suspendRun();
252
275
  ctxBars.setAll(barsInput);
253
276
  chartCtx = { ...chart2 };
254
277
  runNow = chart2.now ?? Date.now();
@@ -263,6 +286,7 @@ function executeScript(body, barsInput, inputs = {}, chart2 = {}, options = {})
263
286
  try {
264
287
  body();
265
288
  if (missingSymbols.length) throw new MissingSymbolDataError(missingSymbols);
289
+ if (inc) incremental.bodyDone();
266
290
  const c = ctx;
267
291
  return {
268
292
  metadata: {
@@ -306,6 +330,7 @@ function executeScript(body, barsInput, inputs = {}, chart2 = {}, options = {})
306
330
  } finally {
307
331
  ctx = null;
308
332
  if (outer) restoreRunState(outer);
333
+ incremental.endRun(outerInc);
309
334
  }
310
335
  }
311
336
  function executeIntrabar(body, bars2, inputs, chart2, options) {
@@ -328,9 +353,142 @@ function executeIntrabar(body, bars2, inputs, chart2, options) {
328
353
  { strategyEngine: () => options.engine }
329
354
  );
330
355
  }
356
+ const LAST_BAR_FIELDS = /* @__PURE__ */ new Set(["lastBarConfirmed", "lastBarNew", "realtime", "now"]);
357
+ function sameChart(a, b) {
358
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
359
+ for (const k of keys) {
360
+ if (LAST_BAR_FIELDS.has(k)) continue;
361
+ if (!Object.is(a[k], b[k])) return false;
362
+ }
363
+ return true;
364
+ }
365
+ function copyBars(b, copy = null, from = 0) {
366
+ let values = copy?.values ?? new Float64Array(Math.max(16, b.length) * 6);
367
+ if (values.length < b.length * 6) {
368
+ const grown = new Float64Array(b.length * 12);
369
+ grown.set(values.subarray(0, from * 6));
370
+ values = grown;
371
+ }
372
+ for (let i = from; i < b.length; i++) {
373
+ const x = b[i];
374
+ const o = i * 6;
375
+ values[o] = x.time;
376
+ values[o + 1] = x.open;
377
+ values[o + 2] = x.high;
378
+ values[o + 3] = x.low;
379
+ values[o + 4] = x.close;
380
+ values[o + 5] = x.volume ?? NaN;
381
+ }
382
+ return { n: b.length, values };
383
+ }
384
+ const sameNumber = (a, b) => a === b ? a !== 0 || 1 / a === 1 / b : a !== a && b !== b;
385
+ function sameBars(copy, b, count) {
386
+ const v = copy.values;
387
+ for (let i = 0; i < count; i++) {
388
+ const x = b[i];
389
+ const o = i * 6;
390
+ if (!sameNumber(v[o], x.time) || !sameNumber(v[o + 1], x.open) || !sameNumber(v[o + 2], x.high) || !sameNumber(v[o + 3], x.low) || !sameNumber(v[o + 4], x.close) || !sameNumber(v[o + 5], x.volume ?? NaN)) {
391
+ return false;
392
+ }
393
+ }
394
+ return true;
395
+ }
396
+ function sameResult(a, b) {
397
+ if (Object.is(a, b)) return true;
398
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
399
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
400
+ const ka = Object.keys(a);
401
+ const kb = Object.keys(b);
402
+ if (ka.length !== kb.length) return false;
403
+ for (const k of ka) {
404
+ if (!sameResult(a[k], b[k])) return false;
405
+ }
406
+ return true;
407
+ }
408
+ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
409
+ let memory = null;
410
+ let previous = null;
411
+ let previousChart = chart2;
412
+ let reason = null;
413
+ let verifyLeft = options.verify ?? 2;
414
+ const newRun = (mode, b, from) => {
415
+ const next = incremental.emptyMemory(b.length);
416
+ if (mode === "resume" && memory) next.drawingBlock = memory.drawingBlock;
417
+ return {
418
+ mode,
419
+ from,
420
+ bars: b,
421
+ prev: mode === "resume" ? memory : null,
422
+ next,
423
+ seriesSlot: 0,
424
+ taSlot: 0,
425
+ blockSlot: 0,
426
+ plotSlot: 0,
427
+ boxSlot: 0,
428
+ drawSlot: 0,
429
+ block: -1,
430
+ boxes: [],
431
+ blocked: null
432
+ };
433
+ };
434
+ const full = (b, c) => {
435
+ memory = null;
436
+ if (reason) return { ...runBody(body, b, inputs, c, options, null), from: 0, incremental: false };
437
+ const inc = newRun("record", b, 0);
438
+ const result = runBody(body, b, inputs, c, options, inc);
439
+ if (inc.blocked) reason = inc.blocked;
440
+ else memory = inc.next;
441
+ return { ...result, from: 0, incremental: false };
442
+ };
443
+ return {
444
+ get fullRunReason() {
445
+ return reason;
446
+ },
447
+ update(b, c = previousChart) {
448
+ const last = previous;
449
+ const m = memory;
450
+ const resumable = !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c) && sameBars(last, b, last.n - 1);
451
+ previousChart = c;
452
+ let out;
453
+ if (!resumable) {
454
+ out = full(b, c);
455
+ } else {
456
+ const from = last.n - 1;
457
+ const inc = newRun("resume", b, from);
458
+ let result = null;
459
+ try {
460
+ result = runBody(body, b, inputs, c, options, inc);
461
+ } catch (err) {
462
+ memory = null;
463
+ previous = null;
464
+ if (!(err instanceof incremental.ResumeAborted)) throw err;
465
+ reason = err.reason;
466
+ }
467
+ if (result && verifyLeft > 0) {
468
+ verifyLeft--;
469
+ const check = runBody(body, b, inputs, c, options, null);
470
+ if (!sameResult(result, check)) {
471
+ reason = "a resumed update differs from a full run (state kept outside persist()?)";
472
+ result = check;
473
+ }
474
+ }
475
+ if (result && !reason) {
476
+ memory = inc.next;
477
+ out = { ...result, from, incremental: true };
478
+ } else {
479
+ out = result && reason ? { ...result, from: 0, incremental: false } : full(b, c);
480
+ memory = null;
481
+ }
482
+ }
483
+ previous = out.incremental && last ? copyBars(b, last, last.n - 1) : copyBars(b);
484
+ return out;
485
+ }
486
+ };
487
+ }
331
488
  let varipRun = { slot: 0, recorder: null, mode: "live", scratch: null };
332
489
  function varip(initial) {
333
490
  collector();
491
+ incremental.nonIncremental("varip");
334
492
  const slot = varipRun.slot++;
335
493
  const state = varipRun;
336
494
  const rec = state.recorder;
@@ -356,6 +514,52 @@ function varip(initial) {
356
514
  }
357
515
  };
358
516
  }
517
+ function persist(initial) {
518
+ collector();
519
+ if (currentBar >= 0) {
520
+ throw new Error("persist() is declared in the script body, outside the eachBar() callback (once per run).");
521
+ }
522
+ let current = initial;
523
+ const run = incremental.active;
524
+ if (!run) {
525
+ return {
526
+ get value() {
527
+ return current;
528
+ },
529
+ set value(v) {
530
+ current = v;
531
+ }
532
+ };
533
+ }
534
+ const slot = run.boxSlot++;
535
+ run.boxes[slot] = {
536
+ get: () => current,
537
+ set: (v) => {
538
+ current = v;
539
+ }
540
+ };
541
+ if (run.mode === "resume") run.next.boxOwner[slot] = run.prev?.boxOwner[slot];
542
+ const touch = () => {
543
+ if (incremental.active !== run) return;
544
+ if (run.block < 0) {
545
+ incremental.nonIncremental("persist() used outside eachBar()");
546
+ return;
547
+ }
548
+ const owner = run.next.boxOwner[slot];
549
+ if (owner === void 0) run.next.boxOwner[slot] = run.block;
550
+ else if (owner !== run.block) incremental.nonIncremental("persist() used by two eachBar() blocks");
551
+ };
552
+ return {
553
+ get value() {
554
+ touch();
555
+ return current;
556
+ },
557
+ set value(v) {
558
+ touch();
559
+ current = v;
560
+ }
561
+ };
562
+ }
359
563
  function indicator(title, options = {}) {
360
564
  const c = collector();
361
565
  declare(c, "indicator");
@@ -521,7 +725,10 @@ const ohlc4 = new import_series.Series(ctxBars, (b) => (b.open + b.high + b.low
521
725
  const hlcc4 = new import_series.Series(ctxBars, (b) => (b.high + b.low + 2 * b.close) / 4);
522
726
  const bar_index = new import_series.Series(ctxBars, (_b, i) => i);
523
727
  bar_index.fullHistory = true;
524
- const last_bar_index = new import_series.Series(ctxBars, (_b, _i, data) => data.length - 1);
728
+ const last_bar_index = new import_series.Series(ctxBars, (_b, _i, data) => {
729
+ incremental.nonIncremental("last_bar_index");
730
+ return data.length - 1;
731
+ });
525
732
  last_bar_index.fullHistory = true;
526
733
  for (const s of [hl2, hlc3, ohlc4, hlcc4]) s.fullHistory = true;
527
734
  const SOURCES = { open, high, low, close, volume, hl2, hlc3, ohlc4, hlcc4 };
@@ -604,21 +811,136 @@ function outsideBarLoops(members) {
604
811
  }
605
812
  return out;
606
813
  }
814
+ function stepped(kind, make, sources, args, outs, full) {
815
+ const run = incremental.active;
816
+ if (!run) return full();
817
+ const slot = run.taSlot++;
818
+ const b = sources[0] ? sources[0].bars : bars();
819
+ const n = b.length;
820
+ const values = sources.map((s) => s.toArray());
821
+ let mem;
822
+ let from = 0;
823
+ const prev = run.mode === "resume" ? run.prev?.ta[slot] : void 0;
824
+ if (run.mode === "resume") {
825
+ if (!prev || prev.kind !== kind) incremental.nonIncremental(`a new ta.${kind}() call`);
826
+ from = run.from;
827
+ mem = { kind, machine: prev.machine, outs: prev.outs };
828
+ } else {
829
+ mem = { kind, machine: make(), outs: Array.from({ length: outs }, () => []) };
830
+ }
831
+ for (const o of mem.outs) o.length = n;
832
+ for (let i = from; i < n; i++) {
833
+ const r = mem.machine.step(...args(values, i));
834
+ if (outs === 1) mem.outs[0][i] = typeof r === "boolean" ? r ? 1 : 0 : r;
835
+ else for (let k = 0; k < outs; k++) mem.outs[k][i] = r[k];
836
+ if (i < n - 1) mem.machine.commit();
837
+ }
838
+ run.next.ta[slot] = mem;
839
+ const series = mem.outs.map((o) => import_series.Series.fromArray(b, o));
840
+ return outs === 1 ? series[0] : series;
841
+ }
842
+ const at = (v, k, i) => v[k][i];
843
+ const steppedTa = {
844
+ sma: (source, length) => typeof length === "number" ? stepped("sma", import_step.taStep.sma, [source], (v, i) => [at(v, 0, i), length], 1, () => taSeries.sma(source, length)) : taSeries.sma(source, length),
845
+ ema: (source, length) => stepped("ema", () => import_step.taStep.ema(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.ema(source, length)),
846
+ rma: (source, length) => stepped("rma", () => import_step.taStep.rma(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.rma(source, length)),
847
+ wma: (source, length) => typeof length === "number" ? stepped("wma", import_step.taStep.wma, [source], (v, i) => [at(v, 0, i), length], 1, () => taSeries.wma(source, length)) : taSeries.wma(source, length),
848
+ rsi: (source, length) => typeof length === "number" ? stepped("rsi", () => import_step.taStep.rsi(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.rsi(source, length)) : taSeries.rsi(source, length),
849
+ change: (source, length = 1) => stepped("change", () => import_step.taStep.change(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.change(source, length)),
850
+ mom: (source, length) => stepped("mom", () => import_step.taStep.mom(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.mom(source, length)),
851
+ roc: (source, length) => stepped("roc", () => import_step.taStep.roc(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.roc(source, length)),
852
+ cum: (source) => stepped("cum", import_step.taStep.cum, [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.cum(source)),
853
+ stdev: (source, length, biased = true) => typeof length === "number" ? stepped("stdev", import_step.taStep.stdev, [source], (v, i) => [at(v, 0, i), length, biased], 1, () => taSeries.stdev(source, length, biased)) : taSeries.stdev(source, length, biased),
854
+ variance: (source, length, biased = true) => typeof length === "number" ? stepped("variance", import_step.taStep.variance, [source], (v, i) => [at(v, 0, i), length, biased], 1, () => taSeries.variance(source, length, biased)) : taSeries.variance(source, length, biased),
855
+ crossover: (a, b) => stepped(
856
+ "crossover",
857
+ import_step.taStep.crossover,
858
+ typeof b === "number" ? [a] : [a, b],
859
+ (v, i) => [at(v, 0, i), typeof b === "number" ? b : at(v, 1, i)],
860
+ 1,
861
+ () => taSeries.crossover(a, b)
862
+ ),
863
+ crossunder: (a, b) => stepped(
864
+ "crossunder",
865
+ import_step.taStep.crossunder,
866
+ typeof b === "number" ? [a] : [a, b],
867
+ (v, i) => [at(v, 0, i), typeof b === "number" ? b : at(v, 1, i)],
868
+ 1,
869
+ () => taSeries.crossunder(a, b)
870
+ ),
871
+ cross: (a, b) => stepped(
872
+ "cross",
873
+ import_step.taStep.cross,
874
+ typeof b === "number" ? [a] : [a, b],
875
+ (v, i) => [at(v, 0, i), typeof b === "number" ? b : at(v, 1, i)],
876
+ 1,
877
+ () => taSeries.cross(a, b)
878
+ ),
879
+ barssince: (condition) => stepped("barssince", import_step.taStep.barssince, [condition], (v, i) => [at(v, 0, i) === 1], 1, () => taSeries.barssince(condition)),
880
+ valuewhen: (condition, source, occurrence) => stepped(
881
+ "valuewhen",
882
+ () => import_step.taStep.valuewhen(occurrence),
883
+ [condition, source],
884
+ (v, i) => [at(v, 0, i) === 1, at(v, 1, i)],
885
+ 1,
886
+ () => taSeries.valuewhen(condition, source, occurrence)
887
+ ),
888
+ macd: (source, fastLength, slowLength, signalLength) => stepped("macd", () => import_step.taStep.macd(fastLength, slowLength, signalLength), [source], (v, i) => [at(v, 0, i)], 3, () => taSeries.macd(source, fastLength, slowLength, signalLength)),
889
+ bb: (source, length, mult) => stepped("bb", () => import_step.taStep.bb(length, mult), [source], (v, i) => [at(v, 0, i)], 3, () => taSeries.bb(source, length, mult)),
890
+ hma: (source, length) => stepped("hma", () => import_step.taStep.hma(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.hma(source, length)),
891
+ dev: (source, length) => stepped("dev", () => import_step.taStep.dev(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.dev(source, length)),
892
+ cci: (source, length) => stepped("cci", () => import_step.taStep.cci(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.cci(source, length)),
893
+ percentrank: (source, length) => stepped("percentrank", () => import_step.taStep.percentrank(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.percentrank(source, length)),
894
+ median: (source, length) => stepped("median", () => import_step.taStep.median(length), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.median(source, length)),
895
+ linreg: (source, length, offset = 0) => stepped("linreg", () => import_step.taStep.linreg(length, offset), [source], (v, i) => [at(v, 0, i)], 1, () => taSeries.linreg(source, length, offset)),
896
+ correlation: (a, b, length) => stepped("correlation", () => import_step.taStep.correlation(length), [a, b], (v, i) => [at(v, 0, i), at(v, 1, i)], 1, () => taSeries.correlation(a, b, length)),
897
+ stoch: (source, highSource, lowSource, length) => stepped(
898
+ "stoch",
899
+ () => import_step.taStep.stoch(length),
900
+ [source, highSource, lowSource],
901
+ (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)],
902
+ 1,
903
+ () => taSeries.stoch(source, highSource, lowSource, length)
904
+ )
905
+ };
607
906
  const ta = strictNamespace("ta", outsideBarLoops({
608
907
  ...taSeries,
609
- tr: (handleNa = false) => taSeries.tr(bars(), handleNa),
610
- atr: (length) => taSeries.atr(bars(), length),
908
+ ...steppedTa,
909
+ tr: (handleNa = false) => stepped("tr", () => import_step.taStep.tr(handleNa), [high, low, close], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1, () => taSeries.tr(bars(), handleNa)),
910
+ atr: (length) => stepped("atr", () => import_step.taStep.atr(length), [high, low, close], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1, () => taSeries.atr(bars(), length)),
611
911
  sar: (start, inc, max) => taSeries.sar(bars(), start, inc, max),
612
- wpr: (length) => taSeries.wpr(bars(), length),
613
- supertrend: (factor, atrLength) => taSeries.supertrend(bars(), factor, atrLength),
614
- dmi: (diLength, adxSmoothing) => taSeries.dmi(bars(), diLength, adxSmoothing),
912
+ wpr: (length = 14) => stepped("wpr", () => import_step.taStep.wpr(length), [high, low, close], (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)], 1, () => taSeries.wpr(bars(), length)),
913
+ supertrend: (factor, atrLength) => stepped(
914
+ "supertrend",
915
+ () => import_step.taStep.supertrend(factor, atrLength),
916
+ [high, low, close],
917
+ (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)],
918
+ 2,
919
+ () => taSeries.supertrend(bars(), factor, atrLength)
920
+ ),
921
+ dmi: (diLength, adxSmoothing) => stepped(
922
+ "dmi",
923
+ () => import_step.taStep.dmi(diLength, adxSmoothing),
924
+ [high, low, close],
925
+ (v, i) => [at(v, 0, i), at(v, 1, i), at(v, 2, i)],
926
+ 3,
927
+ () => taSeries.dmi(bars(), diLength, adxSmoothing)
928
+ ),
615
929
  kc: (source, length, mult, useTrueRange) => taSeries.kc(bars(), source, length, mult, useTrueRange),
616
930
  kcw: (source, length, mult, useTrueRange) => taSeries.kcw(bars(), source, length, mult, useTrueRange),
617
931
  ichimoku: (conversionPeriods, basePeriods, laggingSpan2Periods, displacement) => taSeries.ichimoku(bars(), conversionPeriods, basePeriods, laggingSpan2Periods, displacement),
618
932
  /** `ta.highest(source, length)` or `ta.highest(length)` (source = high). */
619
- highest: (sourceOrLength, length) => typeof sourceOrLength === "number" ? taSeries.highest(high, sourceOrLength) : taSeries.highest(sourceOrLength, length),
933
+ highest: (sourceOrLength, length) => {
934
+ const source = typeof sourceOrLength === "number" ? high : sourceOrLength;
935
+ const len = typeof sourceOrLength === "number" ? sourceOrLength : length;
936
+ return typeof len === "number" ? stepped("highest", import_step.taStep.highest, [source], (v, i) => [at(v, 0, i), len], 1, () => taSeries.highest(source, len)) : taSeries.highest(source, len);
937
+ },
620
938
  /** `ta.lowest(source, length)` or `ta.lowest(length)` (source = low). */
621
- lowest: (sourceOrLength, length) => typeof sourceOrLength === "number" ? taSeries.lowest(low, sourceOrLength) : taSeries.lowest(sourceOrLength, length),
939
+ lowest: (sourceOrLength, length) => {
940
+ const source = typeof sourceOrLength === "number" ? low : sourceOrLength;
941
+ const len = typeof sourceOrLength === "number" ? sourceOrLength : length;
942
+ return typeof len === "number" ? stepped("lowest", import_step.taStep.lowest, [source], (v, i) => [at(v, 0, i), len], 1, () => taSeries.lowest(source, len)) : taSeries.lowest(source, len);
943
+ },
622
944
  /** `ta.highestbars(source, length)` or `ta.highestbars(length)` (source = high); offsets are 0 or negative. */
623
945
  highestbars: (sourceOrLength, length) => typeof sourceOrLength === "number" ? taSeries.highestbars(high, sourceOrLength) : taSeries.highestbars(sourceOrLength, length),
624
946
  /** `ta.lowestbars(source, length)` or `ta.lowestbars(length)` (source = low); offsets are 0 or negative. */
@@ -632,9 +954,9 @@ const ta = strictNamespace("ta", outsideBarLoops({
632
954
  return stdev_mult === void 0 ? taSeries.vwap(source, volume, a) : taSeries.vwapBands(source, volume, a, stdev_mult);
633
955
  }),
634
956
  /** `ta.vwma(source, length)` with the chart volume, as PineScript; a third argument gives another volume. */
635
- vwma: (source, length, vol) => taSeries.vwma(source, length, vol ?? volume),
957
+ vwma: (source, length, vol) => stepped("vwma", () => import_step.taStep.vwma(length), [source, vol ?? volume], (v, i) => [at(v, 0, i), at(v, 1, i)], 1, () => taSeries.vwma(source, length, vol ?? volume)),
636
958
  /** `ta.mfi(source, length)` with the chart volume (PineScript takes 2 arguments). */
637
- mfi: (source, length) => taSeries.mfi(source, length, volume),
959
+ mfi: (source, length) => stepped("mfi", () => import_step.taStep.mfi(length), [source, volume], (v, i) => [at(v, 0, i), at(v, 1, i)], 1, () => taSeries.mfi(source, length, volume)),
638
960
  /** `ta.pivothigh(source, leftbars, rightbars)` or `ta.pivothigh(leftbars, rightbars)` (source = high).
639
961
  * The value appears `rightbars` bars after the pivot bar, as in PineScript. */
640
962
  pivothigh: (sourceOrLeft, leftOrRight, rightbars) => typeof sourceOrLeft === "number" ? taSeries.pivothigh(high, sourceOrLeft, leftOrRight) : taSeries.pivothigh(sourceOrLeft, leftOrRight, rightbars),
@@ -654,6 +976,19 @@ function fixnan(source) {
654
976
  if (source instanceof import_series.Series) return import_series.Series.fromArray(ctxBars, (0, import_utils.fixnan)(source.toArray()));
655
977
  return withPalette((0, import_utils.fixnan)(source), colorPalettes.get(source));
656
978
  }
979
+ function na(x) {
980
+ if (x instanceof import_series.Series) return import_series.Series.fromArray(ctxBars, x.toArray().map((v) => (0, import_utils.isNA)(v) ? 1 : 0));
981
+ return (0, import_utils.isNA)(x);
982
+ }
983
+ function nz(source, replacement = 0) {
984
+ if (source instanceof import_series.Series) {
985
+ const values = source.toArray();
986
+ const rep = replacement instanceof import_series.Series ? replacement.toArray() : void 0;
987
+ const r = rep ? 0 : Number(replacement);
988
+ return import_series.Series.fromArray(ctxBars, values.map((v, i) => (0, import_utils.isNA)(v) ? rep ? rep[i] : r : v));
989
+ }
990
+ return (0, import_utils.nz)(source, replacement);
991
+ }
657
992
  const colorPalettes = /* @__PURE__ */ new WeakMap();
658
993
  function withPalette(colors, palette) {
659
994
  if (palette?.length) colorPalettes.set(colors, palette);
@@ -667,9 +1002,23 @@ function colorWhen(cond, colorTrue, colorFalse) {
667
1002
  const palette = [colorTrue, ...colorFalse !== void 0 && colorFalse !== colorTrue ? [colorFalse] : []];
668
1003
  return withPalette(colors, palette);
669
1004
  }
1005
+ function fromGradient(value, bottom_value, top_value, bottom_color, top_color) {
1006
+ const args = [value, bottom_value, top_value];
1007
+ if (!args.some((a) => a instanceof import_series.Series) && !Array.isArray(bottom_color) && !Array.isArray(top_color)) {
1008
+ return colorCore.from_gradient(value, bottom_value, top_value, bottom_color, top_color);
1009
+ }
1010
+ const [v, lo, hi] = args.map((a) => a instanceof import_series.Series ? a.toArray() : void 0);
1011
+ const at2 = (arr, num, i) => arr ? arr[i] : num;
1012
+ const colorAt = (c, i) => Array.isArray(c) ? c[i] : c;
1013
+ return Array.from(
1014
+ { length: ctxBars.length },
1015
+ (_, i) => colorCore.from_gradient(at2(v, value, i), at2(lo, bottom_value, i), at2(hi, top_value, i), colorAt(bottom_color, i), colorAt(top_color, i))
1016
+ );
1017
+ }
670
1018
  const color = strictNamespace("color", {
671
1019
  ...colorCore,
672
1020
  new: colorCore.new_color,
1021
+ from_gradient: fromGradient,
673
1022
  when: colorWhen
674
1023
  });
675
1024
  const DEFAULT_OUTPUT_TITLES = {
@@ -707,14 +1056,34 @@ function plotImpl(series, title = DEFAULT_OUTPUT_TITLES.plot, options = {}) {
707
1056
  c.outputs.push({ kind: "plot", id, title });
708
1057
  const values = series.toArray();
709
1058
  const b = bars();
710
- const data = [];
1059
+ let data = [];
711
1060
  const off = Math.trunc(options.offset ?? 0);
712
- for (let j = Math.max(0, off); j < b.length && j - off < b.length; j++) {
1061
+ let j0 = Math.max(0, off);
1062
+ const run = incremental.active;
1063
+ const slot = run ? run.plotSlot++ : -1;
1064
+ let pos = [];
1065
+ const prev = run?.mode === "resume" ? run.prev?.plots[slot] : void 0;
1066
+ const sig = `${off}|${perBar2 ? "colors" : ""}`;
1067
+ if (prev && prev.sig === sig) {
1068
+ const from = Math.max(j0, run.from + Math.min(0, off));
1069
+ if (from < prev.pos.length) {
1070
+ data = prev.data.slice(0, prev.pos[from]);
1071
+ pos = prev.pos;
1072
+ pos.length = from;
1073
+ j0 = from;
1074
+ }
1075
+ }
1076
+ for (let j2 = 0; j2 < j0; j2++) pos[j2] ??= 0;
1077
+ let j = j0;
1078
+ for (; j < b.length && j - off < b.length; j++) {
1079
+ pos[j] = data.length;
713
1080
  const i = j - off;
714
1081
  const value = values[i];
715
1082
  if (value === void 0 || !Number.isFinite(value)) continue;
716
1083
  data.push(perBar2 ? { time: b[j].time, value, color: perBar2[i] } : { time: b[j].time, value });
717
1084
  }
1085
+ for (; j <= b.length; j++) pos[j] = data.length;
1086
+ if (run) run.next.plots[slot] = { data, pos, sig };
718
1087
  c.plots[id] = data;
719
1088
  return { id, kind: "plot" };
720
1089
  }
@@ -1078,20 +1447,81 @@ function plotbar(open2, high2, low2, close2, title, options = {}) {
1078
1447
  }
1079
1448
  const HISTORY_START = 242;
1080
1449
  const HISTORY_GROWTH = 1.618034;
1450
+ const outerWritesCache = /* @__PURE__ */ new Map();
1451
+ function outerWritesOf(fn) {
1452
+ const src = fn.toString();
1453
+ let found = outerWritesCache.get(src);
1454
+ if (!found) {
1455
+ found = (0, import_closure_writes.outerWrites)(src);
1456
+ if (outerWritesCache.size > 500) outerWritesCache.clear();
1457
+ outerWritesCache.set(src, found);
1458
+ }
1459
+ return found;
1460
+ }
1081
1461
  function eachBar(fn) {
1462
+ return runBlock(fn);
1463
+ }
1464
+ function runBlock(fn, extra, own = fn) {
1082
1465
  collector();
1083
1466
  const b = bars();
1084
1467
  const lastIndex = intrabarRun ? intrabarRun.lastBarIndex : b.length - 1;
1085
- const out = new Array(b.length).fill(NaN);
1468
+ let out = new Array(b.length).fill(NaN);
1086
1469
  const cache = /* @__PURE__ */ new Map();
1087
1470
  const buffers = /* @__PURE__ */ new Map();
1471
+ const bufferKey = (src) => incremental.slotOf(src) ?? src;
1088
1472
  let pendingError = null;
1089
1473
  let idx = 0;
1474
+ const run = incremental.active;
1475
+ const slot = run ? run.block < 0 ? run.blockSlot++ : -1 : -1;
1476
+ if (run && slot < 0) incremental.nonIncremental("eachBar() inside eachBar()");
1477
+ if (run?.mode === "record") {
1478
+ const written = outerWritesOf(own);
1479
+ if (written.length) incremental.nonIncremental(`eachBar() callback changes ${written.join(", ")} outside it (use persist())`);
1480
+ }
1481
+ let start = 0;
1482
+ let checkpoint = null;
1483
+ if (run?.mode === "resume") {
1484
+ const prev = run.prev?.blocks[slot];
1485
+ if (!prev) incremental.nonIncremental("a new eachBar() block");
1486
+ start = run.from;
1487
+ out = prev.out;
1488
+ out.length = b.length;
1489
+ for (let i = start; i < b.length; i++) out[i] = NaN;
1490
+ checkpoint = prev.checkpoint;
1491
+ if (run.prev.drawingBlock === slot && checkpoint.drawings) (0, import_registry.restoreDrawingCheckpoint)(checkpoint.drawings);
1492
+ if (extra) extra.restore(checkpoint.extra);
1493
+ for (const [key, state] of checkpoint.buffers) buffers.set(key, { ...state });
1494
+ pendingError = checkpoint.pendingError ? { ...checkpoint.pendingError } : null;
1495
+ for (const [box2, value] of checkpoint.boxes) {
1496
+ const ref = run.boxes[box2];
1497
+ if (!ref) incremental.nonIncremental("a persist() box of the previous run is missing");
1498
+ ref.set(incremental.copyValue(value));
1499
+ }
1500
+ }
1501
+ const takeCheckpoint = () => {
1502
+ const boxes = /* @__PURE__ */ new Map();
1503
+ run.boxes.forEach((ref, box2) => {
1504
+ if (run.next.boxOwner[box2] === slot) boxes.set(box2, incremental.copyValue(ref.get()));
1505
+ });
1506
+ const kept = /* @__PURE__ */ new Map();
1507
+ for (const [key, state] of buffers) kept.set(key, { ...state });
1508
+ return {
1509
+ boxes,
1510
+ buffers: kept,
1511
+ pendingError: pendingError ? { ...pendingError } : null,
1512
+ drawings: (0, import_registry.saveDrawingCheckpoint)(),
1513
+ extra: extra?.save()
1514
+ };
1515
+ };
1516
+ if (run && b.length - 2 < start && !checkpoint) checkpoint = takeCheckpoint();
1090
1517
  const ctx2 = {
1091
1518
  get i() {
1092
1519
  return idx;
1093
1520
  },
1094
- n: lastIndex + 1,
1521
+ get n() {
1522
+ if (run) incremental.nonIncremental("BarContext.n");
1523
+ return lastIndex + 1;
1524
+ },
1095
1525
  get time() {
1096
1526
  return b[idx].time;
1097
1527
  },
@@ -1115,8 +1545,8 @@ function eachBar(fn) {
1115
1545
  if (!src.fullHistory && offset > 0) {
1116
1546
  const k = Math.floor(offset);
1117
1547
  if (k > import_series.MAX_BARS_BACK) throw new Error((0, import_series.historyError)(idx, k));
1118
- let buffer = buffers.get(src);
1119
- if (!buffer) buffers.set(src, buffer = { size: HISTORY_START, previous: 0, barMax: 0 });
1548
+ let buffer = buffers.get(bufferKey(src));
1549
+ if (!buffer) buffers.set(bufferKey(src), buffer = { size: HISTORY_START, previous: 0, barMax: 0 });
1120
1550
  if (k > buffer.barMax) buffer.barMax = k;
1121
1551
  }
1122
1552
  let vals = cache.get(src);
@@ -1132,8 +1562,10 @@ function eachBar(fn) {
1132
1562
  return j >= 0 ? out[j] : NaN;
1133
1563
  }
1134
1564
  };
1565
+ const outerBlock = run ? run.block : -1;
1566
+ if (run) run.block = slot;
1135
1567
  try {
1136
- for (idx = 0; idx < b.length; idx++) {
1568
+ for (idx = start; idx < b.length; idx++) {
1137
1569
  if (pendingError && pendingError.bar === idx) throw new Error((0, import_series.historyError)(idx, pendingError.barsBack));
1138
1570
  currentBar = idx;
1139
1571
  (0, import_registry.setDrawingBar)(idx, idx === lastIndex, lastIndex);
@@ -1149,11 +1581,14 @@ function eachBar(fn) {
1149
1581
  buffer.previous = k;
1150
1582
  buffer.barMax = 0;
1151
1583
  }
1584
+ if (run && idx === b.length - 2) checkpoint = takeCheckpoint();
1152
1585
  }
1153
1586
  } finally {
1154
1587
  currentBar = -1;
1155
1588
  (0, import_registry.setDrawingBar)(null);
1589
+ if (run) run.block = outerBlock;
1156
1590
  }
1591
+ if (run) run.next.blocks[slot] = { out, checkpoint };
1157
1592
  return import_series.Series.fromArray(ctxBars, out);
1158
1593
  }
1159
1594
  let currentBar = -1;
@@ -1263,7 +1698,12 @@ function strategyEachBar(fn) {
1263
1698
  });
1264
1699
  }
1265
1700
  const recorder = scriptOptions.recorder;
1266
- return eachBar((c) => {
1701
+ if (incremental.active) {
1702
+ if (recorder) incremental.nonIncremental("a strategy with intrabar runs (recorder)");
1703
+ if (!engine.save || !engine.restore) incremental.nonIncremental("a strategy engine without save() / restore()");
1704
+ }
1705
+ const extra = engine.save && engine.restore ? { save: () => engine.save(), restore: (s) => engine.restore(s) } : void 0;
1706
+ return runBlock((c) => {
1267
1707
  engine.processBar(c.i);
1268
1708
  if (recorder) {
1269
1709
  recorder.varipStarts[c.i] = [...recorder.varip];
@@ -1277,7 +1717,7 @@ function strategyEachBar(fn) {
1277
1717
  run.engine = engine;
1278
1718
  engine.processClose(c.i);
1279
1719
  }
1280
- });
1720
+ }, extra, fn);
1281
1721
  }
1282
1722
  function recordingEngine(engine, log) {
1283
1723
  const keep = (v) => {
@@ -1608,7 +2048,10 @@ function timeCloseFunction(tf, session2, timezone) {
1608
2048
  return perBar((t) => close2(t));
1609
2049
  }
1610
2050
  let runNow = NaN;
1611
- const timenow = new import_series.Series(ctxBars, () => runNow);
2051
+ const timenow = new import_series.Series(ctxBars, () => {
2052
+ incremental.nonIncremental("timenow");
2053
+ return runNow;
2054
+ });
1612
2055
  const time = callableSeries(new import_series.Series(ctxBars, (b) => barTime(b)), timeFunction);
1613
2056
  const time_close = callableSeries(
1614
2057
  new import_series.Series(ctxBars, (_b, i) => sessionBars().bars.closeOf(barTime(ctxBars.bars[i]), chartTimeframe().period)),
@@ -1681,6 +2124,7 @@ const barstate = {
1681
2124
  },
1682
2125
  get islastconfirmedhistory() {
1683
2126
  return namedFlag("islastconfirmedhistory", (i, n) => {
2127
+ incremental.nonIncremental("barstate.islastconfirmedhistory");
1684
2128
  const lastConfirmed = chartCtx.lastBarConfirmed === true && chartCtx.realtime !== true;
1685
2129
  const count = chartBarCount(n);
1686
2130
  return i === (lastConfirmed ? count - 1 : count - 2);
@@ -1738,6 +2182,7 @@ function valuesOf(value, length) {
1738
2182
  }
1739
2183
  function security(symbol, timeframe2, expression, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = false) {
1740
2184
  collector();
2185
+ incremental.nonIncremental("request.security");
1741
2186
  const heikin = symbol.startsWith(HEIKIN_ASHI);
1742
2187
  const base = heikin ? symbol.slice(HEIKIN_ASHI.length) : symbol;
1743
2188
  const chart2 = chartTimeframe();
@@ -1762,8 +2207,8 @@ function security(symbol, timeframe2, expression, gaps = barmerge.gaps_off, look
1762
2207
  const calendar2 = timeframeCore.info(tf).isdwm ? regular : loaded;
1763
2208
  const starts = times.map((t) => calendar2.timeOf(t, tf));
1764
2209
  const dayPeriods = times.map((t, i) => {
1765
- const at = loaded.calendar.locate(t);
1766
- const periods = at ? calendar2.calendar.periods(at.date) : [];
2210
+ const at2 = loaded.calendar.locate(t);
2211
+ const periods = at2 ? calendar2.calendar.periods(at2.date) : [];
1767
2212
  return periods.length ? calendar2.timeOf(periods[0][0], tf) : starts[i];
1768
2213
  });
1769
2214
  const tfInfo = timeframeCore.info(tf);
@@ -1859,6 +2304,7 @@ function externalSecurity(symbol, heikin, tf, expression, gaps, lookahead, ignor
1859
2304
  }
1860
2305
  function security_lower_tf(symbol, timeframe2, expression, ignore_invalid_symbol = false) {
1861
2306
  collector();
2307
+ incremental.nonIncremental("request.security_lower_tf");
1862
2308
  const heikin = symbol.startsWith(HEIKIN_ASHI);
1863
2309
  const base = heikin ? symbol.slice(HEIKIN_ASHI.length) : symbol;
1864
2310
  const chart2 = chartTimeframe();