oakscriptjs 0.20.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.
- package/dist/cjs/check/harness.d.ts.map +1 -1
- package/dist/cjs/check/harness.js +35 -1
- package/dist/cjs/check/harness.js.map +2 -2
- package/dist/cjs/script/globals.d.ts +3 -0
- package/dist/cjs/script/globals.d.ts.map +1 -1
- package/dist/cjs/script/globals.js.map +1 -1
- package/dist/cjs/script/index.d.ts +211 -11
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +295 -29
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/cjs/security/events.d.ts +16 -0
- package/dist/cjs/security/events.d.ts.map +1 -0
- package/dist/cjs/security/events.js +84 -0
- package/dist/cjs/security/events.js.map +7 -0
- package/dist/esm/check/harness.d.ts.map +1 -1
- package/dist/esm/check/harness.js +25 -0
- package/dist/esm/check/harness.js.map +1 -1
- package/dist/esm/script/globals.d.ts +3 -0
- package/dist/esm/script/globals.d.ts.map +1 -1
- package/dist/esm/script/globals.js.map +1 -1
- package/dist/esm/script/index.d.ts +211 -11
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +314 -35
- package/dist/esm/script/index.js.map +1 -1
- package/dist/esm/security/events.d.ts +16 -0
- package/dist/esm/security/events.d.ts.map +1 -0
- package/dist/esm/security/events.js +78 -0
- package/dist/esm/security/events.js.map +1 -0
- package/package.json +1 -1
- package/skills/oakscript/reference/timeframes-symbols.md +27 -0
- package/src/check/harness.ts +27 -0
- package/src/script/globals.ts +3 -0
- package/src/script/index.ts +502 -43
- package/src/security/events.ts +85 -0
|
@@ -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
|
+
}
|