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
@@ -199,6 +199,9 @@ export interface ScriptRunResult {
199
199
  /** The script called alert() during the run (PineScript `hasAlertFunction` is known at compile time: a host that
200
200
  * needs it before any bar runs reads the source). */
201
201
  hasAlertFunction: boolean;
202
+ /** The script read `chart.left_visible_bar_time` or `chart.right_visible_bar_time`: it has to be calculated again when
203
+ * the visible range of the chart changes. */
204
+ usesVisibleRange: boolean;
202
205
  }
203
206
 
204
207
  export interface AlertConditionConfig {
@@ -277,6 +280,7 @@ interface Collector {
277
280
  /** log.*() messages of the run. */
278
281
  logs: LogEntry[];
279
282
  hasAlertFunction: boolean;
283
+ usesVisibleRange: boolean;
280
284
  strategy?: StrategyRun;
281
285
  }
282
286
 
@@ -336,6 +340,7 @@ function freshCollector(inputValues: Record<string, unknown>): Collector {
336
340
  alertCalls: [],
337
341
  logs: [],
338
342
  hasAlertFunction: false,
343
+ usesVisibleRange: false,
339
344
  };
340
345
  }
341
346
 
@@ -545,6 +550,7 @@ function runBody(
545
550
  logs: c.logs.length ? [...c.logs].sort((x, y) => x.bar - y.bar).slice(-MAX_LOG_ENTRIES) : undefined,
546
551
  },
547
552
  hasAlertFunction: c.hasAlertFunction,
553
+ usesVisibleRange: c.usesVisibleRange,
548
554
  };
549
555
  } finally {
550
556
  ctx = null;
@@ -823,10 +829,13 @@ export interface ScriptRun {
823
829
  /** Chart context fields that only change the last bar (barstate of the forming bar). */
824
830
  const LAST_BAR_FIELDS = new Set(['lastBarConfirmed', 'lastBarNew', 'realtime', 'now']);
825
831
 
826
- function sameChart(a: ChartContext, b: ChartContext): boolean {
832
+ /** The visible range of the chart: part of the chart only for a script that reads it. */
833
+ const VISIBLE_RANGE_FIELDS = new Set(['leftVisibleBarTime', 'rightVisibleBarTime']);
834
+
835
+ function sameChart(a: ChartContext, b: ChartContext, visibleRange = true): boolean {
827
836
  const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
828
837
  for (const k of keys) {
829
- if (LAST_BAR_FIELDS.has(k)) continue;
838
+ if (LAST_BAR_FIELDS.has(k) || (!visibleRange && VISIBLE_RANGE_FIELDS.has(k))) continue;
830
839
  if (!Object.is((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k])) return false;
831
840
  }
832
841
  return true;
@@ -917,6 +926,8 @@ export function createScriptRun(
917
926
  let previousChart: ChartContext = chart;
918
927
  let reason: string | null = null;
919
928
  let verifyLeft = options.verify ?? 2;
929
+ /** A bar calculated so far read the visible range. */
930
+ let rangeRead = false;
920
931
  /** The log messages of the last update: a resumed update keeps those of the bars it does not calculate again. */
921
932
  let lastLogs: LogEntry[] | undefined;
922
933
 
@@ -941,7 +952,7 @@ export function createScriptRun(
941
952
  const last = previous;
942
953
  const m = memory;
943
954
  const resumable =
944
- !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c) &&
955
+ !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c, rangeRead) &&
945
956
  sameBars(last, b, last.n - 1);
946
957
  previousChart = c;
947
958
  let out: ScriptRunUpdate;
@@ -954,6 +965,7 @@ export function createScriptRun(
954
965
  try {
955
966
  result = runBody(body, b, inputs, c, options, inc);
956
967
  result.result.logs = resumedLogs(lastLogs, result.result.logs, from);
968
+ result.usesVisibleRange ||= rangeRead;
957
969
  } catch (err) {
958
970
  // the memory is changed in place by a resumed run: an error leaves it unusable
959
971
  memory = null;
@@ -964,6 +976,7 @@ export function createScriptRun(
964
976
  if (result && verifyLeft > 0) {
965
977
  verifyLeft--;
966
978
  const check = runBody(body, b, inputs, c, options, null);
979
+ check.usesVisibleRange = result.usesVisibleRange;
967
980
  if (!sameResult(result, check)) {
968
981
  reason = 'a resumed update differs from a full run (state kept outside persist()?)';
969
982
  result = check;
@@ -979,6 +992,7 @@ export function createScriptRun(
979
992
  }
980
993
  previous = out.incremental && last ? copyBars(b, last, last.n - 1) : copyBars(b);
981
994
  lastLogs = out.result.logs;
995
+ rangeRead = out.usesVisibleRange;
982
996
  return out;
983
997
  },
984
998
  };
@@ -3058,6 +3072,9 @@ export const strategy = Object.defineProperties(
3058
3072
  variableGetters
3059
3073
  ) as StrategyApi;
3060
3074
 
3075
+ /** Types of chart PineScript tells apart (`chart.is_*`); every other type is 'standard'. */
3076
+ export type ChartType = 'standard' | 'heikinashi' | 'renko' | 'kagi' | 'linebreak' | 'pnf' | 'range';
3077
+
3061
3078
  // ── Chart context (issue #100) ───────────────────────────────────────────────
3062
3079
 
3063
3080
  /**
@@ -3116,6 +3133,15 @@ export interface ChartContext {
3116
3133
  foregroundColor?: string;
3117
3134
  /** Chart background colour (PineScript `chart.bg_color`), e.g. "#0F0F0F" on a dark chart */
3118
3135
  backgroundColor?: string;
3136
+ /**
3137
+ * Times (ms) of the first and the last bar in view (PineScript `chart.left_visible_bar_time` and
3138
+ * `chart.right_visible_bar_time`); default: the first and the last bar. A script that reads them
3139
+ * ({@link ScriptRunResult.usesVisibleRange}) has to be calculated again when they change.
3140
+ */
3141
+ leftVisibleBarTime?: number;
3142
+ rightVisibleBarTime?: number;
3143
+ /** Type of the chart (PineScript `chart.is_standard`, `chart.is_heikinashi`...); default 'standard' */
3144
+ chartType?: ChartType;
3119
3145
  /** Current time in ms (PineScript `timenow`); default: the clock of the computer at the start of the run */
3120
3146
  now?: number;
3121
3147
  /**
@@ -3280,6 +3306,19 @@ function chartText(
3280
3306
  return value;
3281
3307
  }
3282
3308
 
3309
+ /** A visible range value: the chart context's, else the time of bar `index` (NaN without bars). */
3310
+ function visibleBarTime(value: number | undefined, index: number): number {
3311
+ collector().usesVisibleRange = true;
3312
+ if (value !== undefined) return value;
3313
+ const bar = ctxBars.bars[index];
3314
+ return bar ? barTime(bar) : NaN;
3315
+ }
3316
+
3317
+ function chartTypeIs(type: ChartType): boolean {
3318
+ collector();
3319
+ return (chartCtx.chartType ?? 'standard') === type;
3320
+ }
3321
+
3283
3322
  /** `[minmove, pricescale]` of the chart symbol: from the chart context, else from its mintick. */
3284
3323
  function symbolScale(): [number, number] {
3285
3324
  collector();
@@ -3988,7 +4027,7 @@ export interface LabelNewArgs {
3988
4027
  y?: Numeric;
3989
4028
  point?: ChartPoint;
3990
4029
  text?: string;
3991
- xloc?: 'bar_index' | 'bar_time';
4030
+ xloc?: 'bar_index' | 'bar_time' | 'pane';
3992
4031
  yloc?: 'price' | 'abovebar' | 'belowbar';
3993
4032
  color?: Colour;
3994
4033
  style?: NonNullable<Label['style']>;
@@ -4009,7 +4048,7 @@ export interface LineNewArgs {
4009
4048
  y2?: Numeric;
4010
4049
  first_point?: ChartPoint;
4011
4050
  second_point?: ChartPoint;
4012
- xloc?: 'bar_index' | 'bar_time';
4051
+ xloc?: 'bar_index' | 'bar_time' | 'pane';
4013
4052
  extend?: 'none' | 'left' | 'right' | 'both';
4014
4053
  color?: Colour;
4015
4054
  style?: 'solid' | 'dotted' | 'dashed' | 'arrow_left' | 'arrow_right' | 'arrow_both';
@@ -4029,7 +4068,7 @@ export interface BoxNewArgs {
4029
4068
  border_width?: Numeric;
4030
4069
  border_style?: 'solid' | 'dotted' | 'dashed';
4031
4070
  extend?: 'none' | 'left' | 'right' | 'both';
4032
- xloc?: 'bar_index' | 'bar_time';
4071
+ xloc?: 'bar_index' | 'bar_time' | 'pane';
4033
4072
  bgcolor?: Colour;
4034
4073
  text?: string;
4035
4074
  text_size?: string | number;
@@ -4078,7 +4117,7 @@ export interface PolylineNewArgs {
4078
4117
  points?: ChartPoint[];
4079
4118
  curved?: boolean;
4080
4119
  closed?: boolean;
4081
- xloc?: 'bar_index' | 'bar_time';
4120
+ xloc?: 'bar_index' | 'bar_time' | 'pane';
4082
4121
  line_color?: Colour;
4083
4122
  fill_color?: Colour | null;
4084
4123
  line_style?: 'solid' | 'dotted' | 'dashed';
@@ -4174,6 +4213,38 @@ export const chart = {
4174
4213
  get bg_color(): string {
4175
4214
  return chartText('backgroundColor', 'chart.bg_color');
4176
4215
  },
4216
+ /** Time (ms) of the first bar in view (PineScript `chart.left_visible_bar_time`), from the chart context; without it,
4217
+ * of the first bar. */
4218
+ get left_visible_bar_time(): number {
4219
+ return visibleBarTime(chartCtx.leftVisibleBarTime, 0);
4220
+ },
4221
+ /** Time (ms) of the last bar in view (PineScript `chart.right_visible_bar_time`), from the chart context; without it,
4222
+ * of the last bar. */
4223
+ get right_visible_bar_time(): number {
4224
+ return visibleBarTime(chartCtx.rightVisibleBarTime, ctxBars.bars.length - 1);
4225
+ },
4226
+ /** The chart is not one of the types below (PineScript `chart.is_standard`). */
4227
+ get is_standard(): boolean {
4228
+ return chartTypeIs('standard');
4229
+ },
4230
+ get is_heikinashi(): boolean {
4231
+ return chartTypeIs('heikinashi');
4232
+ },
4233
+ get is_renko(): boolean {
4234
+ return chartTypeIs('renko');
4235
+ },
4236
+ get is_kagi(): boolean {
4237
+ return chartTypeIs('kagi');
4238
+ },
4239
+ get is_linebreak(): boolean {
4240
+ return chartTypeIs('linebreak');
4241
+ },
4242
+ get is_pnf(): boolean {
4243
+ return chartTypeIs('pnf');
4244
+ },
4245
+ get is_range(): boolean {
4246
+ return chartTypeIs('range');
4247
+ },
4177
4248
  point: {
4178
4249
  ...chartPointCore,
4179
4250
  now(price?: number): ChartPoint {
@@ -70,16 +70,16 @@ export interface PineMatrix<T> {
70
70
  * for detecting trend line breakouts, calculating support/resistance levels, etc.
71
71
  */
72
72
  export interface Line {
73
- /** First point's x coordinate (bar index or UNIX timestamp) */
73
+ /** First point's x coordinate (bar index, UNIX timestamp, or percent of the pane's width with xloc 'pane') */
74
74
  x1: number;
75
75
  /** First point's y coordinate (price level) */
76
76
  y1: number;
77
- /** Second point's x coordinate (bar index or UNIX timestamp) */
77
+ /** Second point's x coordinate (bar index, UNIX timestamp, or percent of the pane's width with xloc 'pane') */
78
78
  x2: number;
79
79
  /** Second point's y coordinate (price level) */
80
80
  y2: number;
81
81
  /** X-axis coordinate system */
82
- xloc: 'bar_index' | 'bar_time';
82
+ xloc: 'bar_index' | 'bar_time' | 'pane';
83
83
  /** Extension mode */
84
84
  extend: 'none' | 'left' | 'right' | 'both';
85
85
  /** Line color (stored but not used for calculations) */
@@ -99,16 +99,16 @@ export interface Line {
99
99
  * range breakout analysis, and consolidation zone identification.
100
100
  */
101
101
  export interface Box {
102
- /** Left border x coordinate (bar index or UNIX timestamp) */
102
+ /** Left border x coordinate (bar index, UNIX timestamp, or percent of the pane's width with xloc 'pane') */
103
103
  left: number;
104
104
  /** Top border y coordinate (price level) */
105
105
  top: number;
106
- /** Right border x coordinate (bar index or UNIX timestamp) */
106
+ /** Right border x coordinate (bar index, UNIX timestamp, or percent of the pane's width with xloc 'pane') */
107
107
  right: number;
108
108
  /** Bottom border y coordinate (price level) */
109
109
  bottom: number;
110
110
  /** X-axis coordinate system */
111
- xloc: 'bar_index' | 'bar_time';
111
+ xloc: 'bar_index' | 'bar_time' | 'pane';
112
112
  /** Horizontal extension mode */
113
113
  extend: 'none' | 'left' | 'right' | 'both';
114
114
  /** Border color (stored but not used for calculations) */
@@ -145,12 +145,12 @@ export interface Box {
145
145
  * Labels have limited computational value. Primarily used for marking points and displaying text.
146
146
  */
147
147
  export interface Label {
148
- /** X coordinate (bar index or UNIX timestamp) */
148
+ /** X coordinate (bar index, UNIX timestamp, or percent of the pane's width with xloc 'pane') */
149
149
  x: number;
150
150
  /** Y coordinate (price level when yloc = 'price') */
151
151
  y: number;
152
152
  /** X-axis coordinate system */
153
- xloc: 'bar_index' | 'bar_time';
153
+ xloc: 'bar_index' | 'bar_time' | 'pane';
154
154
  /** Y-axis positioning mode */
155
155
  yloc: 'price' | 'abovebar' | 'belowbar';
156
156
  /** Label text content */
@@ -357,13 +357,15 @@ export type na = null;
357
357
 
358
358
  /**
359
359
  * X-axis location constants
360
- * Used to specify whether x-coordinates are bar indices or timestamps
360
+ * Used to specify whether x-coordinates are bar indices, timestamps or places on the pane
361
361
  */
362
362
  export const xloc = {
363
363
  /** X-coordinates are bar indices */
364
364
  bar_index: 'bar_index' as const,
365
365
  /** X-coordinates are UNIX timestamps in milliseconds */
366
366
  bar_time: 'bar_time' as const,
367
+ /** X-coordinates are percents of the pane's width: 0 left edge, 100 right edge (not a PineScript value) */
368
+ pane: 'pane' as const,
367
369
  };
368
370
 
369
371
  /**
@@ -411,8 +413,8 @@ export interface Polyline {
411
413
  readonly curved: boolean;
412
414
  /** If true, connect the first point to the last point */
413
415
  readonly closed: boolean;
414
- /** X-coordinate mode: 'bar_index' or 'bar_time' */
415
- readonly xloc: 'bar_index' | 'bar_time';
416
+ /** X-coordinate mode: 'bar_index', 'bar_time' or 'pane' */
417
+ readonly xloc: 'bar_index' | 'bar_time' | 'pane';
416
418
  /** Color of line segments */
417
419
  readonly line_color: color;
418
420
  /** Fill color for closed polylines (null for no fill) */
@@ -438,8 +438,9 @@ export interface LabelData {
438
438
  time: number;
439
439
  /** 'bar_time': the x coordinates are times given by the script (xloc.bar_time). A time after the last bar is
440
440
  * drawn on the future bar that holds it, on the chart's calendar (trading sessions). Omitted: bar indexes (a
441
- * future bar index k bars after the last bar has the time lastTime + k * interval). */
442
- xloc?: 'bar_time';
441
+ * future bar index k bars after the last bar has the time lastTime + k * interval). 'pane': the times are percents
442
+ * of the pane's width (0 left edge, 100 right edge): drawn there whatever bars are in view. */
443
+ xloc?: 'bar_time' | 'pane';
443
444
  /** Price; not used with yloc 'abovebar' / 'belowbar' */
444
445
  price: number;
445
446
  text: string;
@@ -476,8 +477,9 @@ export interface LineDrawingData {
476
477
  price2: number;
477
478
  /** 'bar_time': the x coordinates are times given by the script (xloc.bar_time). A time after the last bar is
478
479
  * drawn on the future bar that holds it, on the chart's calendar (trading sessions). Omitted: bar indexes (a
479
- * future bar index k bars after the last bar has the time lastTime + k * interval). */
480
- xloc?: 'bar_time';
480
+ * future bar index k bars after the last bar has the time lastTime + k * interval). 'pane': the times are percents
481
+ * of the pane's width (0 left edge, 100 right edge): drawn there whatever bars are in view. */
482
+ xloc?: 'bar_time' | 'pane';
481
483
  /** Omitted: #2962FF (color.blue; #2196F3 in a v4 script) */
482
484
  color?: string;
483
485
  /** Omitted: 1. A width of 0 is kept (PineScript `width = 0`) */
@@ -499,8 +501,9 @@ export interface BoxData {
499
501
  price2: number;
500
502
  /** 'bar_time': the x coordinates are times given by the script (xloc.bar_time). A time after the last bar is
501
503
  * drawn on the future bar that holds it, on the chart's calendar (trading sessions). Omitted: bar indexes (a
502
- * future bar index k bars after the last bar has the time lastTime + k * interval). */
503
- xloc?: 'bar_time';
504
+ * future bar index k bars after the last bar has the time lastTime + k * interval). 'pane': the times are percents
505
+ * of the pane's width (0 left edge, 100 right edge): drawn there whatever bars are in view. */
506
+ xloc?: 'bar_time' | 'pane';
504
507
  /** Omitted: #2962FF (color.blue; #2196F3 in a v4 script) */
505
508
  bgColor?: string;
506
509
  /** Omitted: #2962FF (color.blue; #2196F3 in a v4 script) */
@@ -544,8 +547,9 @@ export interface PolylineData {
544
547
  points: Array<{ time: number; price: number }>;
545
548
  /** 'bar_time': the x coordinates are times given by the script (xloc.bar_time). A time after the last bar is
546
549
  * drawn on the future bar that holds it, on the chart's calendar (trading sessions). Omitted: bar indexes (a
547
- * future bar index k bars after the last bar has the time lastTime + k * interval). */
548
- xloc?: 'bar_time';
550
+ * future bar index k bars after the last bar has the time lastTime + k * interval). 'pane': the times are percents
551
+ * of the pane's width (0 left edge, 100 right edge): drawn there whatever bars are in view. */
552
+ xloc?: 'bar_time' | 'pane';
549
553
  /** Curved segments between the points */
550
554
  curved?: boolean;
551
555
  /** The last point is connected to the first */