oakscriptjs 0.16.0 → 0.17.1
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/incremental.d.ts +4 -0
- package/dist/cjs/script/incremental.d.ts.map +1 -1
- package/dist/cjs/script/incremental.js +32 -1
- package/dist/cjs/script/incremental.js.map +3 -3
- package/dist/cjs/script/index.d.ts +61 -9
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/script/index.js +173 -48
- 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/incremental.d.ts +4 -0
- package/dist/esm/script/incremental.d.ts.map +1 -1
- package/dist/esm/script/incremental.js +40 -1
- package/dist/esm/script/incremental.js.map +1 -1
- package/dist/esm/script/index.d.ts +61 -9
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/script/index.js +229 -56
- package/dist/esm/script/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/oakscript/reference/pine-differences.md +3 -3
- package/skills/oakscript/reference/timeframes-symbols.md +10 -0
- package/src/index.ts +1 -1
- package/src/script/incremental.ts +35 -1
- package/src/script/index.ts +295 -61
package/dist/cjs/script/index.js
CHANGED
|
@@ -235,6 +235,13 @@ class StrategyRecorder {
|
|
|
235
235
|
varipStarts = [];
|
|
236
236
|
/** Current `varip` values: kept through the intrabar runs (no rollback). */
|
|
237
237
|
varip = [];
|
|
238
|
+
/**
|
|
239
|
+
* Why the intrabar runs of this recorder calculate all the bars before their bar (a feature that is not resumed,
|
|
240
|
+
* or a verification difference); null while they are resumed (see {@link executeIntrabar}).
|
|
241
|
+
*/
|
|
242
|
+
get intrabarFullRunReason() {
|
|
243
|
+
return intrabarChains.get(this)?.reason ?? null;
|
|
244
|
+
}
|
|
238
245
|
}
|
|
239
246
|
function saveRunState() {
|
|
240
247
|
return {
|
|
@@ -271,6 +278,7 @@ function executeScript(body, barsInput, inputs = {}, chart2 = {}, options = {})
|
|
|
271
278
|
}
|
|
272
279
|
function runBody(body, barsInput, inputs, chart2, options, inc) {
|
|
273
280
|
const outer = ctx ? saveRunState() : null;
|
|
281
|
+
if (options.recorder) intrabarChains.delete(options.recorder);
|
|
274
282
|
const outerInc = inc ? incremental.beginRun(inc) : incremental.suspendRun();
|
|
275
283
|
ctxBars.setAll(barsInput);
|
|
276
284
|
chartCtx = { ...chart2 };
|
|
@@ -333,25 +341,142 @@ function runBody(body, barsInput, inputs, chart2, options, inc) {
|
|
|
333
341
|
incremental.endRun(outerInc);
|
|
334
342
|
}
|
|
335
343
|
}
|
|
344
|
+
const intrabarChains = /* @__PURE__ */ new WeakMap();
|
|
345
|
+
function intrabarEngine(engine, log, writes) {
|
|
346
|
+
const read = (name, args, value) => {
|
|
347
|
+
log?.push([name, ...args, value]);
|
|
348
|
+
return value;
|
|
349
|
+
};
|
|
350
|
+
const write = (name, args, call) => {
|
|
351
|
+
log?.push([name, ...args]);
|
|
352
|
+
if (writes) writes.push(call);
|
|
353
|
+
else call();
|
|
354
|
+
};
|
|
355
|
+
return {
|
|
356
|
+
...forward(engine),
|
|
357
|
+
entry: (id, direction, options) => write("entry", [id, direction, options], () => engine.entry(id, direction, options)),
|
|
358
|
+
order: (id, direction, options) => write("order", [id, direction, options], () => engine.order(id, direction, options)),
|
|
359
|
+
exit: (id, options) => write("exit", [id, options], () => engine.exit(id, options)),
|
|
360
|
+
close: (id, options) => write("close", [id, options], () => engine.close(id, options)),
|
|
361
|
+
close_all: (options) => write("close_all", [options], () => engine.close_all(options)),
|
|
362
|
+
cancel: (id) => write("cancel", [id], () => engine.cancel(id)),
|
|
363
|
+
cancel_all: () => write("cancel_all", [], () => engine.cancel_all()),
|
|
364
|
+
get: (name) => read("get", [name], engine.get(name)),
|
|
365
|
+
openTrade: (index) => read("openTrade", [index], engine.openTrade(index)),
|
|
366
|
+
closedTrade: (index) => read("closedTrade", [index], engine.closedTrade(index)),
|
|
367
|
+
defaultEntryQty: engine.defaultEntryQty ? (price) => read("defaultEntryQty", [price], engine.defaultEntryQty(price)) : void 0
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function setVarip(recorder, values) {
|
|
371
|
+
recorder.varip.length = 0;
|
|
372
|
+
recorder.varip.push(...values);
|
|
373
|
+
}
|
|
336
374
|
function executeIntrabar(body, bars2, inputs, chart2, options) {
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
375
|
+
const recorder = options.recorder;
|
|
376
|
+
const runChart = { ...chart2, lastBarConfirmed: true, realtime: false };
|
|
377
|
+
const execute = (engine, inc2) => {
|
|
378
|
+
const run = { bar: options.bar, lastBarIndex: options.lastBarIndex, engine, recorder };
|
|
379
|
+
runBody(
|
|
380
|
+
() => {
|
|
381
|
+
intrabarRun = run;
|
|
382
|
+
varipRun = { slot: 0, recorder, mode: "live", scratch: null };
|
|
383
|
+
body();
|
|
384
|
+
},
|
|
385
|
+
bars2,
|
|
386
|
+
inputs,
|
|
387
|
+
runChart,
|
|
388
|
+
{ strategyEngine: () => engine },
|
|
389
|
+
inc2
|
|
390
|
+
);
|
|
391
|
+
};
|
|
392
|
+
const fullRun = (reason) => {
|
|
393
|
+
execute(options.engine, null);
|
|
394
|
+
return { from: 0, incremental: false, fullRunReason: reason };
|
|
395
|
+
};
|
|
396
|
+
if (options.resume === false) return fullRun("resume: false");
|
|
397
|
+
const n = bars2.length;
|
|
398
|
+
if (options.bar !== n - 1) return fullRun("the bar is not the last of the bars");
|
|
399
|
+
let chain = intrabarChains.get(recorder);
|
|
400
|
+
if (!chain || chain.body !== body || chain.engine !== options.engine || chain.lastBarIndex !== options.lastBarIndex || !sameResult(chain.inputs, inputs) || !sameChart(chain.chart, runChart)) {
|
|
401
|
+
chain = {
|
|
402
|
+
body,
|
|
403
|
+
inputs,
|
|
404
|
+
chart: runChart,
|
|
405
|
+
engine: options.engine,
|
|
406
|
+
lastBarIndex: options.lastBarIndex,
|
|
407
|
+
memory: null,
|
|
408
|
+
n: 0,
|
|
409
|
+
reason: null,
|
|
410
|
+
verifyLeft: options.verify ?? 2
|
|
411
|
+
};
|
|
412
|
+
intrabarChains.set(recorder, chain);
|
|
413
|
+
}
|
|
414
|
+
if (chain.reason) return fullRun(chain.reason);
|
|
415
|
+
const memory = chain.memory;
|
|
416
|
+
chain.memory = null;
|
|
417
|
+
if (!memory || n < chain.n) {
|
|
418
|
+
const inc2 = newActiveRun("record", bars2, 0, null);
|
|
419
|
+
execute(options.engine, inc2);
|
|
420
|
+
if (inc2.blocked) chain.reason = inc2.blocked;
|
|
421
|
+
else {
|
|
422
|
+
chain.memory = inc2.next;
|
|
423
|
+
chain.n = n;
|
|
424
|
+
}
|
|
425
|
+
return { from: 0, incremental: false, fullRunReason: chain.reason };
|
|
426
|
+
}
|
|
427
|
+
const from = chain.n - 1;
|
|
428
|
+
const start = incremental.plainCopy(recorder.varip);
|
|
429
|
+
if (start === incremental.NOT_COPIED) {
|
|
430
|
+
chain.reason = "a varip value that is not plain data";
|
|
431
|
+
return fullRun(chain.reason);
|
|
432
|
+
}
|
|
433
|
+
const verifying = chain.verifyLeft > 0;
|
|
434
|
+
const log = verifying ? [] : null;
|
|
435
|
+
const writes = [];
|
|
436
|
+
const inc = newActiveRun("resume", bars2, from, memory);
|
|
437
|
+
try {
|
|
438
|
+
execute(intrabarEngine(options.engine, log, writes), inc);
|
|
439
|
+
} catch (err) {
|
|
440
|
+
setVarip(recorder, start);
|
|
441
|
+
if (err instanceof incremental.ResumeAborted) chain.reason = err.reason;
|
|
442
|
+
return fullRun(chain.reason);
|
|
443
|
+
}
|
|
444
|
+
if (verifying) {
|
|
445
|
+
chain.verifyLeft--;
|
|
446
|
+
const resumedVarip = incremental.plainCopy(recorder.varip);
|
|
447
|
+
setVarip(recorder, start);
|
|
448
|
+
const fullLog = [];
|
|
449
|
+
execute(intrabarEngine(options.engine, fullLog, null), null);
|
|
450
|
+
if (resumedVarip === incremental.NOT_COPIED || !sameResult(log, fullLog) || !sameResult(resumedVarip, recorder.varip)) {
|
|
451
|
+
chain.reason = "a resumed intrabar run differs from a full run (state kept outside persist()?)";
|
|
452
|
+
return { from: 0, incremental: false, fullRunReason: chain.reason };
|
|
453
|
+
}
|
|
454
|
+
} else {
|
|
455
|
+
for (const call of writes) call();
|
|
456
|
+
}
|
|
457
|
+
chain.memory = inc.next;
|
|
458
|
+
chain.n = n;
|
|
459
|
+
return { from, incremental: true, fullRunReason: null };
|
|
460
|
+
}
|
|
461
|
+
function newActiveRun(mode, b, from, prev) {
|
|
462
|
+
const next = incremental.emptyMemory(b.length);
|
|
463
|
+
if (mode === "resume" && prev) next.drawingBlock = prev.drawingBlock;
|
|
464
|
+
return {
|
|
465
|
+
mode,
|
|
466
|
+
from,
|
|
467
|
+
bars: b,
|
|
468
|
+
prev: mode === "resume" ? prev : null,
|
|
469
|
+
next,
|
|
470
|
+
seriesSlot: 0,
|
|
471
|
+
taSlot: 0,
|
|
472
|
+
blockSlot: 0,
|
|
473
|
+
plotSlot: 0,
|
|
474
|
+
boxSlot: 0,
|
|
475
|
+
drawSlot: 0,
|
|
476
|
+
block: -1,
|
|
477
|
+
boxes: [],
|
|
478
|
+
blocked: null
|
|
342
479
|
};
|
|
343
|
-
executeScript(
|
|
344
|
-
() => {
|
|
345
|
-
intrabarRun = run;
|
|
346
|
-
varipRun = { slot: 0, recorder: options.recorder, mode: "live", scratch: null };
|
|
347
|
-
body();
|
|
348
|
-
},
|
|
349
|
-
bars2,
|
|
350
|
-
inputs,
|
|
351
|
-
// History bars are confirmed, also on their ticks (the reference app: a barstate.isconfirmed entry fills on them).
|
|
352
|
-
{ ...chart2, lastBarConfirmed: true, realtime: false },
|
|
353
|
-
{ strategyEngine: () => options.engine }
|
|
354
|
-
);
|
|
355
480
|
}
|
|
356
481
|
const LAST_BAR_FIELDS = /* @__PURE__ */ new Set(["lastBarConfirmed", "lastBarNew", "realtime", "now"]);
|
|
357
482
|
function sameChart(a, b) {
|
|
@@ -411,26 +536,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
|
|
|
411
536
|
let previousChart = chart2;
|
|
412
537
|
let reason = null;
|
|
413
538
|
let verifyLeft = options.verify ?? 2;
|
|
414
|
-
const newRun = (mode, b, from) =>
|
|
415
|
-
const next = incremental.emptyMemory(b.length);
|
|
416
|
-
if (mode === "resume" && memory) next.drawingBlock = memory.drawingBlock;
|
|
417
|
-
return {
|
|
418
|
-
mode,
|
|
419
|
-
from,
|
|
420
|
-
bars: b,
|
|
421
|
-
prev: mode === "resume" ? memory : null,
|
|
422
|
-
next,
|
|
423
|
-
seriesSlot: 0,
|
|
424
|
-
taSlot: 0,
|
|
425
|
-
blockSlot: 0,
|
|
426
|
-
plotSlot: 0,
|
|
427
|
-
boxSlot: 0,
|
|
428
|
-
drawSlot: 0,
|
|
429
|
-
block: -1,
|
|
430
|
-
boxes: [],
|
|
431
|
-
blocked: null
|
|
432
|
-
};
|
|
433
|
-
};
|
|
539
|
+
const newRun = (mode, b, from) => newActiveRun(mode, b, from, memory);
|
|
434
540
|
const full = (b, c) => {
|
|
435
541
|
memory = null;
|
|
436
542
|
if (reason) return { ...runBody(body, b, inputs, c, options, null), from: 0, incremental: false };
|
|
@@ -488,7 +594,7 @@ function createScriptRun(body, inputs = {}, chart2 = {}, options = {}) {
|
|
|
488
594
|
let varipRun = { slot: 0, recorder: null, mode: "live", scratch: null };
|
|
489
595
|
function varip(initial) {
|
|
490
596
|
collector();
|
|
491
|
-
incremental.nonIncremental("varip");
|
|
597
|
+
if (!intrabarRun) incremental.nonIncremental("varip");
|
|
492
598
|
const slot = varipRun.slot++;
|
|
493
599
|
const state = varipRun;
|
|
494
600
|
const rec = state.recorder;
|
|
@@ -504,7 +610,10 @@ function varip(initial) {
|
|
|
504
610
|
};
|
|
505
611
|
}
|
|
506
612
|
if (rec.varip.length <= slot) rec.varip[slot] = initial;
|
|
507
|
-
const store = () =>
|
|
613
|
+
const store = () => {
|
|
614
|
+
if (incremental.active && !ctx?.strategy?.inLoop) incremental.nonIncremental("varip used outside strategy.eachBar()");
|
|
615
|
+
return state.mode === "replay" && state.scratch ? state.scratch : rec.varip;
|
|
616
|
+
};
|
|
508
617
|
return {
|
|
509
618
|
get value() {
|
|
510
619
|
return store()[slot];
|
|
@@ -726,8 +835,8 @@ const hlcc4 = new import_series.Series(ctxBars, (b) => (b.high + b.low + 2 * b.c
|
|
|
726
835
|
const bar_index = new import_series.Series(ctxBars, (_b, i) => i);
|
|
727
836
|
bar_index.fullHistory = true;
|
|
728
837
|
const last_bar_index = new import_series.Series(ctxBars, (_b, _i, data) => {
|
|
729
|
-
incremental.nonIncremental("last_bar_index");
|
|
730
|
-
return data.length - 1;
|
|
838
|
+
if (!intrabarRun) incremental.nonIncremental("last_bar_index");
|
|
839
|
+
return chartBarCount(data.length) - 1;
|
|
731
840
|
});
|
|
732
841
|
last_bar_index.fullHistory = true;
|
|
733
842
|
for (const s of [hl2, hlc3, ohlc4, hlcc4]) s.fullHistory = true;
|
|
@@ -1465,7 +1574,7 @@ function runBlock(fn, extra, own = fn) {
|
|
|
1465
1574
|
collector();
|
|
1466
1575
|
const b = bars();
|
|
1467
1576
|
const lastIndex = intrabarRun ? intrabarRun.lastBarIndex : b.length - 1;
|
|
1468
|
-
let out =
|
|
1577
|
+
let out = [];
|
|
1469
1578
|
const cache = /* @__PURE__ */ new Map();
|
|
1470
1579
|
const buffers = /* @__PURE__ */ new Map();
|
|
1471
1580
|
const bufferKey = (src) => incremental.slotOf(src) ?? src;
|
|
@@ -1480,7 +1589,8 @@ function runBlock(fn, extra, own = fn) {
|
|
|
1480
1589
|
}
|
|
1481
1590
|
let start = 0;
|
|
1482
1591
|
let checkpoint = null;
|
|
1483
|
-
if (run?.mode
|
|
1592
|
+
if (run?.mode !== "resume") out = new Array(b.length).fill(NaN);
|
|
1593
|
+
else {
|
|
1484
1594
|
const prev = run.prev?.blocks[slot];
|
|
1485
1595
|
if (!prev) incremental.nonIncremental("a new eachBar() block");
|
|
1486
1596
|
start = run.from;
|
|
@@ -1519,7 +1629,7 @@ function runBlock(fn, extra, own = fn) {
|
|
|
1519
1629
|
return idx;
|
|
1520
1630
|
},
|
|
1521
1631
|
get n() {
|
|
1522
|
-
if (run) incremental.nonIncremental("BarContext.n");
|
|
1632
|
+
if (run && !intrabarRun) incremental.nonIncremental("BarContext.n");
|
|
1523
1633
|
return lastIndex + 1;
|
|
1524
1634
|
},
|
|
1525
1635
|
get time() {
|
|
@@ -1676,7 +1786,7 @@ function strategyEachBar(fn) {
|
|
|
1676
1786
|
const varipState = varipRun;
|
|
1677
1787
|
const replayLog = { answers: [], next: 0 };
|
|
1678
1788
|
const replayed = replayEngine(replayLog);
|
|
1679
|
-
return
|
|
1789
|
+
return runBlock((c) => {
|
|
1680
1790
|
const replay = c.i < intrabar.bar;
|
|
1681
1791
|
if (replay) {
|
|
1682
1792
|
replayLog.answers = intrabar.recorder.answers[c.i] ?? [];
|
|
@@ -1695,7 +1805,7 @@ function strategyEachBar(fn) {
|
|
|
1695
1805
|
varipState.mode = "live";
|
|
1696
1806
|
varipState.scratch = null;
|
|
1697
1807
|
}
|
|
1698
|
-
});
|
|
1808
|
+
}, void 0, fn);
|
|
1699
1809
|
}
|
|
1700
1810
|
const recorder = scriptOptions.recorder;
|
|
1701
1811
|
if (incremental.active) {
|
|
@@ -2124,7 +2234,7 @@ const barstate = {
|
|
|
2124
2234
|
},
|
|
2125
2235
|
get islastconfirmedhistory() {
|
|
2126
2236
|
return namedFlag("islastconfirmedhistory", (i, n) => {
|
|
2127
|
-
incremental.nonIncremental("barstate.islastconfirmedhistory");
|
|
2237
|
+
if (!intrabarRun) incremental.nonIncremental("barstate.islastconfirmedhistory");
|
|
2128
2238
|
const lastConfirmed = chartCtx.lastBarConfirmed === true && chartCtx.realtime !== true;
|
|
2129
2239
|
const count = chartBarCount(n);
|
|
2130
2240
|
return i === (lastConfirmed ? count - 1 : count - 2);
|
|
@@ -2319,8 +2429,13 @@ function security_lower_tf(symbol, timeframe2, expression, ignore_invalid_symbol
|
|
|
2319
2429
|
const mapped = values.map((v) => ranges.map(([from, to]) => v.slice(from, to)));
|
|
2320
2430
|
return Array.isArray(result) ? mapped : mapped[0];
|
|
2321
2431
|
}
|
|
2432
|
+
function seed(source, symbol, expression, ignore_invalid_symbol = false) {
|
|
2433
|
+
const ticker2 = chartCtx.seedSymbol?.(source, symbol) ?? `${source.toUpperCase()}:${symbol.toUpperCase()}`;
|
|
2434
|
+
return security(ticker2, "", expression, barmerge.gaps_off, barmerge.lookahead_off, ignore_invalid_symbol);
|
|
2435
|
+
}
|
|
2322
2436
|
const SECURITY_PARAMETERS = ["symbol", "timeframe", "expression", "gaps", "lookahead", "ignore_invalid_symbol", "currency", "calc_bars_count"];
|
|
2323
2437
|
const SECURITY_LOWER_TF_PARAMETERS = ["symbol", "timeframe", "expression", "ignore_invalid_symbol", "currency", "ignore_invalid_timeframe", "calc_bars_count"];
|
|
2438
|
+
const SEED_PARAMETERS = ["source", "symbol", "expression", "ignore_invalid_symbol", "calc_bars_count"];
|
|
2324
2439
|
function unsupportedSecurityArgs(name, currency2, calcBarsCount) {
|
|
2325
2440
|
if (currency2 !== void 0 && currency2 !== "" && currency2 !== "NONE") {
|
|
2326
2441
|
throw new Error(`${name}(): the currency argument (conversion of the values) is not supported.`);
|
|
@@ -2338,7 +2453,12 @@ function securityLowerTfCall(...args) {
|
|
|
2338
2453
|
if (a[5] !== void 0 && a[5] !== false) throw new Error("request.security_lower_tf(): the ignore_invalid_timeframe argument is not supported.");
|
|
2339
2454
|
return security_lower_tf(a[0], a[1], a[2], a[3]);
|
|
2340
2455
|
}
|
|
2341
|
-
|
|
2456
|
+
function seedCall(...args) {
|
|
2457
|
+
const a = (0, import_drawing_args.namedArguments)("request.seed", SEED_PARAMETERS, args);
|
|
2458
|
+
unsupportedSecurityArgs("request.seed", void 0, a[4]);
|
|
2459
|
+
return seed(a[0], a[1], a[2], a[3]);
|
|
2460
|
+
}
|
|
2461
|
+
const request = strictNamespace("request", { security: securityCall, security_lower_tf: securityLowerTfCall, seed: seedCall });
|
|
2342
2462
|
const loopBar = () => currentBar;
|
|
2343
2463
|
function drawings(name, members, all) {
|
|
2344
2464
|
const out = (0, import_drawing_args.drawingNamespace)(name, members, loopBar);
|
|
@@ -2424,6 +2544,11 @@ void _saveRandom;
|
|
|
2424
2544
|
void _restoreRandom;
|
|
2425
2545
|
const math = strictNamespace("math", {
|
|
2426
2546
|
...mathScript,
|
|
2547
|
+
// the generators start again on each run: a resumed run would draw other numbers (#206)
|
|
2548
|
+
random: ((min, max, seed2) => {
|
|
2549
|
+
incremental.nonIncremental("math.random");
|
|
2550
|
+
return mathCore.random(min, max, seed2);
|
|
2551
|
+
}),
|
|
2427
2552
|
round_to_mintick: ((number, mintick) => {
|
|
2428
2553
|
const tick = mintick ?? syminfo.mintick;
|
|
2429
2554
|
return number instanceof import_series.Series ? mathCore.round_to_mintick(number, tick) : mathCore.round_to_mintick(number, tick);
|