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.
- package/dist/cjs/box/index.d.ts +3 -3
- package/dist/cjs/box/index.d.ts.map +1 -1
- package/dist/cjs/box/index.js.map +2 -2
- 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/drawing/output.d.ts +2 -1
- package/dist/cjs/drawing/output.d.ts.map +1 -1
- package/dist/cjs/drawing/output.js +5 -4
- package/dist/cjs/drawing/output.js.map +2 -2
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/label/index.d.ts +2 -2
- package/dist/cjs/label/index.d.ts.map +1 -1
- package/dist/cjs/label/index.js.map +2 -2
- package/dist/cjs/line/index.d.ts +4 -4
- package/dist/cjs/line/index.d.ts.map +1 -1
- package/dist/cjs/line/index.js +1 -1
- package/dist/cjs/line/index.js.map +2 -2
- package/dist/cjs/polyline/index.d.ts +2 -2
- package/dist/cjs/polyline/index.d.ts.map +1 -1
- package/dist/cjs/polyline/index.js.map +2 -2
- package/dist/cjs/script/constants.d.ts +6 -1
- package/dist/cjs/script/constants.d.ts.map +1 -1
- package/dist/cjs/script/constants.js +2 -1
- package/dist/cjs/script/constants.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 +255 -28
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +349 -34
- 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/cjs/types/index.d.ts +13 -11
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +3 -1
- package/dist/cjs/types/index.js.map +2 -2
- package/dist/cjs/types/metadata.d.ts +12 -8
- package/dist/cjs/types/metadata.d.ts.map +1 -1
- package/dist/cjs/types/metadata.js.map +1 -1
- package/dist/esm/box/index.d.ts +3 -3
- package/dist/esm/box/index.d.ts.map +1 -1
- package/dist/esm/box/index.js +1 -1
- package/dist/esm/box/index.js.map +1 -1
- 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/drawing/output.d.ts +2 -1
- package/dist/esm/drawing/output.d.ts.map +1 -1
- package/dist/esm/drawing/output.js +9 -7
- package/dist/esm/drawing/output.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/label/index.d.ts +2 -2
- package/dist/esm/label/index.d.ts.map +1 -1
- package/dist/esm/label/index.js.map +1 -1
- package/dist/esm/line/index.d.ts +4 -4
- package/dist/esm/line/index.d.ts.map +1 -1
- package/dist/esm/line/index.js +3 -3
- package/dist/esm/line/index.js.map +1 -1
- package/dist/esm/polyline/index.d.ts +2 -2
- package/dist/esm/polyline/index.d.ts.map +1 -1
- package/dist/esm/polyline/index.js +1 -1
- package/dist/esm/polyline/index.js.map +1 -1
- package/dist/esm/script/constants.d.ts +6 -1
- package/dist/esm/script/constants.d.ts.map +1 -1
- package/dist/esm/script/constants.js +6 -1
- package/dist/esm/script/constants.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 +255 -28
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +370 -38
- 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/dist/esm/types/index.d.ts +13 -11
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/types/index.js +3 -1
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/metadata.d.ts +12 -8
- package/dist/esm/types/metadata.d.ts.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/pine-differences.md +2 -0
- package/skills/oakscript/reference/plotting.md +24 -1
- package/skills/oakscript/reference/timeframes-symbols.md +27 -0
- package/src/box/index.ts +4 -4
- package/src/check/harness.ts +27 -0
- package/src/drawing/output.ts +12 -8
- package/src/index.ts +1 -1
- package/src/label/index.ts +3 -3
- package/src/line/index.ts +6 -6
- package/src/polyline/index.ts +2 -2
- package/src/script/constants.ts +6 -1
- package/src/script/globals.ts +3 -0
- package/src/script/index.ts +580 -50
- package/src/security/events.ts +85 -0
- package/src/types/index.ts +13 -11
- package/src/types/metadata.ts +12 -8
package/dist/esm/script/index.js
CHANGED
|
@@ -84,6 +84,7 @@ import { TradingCalendar } from '../session/calendar.js';
|
|
|
84
84
|
import { SessionBars, sessionFlags, strayPeriods } from '../session/bars.js';
|
|
85
85
|
import { afterCompletion, heikinAshi, mapToChart, periodsOf, resample } from '../security/resample.js';
|
|
86
86
|
import { securityLowerTf, securityOther } from '../security/other.js';
|
|
87
|
+
import { eventsOnBars } from '../security/events.js';
|
|
87
88
|
import { position } from '../types/index.js';
|
|
88
89
|
import { STRATEGY_CONSTANTS, STRATEGY_DEFAULTS, STRATEGY_NUMBER_VARIABLES, STRATEGY_TEXT_VARIABLES, } from '../strategy/index.js';
|
|
89
90
|
import { sessionPeriods } from '../session/context.js';
|
|
@@ -128,6 +129,7 @@ function freshCollector(inputValues) {
|
|
|
128
129
|
alertCalls: [],
|
|
129
130
|
logs: [],
|
|
130
131
|
hasAlertFunction: false,
|
|
132
|
+
usesVisibleRange: false,
|
|
131
133
|
};
|
|
132
134
|
}
|
|
133
135
|
/** Stable input id from the title ("ATR / Range Period" → "atr_range_period"). */
|
|
@@ -227,7 +229,16 @@ function runBody(body, barsInput, inputs, chart, options, inc) {
|
|
|
227
229
|
mathCore.resetRandom();
|
|
228
230
|
missingSymbols = [];
|
|
229
231
|
try {
|
|
230
|
-
|
|
232
|
+
try {
|
|
233
|
+
body();
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
// An expression of request.security runs on no bar and without the symbol's properties while its bars are not
|
|
237
|
+
// given (syminfo.sector there throws): the run waits for the data, not on the error.
|
|
238
|
+
if (missingSymbols.length && !(err instanceof MissingSymbolDataError))
|
|
239
|
+
throw new MissingSymbolDataError(missingSymbols);
|
|
240
|
+
throw err;
|
|
241
|
+
}
|
|
231
242
|
if (missingSymbols.length)
|
|
232
243
|
throw new MissingSymbolDataError(missingSymbols);
|
|
233
244
|
// the outputs read the drawings: not a drawing of the script
|
|
@@ -274,6 +285,7 @@ function runBody(body, barsInput, inputs, chart, options, inc) {
|
|
|
274
285
|
logs: c.logs.length ? [...c.logs].sort((x, y) => x.bar - y.bar).slice(-MAX_LOG_ENTRIES) : undefined,
|
|
275
286
|
},
|
|
276
287
|
hasAlertFunction: c.hasAlertFunction,
|
|
288
|
+
usesVisibleRange: c.usesVisibleRange,
|
|
277
289
|
};
|
|
278
290
|
}
|
|
279
291
|
finally {
|
|
@@ -460,10 +472,12 @@ function newActiveRun(mode, b, from, prev) {
|
|
|
460
472
|
}
|
|
461
473
|
/** Chart context fields that only change the last bar (barstate of the forming bar). */
|
|
462
474
|
const LAST_BAR_FIELDS = new Set(['lastBarConfirmed', 'lastBarNew', 'realtime', 'now']);
|
|
463
|
-
|
|
475
|
+
/** The visible range of the chart: part of the chart only for a script that reads it. */
|
|
476
|
+
const VISIBLE_RANGE_FIELDS = new Set(['leftVisibleBarTime', 'rightVisibleBarTime']);
|
|
477
|
+
function sameChart(a, b, visibleRange = true) {
|
|
464
478
|
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
465
479
|
for (const k of keys) {
|
|
466
|
-
if (LAST_BAR_FIELDS.has(k))
|
|
480
|
+
if (LAST_BAR_FIELDS.has(k) || (!visibleRange && VISIBLE_RANGE_FIELDS.has(k)))
|
|
467
481
|
continue;
|
|
468
482
|
if (!Object.is(a[k], b[k]))
|
|
469
483
|
return false;
|
|
@@ -543,6 +557,8 @@ export function createScriptRun(body, inputs = {}, chart = {}, options = {}) {
|
|
|
543
557
|
let previousChart = chart;
|
|
544
558
|
let reason = null;
|
|
545
559
|
let verifyLeft = options.verify ?? 2;
|
|
560
|
+
/** A bar calculated so far read the visible range. */
|
|
561
|
+
let rangeRead = false;
|
|
546
562
|
/** The log messages of the last update: a resumed update keeps those of the bars it does not calculate again. */
|
|
547
563
|
let lastLogs;
|
|
548
564
|
const newRun = (mode, b, from) => newActiveRun(mode, b, from, memory);
|
|
@@ -565,7 +581,7 @@ export function createScriptRun(body, inputs = {}, chart = {}, options = {}) {
|
|
|
565
581
|
update(b, c = previousChart) {
|
|
566
582
|
const last = previous;
|
|
567
583
|
const m = memory;
|
|
568
|
-
const resumable = !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c) &&
|
|
584
|
+
const resumable = !reason && m !== null && last !== null && last.n >= 1 && b.length >= last.n && sameChart(previousChart, c, rangeRead) &&
|
|
569
585
|
sameBars(last, b, last.n - 1);
|
|
570
586
|
previousChart = c;
|
|
571
587
|
let out;
|
|
@@ -579,6 +595,7 @@ export function createScriptRun(body, inputs = {}, chart = {}, options = {}) {
|
|
|
579
595
|
try {
|
|
580
596
|
result = runBody(body, b, inputs, c, options, inc);
|
|
581
597
|
result.result.logs = resumedLogs(lastLogs, result.result.logs, from);
|
|
598
|
+
result.usesVisibleRange ||= rangeRead;
|
|
582
599
|
}
|
|
583
600
|
catch (err) {
|
|
584
601
|
// the memory is changed in place by a resumed run: an error leaves it unusable
|
|
@@ -591,6 +608,7 @@ export function createScriptRun(body, inputs = {}, chart = {}, options = {}) {
|
|
|
591
608
|
if (result && verifyLeft > 0) {
|
|
592
609
|
verifyLeft--;
|
|
593
610
|
const check = runBody(body, b, inputs, c, options, null);
|
|
611
|
+
check.usesVisibleRange = result.usesVisibleRange;
|
|
594
612
|
if (!sameResult(result, check)) {
|
|
595
613
|
reason = 'a resumed update differs from a full run (state kept outside persist()?)';
|
|
596
614
|
result = check;
|
|
@@ -607,6 +625,7 @@ export function createScriptRun(body, inputs = {}, chart = {}, options = {}) {
|
|
|
607
625
|
}
|
|
608
626
|
previous = out.incremental && last ? copyBars(b, last, last.n - 1) : copyBars(b);
|
|
609
627
|
lastLogs = out.result.logs;
|
|
628
|
+
rangeRead = out.usesVisibleRange;
|
|
610
629
|
return out;
|
|
611
630
|
},
|
|
612
631
|
};
|
|
@@ -1190,7 +1209,7 @@ export function fixnan(source) {
|
|
|
1190
1209
|
export function na(x) {
|
|
1191
1210
|
if (x instanceof Series)
|
|
1192
1211
|
return Series.fromArray(ctxBars, x.toArray().map((v) => (isNA(v) ? 1 : 0)));
|
|
1193
|
-
return isNA(x);
|
|
1212
|
+
return x === '' || isNA(x);
|
|
1194
1213
|
}
|
|
1195
1214
|
export function nz(source, replacement = 0) {
|
|
1196
1215
|
if (source instanceof Series) {
|
|
@@ -2245,6 +2264,13 @@ for (const name of [...STRATEGY_NUMBER_VARIABLES, ...STRATEGY_TEXT_VARIABLES]) {
|
|
|
2245
2264
|
* ```
|
|
2246
2265
|
*/
|
|
2247
2266
|
export const strategy = Object.defineProperties(Object.assign((title, options) => declareStrategy(title, options), strategyMembers), variableGetters);
|
|
2267
|
+
/** The symbol fields a {@link SymbolInfo} gives to `syminfo.*` inside `request.security`. */
|
|
2268
|
+
const SYMBOL_INFO_KEYS = [
|
|
2269
|
+
'timezone', 'session', 'regularSession', 'mintick', 'minmove', 'pricescale', 'pointvalue', 'mincontract', 'symbolType',
|
|
2270
|
+
'currency', 'basecurrency', 'root', 'description', 'prefix', 'ticker', 'country', 'currentContract', 'expirationDate',
|
|
2271
|
+
'sector', 'industry', 'isin', 'volumetype', 'employees', 'shareholders', 'sharesOutstandingTotal',
|
|
2272
|
+
'sharesOutstandingFloat', 'recommendations', 'targetPrice',
|
|
2273
|
+
];
|
|
2248
2274
|
/**
|
|
2249
2275
|
* Thrown by `executeScript` when `request.security` asked for bars of another symbol that `ChartContext.bars` did not
|
|
2250
2276
|
* give (#179). `requests` lists them all, once each: load them, then run the script again.
|
|
@@ -2252,8 +2278,12 @@ export const strategy = Object.defineProperties(Object.assign((title, options) =
|
|
|
2252
2278
|
export class MissingSymbolDataError extends Error {
|
|
2253
2279
|
requests;
|
|
2254
2280
|
constructor(requests) {
|
|
2255
|
-
|
|
2256
|
-
|
|
2281
|
+
const bars = requests.filter((r) => !r.events);
|
|
2282
|
+
const events = requests.filter((r) => r.events);
|
|
2283
|
+
super([
|
|
2284
|
+
bars.length ? `request.security needs the bars of ${bars.map((r) => `${r.symbol} (${r.timeframe})`).join(', ')}: pass them with ChartContext.bars.` : '',
|
|
2285
|
+
events.length ? `The script needs the events of ${events.map((r) => r.symbol).join(', ')}: pass them with ChartContext.events.` : '',
|
|
2286
|
+
].filter(Boolean).join(' '));
|
|
2257
2287
|
this.requests = requests;
|
|
2258
2288
|
this.name = 'MissingSymbolDataError';
|
|
2259
2289
|
}
|
|
@@ -2335,14 +2365,68 @@ function symbolProperty(key) {
|
|
|
2335
2365
|
}
|
|
2336
2366
|
return value;
|
|
2337
2367
|
}
|
|
2338
|
-
/** A text value of the chart or the symbol that the host passes in the chart context. */
|
|
2368
|
+
/** A text value of the chart or the symbol that the host passes in the chart context; null (none) is "". */
|
|
2339
2369
|
function chartText(key, name) {
|
|
2340
2370
|
collector();
|
|
2341
2371
|
const value = chartCtx[key];
|
|
2342
2372
|
if (value === undefined) {
|
|
2343
2373
|
throw new Error(`${name} is not known: pass it as executeScript(body, bars, inputs, { ${key} }).`);
|
|
2344
2374
|
}
|
|
2345
|
-
|
|
2375
|
+
// PineScript's na text is "" (`na("")` is true)
|
|
2376
|
+
return value ?? '';
|
|
2377
|
+
}
|
|
2378
|
+
/** A number of the symbol that the host passes in the chart context; null (none) is na. */
|
|
2379
|
+
function symbolNumber(key, name) {
|
|
2380
|
+
collector();
|
|
2381
|
+
const value = chartCtx[key];
|
|
2382
|
+
if (value === undefined) {
|
|
2383
|
+
throw new Error(`${name} is not known: pass it as executeScript(body, bars, inputs, { ${key} }).`);
|
|
2384
|
+
}
|
|
2385
|
+
return value ?? NaN;
|
|
2386
|
+
}
|
|
2387
|
+
/** A value of the analysts' figures of the symbol (`recommendations`, `targetPrice`); null (none) is na. */
|
|
2388
|
+
function analystNumber(key, field, name) {
|
|
2389
|
+
collector();
|
|
2390
|
+
const group = chartCtx[key];
|
|
2391
|
+
if (group === undefined) {
|
|
2392
|
+
throw new Error(`${name} is not known: pass it as executeScript(body, bars, inputs, { ${key} }).`);
|
|
2393
|
+
}
|
|
2394
|
+
return group === null ? NaN : (group[field] ?? NaN);
|
|
2395
|
+
}
|
|
2396
|
+
/** The ticker id of the chart (or of the symbol of `request.security`); throws when not known. */
|
|
2397
|
+
function chartTickerid() {
|
|
2398
|
+
collector();
|
|
2399
|
+
if (!chartCtx.tickerid) {
|
|
2400
|
+
throw new Error('The chart symbol is not known: pass it as executeScript(body, bars, inputs, { tickerid }).');
|
|
2401
|
+
}
|
|
2402
|
+
return chartCtx.tickerid;
|
|
2403
|
+
}
|
|
2404
|
+
/** A ticker id cut at its prefix: ["NASDAQ", "AAPL"], ["", "AAPL"] without one. */
|
|
2405
|
+
function splitTickerid(tickerid) {
|
|
2406
|
+
const at = tickerid.indexOf(':');
|
|
2407
|
+
return at < 0 ? ['', tickerid] : [tickerid.slice(0, at), tickerid.slice(at + 1)];
|
|
2408
|
+
}
|
|
2409
|
+
/** Month codes of futures contracts (F = January ... Z = December). */
|
|
2410
|
+
const CONTRACT_SUFFIX = /^(.+?)[FGHJKMNQUVXZ](\d{4}|\d{2})$/;
|
|
2411
|
+
/** Root of a ticker: without the "1!" of a continuous future, or the month code and year of a future's contract. */
|
|
2412
|
+
function tickerRoot(ticker, symbolType) {
|
|
2413
|
+
const continuous = /^(.+?)\d+!$/.exec(ticker);
|
|
2414
|
+
if (continuous)
|
|
2415
|
+
return continuous[1];
|
|
2416
|
+
const contract = symbolType === 'futures' ? CONTRACT_SUFFIX.exec(ticker) : null;
|
|
2417
|
+
return contract ? contract[1] : ticker;
|
|
2418
|
+
}
|
|
2419
|
+
/** A visible range value: the chart context's, else the time of bar `index` (NaN without bars). */
|
|
2420
|
+
function visibleBarTime(value, index) {
|
|
2421
|
+
collector().usesVisibleRange = true;
|
|
2422
|
+
if (value !== undefined)
|
|
2423
|
+
return value;
|
|
2424
|
+
const bar = ctxBars.bars[index];
|
|
2425
|
+
return bar ? barTime(bar) : NaN;
|
|
2426
|
+
}
|
|
2427
|
+
function chartTypeIs(type) {
|
|
2428
|
+
collector();
|
|
2429
|
+
return (chartCtx.chartType ?? 'standard') === type;
|
|
2346
2430
|
}
|
|
2347
2431
|
/** `[minmove, pricescale]` of the chart symbol: from the chart context, else from its mintick. */
|
|
2348
2432
|
function symbolScale() {
|
|
@@ -2354,9 +2438,8 @@ function symbolScale() {
|
|
|
2354
2438
|
return [minmove ?? move, scale];
|
|
2355
2439
|
}
|
|
2356
2440
|
/**
|
|
2357
|
-
* PineScript `syminfo
|
|
2358
|
-
*
|
|
2359
|
-
* `syminfo.currency`, `syminfo.basecurrency`, `syminfo.root` and `syminfo.description`, from the chart context.
|
|
2441
|
+
* PineScript `syminfo.*`, from the chart context (inside `request.security`, the requested symbol's). A field the
|
|
2442
|
+
* symbol has none of is na ("" for a text); a field the host did not give throws.
|
|
2360
2443
|
*/
|
|
2361
2444
|
export const syminfo = {
|
|
2362
2445
|
get mintick() {
|
|
@@ -2378,11 +2461,19 @@ export const syminfo = {
|
|
|
2378
2461
|
return exchangeTimezone();
|
|
2379
2462
|
},
|
|
2380
2463
|
get tickerid() {
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
return chartCtx.
|
|
2464
|
+
return chartTickerid();
|
|
2465
|
+
},
|
|
2466
|
+
get main_tickerid() {
|
|
2467
|
+
const id = chartTickerid();
|
|
2468
|
+
return chartCtx.mainTickerid ?? id;
|
|
2469
|
+
},
|
|
2470
|
+
get ticker() {
|
|
2471
|
+
const id = chartTickerid();
|
|
2472
|
+
return chartCtx.ticker ?? splitTickerid(id)[1];
|
|
2473
|
+
},
|
|
2474
|
+
get prefix() {
|
|
2475
|
+
const id = chartTickerid();
|
|
2476
|
+
return chartCtx.prefix === undefined ? splitTickerid(id)[0] : (chartCtx.prefix ?? '');
|
|
2386
2477
|
},
|
|
2387
2478
|
get session() {
|
|
2388
2479
|
collector();
|
|
@@ -2398,11 +2489,85 @@ export const syminfo = {
|
|
|
2398
2489
|
return chartText('basecurrency', 'syminfo.basecurrency');
|
|
2399
2490
|
},
|
|
2400
2491
|
get root() {
|
|
2401
|
-
|
|
2492
|
+
if (chartCtx.root !== undefined)
|
|
2493
|
+
return chartText('root', 'syminfo.root');
|
|
2494
|
+
return tickerRoot(syminfo.ticker, chartCtx.symbolType);
|
|
2402
2495
|
},
|
|
2403
2496
|
get description() {
|
|
2404
2497
|
return chartText('description', 'syminfo.description');
|
|
2405
2498
|
},
|
|
2499
|
+
get country() {
|
|
2500
|
+
return chartText('country', 'syminfo.country');
|
|
2501
|
+
},
|
|
2502
|
+
get current_contract() {
|
|
2503
|
+
return chartText('currentContract', 'syminfo.current_contract');
|
|
2504
|
+
},
|
|
2505
|
+
get expiration_date() {
|
|
2506
|
+
return symbolNumber('expirationDate', 'syminfo.expiration_date');
|
|
2507
|
+
},
|
|
2508
|
+
get sector() {
|
|
2509
|
+
return chartText('sector', 'syminfo.sector');
|
|
2510
|
+
},
|
|
2511
|
+
get industry() {
|
|
2512
|
+
return chartText('industry', 'syminfo.industry');
|
|
2513
|
+
},
|
|
2514
|
+
get isin() {
|
|
2515
|
+
return chartText('isin', 'syminfo.isin');
|
|
2516
|
+
},
|
|
2517
|
+
get volumetype() {
|
|
2518
|
+
return chartText('volumetype', 'syminfo.volumetype');
|
|
2519
|
+
},
|
|
2520
|
+
get employees() {
|
|
2521
|
+
return symbolNumber('employees', 'syminfo.employees');
|
|
2522
|
+
},
|
|
2523
|
+
get shareholders() {
|
|
2524
|
+
return symbolNumber('shareholders', 'syminfo.shareholders');
|
|
2525
|
+
},
|
|
2526
|
+
get shares_outstanding_total() {
|
|
2527
|
+
return symbolNumber('sharesOutstandingTotal', 'syminfo.shares_outstanding_total');
|
|
2528
|
+
},
|
|
2529
|
+
get shares_outstanding_float() {
|
|
2530
|
+
return symbolNumber('sharesOutstandingFloat', 'syminfo.shares_outstanding_float');
|
|
2531
|
+
},
|
|
2532
|
+
get recommendations_buy() {
|
|
2533
|
+
return analystNumber('recommendations', 'buy', 'syminfo.recommendations_buy');
|
|
2534
|
+
},
|
|
2535
|
+
get recommendations_buy_strong() {
|
|
2536
|
+
return analystNumber('recommendations', 'buy_strong', 'syminfo.recommendations_buy_strong');
|
|
2537
|
+
},
|
|
2538
|
+
get recommendations_hold() {
|
|
2539
|
+
return analystNumber('recommendations', 'hold', 'syminfo.recommendations_hold');
|
|
2540
|
+
},
|
|
2541
|
+
get recommendations_sell() {
|
|
2542
|
+
return analystNumber('recommendations', 'sell', 'syminfo.recommendations_sell');
|
|
2543
|
+
},
|
|
2544
|
+
get recommendations_sell_strong() {
|
|
2545
|
+
return analystNumber('recommendations', 'sell_strong', 'syminfo.recommendations_sell_strong');
|
|
2546
|
+
},
|
|
2547
|
+
get recommendations_total() {
|
|
2548
|
+
return analystNumber('recommendations', 'total', 'syminfo.recommendations_total');
|
|
2549
|
+
},
|
|
2550
|
+
get recommendations_date() {
|
|
2551
|
+
return analystNumber('recommendations', 'date', 'syminfo.recommendations_date');
|
|
2552
|
+
},
|
|
2553
|
+
get target_price_average() {
|
|
2554
|
+
return analystNumber('targetPrice', 'average', 'syminfo.target_price_average');
|
|
2555
|
+
},
|
|
2556
|
+
get target_price_high() {
|
|
2557
|
+
return analystNumber('targetPrice', 'high', 'syminfo.target_price_high');
|
|
2558
|
+
},
|
|
2559
|
+
get target_price_low() {
|
|
2560
|
+
return analystNumber('targetPrice', 'low', 'syminfo.target_price_low');
|
|
2561
|
+
},
|
|
2562
|
+
get target_price_median() {
|
|
2563
|
+
return analystNumber('targetPrice', 'median', 'syminfo.target_price_median');
|
|
2564
|
+
},
|
|
2565
|
+
get target_price_estimates() {
|
|
2566
|
+
return analystNumber('targetPrice', 'estimates', 'syminfo.target_price_estimates');
|
|
2567
|
+
},
|
|
2568
|
+
get target_price_date() {
|
|
2569
|
+
return analystNumber('targetPrice', 'date', 'syminfo.target_price_date');
|
|
2570
|
+
},
|
|
2406
2571
|
};
|
|
2407
2572
|
function calendar(fn) {
|
|
2408
2573
|
return ((time, timezone) => {
|
|
@@ -2755,25 +2920,12 @@ function runExternal(symbol, heikin, tf, expression, ignoreInvalid, own) {
|
|
|
2755
2920
|
let result;
|
|
2756
2921
|
let values;
|
|
2757
2922
|
ctxBars.setAll(bars);
|
|
2758
|
-
// the expression sees that symbol: its ticker, timeframe, calendar and properties
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
session: info.session,
|
|
2765
|
-
regularSession: info.regularSession,
|
|
2766
|
-
mintick: info.mintick,
|
|
2767
|
-
minmove: info.minmove,
|
|
2768
|
-
pricescale: info.pricescale,
|
|
2769
|
-
pointvalue: info.pointvalue,
|
|
2770
|
-
mincontract: info.mincontract,
|
|
2771
|
-
symbolType: info.symbolType,
|
|
2772
|
-
currency: info.currency,
|
|
2773
|
-
basecurrency: info.basecurrency,
|
|
2774
|
-
root: info.root,
|
|
2775
|
-
description: info.description,
|
|
2776
|
-
};
|
|
2923
|
+
// the expression sees that symbol: its ticker, timeframe, calendar and properties (the chart's own symbol stays the
|
|
2924
|
+
// main one)
|
|
2925
|
+
const seen = { ...chartCtx, tickerid: symbol, timeframe: tf, mainTickerid: chartCtx.mainTickerid ?? chartCtx.tickerid };
|
|
2926
|
+
for (const key of SYMBOL_INFO_KEYS)
|
|
2927
|
+
seen[key] = info[key];
|
|
2928
|
+
chartCtx = seen;
|
|
2777
2929
|
try {
|
|
2778
2930
|
result = expression();
|
|
2779
2931
|
const parts = Array.isArray(result) ? result : [result];
|
|
@@ -2877,7 +3029,155 @@ function seedCall(...args) {
|
|
|
2877
3029
|
unsupportedSecurityArgs('request.seed', undefined, a[4]);
|
|
2878
3030
|
return seed(a[0], a[1], a[2], a[3]);
|
|
2879
3031
|
}
|
|
2880
|
-
|
|
3032
|
+
// ── request.dividends / request.splits / request.earnings (#211) ─────────────
|
|
3033
|
+
/** The events of `ticker` ("" or the chart's: the chart symbol); {} while the host has not given them (the run ends with
|
|
3034
|
+
* a MissingSymbolDataError). */
|
|
3035
|
+
function symbolEvents(ticker) {
|
|
3036
|
+
collector();
|
|
3037
|
+
const symbol = ticker === '' ? chartTickerid() : ticker;
|
|
3038
|
+
const events = chartCtx.events?.(symbol);
|
|
3039
|
+
if (events === undefined) {
|
|
3040
|
+
if (!missingSymbols.some((r) => r.events && r.symbol === symbol))
|
|
3041
|
+
missingSymbols.push({ symbol, timeframe: '', events: true });
|
|
3042
|
+
return {};
|
|
3043
|
+
}
|
|
3044
|
+
return events;
|
|
3045
|
+
}
|
|
3046
|
+
/** One value of each event; throws when no event has it (the host does not have it), na where one has none. */
|
|
3047
|
+
function eventField(list, field, name, ticker) {
|
|
3048
|
+
if (list.length && list.every((e) => e[field] === undefined)) {
|
|
3049
|
+
throw new Error(`${name}: the ${String(field)} values of ${ticker || 'the chart symbol'} are not known.`);
|
|
3050
|
+
}
|
|
3051
|
+
return list.map((e) => {
|
|
3052
|
+
const v = e[field];
|
|
3053
|
+
return typeof v === 'number' ? v : NaN;
|
|
3054
|
+
});
|
|
3055
|
+
}
|
|
3056
|
+
/** A Series of event values on the chart bars, placed by PineScript's rules (see `security/events`). */
|
|
3057
|
+
function eventSeries(times, values, gaps, lookahead) {
|
|
3058
|
+
incremental.nonIncremental('request.dividends / splits / earnings');
|
|
3059
|
+
const opens = ctxBars.bars.map(barTime);
|
|
3060
|
+
const tf = chartTimeframe().period;
|
|
3061
|
+
const span = timeframeCore.info(tf).isticks ? 0 : timeframeCore.in_seconds(tf) * 1000;
|
|
3062
|
+
const { bars } = sessionBars();
|
|
3063
|
+
// a bar without a session period closes where the next one opens
|
|
3064
|
+
const closes = opens.map((t, i) => {
|
|
3065
|
+
const c = bars.closeOf(t, tf);
|
|
3066
|
+
return Number.isFinite(c) ? c : (opens[i + 1] ?? t + span);
|
|
3067
|
+
});
|
|
3068
|
+
const order = times.map((_, i) => i).sort((a, b) => times[a] - times[b]);
|
|
3069
|
+
const events = order.map((i) => ({ time: times[i], value: values[i] }));
|
|
3070
|
+
return Series.fromArray(ctxBars, eventsOnBars(opens, closes, tf, events, { gaps: gaps === barmerge.gaps_on, lookahead: lookahead === barmerge.lookahead_on }));
|
|
3071
|
+
}
|
|
3072
|
+
/** PineScript `dividends.*`: the fields of `request.dividends` and the next announced dividend of the chart symbol. */
|
|
3073
|
+
export const dividends = {
|
|
3074
|
+
net: 'net',
|
|
3075
|
+
gross: 'gross',
|
|
3076
|
+
/** Amount of the next dividend (net), na when none is announced */
|
|
3077
|
+
get future_amount() {
|
|
3078
|
+
return nextDividend()?.net ?? NaN;
|
|
3079
|
+
},
|
|
3080
|
+
/** Ex-date of the next dividend (ms) */
|
|
3081
|
+
get future_ex_date() {
|
|
3082
|
+
return nextDividend()?.time ?? NaN;
|
|
3083
|
+
},
|
|
3084
|
+
/** Payment date of the next dividend (ms) */
|
|
3085
|
+
get future_pay_date() {
|
|
3086
|
+
return nextDividend()?.payDate ?? NaN;
|
|
3087
|
+
},
|
|
3088
|
+
};
|
|
3089
|
+
/** PineScript `splits.*`: the fields of `request.splits`. */
|
|
3090
|
+
export const splits = { numerator: 'numerator', denominator: 'denominator' };
|
|
3091
|
+
/** PineScript `earnings.*`: the fields of `request.earnings` and the next earnings release of the chart symbol. */
|
|
3092
|
+
export const earnings = {
|
|
3093
|
+
actual: 'actual',
|
|
3094
|
+
estimate: 'estimate',
|
|
3095
|
+
standardized: 'standardized',
|
|
3096
|
+
/** Estimated earnings per share of the next release */
|
|
3097
|
+
get future_eps() {
|
|
3098
|
+
return nextEarnings()?.estimate ?? NaN;
|
|
3099
|
+
},
|
|
3100
|
+
/** Estimated revenue of the next release */
|
|
3101
|
+
get future_revenue() {
|
|
3102
|
+
return nextEarnings()?.revenueEstimate ?? NaN;
|
|
3103
|
+
},
|
|
3104
|
+
/** Time of the next release (ms) */
|
|
3105
|
+
get future_time() {
|
|
3106
|
+
return nextEarnings()?.time ?? NaN;
|
|
3107
|
+
},
|
|
3108
|
+
/** End of the fiscal period of the next release (ms) */
|
|
3109
|
+
get future_period_end_time() {
|
|
3110
|
+
return nextEarnings()?.periodEnd ?? NaN;
|
|
3111
|
+
},
|
|
3112
|
+
};
|
|
3113
|
+
/** The first event after the current time of the run in the chart symbol's list, when the host gives the announced ones. */
|
|
3114
|
+
function nextEvent(kind) {
|
|
3115
|
+
const events = symbolEvents('');
|
|
3116
|
+
if (!Object.keys(events).length)
|
|
3117
|
+
return undefined;
|
|
3118
|
+
if (!events.future?.[kind])
|
|
3119
|
+
throw new Error(`${kind}.future_*: the announced ${kind} of ${chartTickerid()} are not known.`);
|
|
3120
|
+
return events[kind]?.find((e) => e.time > runNow);
|
|
3121
|
+
}
|
|
3122
|
+
const nextDividend = () => nextEvent('dividends');
|
|
3123
|
+
const nextEarnings = () => nextEvent('earnings');
|
|
3124
|
+
const DIVIDENDS_PARAMETERS = ['ticker', 'field', 'gaps', 'lookahead', 'ignore_invalid_symbol', 'currency'];
|
|
3125
|
+
const SPLITS_PARAMETERS = ['ticker', 'field', 'gaps', 'lookahead', 'ignore_invalid_symbol'];
|
|
3126
|
+
/**
|
|
3127
|
+
* PineScript `request.dividends(ticker, field, gaps, lookahead, ignore_invalid_symbol, currency)`: the dividend per
|
|
3128
|
+
* share (`dividends.net`, default, or `dividends.gross`) as declared, from the bar after its ex-date's day (see
|
|
3129
|
+
* `security/events`). na before the first dividend. The events come from `ChartContext.events`.
|
|
3130
|
+
*/
|
|
3131
|
+
function dividendsCall(...args) {
|
|
3132
|
+
const a = namedArguments('request.dividends', DIVIDENDS_PARAMETERS, args);
|
|
3133
|
+
unsupportedSecurityArgs('request.dividends', a[5], undefined);
|
|
3134
|
+
const ticker = a[0] ?? '';
|
|
3135
|
+
const field = a[1] ?? dividends.net;
|
|
3136
|
+
if (field !== dividends.net && field !== dividends.gross)
|
|
3137
|
+
throw new Error(`request.dividends(): unknown field "${field}": dividends.net or dividends.gross.`);
|
|
3138
|
+
const list = symbolEvents(ticker).dividends ?? [];
|
|
3139
|
+
const values = field === dividends.gross ? list.map((d) => d.gross ?? d.net) : eventField(list, 'net', 'request.dividends', ticker);
|
|
3140
|
+
return eventSeries(list.map((d) => d.time), values, a[2], a[3]);
|
|
3141
|
+
}
|
|
3142
|
+
/**
|
|
3143
|
+
* PineScript `request.splits(ticker, field, gaps, lookahead, ignore_invalid_symbol)`: the numerator
|
|
3144
|
+
* (`splits.numerator`) or the denominator (`splits.denominator`) of the last split, from the bar after its day.
|
|
3145
|
+
*/
|
|
3146
|
+
function splitsCall(...args) {
|
|
3147
|
+
const a = namedArguments('request.splits', SPLITS_PARAMETERS, args);
|
|
3148
|
+
const ticker = a[0] ?? '';
|
|
3149
|
+
const field = a[1];
|
|
3150
|
+
if (field !== splits.numerator && field !== splits.denominator) {
|
|
3151
|
+
throw new Error('request.splits(): the field is splits.numerator or splits.denominator.');
|
|
3152
|
+
}
|
|
3153
|
+
const list = symbolEvents(ticker).splits ?? [];
|
|
3154
|
+
return eventSeries(list.map((s) => s.time), eventField(list, field, 'request.splits', ticker), a[2], a[3]);
|
|
3155
|
+
}
|
|
3156
|
+
/**
|
|
3157
|
+
* PineScript `request.earnings(ticker, field, gaps, lookahead, ignore_invalid_symbol, currency)`: earnings per share of
|
|
3158
|
+
* the last release (`earnings.actual`, default, `earnings.estimate` or `earnings.standardized`), from the bar after the
|
|
3159
|
+
* period of the chart timeframe it was published in (the next day on a daily chart).
|
|
3160
|
+
*/
|
|
3161
|
+
function earningsCall(...args) {
|
|
3162
|
+
const a = namedArguments('request.earnings', DIVIDENDS_PARAMETERS, args);
|
|
3163
|
+
unsupportedSecurityArgs('request.earnings', a[5], undefined);
|
|
3164
|
+
const ticker = a[0] ?? '';
|
|
3165
|
+
const field = a[1] ?? earnings.actual;
|
|
3166
|
+
if (field !== earnings.actual && field !== earnings.estimate && field !== earnings.standardized) {
|
|
3167
|
+
throw new Error(`request.earnings(): unknown field "${field}": earnings.actual, earnings.estimate or earnings.standardized.`);
|
|
3168
|
+
}
|
|
3169
|
+
// announced releases still to come have no values yet
|
|
3170
|
+
const list = (symbolEvents(ticker).earnings ?? []).filter((e) => e.time <= runNow);
|
|
3171
|
+
return eventSeries(list.map((e) => e.time), eventField(list, field, 'request.earnings', ticker), a[2], a[3]);
|
|
3172
|
+
}
|
|
3173
|
+
export const request = strictNamespace('request', {
|
|
3174
|
+
security: securityCall,
|
|
3175
|
+
security_lower_tf: securityLowerTfCall,
|
|
3176
|
+
seed: seedCall,
|
|
3177
|
+
dividends: dividendsCall,
|
|
3178
|
+
splits: splitsCall,
|
|
3179
|
+
earnings: earningsCall,
|
|
3180
|
+
});
|
|
2881
3181
|
const loopBar = () => currentBar;
|
|
2882
3182
|
/** Wraps a drawing namespace and keeps its `all` getter (the live drawings in creation order). */
|
|
2883
3183
|
function drawings(name, members, all) {
|
|
@@ -2924,6 +3224,38 @@ export const chart = {
|
|
|
2924
3224
|
get bg_color() {
|
|
2925
3225
|
return chartText('backgroundColor', 'chart.bg_color');
|
|
2926
3226
|
},
|
|
3227
|
+
/** Time (ms) of the first bar in view (PineScript `chart.left_visible_bar_time`), from the chart context; without it,
|
|
3228
|
+
* of the first bar. */
|
|
3229
|
+
get left_visible_bar_time() {
|
|
3230
|
+
return visibleBarTime(chartCtx.leftVisibleBarTime, 0);
|
|
3231
|
+
},
|
|
3232
|
+
/** Time (ms) of the last bar in view (PineScript `chart.right_visible_bar_time`), from the chart context; without it,
|
|
3233
|
+
* of the last bar. */
|
|
3234
|
+
get right_visible_bar_time() {
|
|
3235
|
+
return visibleBarTime(chartCtx.rightVisibleBarTime, ctxBars.bars.length - 1);
|
|
3236
|
+
},
|
|
3237
|
+
/** The chart is not one of the types below (PineScript `chart.is_standard`). */
|
|
3238
|
+
get is_standard() {
|
|
3239
|
+
return chartTypeIs('standard');
|
|
3240
|
+
},
|
|
3241
|
+
get is_heikinashi() {
|
|
3242
|
+
return chartTypeIs('heikinashi');
|
|
3243
|
+
},
|
|
3244
|
+
get is_renko() {
|
|
3245
|
+
return chartTypeIs('renko');
|
|
3246
|
+
},
|
|
3247
|
+
get is_kagi() {
|
|
3248
|
+
return chartTypeIs('kagi');
|
|
3249
|
+
},
|
|
3250
|
+
get is_linebreak() {
|
|
3251
|
+
return chartTypeIs('linebreak');
|
|
3252
|
+
},
|
|
3253
|
+
get is_pnf() {
|
|
3254
|
+
return chartTypeIs('pnf');
|
|
3255
|
+
},
|
|
3256
|
+
get is_range() {
|
|
3257
|
+
return chartTypeIs('range');
|
|
3258
|
+
},
|
|
2927
3259
|
point: {
|
|
2928
3260
|
...chartPointCore,
|
|
2929
3261
|
now(price) {
|