oakscriptjs 0.15.0 → 0.15.2
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/callsite/index.d.ts +3 -3
- package/dist/cjs/callsite/index.d.ts.map +1 -1
- package/dist/cjs/callsite/index.js +61 -7
- package/dist/cjs/callsite/index.js.map +2 -2
- package/dist/cjs/drawing/registry.d.ts +30 -5
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +118 -42
- package/dist/cjs/drawing/registry.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/linefill/index.d.ts.map +1 -1
- package/dist/cjs/linefill/index.js +14 -5
- package/dist/cjs/linefill/index.js.map +2 -2
- package/dist/cjs/math/index.d.ts +2 -6
- package/dist/cjs/math/index.d.ts.map +1 -1
- package/dist/cjs/math/index.js.map +2 -2
- package/dist/cjs/script/index.d.ts.map +1 -1
- package/dist/cjs/str/floatingdecimal.d.ts +3 -0
- package/dist/cjs/str/floatingdecimal.d.ts.map +1 -1
- package/dist/cjs/str/floatingdecimal.js +25 -12
- package/dist/cjs/str/floatingdecimal.js.map +2 -2
- package/dist/cjs/str/messageformat.d.ts.map +1 -1
- package/dist/cjs/str/messageformat.js +27 -8
- package/dist/cjs/str/messageformat.js.map +2 -2
- package/dist/cjs/str/numberformat.d.ts.map +1 -1
- package/dist/cjs/str/numberformat.js +9 -16
- package/dist/cjs/str/numberformat.js.map +3 -3
- package/dist/cjs/ta/index.d.ts +9 -11
- package/dist/cjs/ta/index.d.ts.map +1 -1
- package/dist/cjs/ta/index.js +27 -0
- package/dist/cjs/ta/index.js.map +2 -2
- package/dist/cjs/ta/running-sum.d.ts +20 -19
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +74 -74
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +14 -1
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/esm/callsite/index.d.ts +3 -3
- package/dist/esm/callsite/index.d.ts.map +1 -1
- package/dist/esm/callsite/index.js +79 -10
- package/dist/esm/callsite/index.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +30 -5
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +163 -54
- package/dist/esm/drawing/registry.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/linefill/index.d.ts.map +1 -1
- package/dist/esm/linefill/index.js +17 -7
- package/dist/esm/linefill/index.js.map +1 -1
- package/dist/esm/math/index.d.ts +2 -6
- package/dist/esm/math/index.d.ts.map +1 -1
- package/dist/esm/math/index.js +1 -1
- package/dist/esm/math/index.js.map +1 -1
- package/dist/esm/script/index.d.ts.map +1 -1
- package/dist/esm/str/floatingdecimal.d.ts +3 -0
- package/dist/esm/str/floatingdecimal.d.ts.map +1 -1
- package/dist/esm/str/floatingdecimal.js +27 -11
- package/dist/esm/str/floatingdecimal.js.map +1 -1
- package/dist/esm/str/messageformat.d.ts.map +1 -1
- package/dist/esm/str/messageformat.js +35 -9
- package/dist/esm/str/messageformat.js.map +1 -1
- package/dist/esm/str/numberformat.d.ts.map +1 -1
- package/dist/esm/str/numberformat.js +12 -18
- package/dist/esm/str/numberformat.js.map +1 -1
- package/dist/esm/ta/index.d.ts +9 -11
- package/dist/esm/ta/index.d.ts.map +1 -1
- package/dist/esm/ta/index.js +48 -15
- package/dist/esm/ta/index.js.map +1 -1
- package/dist/esm/ta/running-sum.d.ts +20 -19
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +103 -103
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +23 -3
- package/dist/esm/time/timezone.js.map +1 -1
- package/package.json +1 -1
- package/src/callsite/index.ts +68 -10
- package/src/drawing/registry.ts +167 -50
- package/src/index.ts +1 -1
- package/src/linefill/index.ts +16 -7
- package/src/math/index.ts +3 -7
- package/src/str/floatingdecimal.ts +27 -11
- package/src/str/messageformat.ts +40 -9
- package/src/str/numberformat.ts +13 -17
- package/src/ta/index.ts +54 -15
- package/src/ta/running-sum.ts +94 -99
- package/src/time/timezone.ts +26 -4
package/src/drawing/registry.ts
CHANGED
|
@@ -85,15 +85,73 @@ let limits: Required<DrawingLimits> = {
|
|
|
85
85
|
box: DEFAULT_MAX_COUNT,
|
|
86
86
|
polyline: DEFAULT_MAX_COUNT,
|
|
87
87
|
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Base of the classes that add a private field to an existing object (their constructor returns the object they are
|
|
90
|
+
* given). Such a field replaces a WeakMap entry: a study that creates hundreds of thousands of objects spends most of
|
|
91
|
+
* its drawing time adding WeakMap keys. The field is invisible (not a key, not copied by a spread) and stays for the
|
|
92
|
+
* life of the object, as the entry did.
|
|
93
|
+
*/
|
|
94
|
+
export class Stamped {
|
|
95
|
+
constructor(obj: object) {
|
|
96
|
+
return obj;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The deleted objects. */
|
|
101
|
+
class Deleted extends Stamped {
|
|
102
|
+
#deleted = true;
|
|
103
|
+
static mark(obj: object): void {
|
|
104
|
+
if (!Deleted.has(obj)) new Deleted(obj);
|
|
105
|
+
}
|
|
106
|
+
static has(obj: object): boolean {
|
|
107
|
+
return #deleted in obj;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The `var` records of a run: a reset starts new ones, a nested run saves and puts back those of its run. */
|
|
112
|
+
interface Holds {
|
|
113
|
+
/** The records of the objects whose private record belongs to another run */
|
|
114
|
+
readonly others: WeakMap<object, HoldRecord>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** What the `var` variables of a run did with an object. */
|
|
118
|
+
interface HoldRecord {
|
|
119
|
+
readonly run: Holds;
|
|
120
|
+
/** Number of `var` variables that hold the object (0: not held) */
|
|
121
|
+
count: number;
|
|
122
|
+
/** Bar on which a `var` variable got the object: on that bar the maximum count deletes it only when no other object
|
|
123
|
+
* can go; from the next bar on, never. Undefined outside a bar loop */
|
|
124
|
+
heldAt: number | undefined;
|
|
125
|
+
/** Bar on which the last `var` variable released the object (linefill rule) */
|
|
126
|
+
releasedAt: number | undefined;
|
|
127
|
+
}
|
|
128
|
+
let holds: Holds = { others: new WeakMap() };
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The record of the first run that holds an object, on the object itself (a study holding a new object on every bar
|
|
132
|
+
* would otherwise add a WeakMap key per bar); the records of other runs go to their {@link Holds.others}.
|
|
133
|
+
*/
|
|
134
|
+
class Held extends Stamped {
|
|
135
|
+
#record: HoldRecord;
|
|
136
|
+
private constructor(obj: object, record: HoldRecord) {
|
|
137
|
+
super(obj);
|
|
138
|
+
this.#record = record;
|
|
139
|
+
}
|
|
140
|
+
/** The record of `obj` in the current run, if any. */
|
|
141
|
+
static get(obj: object): HoldRecord | undefined {
|
|
142
|
+
const own = #record in obj ? (obj as Held).#record : undefined;
|
|
143
|
+
return own?.run === holds ? own : holds.others.get(obj);
|
|
144
|
+
}
|
|
145
|
+
/** A new record of `obj` in the current run (it has none). */
|
|
146
|
+
static create(obj: object): HoldRecord {
|
|
147
|
+
const record: HoldRecord = { run: holds, count: 0, heldAt: undefined, releasedAt: undefined };
|
|
148
|
+
if (#record in obj) holds.others.set(obj, record);
|
|
149
|
+
else new Held(obj, record);
|
|
150
|
+
return record;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
93
153
|
/** Bars during which a released linefill is still skipped by the maximum count. */
|
|
94
154
|
const LINEFILL_RELEASE_BARS = 5000;
|
|
95
|
-
/** Bar on which a `var` variable got an object: the maximum count skips it from the next bar on. */
|
|
96
|
-
let heldAt = new WeakMap<object, number>();
|
|
97
155
|
/** Kinds with a maximum count of their own. */
|
|
98
156
|
type CountedKind = 'line' | 'label' | 'box' | 'polyline';
|
|
99
157
|
const COUNTED_KINDS: readonly CountedKind[] = ['line', 'label', 'box', 'polyline'];
|
|
@@ -108,33 +166,47 @@ const PENDING_RESET: Record<CountedKind, number> = { line: 5, label: 5, box: 5,
|
|
|
108
166
|
* variable): the maximum count never deletes it, and it still counts. Returns the object.
|
|
109
167
|
*/
|
|
110
168
|
export function holdDrawing<T extends object>(obj: T): T {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
held.set(obj, (held.get(obj) ?? 0) + 1);
|
|
169
|
+
const record = Held.get(obj) ?? Held.create(obj);
|
|
170
|
+
if (record.count === 0) record.heldAt = currentBar ?? undefined;
|
|
171
|
+
record.count++;
|
|
116
172
|
return obj;
|
|
117
173
|
}
|
|
118
174
|
|
|
119
175
|
/** The `var` variable no longer refers to the object (it got another object or `na`). */
|
|
120
176
|
export function releaseDrawing(obj: object | null | undefined): void {
|
|
121
177
|
if (!obj) return;
|
|
122
|
-
const
|
|
123
|
-
if (
|
|
124
|
-
|
|
178
|
+
const record = Held.get(obj);
|
|
179
|
+
if (!record || record.count === 0) return;
|
|
180
|
+
record.count--;
|
|
181
|
+
if (record.count === 0 && currentBar !== null) record.releasedAt = currentBar;
|
|
125
182
|
}
|
|
126
183
|
|
|
127
184
|
/** Held by a `var` variable since a bar before the current one (or outside a bar loop). */
|
|
128
185
|
function heldBefore(obj: object): boolean {
|
|
129
|
-
|
|
130
|
-
|
|
186
|
+
const record = Held.get(obj);
|
|
187
|
+
if (!record || record.count === 0) return false;
|
|
188
|
+
const bar = record.heldAt;
|
|
131
189
|
return bar === undefined || currentBar === null || bar < currentBar;
|
|
132
190
|
}
|
|
133
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Held by a `var` variable since the current bar, or released by its last variable on the current bar: the maximum
|
|
194
|
+
* count deletes such an object only when the objects it may delete are not enough (#193; 12 `var` lines made on the
|
|
195
|
+
* first bar with a maximum of 5 lose the first 6, #158).
|
|
196
|
+
*/
|
|
197
|
+
function heldOnThisBar(obj: object): boolean {
|
|
198
|
+
if (currentBar === null) return false;
|
|
199
|
+
const record = Held.get(obj);
|
|
200
|
+
if (!record) return false;
|
|
201
|
+
return record.count > 0 ? record.heldAt === currentBar : record.releasedAt === currentBar;
|
|
202
|
+
}
|
|
203
|
+
|
|
134
204
|
/** Linefill rule: held, or released less than 5,000 bars ago. */
|
|
135
205
|
function fillProtected(obj: object): boolean {
|
|
136
|
-
|
|
137
|
-
|
|
206
|
+
const record = Held.get(obj);
|
|
207
|
+
if (!record) return false;
|
|
208
|
+
if (record.count > 0) return true;
|
|
209
|
+
const bar = record.releasedAt;
|
|
138
210
|
return bar !== undefined && currentBar !== null && currentBar - bar < LINEFILL_RELEASE_BARS;
|
|
139
211
|
}
|
|
140
212
|
|
|
@@ -201,8 +273,8 @@ let lastBar: LastBar | null = null;
|
|
|
201
273
|
* Tells the drawing functions which bar is being calculated (`bar_index`), so that a bar index more than 10,000
|
|
202
274
|
* bars back throws as in PineScript; `null` turns the check off. `eachBar` of the script API calls it.
|
|
203
275
|
*
|
|
204
|
-
* The bar also dates the objects held by a `var` variable (
|
|
205
|
-
* the last bar of the data, which has rules of its own for the maximum count. `lastIndex` is the bar index of the
|
|
276
|
+
* The bar also dates the objects held by a `var` variable (on that bar deleted only when no other object can go,
|
|
277
|
+
* never after). `last` says that it is the last bar of the data, which has rules of its own for the maximum count. `lastIndex` is the bar index of the
|
|
206
278
|
* last bar of the data (PineScript `last_bar_index`): with it, a bar index more than 500 bars after it throws, also
|
|
207
279
|
* on the bars before the last one (RE10020).
|
|
208
280
|
*/
|
|
@@ -260,11 +332,38 @@ const COORDINATES: { [K in DrawingKind]: Array<keyof DrawingTypes[K]> } = {
|
|
|
260
332
|
table: [],
|
|
261
333
|
};
|
|
262
334
|
|
|
335
|
+
/** The live linefills of each line, in creation order: deleting a line or making a fill does not scan all the fills. */
|
|
336
|
+
let fillsOfLine = new Map<Line, Linefill[]>();
|
|
337
|
+
|
|
338
|
+
function trackFill(fill: Linefill): void {
|
|
339
|
+
for (const l of fill.line1 === fill.line2 ? [fill.line1] : [fill.line1, fill.line2]) {
|
|
340
|
+
const fills = fillsOfLine.get(l);
|
|
341
|
+
if (fills) fills.push(fill);
|
|
342
|
+
else fillsOfLine.set(l, [fill]);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function untrackFill(fill: Linefill): void {
|
|
347
|
+
for (const l of fill.line1 === fill.line2 ? [fill.line1] : [fill.line1, fill.line2]) {
|
|
348
|
+
const fills = fillsOfLine.get(l);
|
|
349
|
+
if (!fills) continue;
|
|
350
|
+
const k = fills.indexOf(fill);
|
|
351
|
+
if (k !== -1) fills.splice(k, 1);
|
|
352
|
+
if (fills.length === 0) fillsOfLine.delete(l);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** The live linefills that use `line`, in creation order (do not modify). */
|
|
357
|
+
export function liveFillsOf(line: Line): readonly Linefill[] {
|
|
358
|
+
return fillsOfLine.get(line) ?? [];
|
|
359
|
+
}
|
|
360
|
+
|
|
263
361
|
function markDeleted<K extends DrawingKind>(kind: K, obj: DrawingTypes[K], explicit: boolean): void {
|
|
264
|
-
|
|
362
|
+
Deleted.mark(obj);
|
|
265
363
|
for (const field of COORDINATES[kind]) (obj as unknown as Record<string, unknown>)[field as string] = NaN;
|
|
266
364
|
if (kind === 'line' && explicit) {
|
|
267
|
-
|
|
365
|
+
const fills = fillsOfLine.get(obj as Line);
|
|
366
|
+
if (fills) for (const fill of [...fills]) remove('linefill', fill);
|
|
268
367
|
}
|
|
269
368
|
}
|
|
270
369
|
|
|
@@ -287,19 +386,36 @@ function cutOldest<K extends DrawingKind>(
|
|
|
287
386
|
if (kind === 'table') return []; // tables have no maximum count
|
|
288
387
|
const max = limits[kind === 'linefill' ? 'line' : (kind as keyof DrawingLimits)] + allowance(kind);
|
|
289
388
|
if (list.length <= max + SLACK) return [];
|
|
290
|
-
const kept: Array<DrawingTypes[K]> = [];
|
|
291
|
-
const cut: Array<DrawingTypes[K]> = [];
|
|
292
389
|
let excess = list.length - max;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
390
|
+
const candidate = (old: DrawingTypes[K]): boolean =>
|
|
391
|
+
old !== obj && (!only || only(old)) && !(kind === 'linefill' ? fillProtected(old) : heldBefore(old));
|
|
392
|
+
// positions of the objects to delete. The oldest first; the objects a `var` variable got (or released) on this bar
|
|
393
|
+
// only when the others are not enough (the two passes pick different objects)
|
|
394
|
+
const gone: number[] = [];
|
|
395
|
+
for (const weak of kind === 'linefill' ? [false] : [false, true]) {
|
|
396
|
+
const before = gone.length;
|
|
397
|
+
for (let k = 0; k < list.length && excess > 0; k++) {
|
|
398
|
+
const old = list[k]!;
|
|
399
|
+
if (candidate(old) && (kind === 'linefill' || heldOnThisBar(old) === weak)) {
|
|
400
|
+
excess--;
|
|
401
|
+
gone.push(k);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (before > 0 && gone.length > before) gone.sort((a, b) => a - b);
|
|
405
|
+
}
|
|
406
|
+
const cut = gone.map((k) => list[k]!);
|
|
407
|
+
// the others keep their order; usually the cut is the oldest run of objects, removed at once
|
|
408
|
+
if (gone.length > 0 && gone[gone.length - 1]! - gone[0]! === gone.length - 1) list.splice(gone[0]!, gone.length);
|
|
409
|
+
else if (gone.length > 0) {
|
|
410
|
+
let to = gone[0]!;
|
|
411
|
+
for (let from = to, g = 0; from < list.length; from++) {
|
|
412
|
+
if (from === gone[g]) g++;
|
|
413
|
+
else list[to++] = list[from]!;
|
|
414
|
+
}
|
|
415
|
+
list.length = to;
|
|
298
416
|
}
|
|
299
417
|
// linefills: still above the maximum + 5, the oldest go, the held ones too
|
|
300
|
-
if (kind === 'linefill' &&
|
|
301
|
-
list.length = 0;
|
|
302
|
-
list.push(...kept);
|
|
418
|
+
if (kind === 'linefill' && list.length > max + SLACK) cut.push(...list.splice(0, list.length - max));
|
|
303
419
|
return cut;
|
|
304
420
|
}
|
|
305
421
|
|
|
@@ -318,9 +434,13 @@ export function register<K extends DrawingKind>(kind: K, obj: DrawingTypes[K]):
|
|
|
318
434
|
if (lastBar && isCounted(kind)) return registerLast(lastBar, kind, obj);
|
|
319
435
|
if (isCounted(kind) && pending[kind] >= PENDING_RESET[kind]) pending[kind] = 0;
|
|
320
436
|
list.push(obj);
|
|
437
|
+
if (kind === 'linefill') trackFill(obj as Linefill);
|
|
321
438
|
const cut = cutOldest(kind, list, obj);
|
|
322
439
|
if (kind === 'polyline') pending.polyline += cut.length;
|
|
323
|
-
for (const old of cut)
|
|
440
|
+
for (const old of cut) {
|
|
441
|
+
if (kind === 'linefill') untrackFill(old as Linefill);
|
|
442
|
+
markDeleted(kind, old, false);
|
|
443
|
+
}
|
|
324
444
|
return obj;
|
|
325
445
|
}
|
|
326
446
|
|
|
@@ -334,7 +454,7 @@ function registerLast<K extends DrawingKind>(bar: LastBar, kind: K, obj: Drawing
|
|
|
334
454
|
const list = live[kind];
|
|
335
455
|
list.push(obj);
|
|
336
456
|
const isOld = (old: object): boolean => bar.old.has(old);
|
|
337
|
-
if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old))) {
|
|
457
|
+
if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old) && !heldOnThisBar(old))) {
|
|
338
458
|
// the objects of the bars before only; a line takes its linefills with it
|
|
339
459
|
const cut = cutOldest(kind, list, obj, isOld);
|
|
340
460
|
if (kind === 'polyline') {
|
|
@@ -355,18 +475,20 @@ function registerLast<K extends DrawingKind>(bar: LastBar, kind: K, obj: Drawing
|
|
|
355
475
|
/** Deletes an object (no effect when it is already deleted). */
|
|
356
476
|
export function remove<K extends DrawingKind>(kind: K, obj: DrawingTypes[K]): void {
|
|
357
477
|
const list = live[kind];
|
|
358
|
-
|
|
478
|
+
// from the end: an object is in the list once, and scripts mostly delete their newest objects
|
|
479
|
+
const index = list.lastIndexOf(obj);
|
|
359
480
|
if (index !== -1) {
|
|
360
481
|
list.splice(index, 1);
|
|
482
|
+
if (kind === 'linefill') untrackFill(obj as Linefill);
|
|
361
483
|
if (isCounted(kind)) pending[kind]++;
|
|
362
484
|
if (kind === 'polyline' && lastBar?.old.has(obj)) lastBar.oldPolylinesGone++;
|
|
363
485
|
}
|
|
364
|
-
if (!
|
|
486
|
+
if (!Deleted.has(obj)) markDeleted(kind, obj, true);
|
|
365
487
|
}
|
|
366
488
|
|
|
367
489
|
/** True when the object was deleted (by `delete` or by the maximum count). */
|
|
368
490
|
export function isDeleted(obj: unknown): boolean {
|
|
369
|
-
return typeof obj === 'object' && obj !== null &&
|
|
491
|
+
return typeof obj === 'object' && obj !== null && Deleted.has(obj);
|
|
370
492
|
}
|
|
371
493
|
|
|
372
494
|
/** The live objects of a kind, in creation order (a new array). */
|
|
@@ -377,10 +499,9 @@ export function all<K extends DrawingKind>(kind: K): Array<DrawingTypes[K]> {
|
|
|
377
499
|
/** Forgets all objects (without deleting them) and sets the maximum counts (default 50). */
|
|
378
500
|
export function resetDrawings(newLimits: DrawingLimits = {}): void {
|
|
379
501
|
for (const kind of Object.keys(live) as DrawingKind[]) live[kind].length = 0;
|
|
502
|
+
fillsOfLine = new Map();
|
|
380
503
|
created.clear();
|
|
381
|
-
|
|
382
|
-
releasedAt = new WeakMap();
|
|
383
|
-
heldAt = new WeakMap();
|
|
504
|
+
holds = { others: new WeakMap() };
|
|
384
505
|
pending = { line: 0, label: 0, box: 0, polyline: 0 };
|
|
385
506
|
currentBar = null;
|
|
386
507
|
lastBarIndex = null;
|
|
@@ -402,9 +523,7 @@ export function setDrawingLimits(newLimits: DrawingLimits): void {
|
|
|
402
523
|
export interface DrawingState {
|
|
403
524
|
live: { [K in DrawingKind]: Array<DrawingTypes[K]> };
|
|
404
525
|
created: DrawingKind[];
|
|
405
|
-
|
|
406
|
-
releasedAt: WeakMap<object, number>;
|
|
407
|
-
heldAt: WeakMap<object, number>;
|
|
526
|
+
holds: Holds;
|
|
408
527
|
pending: Record<CountedKind, number>;
|
|
409
528
|
currentBar: number | null;
|
|
410
529
|
lastBarIndex: number | null;
|
|
@@ -424,9 +543,7 @@ export function saveDrawingState(): DrawingState {
|
|
|
424
543
|
table: [...live.table],
|
|
425
544
|
},
|
|
426
545
|
created: [...created],
|
|
427
|
-
|
|
428
|
-
releasedAt,
|
|
429
|
-
heldAt,
|
|
546
|
+
holds,
|
|
430
547
|
pending: { ...pending },
|
|
431
548
|
currentBar,
|
|
432
549
|
lastBarIndex,
|
|
@@ -442,11 +559,11 @@ export function restoreDrawingState(s: DrawingState): void {
|
|
|
442
559
|
list.length = 0;
|
|
443
560
|
list.push(...(s.live[kind] as object[]));
|
|
444
561
|
}
|
|
562
|
+
fillsOfLine = new Map();
|
|
563
|
+
for (const fill of live.linefill) trackFill(fill);
|
|
445
564
|
created.clear();
|
|
446
565
|
for (const kind of s.created) created.add(kind);
|
|
447
|
-
|
|
448
|
-
releasedAt = s.releasedAt;
|
|
449
|
-
heldAt = s.heldAt;
|
|
566
|
+
holds = s.holds;
|
|
450
567
|
pending = { ...s.pending };
|
|
451
568
|
currentBar = s.currentBar;
|
|
452
569
|
lastBarIndex = s.lastBarIndex;
|
package/src/index.ts
CHANGED
|
@@ -225,7 +225,7 @@ export type { LightweightSeriesDefinitions } from './runtime/adapters/Lightweigh
|
|
|
225
225
|
export { SimpleInputAdapter } from './runtime/adapters/SimpleInputAdapter.js';
|
|
226
226
|
|
|
227
227
|
// Version
|
|
228
|
-
export const VERSION = '0.15.
|
|
228
|
+
export const VERSION = '0.15.2';
|
|
229
229
|
|
|
230
230
|
/**
|
|
231
231
|
* Library information
|
package/src/linefill/index.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @version 6
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import { all as allDrawings, isDeleted, register, remove } from '../drawing/registry.js';
|
|
13
|
+
import { Stamped, all as allDrawings, isDeleted, liveFillsOf, register, remove } from '../drawing/registry.js';
|
|
14
14
|
import type { Linefill, Line, color } from '../types/index.js';
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -46,13 +46,13 @@ export function new_linefill(line1: Line, line2: Line, fillColor?: color): Linef
|
|
|
46
46
|
// PineScript: an na line gives an na linefill
|
|
47
47
|
if (line1 === null || line1 === undefined || line2 === null || line2 === undefined) return null as unknown as Linefill;
|
|
48
48
|
let found: Linefill | undefined;
|
|
49
|
-
for (const fill of
|
|
49
|
+
for (const fill of liveFillsOf(line1)) {
|
|
50
50
|
const has = (l: Line): boolean => fill.line1 === l || fill.line2 === l;
|
|
51
|
-
if (has(
|
|
51
|
+
if (has(line2) && (!found || Touched.get(fill) > Touched.get(found))) found = fill;
|
|
52
52
|
}
|
|
53
53
|
if (found) {
|
|
54
54
|
found.color = fillColor;
|
|
55
|
-
|
|
55
|
+
Touched.set(found, ++touchCount);
|
|
56
56
|
return found;
|
|
57
57
|
}
|
|
58
58
|
const fill = register('linefill', {
|
|
@@ -60,12 +60,21 @@ export function new_linefill(line1: Line, line2: Line, fillColor?: color): Linef
|
|
|
60
60
|
line2,
|
|
61
61
|
color: fillColor
|
|
62
62
|
});
|
|
63
|
-
|
|
63
|
+
Touched.set(fill, ++touchCount);
|
|
64
64
|
return fill;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
/** Order in which the linefills were created or returned by `linefill.new` (the last one wins a lookup). */
|
|
68
|
-
|
|
67
|
+
/** Order in which the linefills were created or returned by `linefill.new` (the last one wins a lookup); 0 before. */
|
|
68
|
+
class Touched extends Stamped {
|
|
69
|
+
#touch = 0;
|
|
70
|
+
static get(fill: Linefill): number {
|
|
71
|
+
return #touch in fill ? fill.#touch : 0;
|
|
72
|
+
}
|
|
73
|
+
static set(fill: Linefill, order: number): void {
|
|
74
|
+
if (!(#touch in fill)) new Touched(fill);
|
|
75
|
+
(fill as unknown as Touched).#touch = order;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
69
78
|
let touchCount = 0;
|
|
70
79
|
|
|
71
80
|
/**
|
package/src/math/index.ts
CHANGED
|
@@ -467,13 +467,9 @@ function avgOf(nums: number[]): number {
|
|
|
467
467
|
* - Calculates the sum of the last `length` non-`na` values at each point; an `na` value (also +/-Infinity) is
|
|
468
468
|
* skipped and the bar keeps the previous result (as in PineScript)
|
|
469
469
|
* - Returns NaN until `length` non-`na` values are available
|
|
470
|
-
* -
|
|
471
|
-
* bits (bit for bit with PineScript)
|
|
470
|
+
* - The correctly rounded sum of the window values (the exact sum, rounded once), as PineScript (#188)
|
|
472
471
|
* - When passed a Series, returns a Series; when passed an array, returns an array
|
|
473
|
-
* - With a series length,
|
|
474
|
-
* steps of the raw values: a smaller length removes the values that leave the window (oldest first); a larger
|
|
475
|
-
* length adds back the values from the one before the old window down to the one `length` values back (newest
|
|
476
|
-
* first), then the usual step removes the stored value of the one `length` values back. A bar with an `na` value
|
|
472
|
+
* - With a series length, the window is the last `length` values of each bar. A bar with an `na` value
|
|
477
473
|
* gives the sum of the last `length` earlier values
|
|
478
474
|
*
|
|
479
475
|
* @example
|
|
@@ -492,7 +488,7 @@ export function sum(
|
|
|
492
488
|
const sourceArray = source instanceof Series ? source.toArray() : source;
|
|
493
489
|
if (typeof length === 'number') checkSumLength('math.sum', length, 0);
|
|
494
490
|
// PineScript: the sum of the last `length` non-na values (also +/-Infinity skipped); an na bar keeps the previous
|
|
495
|
-
// result.
|
|
491
|
+
// result. The correctly rounded window sum, bit for bit as PineScript (#188).
|
|
496
492
|
const result: series_float =
|
|
497
493
|
typeof length === 'number'
|
|
498
494
|
? runningSum(sourceArray, length)
|
|
@@ -35,10 +35,26 @@ const view = new DataView(new ArrayBuffer(8));
|
|
|
35
35
|
const int32 = (v: bigint): bigint => BigInt.asIntN(32, v);
|
|
36
36
|
const int64 = (v: bigint): bigint => BigInt.asIntN(64, v);
|
|
37
37
|
|
|
38
|
+
/** 5^k, made once (k up to 1074, the binary exponents of the doubles). */
|
|
39
|
+
const powersOf5: bigint[] = [1n];
|
|
40
|
+
export function pow5(k: number): bigint {
|
|
41
|
+
while (powersOf5.length <= k) powersOf5.push(powersOf5[powersOf5.length - 1]! * 5n);
|
|
42
|
+
return powersOf5[k]!;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** `s` without its trailing '0' characters. */
|
|
46
|
+
export function trimZeros(s: string): string {
|
|
47
|
+
let end = s.length;
|
|
48
|
+
while (end > 0 && s.charCodeAt(end - 1) === 48) end--;
|
|
49
|
+
return s.slice(0, end);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Trailing zero bits of a significand (not 0, below 2^53: exact as a double). */
|
|
38
53
|
function trailingZeros(v: bigint): number {
|
|
54
|
+
let x = Number(v);
|
|
39
55
|
let n = 0;
|
|
40
|
-
while (
|
|
41
|
-
|
|
56
|
+
while (x % 2 === 0) {
|
|
57
|
+
x /= 2;
|
|
42
58
|
n++;
|
|
43
59
|
}
|
|
44
60
|
return n;
|
|
@@ -96,7 +112,7 @@ export function convertDouble(x: number): ConvertedDigits {
|
|
|
96
112
|
if (residue >= pow10 >> 1n) lvalue++;
|
|
97
113
|
}
|
|
98
114
|
const s = lvalue.toString();
|
|
99
|
-
return { digits: s
|
|
115
|
+
return { digits: trimZeros(s), decExponent: insignificant + s.length, roundedUp: false, exact: false };
|
|
100
116
|
}
|
|
101
117
|
|
|
102
118
|
let decExp = estimateDecExp(fractBits, binExp);
|
|
@@ -134,9 +150,9 @@ export function convertDouble(x: number): ConvertedDigits {
|
|
|
134
150
|
if (Bbits < 64 && tenSbits < 64) {
|
|
135
151
|
// int or long arithmetic, with its overflows
|
|
136
152
|
const wrap = Bbits < 32 && tenSbits < 32 ? int32 : int64;
|
|
137
|
-
let b = wrap((fractBits *
|
|
138
|
-
const s = wrap(
|
|
139
|
-
let m = wrap(
|
|
153
|
+
let b = wrap((fractBits * pow5(B5)) << BigInt(B2));
|
|
154
|
+
const s = wrap(pow5(S5) << BigInt(S2));
|
|
155
|
+
let m = wrap(pow5(M5) << BigInt(M2));
|
|
140
156
|
const tens = wrap(s * 10n);
|
|
141
157
|
let q = Number(b / s);
|
|
142
158
|
b = wrap(10n * (b % s));
|
|
@@ -162,9 +178,9 @@ export function convertDouble(x: number): ConvertedDigits {
|
|
|
162
178
|
lowDigitDifference = wrap((b << 1n) - tens);
|
|
163
179
|
exact = b === 0n;
|
|
164
180
|
} else {
|
|
165
|
-
let b = (fractBits *
|
|
166
|
-
const s =
|
|
167
|
-
let m =
|
|
181
|
+
let b = (fractBits * pow5(B5)) << BigInt(B2);
|
|
182
|
+
const s = pow5(S5) << BigInt(S2);
|
|
183
|
+
let m = pow5(M5 + 1) << BigInt(M2 + 1);
|
|
168
184
|
const tens = s * 10n;
|
|
169
185
|
let q = Number(b / s);
|
|
170
186
|
b = (b % s) * 10n;
|
|
@@ -204,7 +220,7 @@ export function convertDouble(x: number): ConvertedDigits {
|
|
|
204
220
|
if (digits[digits.length - 1]! % 2 !== 0) roundup();
|
|
205
221
|
} else if (lowDigitDifference > 0n) roundup();
|
|
206
222
|
}
|
|
207
|
-
const text = digits.join('')
|
|
223
|
+
const text = trimZeros(digits.join(''));
|
|
208
224
|
return { digits: text || '0', decExponent, roundedUp, exact };
|
|
209
225
|
}
|
|
210
226
|
|
|
@@ -263,6 +279,6 @@ export function javaFixedDigits(x: number, maxFrac: number): { digits: string; i
|
|
|
263
279
|
count = keep;
|
|
264
280
|
while (count > 1 && digits[count - 1] === 0) count--;
|
|
265
281
|
}
|
|
266
|
-
const text = digits.slice(0, count).join('')
|
|
282
|
+
const text = trimZeros(digits.slice(0, count).join(''));
|
|
267
283
|
return { digits: text, intLen: text ? decimalAt : 0 };
|
|
268
284
|
}
|
package/src/str/messageformat.ts
CHANGED
|
@@ -72,24 +72,39 @@ function formatArgument(
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
/** A `{index[,type[,style]]}` element of a pattern; `inner` is the text between the braces. */
|
|
76
|
+
interface Placeholder {
|
|
77
|
+
inner: string;
|
|
78
|
+
index: number;
|
|
79
|
+
type: string | undefined;
|
|
80
|
+
style: string | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Parsed patterns (literal text and placeholders): a script formats the same pattern on every bar. */
|
|
84
|
+
const templates = new Map<string, Array<string | Placeholder>>();
|
|
85
|
+
/** Cached patterns before the cache restarts. */
|
|
86
|
+
const MAX_TEMPLATES = 1000;
|
|
87
|
+
|
|
88
|
+
function parseTemplate(pattern: string): Array<string | Placeholder> {
|
|
89
|
+
const cached = templates.get(pattern);
|
|
90
|
+
if (cached) return cached;
|
|
91
|
+
const parts: Array<string | Placeholder> = [];
|
|
92
|
+
let text = '';
|
|
78
93
|
for (let i = 0; i < pattern.length; ) {
|
|
79
94
|
const ch = pattern[i]!;
|
|
80
95
|
if (ch === "'") {
|
|
81
96
|
if (pattern[i + 1] === "'") {
|
|
82
|
-
|
|
97
|
+
text += "'";
|
|
83
98
|
i += 2;
|
|
84
99
|
continue;
|
|
85
100
|
}
|
|
86
101
|
const end = pattern.indexOf("'", i + 1);
|
|
87
|
-
|
|
102
|
+
text += pattern.slice(i + 1, end === -1 ? undefined : end);
|
|
88
103
|
i = end === -1 ? pattern.length : end + 1;
|
|
89
104
|
continue;
|
|
90
105
|
}
|
|
91
106
|
if (ch !== '{') {
|
|
92
|
-
|
|
107
|
+
text += ch;
|
|
93
108
|
i++;
|
|
94
109
|
continue;
|
|
95
110
|
}
|
|
@@ -105,10 +120,26 @@ export function formatMessage(pattern: string, args: unknown[], timezone: string
|
|
|
105
120
|
}
|
|
106
121
|
const inner = pattern.slice(i + 1, j - 1);
|
|
107
122
|
const [index = '', type, ...rest] = inner.split(',');
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
123
|
+
if (text) parts.push(text);
|
|
124
|
+
text = '';
|
|
125
|
+
parts.push({ inner, index: Number(index.trim()), type, style: rest.length ? rest.join(',') : undefined });
|
|
111
126
|
i = j;
|
|
112
127
|
}
|
|
128
|
+
if (text) parts.push(text);
|
|
129
|
+
if (templates.size >= MAX_TEMPLATES) templates.clear();
|
|
130
|
+
templates.set(pattern, parts);
|
|
131
|
+
return parts;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Formats `pattern` with `args` like PineScript `str.format`; dates use `timezone` (read only when a date is formatted). */
|
|
135
|
+
export function formatMessage(pattern: string, args: unknown[], timezone: string | (() => string)): string {
|
|
136
|
+
let out = '';
|
|
137
|
+
for (const part of parseTemplate(pattern)) {
|
|
138
|
+
if (typeof part === 'string') out += part;
|
|
139
|
+
else {
|
|
140
|
+
const n = part.index;
|
|
141
|
+
out += n < args.length ? formatArgument(args[n], part.type, part.style, timezone) : `{${part.inner}}`;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
113
144
|
return out;
|
|
114
145
|
}
|
package/src/str/numberformat.ts
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* 0.1 with 20 '#' decimals gives "0.1" in both rounding modes
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import { javaFixedDigits } from './floatingdecimal.js';
|
|
25
|
+
import { javaFixedDigits, pow5, trimZeros } from './floatingdecimal.js';
|
|
26
26
|
|
|
27
27
|
export type Rounding = 'halfUpShortest' | 'halfUpExact' | 'halfEvenExact' | 'halfEvenConverted';
|
|
28
28
|
|
|
@@ -134,30 +134,26 @@ function shortestDigits(x: number): Digits {
|
|
|
134
134
|
return { digits, intLen: digits ? intLen : 0 };
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
+
const bitsView = new DataView(new ArrayBuffer(8));
|
|
138
|
+
|
|
137
139
|
/** Exact decimal expansion of the double |x|. */
|
|
138
140
|
function exactDigits(x: number): Digits {
|
|
139
141
|
const abs = Math.abs(x);
|
|
140
142
|
if (abs === 0) return { digits: '', intLen: 0 };
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
// an integer below 2^53 prints exactly
|
|
144
|
+
if (Number.isSafeInteger(abs)) {
|
|
145
|
+
const str = String(abs);
|
|
146
|
+
return { digits: trimZeros(str), intLen: str.length };
|
|
147
|
+
}
|
|
148
|
+
bitsView.setFloat64(0, abs);
|
|
149
|
+
const bits = bitsView.getBigUint64(0);
|
|
144
150
|
const expBits = Number((bits >> 52n) & 0x7ffn);
|
|
145
151
|
let mant = bits & 0xfffffffffffffn;
|
|
146
152
|
const e2 = expBits === 0 ? -1074 : expBits - 1075;
|
|
147
153
|
if (expBits !== 0) mant |= 1n << 52n;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
str = (mant << BigInt(e2)).toString();
|
|
152
|
-
fracDigits = 0;
|
|
153
|
-
} else {
|
|
154
|
-
str = (mant * 5n ** BigInt(-e2)).toString();
|
|
155
|
-
fracDigits = -e2;
|
|
156
|
-
}
|
|
157
|
-
const intLen = str.length - fracDigits;
|
|
158
|
-
const lead = str.length - str.replace(/^0+/, '').length;
|
|
159
|
-
const digits = str.slice(lead).replace(/0+$/, '');
|
|
160
|
-
return { digits, intLen: intLen - lead };
|
|
154
|
+
// the digits of mant × 2^e2 (mant × 5^-e2 / 10^-e2 when e2 < 0); mant is not 0, so no leading zero
|
|
155
|
+
const str = (e2 >= 0 ? mant << BigInt(e2) : mant * pow5(-e2)).toString();
|
|
156
|
+
return { digits: trimZeros(str), intLen: str.length - Math.max(0, -e2) };
|
|
161
157
|
}
|
|
162
158
|
|
|
163
159
|
/** Rounds to `frac` digits after the point. */
|