oakscriptjs 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/dist/cjs/box/index.d.ts +3 -3
  2. package/dist/cjs/box/index.d.ts.map +1 -1
  3. package/dist/cjs/box/index.js.map +2 -2
  4. package/dist/cjs/drawing/output.d.ts +2 -1
  5. package/dist/cjs/drawing/output.d.ts.map +1 -1
  6. package/dist/cjs/drawing/output.js +5 -4
  7. package/dist/cjs/drawing/output.js.map +2 -2
  8. package/dist/cjs/index.d.ts +1 -1
  9. package/dist/cjs/index.js +1 -1
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/label/index.d.ts +2 -2
  12. package/dist/cjs/label/index.d.ts.map +1 -1
  13. package/dist/cjs/label/index.js.map +2 -2
  14. package/dist/cjs/line/index.d.ts +4 -4
  15. package/dist/cjs/line/index.d.ts.map +1 -1
  16. package/dist/cjs/line/index.js +1 -1
  17. package/dist/cjs/line/index.js.map +2 -2
  18. package/dist/cjs/polyline/index.d.ts +2 -2
  19. package/dist/cjs/polyline/index.d.ts.map +1 -1
  20. package/dist/cjs/polyline/index.js.map +2 -2
  21. package/dist/cjs/script/constants.d.ts +6 -1
  22. package/dist/cjs/script/constants.d.ts.map +1 -1
  23. package/dist/cjs/script/constants.js +2 -1
  24. package/dist/cjs/script/constants.js.map +2 -2
  25. package/dist/cjs/script/index.d.ts +44 -17
  26. package/dist/cjs/script/index.d.ts.map +1 -1
  27. package/dist/cjs/script/index.js +54 -5
  28. package/dist/cjs/script/index.js.map +2 -2
  29. package/dist/cjs/types/index.d.ts +13 -11
  30. package/dist/cjs/types/index.d.ts.map +1 -1
  31. package/dist/cjs/types/index.js +3 -1
  32. package/dist/cjs/types/index.js.map +2 -2
  33. package/dist/cjs/types/metadata.d.ts +12 -8
  34. package/dist/cjs/types/metadata.d.ts.map +1 -1
  35. package/dist/cjs/types/metadata.js.map +1 -1
  36. package/dist/esm/box/index.d.ts +3 -3
  37. package/dist/esm/box/index.d.ts.map +1 -1
  38. package/dist/esm/box/index.js +1 -1
  39. package/dist/esm/box/index.js.map +1 -1
  40. package/dist/esm/drawing/output.d.ts +2 -1
  41. package/dist/esm/drawing/output.d.ts.map +1 -1
  42. package/dist/esm/drawing/output.js +9 -7
  43. package/dist/esm/drawing/output.js.map +1 -1
  44. package/dist/esm/index.d.ts +1 -1
  45. package/dist/esm/index.js +1 -1
  46. package/dist/esm/label/index.d.ts +2 -2
  47. package/dist/esm/label/index.d.ts.map +1 -1
  48. package/dist/esm/label/index.js.map +1 -1
  49. package/dist/esm/line/index.d.ts +4 -4
  50. package/dist/esm/line/index.d.ts.map +1 -1
  51. package/dist/esm/line/index.js +3 -3
  52. package/dist/esm/line/index.js.map +1 -1
  53. package/dist/esm/polyline/index.d.ts +2 -2
  54. package/dist/esm/polyline/index.d.ts.map +1 -1
  55. package/dist/esm/polyline/index.js +1 -1
  56. package/dist/esm/polyline/index.js.map +1 -1
  57. package/dist/esm/script/constants.d.ts +6 -1
  58. package/dist/esm/script/constants.d.ts.map +1 -1
  59. package/dist/esm/script/constants.js +6 -1
  60. package/dist/esm/script/constants.js.map +1 -1
  61. package/dist/esm/script/index.d.ts +44 -17
  62. package/dist/esm/script/index.d.ts.map +1 -1
  63. package/dist/esm/script/index.js +56 -3
  64. package/dist/esm/script/index.js.map +1 -1
  65. package/dist/esm/types/index.d.ts +13 -11
  66. package/dist/esm/types/index.d.ts.map +1 -1
  67. package/dist/esm/types/index.js +3 -1
  68. package/dist/esm/types/index.js.map +1 -1
  69. package/dist/esm/types/metadata.d.ts +12 -8
  70. package/dist/esm/types/metadata.d.ts.map +1 -1
  71. package/package.json +1 -1
  72. package/skills/oakscript/reference/pine-differences.md +2 -0
  73. package/skills/oakscript/reference/plotting.md +24 -1
  74. package/src/box/index.ts +4 -4
  75. package/src/drawing/output.ts +12 -8
  76. package/src/index.ts +1 -1
  77. package/src/label/index.ts +3 -3
  78. package/src/line/index.ts +6 -6
  79. package/src/polyline/index.ts +2 -2
  80. package/src/script/constants.ts +6 -1
  81. package/src/script/index.ts +78 -7
  82. package/src/types/index.ts +13 -11
  83. package/src/types/metadata.ts +12 -8
@@ -219,7 +219,8 @@ function freshCollector(inputValues) {
219
219
  alerts: [],
220
220
  alertCalls: [],
221
221
  logs: [],
222
- hasAlertFunction: false
222
+ hasAlertFunction: false,
223
+ usesVisibleRange: false
223
224
  };
224
225
  }
225
226
  function slugId(title, fallback, taken) {
@@ -339,7 +340,8 @@ function runBody(body, barsInput, inputs, chart2, options, inc) {
339
340
  // By bar, in call order on each bar (the top-level calls make theirs on every bar at once).
340
341
  logs: c.logs.length ? [...c.logs].sort((x, y) => x.bar - y.bar).slice(-MAX_LOG_ENTRIES) : void 0
341
342
  },
342
- hasAlertFunction: c.hasAlertFunction
343
+ hasAlertFunction: c.hasAlertFunction,
344
+ usesVisibleRange: c.usesVisibleRange
343
345
  };
344
346
  } finally {
345
347
  ctx = null;
@@ -485,10 +487,11 @@ function newActiveRun(mode, b, from, prev) {
485
487
  };
486
488
  }
487
489
  const LAST_BAR_FIELDS = /* @__PURE__ */ new Set(["lastBarConfirmed", "lastBarNew", "realtime", "now"]);
488
- function sameChart(a, b) {
490
+ const VISIBLE_RANGE_FIELDS = /* @__PURE__ */ new Set(["leftVisibleBarTime", "rightVisibleBarTime"]);
491
+ function sameChart(a, b, visibleRange = true) {
489
492
  const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
490
493
  for (const k of keys) {
491
- if (LAST_BAR_FIELDS.has(k)) continue;
494
+ if (LAST_BAR_FIELDS.has(k) || !visibleRange && VISIBLE_RANGE_FIELDS.has(k)) continue;
492
495
  if (!Object.is(a[k], b[k])) return false;
493
496
  }
494
497
  return true;
@@ -542,6 +545,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
542
545
  let previousChart = chart2;
543
546
  let reason = null;
544
547
  let verifyLeft = options.verify ?? 2;
548
+ let rangeRead = false;
545
549
  let lastLogs;
546
550
  const newRun = (mode, b, from) => newActiveRun(mode, b, from, memory);
547
551
  const full = (b, c) => {
@@ -560,7 +564,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
560
564
  update(b, c = previousChart) {
561
565
  const last = previous;
562
566
  const m = memory;
563
- const resumable = !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c) && sameBars(last, b, last.n - 1);
567
+ const resumable = !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c, rangeRead) && sameBars(last, b, last.n - 1);
564
568
  previousChart = c;
565
569
  let out;
566
570
  if (!resumable) {
@@ -572,6 +576,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
572
576
  try {
573
577
  result = runBody(body, b, inputs, c, options, inc);
574
578
  result.result.logs = resumedLogs(lastLogs, result.result.logs, from);
579
+ result.usesVisibleRange ||= rangeRead;
575
580
  } catch (err) {
576
581
  memory = null;
577
582
  previous = null;
@@ -581,6 +586,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
581
586
  if (result && verifyLeft > 0) {
582
587
  verifyLeft--;
583
588
  const check = runBody(body, b, inputs, c, options, null);
589
+ check.usesVisibleRange = result.usesVisibleRange;
584
590
  if (!sameResult(result, check)) {
585
591
  reason = "a resumed update differs from a full run (state kept outside persist()?)";
586
592
  result = check;
@@ -596,6 +602,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
596
602
  }
597
603
  previous = out.incremental && last ? copyBars(b, last, last.n - 1) : copyBars(b);
598
604
  lastLogs = out.result.logs;
605
+ rangeRead = out.usesVisibleRange;
599
606
  return out;
600
607
  }
601
608
  };
@@ -2031,6 +2038,16 @@ function chartText(key, name) {
2031
2038
  }
2032
2039
  return value;
2033
2040
  }
2041
+ function visibleBarTime(value, index) {
2042
+ collector().usesVisibleRange = true;
2043
+ if (value !== void 0) return value;
2044
+ const bar = ctxBars.bars[index];
2045
+ return bar ? barTime(bar) : NaN;
2046
+ }
2047
+ function chartTypeIs(type) {
2048
+ collector();
2049
+ return (chartCtx.chartType ?? "standard") === type;
2050
+ }
2034
2051
  function symbolScale() {
2035
2052
  collector();
2036
2053
  const { minmove, pricescale } = chartCtx;
@@ -2496,6 +2513,38 @@ const chart = {
2496
2513
  get bg_color() {
2497
2514
  return chartText("backgroundColor", "chart.bg_color");
2498
2515
  },
2516
+ /** Time (ms) of the first bar in view (PineScript `chart.left_visible_bar_time`), from the chart context; without it,
2517
+ * of the first bar. */
2518
+ get left_visible_bar_time() {
2519
+ return visibleBarTime(chartCtx.leftVisibleBarTime, 0);
2520
+ },
2521
+ /** Time (ms) of the last bar in view (PineScript `chart.right_visible_bar_time`), from the chart context; without it,
2522
+ * of the last bar. */
2523
+ get right_visible_bar_time() {
2524
+ return visibleBarTime(chartCtx.rightVisibleBarTime, ctxBars.bars.length - 1);
2525
+ },
2526
+ /** The chart is not one of the types below (PineScript `chart.is_standard`). */
2527
+ get is_standard() {
2528
+ return chartTypeIs("standard");
2529
+ },
2530
+ get is_heikinashi() {
2531
+ return chartTypeIs("heikinashi");
2532
+ },
2533
+ get is_renko() {
2534
+ return chartTypeIs("renko");
2535
+ },
2536
+ get is_kagi() {
2537
+ return chartTypeIs("kagi");
2538
+ },
2539
+ get is_linebreak() {
2540
+ return chartTypeIs("linebreak");
2541
+ },
2542
+ get is_pnf() {
2543
+ return chartTypeIs("pnf");
2544
+ },
2545
+ get is_range() {
2546
+ return chartTypeIs("range");
2547
+ },
2499
2548
  point: {
2500
2549
  ...chartPointCore,
2501
2550
  now(price) {