oakscriptjs 0.19.0 → 0.21.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 (107) 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/check/harness.d.ts.map +1 -1
  5. package/dist/cjs/check/harness.js +35 -1
  6. package/dist/cjs/check/harness.js.map +2 -2
  7. package/dist/cjs/drawing/output.d.ts +2 -1
  8. package/dist/cjs/drawing/output.d.ts.map +1 -1
  9. package/dist/cjs/drawing/output.js +5 -4
  10. package/dist/cjs/drawing/output.js.map +2 -2
  11. package/dist/cjs/index.d.ts +1 -1
  12. package/dist/cjs/index.js +1 -1
  13. package/dist/cjs/index.js.map +1 -1
  14. package/dist/cjs/label/index.d.ts +2 -2
  15. package/dist/cjs/label/index.d.ts.map +1 -1
  16. package/dist/cjs/label/index.js.map +2 -2
  17. package/dist/cjs/line/index.d.ts +4 -4
  18. package/dist/cjs/line/index.d.ts.map +1 -1
  19. package/dist/cjs/line/index.js +1 -1
  20. package/dist/cjs/line/index.js.map +2 -2
  21. package/dist/cjs/polyline/index.d.ts +2 -2
  22. package/dist/cjs/polyline/index.d.ts.map +1 -1
  23. package/dist/cjs/polyline/index.js.map +2 -2
  24. package/dist/cjs/script/constants.d.ts +6 -1
  25. package/dist/cjs/script/constants.d.ts.map +1 -1
  26. package/dist/cjs/script/constants.js +2 -1
  27. package/dist/cjs/script/constants.js.map +2 -2
  28. package/dist/cjs/script/globals.d.ts +3 -0
  29. package/dist/cjs/script/globals.d.ts.map +1 -1
  30. package/dist/cjs/script/globals.js.map +1 -1
  31. package/dist/cjs/script/index.d.ts +255 -28
  32. package/dist/cjs/script/index.d.ts.map +1 -1
  33. package/dist/cjs/script/index.js +349 -34
  34. package/dist/cjs/script/index.js.map +3 -3
  35. package/dist/cjs/security/events.d.ts +16 -0
  36. package/dist/cjs/security/events.d.ts.map +1 -0
  37. package/dist/cjs/security/events.js +84 -0
  38. package/dist/cjs/security/events.js.map +7 -0
  39. package/dist/cjs/types/index.d.ts +13 -11
  40. package/dist/cjs/types/index.d.ts.map +1 -1
  41. package/dist/cjs/types/index.js +3 -1
  42. package/dist/cjs/types/index.js.map +2 -2
  43. package/dist/cjs/types/metadata.d.ts +12 -8
  44. package/dist/cjs/types/metadata.d.ts.map +1 -1
  45. package/dist/cjs/types/metadata.js.map +1 -1
  46. package/dist/esm/box/index.d.ts +3 -3
  47. package/dist/esm/box/index.d.ts.map +1 -1
  48. package/dist/esm/box/index.js +1 -1
  49. package/dist/esm/box/index.js.map +1 -1
  50. package/dist/esm/check/harness.d.ts.map +1 -1
  51. package/dist/esm/check/harness.js +25 -0
  52. package/dist/esm/check/harness.js.map +1 -1
  53. package/dist/esm/drawing/output.d.ts +2 -1
  54. package/dist/esm/drawing/output.d.ts.map +1 -1
  55. package/dist/esm/drawing/output.js +9 -7
  56. package/dist/esm/drawing/output.js.map +1 -1
  57. package/dist/esm/index.d.ts +1 -1
  58. package/dist/esm/index.js +1 -1
  59. package/dist/esm/label/index.d.ts +2 -2
  60. package/dist/esm/label/index.d.ts.map +1 -1
  61. package/dist/esm/label/index.js.map +1 -1
  62. package/dist/esm/line/index.d.ts +4 -4
  63. package/dist/esm/line/index.d.ts.map +1 -1
  64. package/dist/esm/line/index.js +3 -3
  65. package/dist/esm/line/index.js.map +1 -1
  66. package/dist/esm/polyline/index.d.ts +2 -2
  67. package/dist/esm/polyline/index.d.ts.map +1 -1
  68. package/dist/esm/polyline/index.js +1 -1
  69. package/dist/esm/polyline/index.js.map +1 -1
  70. package/dist/esm/script/constants.d.ts +6 -1
  71. package/dist/esm/script/constants.d.ts.map +1 -1
  72. package/dist/esm/script/constants.js +6 -1
  73. package/dist/esm/script/constants.js.map +1 -1
  74. package/dist/esm/script/globals.d.ts +3 -0
  75. package/dist/esm/script/globals.d.ts.map +1 -1
  76. package/dist/esm/script/globals.js.map +1 -1
  77. package/dist/esm/script/index.d.ts +255 -28
  78. package/dist/esm/script/index.d.ts.map +1 -1
  79. package/dist/esm/script/index.js +370 -38
  80. package/dist/esm/script/index.js.map +1 -1
  81. package/dist/esm/security/events.d.ts +16 -0
  82. package/dist/esm/security/events.d.ts.map +1 -0
  83. package/dist/esm/security/events.js +78 -0
  84. package/dist/esm/security/events.js.map +1 -0
  85. package/dist/esm/types/index.d.ts +13 -11
  86. package/dist/esm/types/index.d.ts.map +1 -1
  87. package/dist/esm/types/index.js +3 -1
  88. package/dist/esm/types/index.js.map +1 -1
  89. package/dist/esm/types/metadata.d.ts +12 -8
  90. package/dist/esm/types/metadata.d.ts.map +1 -1
  91. package/package.json +1 -1
  92. package/skills/oakscript/reference/pine-differences.md +2 -0
  93. package/skills/oakscript/reference/plotting.md +24 -1
  94. package/skills/oakscript/reference/timeframes-symbols.md +27 -0
  95. package/src/box/index.ts +4 -4
  96. package/src/check/harness.ts +27 -0
  97. package/src/drawing/output.ts +12 -8
  98. package/src/index.ts +1 -1
  99. package/src/label/index.ts +3 -3
  100. package/src/line/index.ts +6 -6
  101. package/src/polyline/index.ts +2 -2
  102. package/src/script/constants.ts +6 -1
  103. package/src/script/globals.ts +3 -0
  104. package/src/script/index.ts +580 -50
  105. package/src/security/events.ts +85 -0
  106. package/src/types/index.ts +13 -11
  107. package/src/types/metadata.ts +12 -8
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Dividends, splits and earnings on the chart bars (`request.dividends`, `request.splits`, `request.earnings`).
3
+ *
4
+ * PineScript requests an event series on the chart timeframe in UTC: an event belongs to the period of that timeframe
5
+ * that contains its time (its UTC day on a daily chart, its UTC week from Monday on a weekly one, its hour on a 60
6
+ * chart). Measured on PineScript (AAPL, KO, NVDA, NSE:RELIANCE; 1D, 1W, 1M, 60):
7
+ * - lookahead off: the value shows from the first chart bar that closes at or after the end of that period, so a
8
+ * dividend of a Monday shows on Tuesday's daily bar and on Monday's first hourly bar;
9
+ * - lookahead on: from the first chart bar that closes after the period's start (Monday's daily bar);
10
+ * - gaps off keeps the last value on the bars after it (an event before the first bar shows from the first bar);
11
+ * gaps on shows it on that bar only.
12
+ */
13
+ import * as timeframeCore from '../timeframe/index.js';
14
+
15
+ const DAY = 86_400_000;
16
+ /** 1970-01-05, the first Monday of UNIX time. */
17
+ const FIRST_MONDAY = 4 * DAY;
18
+
19
+ /** `[start, end)` in ms of the period of `tf` that contains `time`, in UTC. */
20
+ export function utcPeriod(time: number, tf: string): [number, number] {
21
+ const info = timeframeCore.info(tf);
22
+ if (info.isticks) return [time, time];
23
+ if (info.ismonthly) {
24
+ const d = new Date(time);
25
+ const months = d.getUTCFullYear() * 12 + d.getUTCMonth();
26
+ const first = months - (((months % info.multiplier) + info.multiplier) % info.multiplier);
27
+ return [Date.UTC(Math.floor(first / 12), first % 12, 1), Date.UTC(Math.floor((first + info.multiplier) / 12), (first + info.multiplier) % 12, 1)];
28
+ }
29
+ if (info.isweekly) {
30
+ const span = 7 * DAY * info.multiplier;
31
+ const start = Math.floor((time - FIRST_MONDAY) / span) * span + FIRST_MONDAY;
32
+ return [start, start + span];
33
+ }
34
+ const span = timeframeCore.in_seconds(tf) * 1000;
35
+ const start = Math.floor(time / span) * span;
36
+ return [start, start + span];
37
+ }
38
+
39
+ /** An event's time and its value. */
40
+ export interface EventValue {
41
+ time: number;
42
+ value: number;
43
+ }
44
+
45
+ /**
46
+ * The value of the events on each chart bar. `opens` / `closes`: open and close times of the chart bars in ms,
47
+ * `events` oldest first.
48
+ */
49
+ export function eventsOnBars(
50
+ opens: number[],
51
+ closes: number[],
52
+ tf: string,
53
+ events: EventValue[],
54
+ options: { gaps?: boolean; lookahead?: boolean } = {}
55
+ ): number[] {
56
+ const n = opens.length;
57
+ const out = new Array<number>(n).fill(NaN);
58
+ if (!n) return out;
59
+ // the bar each event shows on (n: after the last bar)
60
+ const arrivals = events.map((e) => {
61
+ const [start, end] = utcPeriod(e.time, tf);
62
+ let lo = 0;
63
+ let hi = n;
64
+ while (lo < hi) {
65
+ const mid = (lo + hi) >> 1;
66
+ if (options.lookahead ? closes[mid]! > start : closes[mid]! >= end) hi = mid;
67
+ else lo = mid + 1;
68
+ }
69
+ // with gaps on, an event that the bar before the first one would have shown is not shown on the first one (that
70
+ // bar closes about one bar length before the first one's open)
71
+ const before = lo === 0 && (options.lookahead ? start : end) <= opens[0]! - (closes[0]! - opens[0]!);
72
+ return { at: lo, before, value: e.value };
73
+ });
74
+ if (options.gaps) {
75
+ for (const a of arrivals) if (a.at < n && !a.before) out[a.at] = a.value;
76
+ return out;
77
+ }
78
+ let k = 0;
79
+ let value = NaN;
80
+ for (let i = 0; i < n; i++) {
81
+ while (k < arrivals.length && arrivals[k]!.at <= i) value = arrivals[k++]!.value;
82
+ out[i] = value;
83
+ }
84
+ return out;
85
+ }
@@ -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 */