oakscriptjs 0.20.0 → 0.22.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/check/spec.d.ts +1 -1
- package/dist/cjs/check/spec.d.ts.map +1 -1
- package/dist/cjs/check/spec.js +1 -1
- package/dist/cjs/check/spec.js.map +2 -2
- package/dist/cjs/runtime/types.d.ts +9 -4
- package/dist/cjs/runtime/types.d.ts.map +1 -1
- package/dist/cjs/runtime/types.js.map +1 -1
- package/dist/cjs/script/globals.d.ts +4 -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 +237 -11
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +354 -32
- 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/check/spec.d.ts +1 -1
- package/dist/esm/check/spec.d.ts.map +1 -1
- package/dist/esm/check/spec.js +1 -1
- package/dist/esm/check/spec.js.map +1 -1
- package/dist/esm/runtime/types.d.ts +9 -4
- package/dist/esm/runtime/types.d.ts.map +1 -1
- package/dist/esm/script/globals.d.ts +4 -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 +237 -11
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +381 -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/package.json +1 -1
- package/skills/oakscript/reference/script-structure.md +12 -2
- package/skills/oakscript/reference/timeframes-symbols.md +27 -0
- package/skills/oakscript/spec.schema.json +2 -1
- package/src/check/harness.ts +27 -0
- package/src/check/spec.ts +1 -1
- package/src/runtime/types.ts +9 -4
- package/src/script/globals.ts +4 -0
- package/src/script/index.ts +581 -46
- package/src/security/events.ts +85 -0
package/dist/cjs/script/index.js
CHANGED
|
@@ -53,8 +53,11 @@ __export(script_exports, {
|
|
|
53
53
|
currency: () => currency,
|
|
54
54
|
dayofmonth: () => dayofmonth,
|
|
55
55
|
dayofweek: () => dayofweek,
|
|
56
|
+
defineEnum: () => defineEnum,
|
|
56
57
|
display: () => display,
|
|
58
|
+
dividends: () => dividends,
|
|
57
59
|
eachBar: () => eachBar,
|
|
60
|
+
earnings: () => earnings,
|
|
58
61
|
executeIntrabar: () => executeIntrabar,
|
|
59
62
|
executeScript: () => executeScript,
|
|
60
63
|
extend: () => extend,
|
|
@@ -106,6 +109,7 @@ __export(script_exports, {
|
|
|
106
109
|
setLogLineResolver: () => setLogLineResolver,
|
|
107
110
|
shape: () => shape,
|
|
108
111
|
size: () => size,
|
|
112
|
+
splits: () => splits,
|
|
109
113
|
str: () => str,
|
|
110
114
|
strategy: () => strategy,
|
|
111
115
|
syminfo: () => syminfo,
|
|
@@ -160,6 +164,7 @@ var import_calendar = require("../session/calendar.js");
|
|
|
160
164
|
var import_bars = require("../session/bars.js");
|
|
161
165
|
var import_resample = require("../security/resample.js");
|
|
162
166
|
var import_other = require("../security/other.js");
|
|
167
|
+
var import_events = require("../security/events.js");
|
|
163
168
|
var import_types = require("../types/index.js");
|
|
164
169
|
var import_strategy = require("../strategy/index.js");
|
|
165
170
|
var import_context = require("../session/context.js");
|
|
@@ -297,7 +302,12 @@ function runBody(body, barsInput, inputs, chart2, options, inc) {
|
|
|
297
302
|
mathCore.resetRandom();
|
|
298
303
|
missingSymbols = [];
|
|
299
304
|
try {
|
|
300
|
-
|
|
305
|
+
try {
|
|
306
|
+
body();
|
|
307
|
+
} catch (err) {
|
|
308
|
+
if (missingSymbols.length && !(err instanceof MissingSymbolDataError)) throw new MissingSymbolDataError(missingSymbols);
|
|
309
|
+
throw err;
|
|
310
|
+
}
|
|
301
311
|
if (missingSymbols.length) throw new MissingSymbolDataError(missingSymbols);
|
|
302
312
|
if (inc) incremental.bodyDone();
|
|
303
313
|
const c = ctx;
|
|
@@ -707,6 +717,34 @@ function declare(c, kind) {
|
|
|
707
717
|
}
|
|
708
718
|
c.declaration = kind;
|
|
709
719
|
}
|
|
720
|
+
class EnumFieldValue {
|
|
721
|
+
constructor(name, title) {
|
|
722
|
+
this.name = name;
|
|
723
|
+
this.title = title;
|
|
724
|
+
Object.freeze(this);
|
|
725
|
+
}
|
|
726
|
+
name;
|
|
727
|
+
title;
|
|
728
|
+
toString() {
|
|
729
|
+
return this.title;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
const enumFieldsOf = /* @__PURE__ */ new WeakMap();
|
|
733
|
+
function defineEnum(fields) {
|
|
734
|
+
if (!(0, import_drawing_args.isPlainObject)(fields) || Object.keys(fields).length === 0) {
|
|
735
|
+
throw new TypeError('defineEnum(): the fields are an object of names and titles, e.g. { sma: "Simple", ema: "Exponential" }.');
|
|
736
|
+
}
|
|
737
|
+
const list = [];
|
|
738
|
+
const out = {};
|
|
739
|
+
for (const [name, title] of Object.entries(fields)) {
|
|
740
|
+
if (title !== void 0 && typeof title !== "string") throw new TypeError(`defineEnum(): the title of "${name}" is a string.`);
|
|
741
|
+
const field = new EnumFieldValue(name, title || name);
|
|
742
|
+
list.push(field);
|
|
743
|
+
out[name] = field;
|
|
744
|
+
}
|
|
745
|
+
for (const field of list) enumFieldsOf.set(field, list);
|
|
746
|
+
return Object.freeze(out);
|
|
747
|
+
}
|
|
710
748
|
function common(opts) {
|
|
711
749
|
const out = {};
|
|
712
750
|
if (opts.group !== void 0) out.group = opts.group;
|
|
@@ -727,15 +765,16 @@ function registerInput(config, fallbackId) {
|
|
|
727
765
|
}
|
|
728
766
|
function registerNumeric(type, defval, title, opts = {}) {
|
|
729
767
|
const v = registerInput(
|
|
730
|
-
{ type, defval, title, min: opts.minval, max: opts.maxval, step: opts.step, ...common(opts) },
|
|
768
|
+
{ type, defval, title, min: opts.minval, max: opts.maxval, step: opts.step, ...opts.options ? { options: opts.options } : {}, ...common(opts) },
|
|
731
769
|
`input_${type}`
|
|
732
770
|
);
|
|
733
771
|
return typeof v === "number" ? v : defval;
|
|
734
772
|
}
|
|
735
773
|
const COMMON_INPUT_KEYS = ["tooltip", "inline", "group", "confirm", "display", "active"];
|
|
736
774
|
const INPUT_KEYS = {
|
|
737
|
-
int: ["defval", "title", "minval", "maxval", "step", ...COMMON_INPUT_KEYS],
|
|
738
|
-
float: ["defval", "title", "minval", "maxval", "step", ...COMMON_INPUT_KEYS],
|
|
775
|
+
int: ["defval", "title", "minval", "maxval", "step", "options", ...COMMON_INPUT_KEYS],
|
|
776
|
+
float: ["defval", "title", "minval", "maxval", "step", "options", ...COMMON_INPUT_KEYS],
|
|
777
|
+
enum: ["defval", "title", "options", ...COMMON_INPUT_KEYS],
|
|
739
778
|
price: ["defval", "title", ...COMMON_INPUT_KEYS],
|
|
740
779
|
bool: ["defval", "title", ...COMMON_INPUT_KEYS],
|
|
741
780
|
string: ["defval", "title", "options", ...COMMON_INPUT_KEYS],
|
|
@@ -832,6 +871,32 @@ const input = strictNamespace("input", {
|
|
|
832
871
|
const a = inputArgs("price", defval, title, opts);
|
|
833
872
|
return registerNumeric("float", a.defval, a.title, a.opts);
|
|
834
873
|
},
|
|
874
|
+
/**
|
|
875
|
+
* A field of an enum made with `defineEnum()`: `input.enum(MaType.sma, "Average")`. The host lists the fields by
|
|
876
|
+
* title (`options`: only these fields) and stores the field name; the call returns the field.
|
|
877
|
+
*/
|
|
878
|
+
enum(defval, title, opts) {
|
|
879
|
+
const a = inputArgs("enum", defval, title, opts);
|
|
880
|
+
const fields = a.defval === void 0 ? void 0 : enumFieldsOf.get(a.defval);
|
|
881
|
+
if (!fields) throw new TypeError('input.enum(): the default is a field of an enum made with defineEnum(), e.g. input.enum(MaType.sma, "Average").');
|
|
882
|
+
const choices = a.opts.options ?? fields;
|
|
883
|
+
for (const f of choices) {
|
|
884
|
+
if (!fields.includes(f)) throw new TypeError("input.enum(): the options are fields of the enum of the default.");
|
|
885
|
+
}
|
|
886
|
+
if (!choices.includes(a.defval)) throw new TypeError("input.enum(): the default is one of the options.");
|
|
887
|
+
const name = registerInput(
|
|
888
|
+
{
|
|
889
|
+
type: "enum",
|
|
890
|
+
defval: a.defval.name,
|
|
891
|
+
title: a.title,
|
|
892
|
+
options: choices.map((f) => f.name),
|
|
893
|
+
optionsTitles: Object.fromEntries(choices.map((f) => [f.name, f.title])),
|
|
894
|
+
...common(a.opts)
|
|
895
|
+
},
|
|
896
|
+
"input_enum"
|
|
897
|
+
);
|
|
898
|
+
return choices.find((f) => f.name === name) ?? a.defval;
|
|
899
|
+
},
|
|
835
900
|
/** Date and time as a UNIX time in milliseconds, e.g. `input.time(timestamp('2024-01-01'), 'Start')`. */
|
|
836
901
|
time(defval, title, opts) {
|
|
837
902
|
const a = inputArgs("time", defval, title, opts);
|
|
@@ -1103,7 +1168,7 @@ function fixnan(source) {
|
|
|
1103
1168
|
}
|
|
1104
1169
|
function na(x) {
|
|
1105
1170
|
if (x instanceof import_series.Series) return import_series.Series.fromArray(ctxBars, x.toArray().map((v) => (0, import_utils.isNA)(v) ? 1 : 0));
|
|
1106
|
-
return (0, import_utils.isNA)(x);
|
|
1171
|
+
return x === "" || (0, import_utils.isNA)(x);
|
|
1107
1172
|
}
|
|
1108
1173
|
function nz(source, replacement = 0) {
|
|
1109
1174
|
if (source instanceof import_series.Series) {
|
|
@@ -1949,10 +2014,45 @@ const strategy = Object.defineProperties(
|
|
|
1949
2014
|
Object.assign((title, options) => declareStrategy(title, options), strategyMembers),
|
|
1950
2015
|
variableGetters
|
|
1951
2016
|
);
|
|
2017
|
+
const SYMBOL_INFO_KEYS = [
|
|
2018
|
+
"timezone",
|
|
2019
|
+
"session",
|
|
2020
|
+
"regularSession",
|
|
2021
|
+
"mintick",
|
|
2022
|
+
"minmove",
|
|
2023
|
+
"pricescale",
|
|
2024
|
+
"pointvalue",
|
|
2025
|
+
"mincontract",
|
|
2026
|
+
"symbolType",
|
|
2027
|
+
"currency",
|
|
2028
|
+
"basecurrency",
|
|
2029
|
+
"root",
|
|
2030
|
+
"description",
|
|
2031
|
+
"prefix",
|
|
2032
|
+
"ticker",
|
|
2033
|
+
"country",
|
|
2034
|
+
"currentContract",
|
|
2035
|
+
"expirationDate",
|
|
2036
|
+
"sector",
|
|
2037
|
+
"industry",
|
|
2038
|
+
"isin",
|
|
2039
|
+
"volumetype",
|
|
2040
|
+
"employees",
|
|
2041
|
+
"shareholders",
|
|
2042
|
+
"sharesOutstandingTotal",
|
|
2043
|
+
"sharesOutstandingFloat",
|
|
2044
|
+
"recommendations",
|
|
2045
|
+
"targetPrice"
|
|
2046
|
+
];
|
|
1952
2047
|
class MissingSymbolDataError extends Error {
|
|
1953
2048
|
constructor(requests) {
|
|
2049
|
+
const bars2 = requests.filter((r) => !r.events);
|
|
2050
|
+
const events = requests.filter((r) => r.events);
|
|
1954
2051
|
super(
|
|
1955
|
-
|
|
2052
|
+
[
|
|
2053
|
+
bars2.length ? `request.security needs the bars of ${bars2.map((r) => `${r.symbol} (${r.timeframe})`).join(", ")}: pass them with ChartContext.bars.` : "",
|
|
2054
|
+
events.length ? `The script needs the events of ${events.map((r) => r.symbol).join(", ")}: pass them with ChartContext.events.` : ""
|
|
2055
|
+
].filter(Boolean).join(" ")
|
|
1956
2056
|
);
|
|
1957
2057
|
this.requests = requests;
|
|
1958
2058
|
this.name = "MissingSymbolDataError";
|
|
@@ -2036,7 +2136,41 @@ function chartText(key, name) {
|
|
|
2036
2136
|
if (value === void 0) {
|
|
2037
2137
|
throw new Error(`${name} is not known: pass it as executeScript(body, bars, inputs, { ${key} }).`);
|
|
2038
2138
|
}
|
|
2039
|
-
return value;
|
|
2139
|
+
return value ?? "";
|
|
2140
|
+
}
|
|
2141
|
+
function symbolNumber(key, name) {
|
|
2142
|
+
collector();
|
|
2143
|
+
const value = chartCtx[key];
|
|
2144
|
+
if (value === void 0) {
|
|
2145
|
+
throw new Error(`${name} is not known: pass it as executeScript(body, bars, inputs, { ${key} }).`);
|
|
2146
|
+
}
|
|
2147
|
+
return value ?? NaN;
|
|
2148
|
+
}
|
|
2149
|
+
function analystNumber(key, field, name) {
|
|
2150
|
+
collector();
|
|
2151
|
+
const group = chartCtx[key];
|
|
2152
|
+
if (group === void 0) {
|
|
2153
|
+
throw new Error(`${name} is not known: pass it as executeScript(body, bars, inputs, { ${key} }).`);
|
|
2154
|
+
}
|
|
2155
|
+
return group === null ? NaN : group[field] ?? NaN;
|
|
2156
|
+
}
|
|
2157
|
+
function chartTickerid() {
|
|
2158
|
+
collector();
|
|
2159
|
+
if (!chartCtx.tickerid) {
|
|
2160
|
+
throw new Error("The chart symbol is not known: pass it as executeScript(body, bars, inputs, { tickerid }).");
|
|
2161
|
+
}
|
|
2162
|
+
return chartCtx.tickerid;
|
|
2163
|
+
}
|
|
2164
|
+
function splitTickerid(tickerid) {
|
|
2165
|
+
const at2 = tickerid.indexOf(":");
|
|
2166
|
+
return at2 < 0 ? ["", tickerid] : [tickerid.slice(0, at2), tickerid.slice(at2 + 1)];
|
|
2167
|
+
}
|
|
2168
|
+
const CONTRACT_SUFFIX = /^(.+?)[FGHJKMNQUVXZ](\d{4}|\d{2})$/;
|
|
2169
|
+
function tickerRoot(ticker2, symbolType) {
|
|
2170
|
+
const continuous = /^(.+?)\d+!$/.exec(ticker2);
|
|
2171
|
+
if (continuous) return continuous[1];
|
|
2172
|
+
const contract = symbolType === "futures" ? CONTRACT_SUFFIX.exec(ticker2) : null;
|
|
2173
|
+
return contract ? contract[1] : ticker2;
|
|
2040
2174
|
}
|
|
2041
2175
|
function visibleBarTime(value, index) {
|
|
2042
2176
|
collector().usesVisibleRange = true;
|
|
@@ -2075,11 +2209,19 @@ const syminfo = {
|
|
|
2075
2209
|
return exchangeTimezone();
|
|
2076
2210
|
},
|
|
2077
2211
|
get tickerid() {
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
return chartCtx.
|
|
2212
|
+
return chartTickerid();
|
|
2213
|
+
},
|
|
2214
|
+
get main_tickerid() {
|
|
2215
|
+
const id = chartTickerid();
|
|
2216
|
+
return chartCtx.mainTickerid ?? id;
|
|
2217
|
+
},
|
|
2218
|
+
get ticker() {
|
|
2219
|
+
const id = chartTickerid();
|
|
2220
|
+
return chartCtx.ticker ?? splitTickerid(id)[1];
|
|
2221
|
+
},
|
|
2222
|
+
get prefix() {
|
|
2223
|
+
const id = chartTickerid();
|
|
2224
|
+
return chartCtx.prefix === void 0 ? splitTickerid(id)[0] : chartCtx.prefix ?? "";
|
|
2083
2225
|
},
|
|
2084
2226
|
get session() {
|
|
2085
2227
|
collector();
|
|
@@ -2095,10 +2237,83 @@ const syminfo = {
|
|
|
2095
2237
|
return chartText("basecurrency", "syminfo.basecurrency");
|
|
2096
2238
|
},
|
|
2097
2239
|
get root() {
|
|
2098
|
-
return chartText("root", "syminfo.root");
|
|
2240
|
+
if (chartCtx.root !== void 0) return chartText("root", "syminfo.root");
|
|
2241
|
+
return tickerRoot(syminfo.ticker, chartCtx.symbolType);
|
|
2099
2242
|
},
|
|
2100
2243
|
get description() {
|
|
2101
2244
|
return chartText("description", "syminfo.description");
|
|
2245
|
+
},
|
|
2246
|
+
get country() {
|
|
2247
|
+
return chartText("country", "syminfo.country");
|
|
2248
|
+
},
|
|
2249
|
+
get current_contract() {
|
|
2250
|
+
return chartText("currentContract", "syminfo.current_contract");
|
|
2251
|
+
},
|
|
2252
|
+
get expiration_date() {
|
|
2253
|
+
return symbolNumber("expirationDate", "syminfo.expiration_date");
|
|
2254
|
+
},
|
|
2255
|
+
get sector() {
|
|
2256
|
+
return chartText("sector", "syminfo.sector");
|
|
2257
|
+
},
|
|
2258
|
+
get industry() {
|
|
2259
|
+
return chartText("industry", "syminfo.industry");
|
|
2260
|
+
},
|
|
2261
|
+
get isin() {
|
|
2262
|
+
return chartText("isin", "syminfo.isin");
|
|
2263
|
+
},
|
|
2264
|
+
get volumetype() {
|
|
2265
|
+
return chartText("volumetype", "syminfo.volumetype");
|
|
2266
|
+
},
|
|
2267
|
+
get employees() {
|
|
2268
|
+
return symbolNumber("employees", "syminfo.employees");
|
|
2269
|
+
},
|
|
2270
|
+
get shareholders() {
|
|
2271
|
+
return symbolNumber("shareholders", "syminfo.shareholders");
|
|
2272
|
+
},
|
|
2273
|
+
get shares_outstanding_total() {
|
|
2274
|
+
return symbolNumber("sharesOutstandingTotal", "syminfo.shares_outstanding_total");
|
|
2275
|
+
},
|
|
2276
|
+
get shares_outstanding_float() {
|
|
2277
|
+
return symbolNumber("sharesOutstandingFloat", "syminfo.shares_outstanding_float");
|
|
2278
|
+
},
|
|
2279
|
+
get recommendations_buy() {
|
|
2280
|
+
return analystNumber("recommendations", "buy", "syminfo.recommendations_buy");
|
|
2281
|
+
},
|
|
2282
|
+
get recommendations_buy_strong() {
|
|
2283
|
+
return analystNumber("recommendations", "buy_strong", "syminfo.recommendations_buy_strong");
|
|
2284
|
+
},
|
|
2285
|
+
get recommendations_hold() {
|
|
2286
|
+
return analystNumber("recommendations", "hold", "syminfo.recommendations_hold");
|
|
2287
|
+
},
|
|
2288
|
+
get recommendations_sell() {
|
|
2289
|
+
return analystNumber("recommendations", "sell", "syminfo.recommendations_sell");
|
|
2290
|
+
},
|
|
2291
|
+
get recommendations_sell_strong() {
|
|
2292
|
+
return analystNumber("recommendations", "sell_strong", "syminfo.recommendations_sell_strong");
|
|
2293
|
+
},
|
|
2294
|
+
get recommendations_total() {
|
|
2295
|
+
return analystNumber("recommendations", "total", "syminfo.recommendations_total");
|
|
2296
|
+
},
|
|
2297
|
+
get recommendations_date() {
|
|
2298
|
+
return analystNumber("recommendations", "date", "syminfo.recommendations_date");
|
|
2299
|
+
},
|
|
2300
|
+
get target_price_average() {
|
|
2301
|
+
return analystNumber("targetPrice", "average", "syminfo.target_price_average");
|
|
2302
|
+
},
|
|
2303
|
+
get target_price_high() {
|
|
2304
|
+
return analystNumber("targetPrice", "high", "syminfo.target_price_high");
|
|
2305
|
+
},
|
|
2306
|
+
get target_price_low() {
|
|
2307
|
+
return analystNumber("targetPrice", "low", "syminfo.target_price_low");
|
|
2308
|
+
},
|
|
2309
|
+
get target_price_median() {
|
|
2310
|
+
return analystNumber("targetPrice", "median", "syminfo.target_price_median");
|
|
2311
|
+
},
|
|
2312
|
+
get target_price_estimates() {
|
|
2313
|
+
return analystNumber("targetPrice", "estimates", "syminfo.target_price_estimates");
|
|
2314
|
+
},
|
|
2315
|
+
get target_price_date() {
|
|
2316
|
+
return analystNumber("targetPrice", "date", "syminfo.target_price_date");
|
|
2102
2317
|
}
|
|
2103
2318
|
};
|
|
2104
2319
|
function calendar(fn) {
|
|
@@ -2390,24 +2605,9 @@ function runExternal(symbol, heikin, tf, expression, ignoreInvalid, own) {
|
|
|
2390
2605
|
let result;
|
|
2391
2606
|
let values;
|
|
2392
2607
|
ctxBars.setAll(bars2);
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
timeframe: tf,
|
|
2397
|
-
timezone: info.timezone,
|
|
2398
|
-
session: info.session,
|
|
2399
|
-
regularSession: info.regularSession,
|
|
2400
|
-
mintick: info.mintick,
|
|
2401
|
-
minmove: info.minmove,
|
|
2402
|
-
pricescale: info.pricescale,
|
|
2403
|
-
pointvalue: info.pointvalue,
|
|
2404
|
-
mincontract: info.mincontract,
|
|
2405
|
-
symbolType: info.symbolType,
|
|
2406
|
-
currency: info.currency,
|
|
2407
|
-
basecurrency: info.basecurrency,
|
|
2408
|
-
root: info.root,
|
|
2409
|
-
description: info.description
|
|
2410
|
-
};
|
|
2608
|
+
const seen = { ...chartCtx, tickerid: symbol, timeframe: tf, mainTickerid: chartCtx.mainTickerid ?? chartCtx.tickerid };
|
|
2609
|
+
for (const key of SYMBOL_INFO_KEYS) seen[key] = info[key];
|
|
2610
|
+
chartCtx = seen;
|
|
2411
2611
|
try {
|
|
2412
2612
|
result = expression();
|
|
2413
2613
|
const parts = Array.isArray(result) ? result : [result];
|
|
@@ -2484,7 +2684,129 @@ function seedCall(...args) {
|
|
|
2484
2684
|
unsupportedSecurityArgs("request.seed", void 0, a[4]);
|
|
2485
2685
|
return seed(a[0], a[1], a[2], a[3]);
|
|
2486
2686
|
}
|
|
2487
|
-
|
|
2687
|
+
function symbolEvents(ticker2) {
|
|
2688
|
+
collector();
|
|
2689
|
+
const symbol = ticker2 === "" ? chartTickerid() : ticker2;
|
|
2690
|
+
const events = chartCtx.events?.(symbol);
|
|
2691
|
+
if (events === void 0) {
|
|
2692
|
+
if (!missingSymbols.some((r) => r.events && r.symbol === symbol)) missingSymbols.push({ symbol, timeframe: "", events: true });
|
|
2693
|
+
return {};
|
|
2694
|
+
}
|
|
2695
|
+
return events;
|
|
2696
|
+
}
|
|
2697
|
+
function eventField(list, field, name, ticker2) {
|
|
2698
|
+
if (list.length && list.every((e) => e[field] === void 0)) {
|
|
2699
|
+
throw new Error(`${name}: the ${String(field)} values of ${ticker2 || "the chart symbol"} are not known.`);
|
|
2700
|
+
}
|
|
2701
|
+
return list.map((e) => {
|
|
2702
|
+
const v = e[field];
|
|
2703
|
+
return typeof v === "number" ? v : NaN;
|
|
2704
|
+
});
|
|
2705
|
+
}
|
|
2706
|
+
function eventSeries(times, values, gaps, lookahead) {
|
|
2707
|
+
incremental.nonIncremental("request.dividends / splits / earnings");
|
|
2708
|
+
const opens = ctxBars.bars.map(barTime);
|
|
2709
|
+
const tf = chartTimeframe().period;
|
|
2710
|
+
const span = timeframeCore.info(tf).isticks ? 0 : timeframeCore.in_seconds(tf) * 1e3;
|
|
2711
|
+
const { bars: bars2 } = sessionBars();
|
|
2712
|
+
const closes = opens.map((t, i) => {
|
|
2713
|
+
const c = bars2.closeOf(t, tf);
|
|
2714
|
+
return Number.isFinite(c) ? c : opens[i + 1] ?? t + span;
|
|
2715
|
+
});
|
|
2716
|
+
const order2 = times.map((_, i) => i).sort((a, b) => times[a] - times[b]);
|
|
2717
|
+
const events = order2.map((i) => ({ time: times[i], value: values[i] }));
|
|
2718
|
+
return import_series.Series.fromArray(
|
|
2719
|
+
ctxBars,
|
|
2720
|
+
(0, import_events.eventsOnBars)(opens, closes, tf, events, { gaps: gaps === barmerge.gaps_on, lookahead: lookahead === barmerge.lookahead_on })
|
|
2721
|
+
);
|
|
2722
|
+
}
|
|
2723
|
+
const dividends = {
|
|
2724
|
+
net: "net",
|
|
2725
|
+
gross: "gross",
|
|
2726
|
+
/** Amount of the next dividend (net), na when none is announced */
|
|
2727
|
+
get future_amount() {
|
|
2728
|
+
return nextDividend()?.net ?? NaN;
|
|
2729
|
+
},
|
|
2730
|
+
/** Ex-date of the next dividend (ms) */
|
|
2731
|
+
get future_ex_date() {
|
|
2732
|
+
return nextDividend()?.time ?? NaN;
|
|
2733
|
+
},
|
|
2734
|
+
/** Payment date of the next dividend (ms) */
|
|
2735
|
+
get future_pay_date() {
|
|
2736
|
+
return nextDividend()?.payDate ?? NaN;
|
|
2737
|
+
}
|
|
2738
|
+
};
|
|
2739
|
+
const splits = { numerator: "numerator", denominator: "denominator" };
|
|
2740
|
+
const earnings = {
|
|
2741
|
+
actual: "actual",
|
|
2742
|
+
estimate: "estimate",
|
|
2743
|
+
standardized: "standardized",
|
|
2744
|
+
/** Estimated earnings per share of the next release */
|
|
2745
|
+
get future_eps() {
|
|
2746
|
+
return nextEarnings()?.estimate ?? NaN;
|
|
2747
|
+
},
|
|
2748
|
+
/** Estimated revenue of the next release */
|
|
2749
|
+
get future_revenue() {
|
|
2750
|
+
return nextEarnings()?.revenueEstimate ?? NaN;
|
|
2751
|
+
},
|
|
2752
|
+
/** Time of the next release (ms) */
|
|
2753
|
+
get future_time() {
|
|
2754
|
+
return nextEarnings()?.time ?? NaN;
|
|
2755
|
+
},
|
|
2756
|
+
/** End of the fiscal period of the next release (ms) */
|
|
2757
|
+
get future_period_end_time() {
|
|
2758
|
+
return nextEarnings()?.periodEnd ?? NaN;
|
|
2759
|
+
}
|
|
2760
|
+
};
|
|
2761
|
+
function nextEvent(kind) {
|
|
2762
|
+
const events = symbolEvents("");
|
|
2763
|
+
if (!Object.keys(events).length) return void 0;
|
|
2764
|
+
if (!events.future?.[kind]) throw new Error(`${kind}.future_*: the announced ${kind} of ${chartTickerid()} are not known.`);
|
|
2765
|
+
return events[kind]?.find((e) => e.time > runNow);
|
|
2766
|
+
}
|
|
2767
|
+
const nextDividend = () => nextEvent("dividends");
|
|
2768
|
+
const nextEarnings = () => nextEvent("earnings");
|
|
2769
|
+
const DIVIDENDS_PARAMETERS = ["ticker", "field", "gaps", "lookahead", "ignore_invalid_symbol", "currency"];
|
|
2770
|
+
const SPLITS_PARAMETERS = ["ticker", "field", "gaps", "lookahead", "ignore_invalid_symbol"];
|
|
2771
|
+
function dividendsCall(...args) {
|
|
2772
|
+
const a = (0, import_drawing_args.namedArguments)("request.dividends", DIVIDENDS_PARAMETERS, args);
|
|
2773
|
+
unsupportedSecurityArgs("request.dividends", a[5], void 0);
|
|
2774
|
+
const ticker2 = a[0] ?? "";
|
|
2775
|
+
const field = a[1] ?? dividends.net;
|
|
2776
|
+
if (field !== dividends.net && field !== dividends.gross) throw new Error(`request.dividends(): unknown field "${field}": dividends.net or dividends.gross.`);
|
|
2777
|
+
const list = symbolEvents(ticker2).dividends ?? [];
|
|
2778
|
+
const values = field === dividends.gross ? list.map((d) => d.gross ?? d.net) : eventField(list, "net", "request.dividends", ticker2);
|
|
2779
|
+
return eventSeries(list.map((d) => d.time), values, a[2], a[3]);
|
|
2780
|
+
}
|
|
2781
|
+
function splitsCall(...args) {
|
|
2782
|
+
const a = (0, import_drawing_args.namedArguments)("request.splits", SPLITS_PARAMETERS, args);
|
|
2783
|
+
const ticker2 = a[0] ?? "";
|
|
2784
|
+
const field = a[1];
|
|
2785
|
+
if (field !== splits.numerator && field !== splits.denominator) {
|
|
2786
|
+
throw new Error("request.splits(): the field is splits.numerator or splits.denominator.");
|
|
2787
|
+
}
|
|
2788
|
+
const list = symbolEvents(ticker2).splits ?? [];
|
|
2789
|
+
return eventSeries(list.map((s) => s.time), eventField(list, field, "request.splits", ticker2), a[2], a[3]);
|
|
2790
|
+
}
|
|
2791
|
+
function earningsCall(...args) {
|
|
2792
|
+
const a = (0, import_drawing_args.namedArguments)("request.earnings", DIVIDENDS_PARAMETERS, args);
|
|
2793
|
+
unsupportedSecurityArgs("request.earnings", a[5], void 0);
|
|
2794
|
+
const ticker2 = a[0] ?? "";
|
|
2795
|
+
const field = a[1] ?? earnings.actual;
|
|
2796
|
+
if (field !== earnings.actual && field !== earnings.estimate && field !== earnings.standardized) {
|
|
2797
|
+
throw new Error(`request.earnings(): unknown field "${field}": earnings.actual, earnings.estimate or earnings.standardized.`);
|
|
2798
|
+
}
|
|
2799
|
+
const list = (symbolEvents(ticker2).earnings ?? []).filter((e) => e.time <= runNow);
|
|
2800
|
+
return eventSeries(list.map((e) => e.time), eventField(list, field, "request.earnings", ticker2), a[2], a[3]);
|
|
2801
|
+
}
|
|
2802
|
+
const request = strictNamespace("request", {
|
|
2803
|
+
security: securityCall,
|
|
2804
|
+
security_lower_tf: securityLowerTfCall,
|
|
2805
|
+
seed: seedCall,
|
|
2806
|
+
dividends: dividendsCall,
|
|
2807
|
+
splits: splitsCall,
|
|
2808
|
+
earnings: earningsCall
|
|
2809
|
+
});
|
|
2488
2810
|
const loopBar = () => currentBar;
|
|
2489
2811
|
function drawings(name, members, all) {
|
|
2490
2812
|
const out = (0, import_drawing_args.drawingNamespace)(name, members, loopBar);
|