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.
Files changed (34) hide show
  1. package/dist/cjs/check/harness.d.ts.map +1 -1
  2. package/dist/cjs/check/harness.js +35 -1
  3. package/dist/cjs/check/harness.js.map +2 -2
  4. package/dist/cjs/script/globals.d.ts +3 -0
  5. package/dist/cjs/script/globals.d.ts.map +1 -1
  6. package/dist/cjs/script/globals.js.map +1 -1
  7. package/dist/cjs/script/index.d.ts +211 -11
  8. package/dist/cjs/script/index.d.ts.map +1 -1
  9. package/dist/cjs/script/index.js +295 -29
  10. package/dist/cjs/script/index.js.map +3 -3
  11. package/dist/cjs/security/events.d.ts +16 -0
  12. package/dist/cjs/security/events.d.ts.map +1 -0
  13. package/dist/cjs/security/events.js +84 -0
  14. package/dist/cjs/security/events.js.map +7 -0
  15. package/dist/esm/check/harness.d.ts.map +1 -1
  16. package/dist/esm/check/harness.js +25 -0
  17. package/dist/esm/check/harness.js.map +1 -1
  18. package/dist/esm/script/globals.d.ts +3 -0
  19. package/dist/esm/script/globals.d.ts.map +1 -1
  20. package/dist/esm/script/globals.js.map +1 -1
  21. package/dist/esm/script/index.d.ts +211 -11
  22. package/dist/esm/script/index.d.ts.map +1 -1
  23. package/dist/esm/script/index.js +314 -35
  24. package/dist/esm/script/index.js.map +1 -1
  25. package/dist/esm/security/events.d.ts +16 -0
  26. package/dist/esm/security/events.d.ts.map +1 -0
  27. package/dist/esm/security/events.js +78 -0
  28. package/dist/esm/security/events.js.map +1 -0
  29. package/package.json +1 -1
  30. package/skills/oakscript/reference/timeframes-symbols.md +27 -0
  31. package/src/check/harness.ts +27 -0
  32. package/src/script/globals.ts +3 -0
  33. package/src/script/index.ts +502 -43
  34. package/src/security/events.ts +85 -0
@@ -0,0 +1,16 @@
1
+ /** `[start, end)` in ms of the period of `tf` that contains `time`, in UTC. */
2
+ export declare function utcPeriod(time: number, tf: string): [number, number];
3
+ /** An event's time and its value. */
4
+ export interface EventValue {
5
+ time: number;
6
+ value: number;
7
+ }
8
+ /**
9
+ * The value of the events on each chart bar. `opens` / `closes`: open and close times of the chart bars in ms,
10
+ * `events` oldest first.
11
+ */
12
+ export declare function eventsOnBars(opens: number[], closes: number[], tf: string, events: EventValue[], options?: {
13
+ gaps?: boolean;
14
+ lookahead?: boolean;
15
+ }): number[];
16
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../../src/security/events.ts"],"names":[],"mappings":"AAkBA,+EAA+E;AAC/E,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAiBpE;AAED,qCAAqC;AACrC,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EAAE,EACf,MAAM,EAAE,MAAM,EAAE,EAChB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,UAAU,EAAE,EACpB,OAAO,GAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAC;IAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAAO,GACpD,MAAM,EAAE,CA8BV"}
@@ -0,0 +1,78 @@
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
+ const DAY = 86_400_000;
15
+ /** 1970-01-05, the first Monday of UNIX time. */
16
+ const FIRST_MONDAY = 4 * DAY;
17
+ /** `[start, end)` in ms of the period of `tf` that contains `time`, in UTC. */
18
+ export function utcPeriod(time, tf) {
19
+ const info = timeframeCore.info(tf);
20
+ if (info.isticks)
21
+ return [time, time];
22
+ if (info.ismonthly) {
23
+ const d = new Date(time);
24
+ const months = d.getUTCFullYear() * 12 + d.getUTCMonth();
25
+ const first = months - (((months % info.multiplier) + info.multiplier) % info.multiplier);
26
+ return [Date.UTC(Math.floor(first / 12), first % 12, 1), Date.UTC(Math.floor((first + info.multiplier) / 12), (first + info.multiplier) % 12, 1)];
27
+ }
28
+ if (info.isweekly) {
29
+ const span = 7 * DAY * info.multiplier;
30
+ const start = Math.floor((time - FIRST_MONDAY) / span) * span + FIRST_MONDAY;
31
+ return [start, start + span];
32
+ }
33
+ const span = timeframeCore.in_seconds(tf) * 1000;
34
+ const start = Math.floor(time / span) * span;
35
+ return [start, start + span];
36
+ }
37
+ /**
38
+ * The value of the events on each chart bar. `opens` / `closes`: open and close times of the chart bars in ms,
39
+ * `events` oldest first.
40
+ */
41
+ export function eventsOnBars(opens, closes, tf, events, options = {}) {
42
+ const n = opens.length;
43
+ const out = new Array(n).fill(NaN);
44
+ if (!n)
45
+ return out;
46
+ // the bar each event shows on (n: after the last bar)
47
+ const arrivals = events.map((e) => {
48
+ const [start, end] = utcPeriod(e.time, tf);
49
+ let lo = 0;
50
+ let hi = n;
51
+ while (lo < hi) {
52
+ const mid = (lo + hi) >> 1;
53
+ if (options.lookahead ? closes[mid] > start : closes[mid] >= end)
54
+ hi = mid;
55
+ else
56
+ lo = mid + 1;
57
+ }
58
+ // with gaps on, an event that the bar before the first one would have shown is not shown on the first one (that
59
+ // bar closes about one bar length before the first one's open)
60
+ const before = lo === 0 && (options.lookahead ? start : end) <= opens[0] - (closes[0] - opens[0]);
61
+ return { at: lo, before, value: e.value };
62
+ });
63
+ if (options.gaps) {
64
+ for (const a of arrivals)
65
+ if (a.at < n && !a.before)
66
+ out[a.at] = a.value;
67
+ return out;
68
+ }
69
+ let k = 0;
70
+ let value = NaN;
71
+ for (let i = 0; i < n; i++) {
72
+ while (k < arrivals.length && arrivals[k].at <= i)
73
+ value = arrivals[k++].value;
74
+ out[i] = value;
75
+ }
76
+ return out;
77
+ }
78
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../../src/security/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,aAAa,MAAM,uBAAuB,CAAC;AAEvD,MAAM,GAAG,GAAG,UAAU,CAAC;AACvB,iDAAiD;AACjD,MAAM,YAAY,GAAG,CAAC,GAAG,GAAG,CAAC;AAE7B,+EAA+E;AAC/E,MAAM,UAAU,SAAS,CAAC,IAAY,EAAE,EAAU;IAChD,MAAM,IAAI,GAAG,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACpC,IAAI,IAAI,CAAC,OAAO;QAAE,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IACtC,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACnB,MAAM,CAAC,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,MAAM,MAAM,GAAG,CAAC,CAAC,cAAc,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;QACzD,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;QAC1F,OAAO,CAAC,IA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,IAAI,KAAK,GAAG,GAAG,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,OAAO,CAAC,GAAG,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,CAAC,CAAE,CAAC,EAAE,IAAI,CAAC;YAAE,KAAK,GAAG,QAAQ,CAAC,CAAC,EAAE,CAAE,CAAC,KAAK,CAAC;QACjF,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;IACjB,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oakscriptjs",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "type": "module",
5
5
  "description": "PineScript v6 compatible technical analysis library for JavaScript/TypeScript",
6
6
  "main": "./dist/cjs/index.js",
@@ -87,6 +87,33 @@ indicator("Seed value", { overlay: false });
87
87
  plot(request.seed("seed_crypto_santiment", "BTC_DEV_ACTIVITY", () => close), "Dev activity");
88
88
  ```
89
89
 
90
+ ### Dividends, splits and earnings
91
+
92
+ `request.dividends(ticker, field?, gaps?, lookahead?)`, `request.splits(ticker, field, gaps?, lookahead?)` and
93
+ `request.earnings(ticker, field?, gaps?, lookahead?)` return a Series of the last event's value: the dividend per
94
+ share as declared (`dividends.net`, `dividends.gross`), the split ratio (`splits.numerator`, `splits.denominator`),
95
+ the earnings per share (`earnings.actual`, `earnings.estimate`, `earnings.standardized`). With the default lookahead
96
+ off, an event shows from the bar after its day on a daily chart (from its day's first bar on an intraday chart).
97
+ A field the app has no data for (an analysts' estimate) stops the script with an error.
98
+
99
+ ```js
100
+ indicator("Dividend yield", { overlay: false });
101
+ const dividend = request.dividends(syminfo.tickerid);
102
+ const split = request.splits(syminfo.tickerid, splits.numerator, barmerge.gaps_on);
103
+ plot(dividend.mul(4).div(close).mul(100), "Yield %", { color: color.teal });
104
+ plotshape(split.gt(0), "Split", { location: location.bottom, color: color.orange });
105
+ ```
106
+
107
+ `dividends.future_amount`, `dividends.future_ex_date`, `earnings.future_time`... give the next announced event (na
108
+ when none is announced).
109
+
110
+ ## Symbol information
111
+
112
+ `syminfo.*`: `tickerid`, `ticker`, `prefix`, `main_tickerid`, `root`, `description`, `type`, `currency`,
113
+ `basecurrency`, `country`, `sector`, `industry`, `isin`, `employees`, `current_contract`, `expiration_date`,
114
+ `mintick`, `pointvalue`, `timezone`, `session`, the share and analyst figures. A field the symbol does not have is
115
+ na (`""` for a text: test it with `na(syminfo.sector)`); a field the app does not know stops the script with an error.
116
+
90
117
  ## Timeframe values
91
118
 
92
119
  - `timeframe.period` (`"60"`, `"1D"`...), `timeframe.multiplier`, `timeframe.isintraday`, `timeframe.isdaily`,
@@ -21,6 +21,7 @@ import {
21
21
  type StrategyRiskRule,
22
22
  type StrategySetup,
23
23
  type StrategyTrade,
24
+ type SymbolEvents,
24
25
  type StrategyVariable,
25
26
  } from '../script/index.js';
26
27
  import type { Bar } from '../types/index.js';
@@ -170,9 +171,35 @@ export function testChart(bars: Bar[], options: TestChartOptions = {}): ChartCon
170
171
  }));
171
172
  },
172
173
  symbolInfo: () => ({ timezone, session, mintick: 0.01, currency: 'USD' }),
174
+ // the other symbol fields: none (na), so that a script reading them runs
175
+ symbolType: 'stock',
176
+ description: 'Synthetic test symbol',
177
+ ...NO_SYMBOL_FIELDS,
178
+ events: () => testEvents(bars),
173
179
  };
174
180
  }
175
181
 
182
+ const NO_SYMBOL_FIELDS = {
183
+ basecurrency: null, country: null, currentContract: null, expirationDate: null, sector: null, industry: null,
184
+ isin: null, volumetype: null, employees: null, shareholders: null, sharesOutstandingTotal: null,
185
+ sharesOutstandingFloat: null, recommendations: null, targetPrice: null,
186
+ } satisfies Partial<ChartContext>;
187
+
188
+ /** Events of the test symbol: a dividend and an earnings release every 90 days of its bars, a 2-for-1 split half-way. */
189
+ function testEvents(bars: Bar[]): SymbolEvents {
190
+ if (!bars.length) return {};
191
+ const first = bars[0]!.time * 1000;
192
+ const last = bars[bars.length - 1]!.time * 1000;
193
+ const QUARTER = 90 * 86_400_000;
194
+ const dividends: SymbolEvents['dividends'] = [];
195
+ const earnings: SymbolEvents['earnings'] = [];
196
+ for (let t = first, k = 1; t <= last; t += QUARTER, k++) {
197
+ dividends.push({ time: t, net: 0.2 + 0.01 * k });
198
+ earnings.push({ time: t + 30 * 86_400_000, actual: 1 + 0.1 * k, estimate: 1 + 0.09 * k, standardized: 1 + 0.1 * k });
199
+ }
200
+ return { dividends, earnings, splits: [{ time: Math.round((first + last) / 2), numerator: 2, denominator: 1 }] };
201
+ }
202
+
176
203
  /**
177
204
  * A small order engine for checks and tests (the library does not fill orders). Market entries and closes fill at the
178
205
  * next bar open; `strategy.exit` stop / limit levels are checked inside each bar (stop first when both are hit).
@@ -87,6 +87,9 @@ declare global {
87
87
  const barstate: typeof S.barstate;
88
88
  const session: typeof S.session;
89
89
  const barmerge: typeof S.barmerge;
90
+ const dividends: typeof S.dividends;
91
+ const splits: typeof S.splits;
92
+ const earnings: typeof S.earnings;
90
93
  const ticker: typeof S.ticker;
91
94
  const request: typeof S.request;
92
95
  const line: typeof S.line;