oakscriptjs 0.17.1 → 0.18.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/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- 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 +34 -1
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +62 -11
- package/dist/cjs/script/index.js.map +3 -3
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +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 +34 -1
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +70 -0
- package/dist/esm/script/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/plotting.md +16 -0
- package/src/index.ts +1 -1
- package/src/script/globals.ts +3 -0
- package/src/script/index.ts +88 -1
package/package.json
CHANGED
|
@@ -195,3 +195,19 @@ eachBar((c) => {
|
|
|
195
195
|
if (c.get(up) === 1) alert("Close " + str.tostring(c.close, "#.##") + " crossed above the SMA", alert.freq_once_per_bar_close);
|
|
196
196
|
});
|
|
197
197
|
```
|
|
198
|
+
|
|
199
|
+
## Logs
|
|
200
|
+
|
|
201
|
+
- `log.info(message, ...args)`, `log.warning(...)`, `log.error(...)`: a message in the host's logs panel. Inside a bar
|
|
202
|
+
loop it is made on that bar; at the top level, on every bar. With arguments, `message` is a `str.format()` pattern
|
|
203
|
+
and a Series argument gives its value on the bar. The messages of a run are in `result.logs` (bar, time, level,
|
|
204
|
+
message, and the script line when the host resolves it with `setLogLineResolver`), by bar, the newest 10,000.
|
|
205
|
+
|
|
206
|
+
```js
|
|
207
|
+
indicator("Logs");
|
|
208
|
+
const ma = ta.sma(close, 20);
|
|
209
|
+
eachBar((c) => {
|
|
210
|
+
if (c.i === c.n - 1) log.info("Last close {0}, SMA {1}", c.close, c.get(ma));
|
|
211
|
+
if (c.volume === 0) log.warning("No volume on bar {0}", c.i);
|
|
212
|
+
});
|
|
213
|
+
```
|
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.18.0';
|
|
236
236
|
|
|
237
237
|
/**
|
|
238
238
|
* Library information
|
package/src/script/globals.ts
CHANGED
|
@@ -99,6 +99,9 @@ declare global {
|
|
|
99
99
|
const text: typeof S.text;
|
|
100
100
|
const chart: typeof S.chart;
|
|
101
101
|
const alert: typeof S.alert;
|
|
102
|
+
const log: typeof S.log;
|
|
103
|
+
const MAX_LOG_ENTRIES: typeof S.MAX_LOG_ENTRIES;
|
|
104
|
+
const setLogLineResolver: typeof S.setLogLineResolver;
|
|
102
105
|
const Series: typeof S.Series;
|
|
103
106
|
const BarData: typeof S.BarData;
|
|
104
107
|
const na: typeof S.na;
|
package/src/script/index.ts
CHANGED
|
@@ -195,7 +195,7 @@ export interface ScriptRunResult {
|
|
|
195
195
|
* last bar. `outputValues`: per plotshape() / plotchar() / plotarrow() id, the value of its series on each bar where
|
|
196
196
|
* it is not na (a condition gives 1 or 0), at the bar it was computed on (the offset not applied).
|
|
197
197
|
*/
|
|
198
|
-
result: IndicatorResult & { alerts?: AlertState[]; alertCalls?: AlertCall[]; outputValues?: Record<string, TimeValue[]
|
|
198
|
+
result: IndicatorResult & { alerts?: AlertState[]; alertCalls?: AlertCall[]; outputValues?: Record<string, TimeValue[]>; logs?: LogEntry[] };
|
|
199
199
|
/** The script called alert() during the run (PineScript `hasAlertFunction` is known at compile time: a host that
|
|
200
200
|
* needs it before any bar runs reads the source). */
|
|
201
201
|
hasAlertFunction: boolean;
|
|
@@ -274,6 +274,8 @@ interface Collector {
|
|
|
274
274
|
alertConfig: AlertConditionConfig[];
|
|
275
275
|
alerts: AlertState[];
|
|
276
276
|
alertCalls: AlertCall[];
|
|
277
|
+
/** log.*() messages of the run. */
|
|
278
|
+
logs: LogEntry[];
|
|
277
279
|
hasAlertFunction: boolean;
|
|
278
280
|
strategy?: StrategyRun;
|
|
279
281
|
}
|
|
@@ -332,6 +334,7 @@ function freshCollector(inputValues: Record<string, unknown>): Collector {
|
|
|
332
334
|
alertConfig: [],
|
|
333
335
|
alerts: [],
|
|
334
336
|
alertCalls: [],
|
|
337
|
+
logs: [],
|
|
335
338
|
hasAlertFunction: false,
|
|
336
339
|
};
|
|
337
340
|
}
|
|
@@ -538,6 +541,8 @@ function runBody(
|
|
|
538
541
|
...(intrabarRun ? {} : drawingOutputs(ctxBars.bars, chartCtx.timeUnit)),
|
|
539
542
|
alerts: c.alerts.length ? c.alerts : undefined,
|
|
540
543
|
alertCalls: c.alertCalls.length ? c.alertCalls : undefined,
|
|
544
|
+
// By bar, in call order on each bar (the top-level calls make theirs on every bar at once).
|
|
545
|
+
logs: c.logs.length ? [...c.logs].sort((x, y) => x.bar - y.bar).slice(-MAX_LOG_ENTRIES) : undefined,
|
|
541
546
|
},
|
|
542
547
|
hasAlertFunction: c.hasAlertFunction,
|
|
543
548
|
};
|
|
@@ -912,6 +917,8 @@ export function createScriptRun(
|
|
|
912
917
|
let previousChart: ChartContext = chart;
|
|
913
918
|
let reason: string | null = null;
|
|
914
919
|
let verifyLeft = options.verify ?? 2;
|
|
920
|
+
/** The log messages of the last update: a resumed update keeps those of the bars it does not calculate again. */
|
|
921
|
+
let lastLogs: LogEntry[] | undefined;
|
|
915
922
|
|
|
916
923
|
const newRun = (mode: 'record' | 'resume', b: Bar[], from: number): incremental.ActiveRun =>
|
|
917
924
|
newActiveRun(mode, b, from, memory);
|
|
@@ -946,6 +953,7 @@ export function createScriptRun(
|
|
|
946
953
|
let result: ScriptRunResult | null = null;
|
|
947
954
|
try {
|
|
948
955
|
result = runBody(body, b, inputs, c, options, inc);
|
|
956
|
+
result.result.logs = resumedLogs(lastLogs, result.result.logs, from);
|
|
949
957
|
} catch (err) {
|
|
950
958
|
// the memory is changed in place by a resumed run: an error leaves it unusable
|
|
951
959
|
memory = null;
|
|
@@ -970,6 +978,7 @@ export function createScriptRun(
|
|
|
970
978
|
}
|
|
971
979
|
}
|
|
972
980
|
previous = out.incremental && last ? copyBars(b, last, last.n - 1) : copyBars(b);
|
|
981
|
+
lastLogs = out.result.logs;
|
|
973
982
|
return out;
|
|
974
983
|
},
|
|
975
984
|
};
|
|
@@ -4234,6 +4243,84 @@ export const alert = Object.assign(alertImpl, {
|
|
|
4234
4243
|
freq_once_per_bar_close: 'once_per_bar_close',
|
|
4235
4244
|
} as const);
|
|
4236
4245
|
|
|
4246
|
+
// ── Logs (log.info / log.warning / log.error) ────────────────────────────────
|
|
4247
|
+
|
|
4248
|
+
/** The level of a log message. */
|
|
4249
|
+
export type LogLevel = 'info' | 'warning' | 'error';
|
|
4250
|
+
|
|
4251
|
+
/** One log.*() message: the bar it was made on (index, and time as given in the input bars), its level and text, and
|
|
4252
|
+
* the line of the script that made it when the host resolves it ({@link setLogLineResolver}). */
|
|
4253
|
+
export interface LogEntry {
|
|
4254
|
+
bar: number;
|
|
4255
|
+
time: number;
|
|
4256
|
+
level: LogLevel;
|
|
4257
|
+
message: string;
|
|
4258
|
+
line?: number;
|
|
4259
|
+
}
|
|
4260
|
+
|
|
4261
|
+
/** The most messages a run keeps: the newest ones. */
|
|
4262
|
+
export const MAX_LOG_ENTRIES = 10_000;
|
|
4263
|
+
|
|
4264
|
+
let logLineResolver: ((stack: string) => number | undefined) | null = null;
|
|
4265
|
+
|
|
4266
|
+
/**
|
|
4267
|
+
* How the host finds the line of the script that called log.*(): `resolver` gets the stack of the call (an Error's
|
|
4268
|
+
* `stack`) and gives the line in the script's own source. Null (the default): messages have no line.
|
|
4269
|
+
*/
|
|
4270
|
+
export function setLogLineResolver(resolver: ((stack: string) => number | undefined) | null): void {
|
|
4271
|
+
logLineResolver = resolver;
|
|
4272
|
+
}
|
|
4273
|
+
|
|
4274
|
+
function logImpl(level: LogLevel, message: string | ReadonlyArray<string | undefined>, args: unknown[]): void {
|
|
4275
|
+
const c = collector();
|
|
4276
|
+
const b = ctxBars.bars;
|
|
4277
|
+
const line = logLineResolver ? logLineResolver(new Error().stack ?? '') : undefined;
|
|
4278
|
+
const add = (i: number): void => {
|
|
4279
|
+
const text = typeof message === 'string' ? message : message[i];
|
|
4280
|
+
if (text === undefined || text === null) return;
|
|
4281
|
+
// A Series argument gives its value on the bar.
|
|
4282
|
+
const values = args.map((a) => (a instanceof Series ? a.get(i) : a));
|
|
4283
|
+
const entry: LogEntry = {
|
|
4284
|
+
bar: i,
|
|
4285
|
+
time: b[i]!.time as number,
|
|
4286
|
+
level,
|
|
4287
|
+
message: args.length ? formatMessage(String(text), values, exchangeTimezone) : String(text),
|
|
4288
|
+
};
|
|
4289
|
+
if (line !== undefined) entry.line = line;
|
|
4290
|
+
c.logs.push(entry);
|
|
4291
|
+
};
|
|
4292
|
+
if (currentBar >= 0) add(currentBar);
|
|
4293
|
+
else for (let i = 0; i < b.length; i++) add(i);
|
|
4294
|
+
if (c.logs.length > MAX_LOG_ENTRIES * 2) c.logs.splice(0, c.logs.length - MAX_LOG_ENTRIES);
|
|
4295
|
+
}
|
|
4296
|
+
|
|
4297
|
+
/**
|
|
4298
|
+
* PineScript `log.info()`, `log.warning()` and `log.error()`: a message for the host's logs panel, made on the current
|
|
4299
|
+
* bar inside eachBar() / strategy.eachBar(), on every bar at the top level of the script (which PineScript runs on
|
|
4300
|
+
* every bar; `message` is then a text or one text per bar, a bar whose text is na makes no message). With arguments,
|
|
4301
|
+
* `message` is a str.format() pattern (`log.info('close {0}', close)`), a Series argument giving its value on the bar.
|
|
4302
|
+
* The messages of a run are in `result.logs`, by bar ({@link MAX_LOG_ENTRIES} newest kept).
|
|
4303
|
+
*/
|
|
4304
|
+
export const log = strictNamespace('log', {
|
|
4305
|
+
info(message: string | ReadonlyArray<string | undefined>, ...args: unknown[]): void {
|
|
4306
|
+
logImpl('info', message, args);
|
|
4307
|
+
},
|
|
4308
|
+
warning(message: string | ReadonlyArray<string | undefined>, ...args: unknown[]): void {
|
|
4309
|
+
logImpl('warning', message, args);
|
|
4310
|
+
},
|
|
4311
|
+
error(message: string | ReadonlyArray<string | undefined>, ...args: unknown[]): void {
|
|
4312
|
+
logImpl('error', message, args);
|
|
4313
|
+
},
|
|
4314
|
+
});
|
|
4315
|
+
|
|
4316
|
+
/** The messages of a resumed run calculated from bar `from`: those of the run before on the bars before `from`, then the
|
|
4317
|
+
* new ones on the bars from `from` (the top-level log calls of a resumed run make them again on every bar). */
|
|
4318
|
+
function resumedLogs(before: LogEntry[] | undefined, now: LogEntry[] | undefined, from: number): LogEntry[] | undefined {
|
|
4319
|
+
const kept = (before ?? []).filter((e) => e.bar < from);
|
|
4320
|
+
const logs = kept.concat((now ?? []).filter((e) => e.bar >= from));
|
|
4321
|
+
return logs.length ? logs.slice(-MAX_LOG_ENTRIES) : undefined;
|
|
4322
|
+
}
|
|
4323
|
+
|
|
4237
4324
|
// ── Convenience re-exports ───────────────────────────────────────────────────
|
|
4238
4325
|
|
|
4239
4326
|
export { Series, BarData };
|