oakscriptjs 0.21.0 → 0.23.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/index.d.ts.map +1 -1
- package/dist/cjs/check/index.js +2 -1
- package/dist/cjs/check/index.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/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/runtime/adapters/SimpleInputAdapter.js +1 -1
- package/dist/cjs/runtime/adapters/SimpleInputAdapter.js.map +2 -2
- package/dist/cjs/runtime/types.d.ts +10 -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 +1 -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 +30 -1
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +66 -7
- package/dist/cjs/script/index.js.map +2 -2
- package/dist/esm/check/index.d.ts.map +1 -1
- package/dist/esm/check/index.js +2 -1
- package/dist/esm/check/index.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/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/runtime/adapters/SimpleInputAdapter.js +1 -1
- package/dist/esm/runtime/adapters/SimpleInputAdapter.js.map +1 -1
- package/dist/esm/runtime/types.d.ts +10 -4
- package/dist/esm/runtime/types.d.ts.map +1 -1
- package/dist/esm/script/globals.d.ts +1 -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 +30 -1
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +74 -6
- package/dist/esm/script/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/script-structure.md +13 -3
- package/skills/oakscript/spec.schema.json +3 -1
- package/src/check/index.ts +2 -1
- package/src/check/spec.ts +1 -1
- package/src/index.ts +1 -1
- package/src/runtime/adapters/SimpleInputAdapter.ts +1 -1
- package/src/runtime/types.ts +10 -4
- package/src/script/globals.ts +1 -0
- package/src/script/index.ts +87 -7
package/package.json
CHANGED
|
@@ -41,9 +41,9 @@ Every kind takes an options object after the title. `...common` stands for the o
|
|
|
41
41
|
|
|
42
42
|
| Call | Returns |
|
|
43
43
|
|---|---|
|
|
44
|
-
| `input.int(defval, title, { minval, maxval, step, ...common })` | integer (rounded) |
|
|
45
|
-
| `input.float(defval, title, { minval, maxval, step, ...common })` | number |
|
|
46
|
-
| `input.price(defval, title, { ...common })` | number (a price level) |
|
|
44
|
+
| `input.int(defval, title, { minval, maxval, step, options: [...], ...common })` | integer (rounded; a choice list with `options`) |
|
|
45
|
+
| `input.float(defval, title, { minval, maxval, step, options: [...], ...common })` | number (a choice list with `options`) |
|
|
46
|
+
| `input.price(defval, title, { ...common })` | number (a price level; with `confirm`, the host can ask for it on the chart, and `inline` shared with an `input.time` makes one point) |
|
|
47
47
|
| `input.bool(defval, title, { ...common })` | boolean |
|
|
48
48
|
| `input.string(defval, title, { options: [...], ...common })` | string (a choice list with `options`) |
|
|
49
49
|
| `input.text_area(defval, title, { ...common })` | multi-line string |
|
|
@@ -53,6 +53,7 @@ Every kind takes an options object after the title. `...common` stands for the o
|
|
|
53
53
|
| `input.session("0930-1600", title, { options, ...common })` | session string |
|
|
54
54
|
| `input.symbol("NASDAQ:AAPL", title, { options, ...common })` | ticker id for `request.security` |
|
|
55
55
|
| `input.time(timestamp("2024-01-01"), title, { ...common })` | UNIX time in ms |
|
|
56
|
+
| `input.enum(MaType.sma, title, { options: [...], ...common })` | a field of an enum made with `defineEnum()` (a choice list of its titles; `options`: some of its fields) |
|
|
56
57
|
|
|
57
58
|
The common options, as in Pine:
|
|
58
59
|
|
|
@@ -79,6 +80,15 @@ const src = mode === "HL2" ? hl2 : close; // inputs are plain values: JS operato
|
|
|
79
80
|
plot(ta.sma(src, maLength), "MA", { color: maColor, display: showMa ? "all" : "none" });
|
|
80
81
|
```
|
|
81
82
|
|
|
83
|
+
Pine's `enum` is `defineEnum({ field: "Title", ... })` (an empty title is the field name). Fields compare with
|
|
84
|
+
`===`, `String(field)` and `str.tostring(field)` give the title, `field.name` the name:
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
const MaType = defineEnum({ sma: "Simple", ema: "Exponential" });
|
|
88
|
+
const maType = input.enum(MaType.sma, "Average");
|
|
89
|
+
const ma = maType === MaType.ema ? ta.ema(close, 20) : ta.sma(close, 20);
|
|
90
|
+
```
|
|
91
|
+
|
|
82
92
|
**Input ids.** The host sets inputs by id: the title in snake case (`"Fast length"` is `fast_length`, a repeated
|
|
83
93
|
title gets `_2`, `_3`). Keep titles stable: renaming one resets the user's value.
|
|
84
94
|
|
package/src/check/index.ts
CHANGED
|
@@ -290,7 +290,8 @@ function alternative(input: InputConfig): unknown {
|
|
|
290
290
|
const d = Number(input.defval);
|
|
291
291
|
return [d + 1, d - 1, d * 2, d + 10].find((v) => Number.isFinite(v) && v !== d && inRange(v));
|
|
292
292
|
}
|
|
293
|
-
case 'float':
|
|
293
|
+
case 'float':
|
|
294
|
+
case 'price': {
|
|
294
295
|
const d = Number(input.defval);
|
|
295
296
|
return [d * 1.5, d + 1, d / 2, d - 1].find((v) => Number.isFinite(v) && v !== d && inRange(v));
|
|
296
297
|
}
|
package/src/check/spec.ts
CHANGED
|
@@ -15,7 +15,7 @@ type Field =
|
|
|
15
15
|
|
|
16
16
|
const num = (doc: string): Field => ({ kind: 'number', doc });
|
|
17
17
|
|
|
18
|
-
export const INPUT_TYPES = ['int', 'float', 'bool', 'string', 'text_area', 'source', 'color', 'timeframe', 'session', 'time', 'symbol'] as const;
|
|
18
|
+
export const INPUT_TYPES = ['int', 'float', 'bool', 'string', 'text_area', 'source', 'color', 'timeframe', 'session', 'time', 'symbol', 'enum', 'price'] as const;
|
|
19
19
|
export const OUTPUT_KINDS = ['plot', 'shape', 'char', 'arrow', 'candle', 'bar', 'hline', 'bgcolor', 'barcolor', 'alertcondition'] as const;
|
|
20
20
|
|
|
21
21
|
/** The keys of a spec checked by checkScript. */
|
package/src/index.ts
CHANGED
|
@@ -232,7 +232,7 @@ export type { LightweightSeriesDefinitions } from './runtime/adapters/Lightweigh
|
|
|
232
232
|
export { SimpleInputAdapter } from './runtime/adapters/SimpleInputAdapter.js';
|
|
233
233
|
|
|
234
234
|
// Version
|
|
235
|
-
export const VERSION = '0.
|
|
235
|
+
export const VERSION = '0.23.0';
|
|
236
236
|
|
|
237
237
|
/**
|
|
238
238
|
* Library information
|
|
@@ -53,7 +53,7 @@ export class SimpleInputAdapter implements InputAdapter {
|
|
|
53
53
|
let validatedValue = value;
|
|
54
54
|
|
|
55
55
|
// Validate numeric inputs
|
|
56
|
-
if ((config.type === 'int' || config.type === 'float') && typeof value === 'number') {
|
|
56
|
+
if ((config.type === 'int' || config.type === 'float' || config.type === 'price') && typeof value === 'number') {
|
|
57
57
|
// Apply min constraint
|
|
58
58
|
if (config.min !== undefined && value < config.min) {
|
|
59
59
|
validatedValue = config.min;
|
package/src/runtime/types.ts
CHANGED
|
@@ -71,9 +71,10 @@ export interface InputConfig {
|
|
|
71
71
|
/**
|
|
72
72
|
* Type of input. `timeframe` and `session` values are strings ("60", "1D", "0930-1600:23456";
|
|
73
73
|
* an empty timeframe means the chart timeframe), `time` values are UNIX times in milliseconds. A `text_area`
|
|
74
|
-
* value is a string that can hold several lines.
|
|
74
|
+
* value is a string that can hold several lines. An `enum` value is the field name of one of its `options`. A `price`
|
|
75
|
+
* value is a number, a price level (a host can let the user pick it on the chart).
|
|
75
76
|
*/
|
|
76
|
-
type: 'int' | 'float' | 'bool' | 'string' | 'text_area' | 'source' | 'color' | 'timeframe' | 'session' | 'time' | 'symbol';
|
|
77
|
+
type: 'int' | 'float' | 'bool' | 'string' | 'text_area' | 'source' | 'color' | 'timeframe' | 'session' | 'time' | 'symbol' | 'enum' | 'price';
|
|
77
78
|
/** Default value */
|
|
78
79
|
defval: unknown;
|
|
79
80
|
/** Display title */
|
|
@@ -84,8 +85,13 @@ export interface InputConfig {
|
|
|
84
85
|
max?: number;
|
|
85
86
|
/** Step size (for numeric inputs) */
|
|
86
87
|
step?: number;
|
|
87
|
-
/**
|
|
88
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Options for dropdown selection: strings (string, timeframe, session and symbol inputs), numbers (int and float
|
|
90
|
+
* inputs) or the field names of an enum input
|
|
91
|
+
*/
|
|
92
|
+
options?: (string | number)[];
|
|
93
|
+
/** Titles of the options of an enum input, by field name */
|
|
94
|
+
optionsTitles?: Record<string, string>;
|
|
89
95
|
/** Settings group header (PineScript `group`) */
|
|
90
96
|
group?: string;
|
|
91
97
|
/** Inputs with the same `inline` id are shown on one line (PineScript `inline`) */
|
package/src/script/globals.ts
CHANGED
|
@@ -50,6 +50,7 @@ declare global {
|
|
|
50
50
|
const holdDrawing: typeof S.holdDrawing;
|
|
51
51
|
const releaseDrawing: typeof S.releaseDrawing;
|
|
52
52
|
const varDrawing: typeof S.varDrawing;
|
|
53
|
+
const defineEnum: typeof S.defineEnum;
|
|
53
54
|
const StrategyRecorder: typeof S.StrategyRecorder;
|
|
54
55
|
const input: typeof S.input;
|
|
55
56
|
const open: typeof S.open;
|
package/src/script/index.ts
CHANGED
|
@@ -1207,6 +1207,55 @@ export interface NumericInputOptions extends InputOptions {
|
|
|
1207
1207
|
minval?: number;
|
|
1208
1208
|
maxval?: number;
|
|
1209
1209
|
step?: number;
|
|
1210
|
+
/** The values to choose from (the host shows a drop-down list). */
|
|
1211
|
+
options?: number[];
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/** A field of an enum made with `defineEnum()`: its name and title. `String(field)` is the title, as PineScript
|
|
1215
|
+
* `str.tostring()` of an enum field. */
|
|
1216
|
+
export interface EnumField {
|
|
1217
|
+
readonly name: string;
|
|
1218
|
+
readonly title: string;
|
|
1219
|
+
toString(): string;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
export interface EnumInputOptions extends InputOptions {
|
|
1223
|
+
/** The fields to choose from (default: every field of the enum). */
|
|
1224
|
+
options?: EnumField[];
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
/** A class, not a plain object: a field passed where the options can be is not read as named arguments. */
|
|
1228
|
+
class EnumFieldValue implements EnumField {
|
|
1229
|
+
constructor(readonly name: string, readonly title: string) {
|
|
1230
|
+
Object.freeze(this);
|
|
1231
|
+
}
|
|
1232
|
+
toString(): string {
|
|
1233
|
+
return this.title;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
/** The fields of each enum, by field: `input.enum()` lists them from its default. */
|
|
1238
|
+
const enumFieldsOf = new WeakMap<EnumField, readonly EnumField[]>();
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* PineScript `enum`: a set of named fields, each with a title (the name when the title is empty).
|
|
1242
|
+
* `const MaType = defineEnum({ sma: "Simple", ema: "Exponential" })`; `MaType.sma.title` is "Simple" and
|
|
1243
|
+
* `input.enum(MaType.sma, "Average")` returns `MaType.sma` or `MaType.ema`. Fields compare with `===`.
|
|
1244
|
+
*/
|
|
1245
|
+
export function defineEnum<K extends string>(fields: Record<K, string>): { readonly [P in K]: EnumField } {
|
|
1246
|
+
if (!isPlainObject(fields) || Object.keys(fields).length === 0) {
|
|
1247
|
+
throw new TypeError('defineEnum(): the fields are an object of names and titles, e.g. { sma: "Simple", ema: "Exponential" }.');
|
|
1248
|
+
}
|
|
1249
|
+
const list: EnumField[] = [];
|
|
1250
|
+
const out: Record<string, EnumField> = {};
|
|
1251
|
+
for (const [name, title] of Object.entries(fields)) {
|
|
1252
|
+
if (title !== undefined && typeof title !== 'string') throw new TypeError(`defineEnum(): the title of "${name}" is a string.`);
|
|
1253
|
+
const field = new EnumFieldValue(name, title || name);
|
|
1254
|
+
list.push(field);
|
|
1255
|
+
out[name] = field;
|
|
1256
|
+
}
|
|
1257
|
+
for (const field of list) enumFieldsOf.set(field, list);
|
|
1258
|
+
return Object.freeze(out) as { readonly [P in K]: EnumField };
|
|
1210
1259
|
}
|
|
1211
1260
|
|
|
1212
1261
|
export interface StringInputOptions extends InputOptions {
|
|
@@ -1236,10 +1285,11 @@ function registerInput<T>(config: Omit<InputConfig, 'id'>, fallbackId: string):
|
|
|
1236
1285
|
return (value === undefined ? config.defval : value) as T;
|
|
1237
1286
|
}
|
|
1238
1287
|
|
|
1239
|
-
|
|
1288
|
+
/** `idKind`: the kind in the id of an input without title (a price input keeps the ids of the float input it was). */
|
|
1289
|
+
function registerNumeric(type: 'int' | 'float' | 'price', defval: number, title?: string, opts: NumericInputOptions = {}, idKind: string = type): number {
|
|
1240
1290
|
const v = registerInput<unknown>(
|
|
1241
|
-
{ type, defval, title, min: opts.minval, max: opts.maxval, step: opts.step, ...common(opts) },
|
|
1242
|
-
`input_${
|
|
1291
|
+
{ type, defval, title, min: opts.minval, max: opts.maxval, step: opts.step, ...(opts.options ? { options: opts.options } : {}), ...common(opts) },
|
|
1292
|
+
`input_${idKind}`
|
|
1243
1293
|
);
|
|
1244
1294
|
return typeof v === 'number' ? v : defval;
|
|
1245
1295
|
}
|
|
@@ -1250,8 +1300,9 @@ type InputArgs<O, D> = O & { title?: string; defval?: D };
|
|
|
1250
1300
|
const COMMON_INPUT_KEYS = ['tooltip', 'inline', 'group', 'confirm', 'display', 'active'] as const;
|
|
1251
1301
|
/** The PineScript arguments of each input kind (named arguments are checked against them). */
|
|
1252
1302
|
const INPUT_KEYS: Record<string, readonly string[]> = {
|
|
1253
|
-
int: ['defval', 'title', 'minval', 'maxval', 'step', ...COMMON_INPUT_KEYS],
|
|
1254
|
-
float: ['defval', 'title', 'minval', 'maxval', 'step', ...COMMON_INPUT_KEYS],
|
|
1303
|
+
int: ['defval', 'title', 'minval', 'maxval', 'step', 'options', ...COMMON_INPUT_KEYS],
|
|
1304
|
+
float: ['defval', 'title', 'minval', 'maxval', 'step', 'options', ...COMMON_INPUT_KEYS],
|
|
1305
|
+
enum: ['defval', 'title', 'options', ...COMMON_INPUT_KEYS],
|
|
1255
1306
|
price: ['defval', 'title', ...COMMON_INPUT_KEYS],
|
|
1256
1307
|
bool: ['defval', 'title', ...COMMON_INPUT_KEYS],
|
|
1257
1308
|
string: ['defval', 'title', 'options', ...COMMON_INPUT_KEYS],
|
|
@@ -1358,10 +1409,39 @@ export const input = strictNamespace('input', {
|
|
|
1358
1409
|
const a = inputArgs<string, StringInputOptions>('symbol', defval, title, opts);
|
|
1359
1410
|
return registerInput<string>({ type: 'symbol', defval: a.defval, title: a.title, options: a.opts.options, ...common(a.opts) }, 'input_symbol');
|
|
1360
1411
|
},
|
|
1361
|
-
/**
|
|
1412
|
+
/**
|
|
1413
|
+
* PineScript `input.price`: a price level, `InputConfig.type` `'price'` (a number field; with `confirm`, a host can
|
|
1414
|
+
* ask for it on the chart; an `inline` id shared with an `input.time` makes one point of both).
|
|
1415
|
+
*/
|
|
1362
1416
|
price(defval: number | InputArgs<InputOptions, number>, title?: string | InputArgs<InputOptions, number>, opts?: InputOptions): number {
|
|
1363
1417
|
const a = inputArgs<number, InputOptions>('price', defval, title, opts);
|
|
1364
|
-
return registerNumeric('
|
|
1418
|
+
return registerNumeric('price', a.defval, a.title, a.opts, 'float');
|
|
1419
|
+
},
|
|
1420
|
+
/**
|
|
1421
|
+
* A field of an enum made with `defineEnum()`: `input.enum(MaType.sma, "Average")`. The host lists the fields by
|
|
1422
|
+
* title (`options`: only these fields) and stores the field name; the call returns the field.
|
|
1423
|
+
*/
|
|
1424
|
+
enum<F extends EnumField>(defval: F | InputArgs<EnumInputOptions, F>, title?: string | InputArgs<EnumInputOptions, F>, opts?: EnumInputOptions): F {
|
|
1425
|
+
const a = inputArgs<F, EnumInputOptions>('enum', defval, title, opts);
|
|
1426
|
+
const fields = a.defval === undefined ? undefined : enumFieldsOf.get(a.defval);
|
|
1427
|
+
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").');
|
|
1428
|
+
const choices = a.opts.options ?? fields;
|
|
1429
|
+
for (const f of choices) {
|
|
1430
|
+
if (!fields.includes(f)) throw new TypeError('input.enum(): the options are fields of the enum of the default.');
|
|
1431
|
+
}
|
|
1432
|
+
if (!choices.includes(a.defval)) throw new TypeError('input.enum(): the default is one of the options.');
|
|
1433
|
+
const name = registerInput<unknown>(
|
|
1434
|
+
{
|
|
1435
|
+
type: 'enum',
|
|
1436
|
+
defval: a.defval.name,
|
|
1437
|
+
title: a.title,
|
|
1438
|
+
options: choices.map((f) => f.name),
|
|
1439
|
+
optionsTitles: Object.fromEntries(choices.map((f) => [f.name, f.title])),
|
|
1440
|
+
...common(a.opts),
|
|
1441
|
+
},
|
|
1442
|
+
'input_enum'
|
|
1443
|
+
);
|
|
1444
|
+
return (choices.find((f) => f.name === name) ?? a.defval) as F;
|
|
1365
1445
|
},
|
|
1366
1446
|
/** Date and time as a UNIX time in milliseconds, e.g. `input.time(timestamp('2024-01-01'), 'Start')`. */
|
|
1367
1447
|
time(defval: number | InputArgs<InputOptions, number>, title?: string | InputArgs<InputOptions, number>, opts?: InputOptions): number {
|