oakscriptjs 0.15.1 → 0.15.3
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.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 +31 -3
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +188 -68
- 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/session/bars.d.ts +3 -0
- package/dist/cjs/session/bars.d.ts.map +1 -1
- package/dist/cjs/session/bars.js +10 -3
- package/dist/cjs/session/bars.js.map +2 -2
- package/dist/cjs/session/calendar.d.ts +19 -6
- package/dist/cjs/session/calendar.d.ts.map +1 -1
- package/dist/cjs/session/calendar.js +57 -18
- package/dist/cjs/session/calendar.js.map +2 -2
- package/dist/cjs/session/context.d.ts.map +1 -1
- package/dist/cjs/session/context.js +25 -8
- package/dist/cjs/session/context.js.map +2 -2
- 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.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 +8 -1
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +13 -1
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/cjs/time/timezone.d.ts +13 -1
- package/dist/cjs/time/timezone.d.ts.map +1 -1
- package/dist/cjs/time/timezone.js +64 -12
- package/dist/cjs/time/timezone.js.map +2 -2
- package/dist/esm/callsite/index.d.ts.map +1 -1
- package/dist/esm/callsite/index.js +76 -7
- package/dist/esm/callsite/index.js.map +1 -1
- package/dist/esm/drawing/registry.d.ts +31 -3
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +237 -80
- 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/session/bars.d.ts +3 -0
- package/dist/esm/session/bars.d.ts.map +1 -1
- package/dist/esm/session/bars.js +11 -3
- package/dist/esm/session/bars.js.map +1 -1
- package/dist/esm/session/calendar.d.ts +19 -6
- package/dist/esm/session/calendar.d.ts.map +1 -1
- package/dist/esm/session/calendar.js +74 -21
- package/dist/esm/session/calendar.js.map +1 -1
- package/dist/esm/session/context.d.ts.map +1 -1
- package/dist/esm/session/context.js +34 -9
- package/dist/esm/session/context.js.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.map +1 -1
- package/dist/esm/ta/index.js +35 -0
- package/dist/esm/ta/index.js.map +1 -1
- package/dist/esm/ta/running-sum.d.ts +8 -1
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +16 -1
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/dist/esm/time/timezone.d.ts +13 -1
- package/dist/esm/time/timezone.d.ts.map +1 -1
- package/dist/esm/time/timezone.js +97 -16
- package/dist/esm/time/timezone.js.map +1 -1
- package/package.json +1 -1
- package/src/callsite/index.ts +65 -7
- package/src/drawing/registry.ts +249 -79
- package/src/index.ts +1 -1
- package/src/linefill/index.ts +16 -7
- package/src/session/bars.ts +11 -3
- package/src/session/calendar.ts +84 -21
- package/src/session/context.ts +36 -9
- 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 +41 -0
- package/src/ta/running-sum.ts +13 -1
- package/src/time/timezone.ts +99 -16
package/src/session/calendar.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* dates (early closes, days off); holidays ("session_holidays") are dates without session
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
import {
|
|
21
|
+
import { civilFromDays, zoneOffset, zonedToUnix } from '../time/timezone.js';
|
|
22
22
|
import { parsePineSession } from '../time/session.js';
|
|
23
23
|
|
|
24
24
|
/** Session information of a symbol (hours, corrections, holidays). */
|
|
@@ -54,13 +54,40 @@ interface Version {
|
|
|
54
54
|
|
|
55
55
|
export const toDateNum = (y: number, m: number, d: number): DateNum => y * 10000 + m * 100 + d;
|
|
56
56
|
const utcOf = (date: DateNum): number => Date.UTC(Math.floor(date / 10000), Math.floor(date / 100) % 100 - 1, date % 100);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
|
|
57
|
+
|
|
58
|
+
const MS_PER_DAY = 86_400_000;
|
|
59
|
+
/** Days of a valid `Date` on each side of 01/01/1970 (8.64e15 ms) */
|
|
60
|
+
const MAX_DAY = 100_000_000;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Days since 01/01/1970 of a date, as `utcOf(date) / 1 day` but without a `Date`: the calendar functions step
|
|
64
|
+
* through days on every bar. Years before 100 and months outside 1-12 keep the `Date.UTC` reading (years 0-99 are
|
|
65
|
+
* 1900-1999 there). NaN outside the `Date` range.
|
|
66
|
+
*/
|
|
67
|
+
function dayOf(date: DateNum): number {
|
|
68
|
+
let y = Math.floor(date / 10000);
|
|
69
|
+
const m = Math.floor(date / 100) % 100;
|
|
70
|
+
if (!(y >= 100 && m >= 1 && m <= 12)) return utcOf(date) / MS_PER_DAY;
|
|
71
|
+
// proleptic Gregorian day count, years starting in March (the leap day last)
|
|
72
|
+
if (m <= 2) y--;
|
|
73
|
+
const era = Math.floor(y / 400);
|
|
74
|
+
const yearOfEra = y - era * 400;
|
|
75
|
+
const dayOfYear = Math.floor((153 * (m > 2 ? m - 3 : m + 9) + 2) / 5) + (date % 100) - 1;
|
|
76
|
+
const day = era * 146_097 + yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear - 719_468;
|
|
77
|
+
return Math.abs(day) <= MAX_DAY ? day : NaN;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Date of a day since 01/01/1970 (the inverse of `dayOf`); NaN outside the `Date` range. */
|
|
81
|
+
function dateOf(day: number): DateNum {
|
|
82
|
+
if (!(Math.abs(day) <= MAX_DAY)) return NaN;
|
|
83
|
+
const { year, month, day: d } = civilFromDays(day);
|
|
84
|
+
return toDateNum(year, month, d);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The date `n` (whole) days after `date`. */
|
|
88
|
+
export const addDays = (date: DateNum, n: number): DateNum => dateOf(dayOf(date) + n);
|
|
89
|
+
/** 1 = Sunday ... 7 = Saturday (01/01/1970 is a Thursday) */
|
|
90
|
+
export const weekdayOf = (date: DateNum): number => ((((dayOf(date) + 4) % 7) + 7) % 7) + 1;
|
|
64
91
|
/** 00:00 UTC of the date, in ms (PineScript `time_tradingday`). */
|
|
65
92
|
export const dateUtcMs = utcOf;
|
|
66
93
|
|
|
@@ -132,13 +159,25 @@ function parseVersion(text: string, from: DateNum, spec: string, rule: 'symbol'
|
|
|
132
159
|
return { from, periods, days };
|
|
133
160
|
}
|
|
134
161
|
|
|
162
|
+
/** Trading day and period of a time (see `TradingCalendar.locate`). */
|
|
163
|
+
interface Located {
|
|
164
|
+
date: DateNum;
|
|
165
|
+
index: number;
|
|
166
|
+
inside: boolean;
|
|
167
|
+
}
|
|
168
|
+
|
|
135
169
|
/** Session periods of every trading day of a symbol, as UNIX times (ms). */
|
|
136
170
|
export class TradingCalendar {
|
|
137
171
|
private readonly versions: Version[];
|
|
138
172
|
private readonly corrections = new Map<DateNum, Period[] | null>();
|
|
139
173
|
private readonly holidays: Set<DateNum>;
|
|
140
174
|
private readonly cache = new Map<DateNum, Array<[number, number]>>();
|
|
175
|
+
/** The date and periods of each day since 01/01/1970 that `search` looked at */
|
|
176
|
+
private readonly days = new Map<number, { date: DateNum; periods: Array<[number, number]> }>();
|
|
141
177
|
private readonly pine: boolean;
|
|
178
|
+
/** The last time located and its result: the functions of one bar locate the same time several times */
|
|
179
|
+
private lastTime = NaN;
|
|
180
|
+
private lastAt: Located | null = null;
|
|
142
181
|
|
|
143
182
|
/**
|
|
144
183
|
* @param timezone - Time zone of the session hours (the exchange time zone)
|
|
@@ -180,7 +219,7 @@ export class TradingCalendar {
|
|
|
180
219
|
if (correction !== undefined) {
|
|
181
220
|
periods = correction ?? [];
|
|
182
221
|
} else if (!this.holidays.has(date)) {
|
|
183
|
-
const version =
|
|
222
|
+
const version = this.versionOf(date);
|
|
184
223
|
if (version.byDay) periods = version.byDay[weekdayOf(date)]!;
|
|
185
224
|
else if (version.days[weekdayOf(date)]) periods = version.periods;
|
|
186
225
|
}
|
|
@@ -204,6 +243,13 @@ export class TradingCalendar {
|
|
|
204
243
|
return out;
|
|
205
244
|
}
|
|
206
245
|
|
|
246
|
+
/** The session version in force on `date` (the last one that starts on or before it). */
|
|
247
|
+
private versionOf(date: DateNum): Version {
|
|
248
|
+
let k = this.versions.length - 1;
|
|
249
|
+
while (k >= 0 && !(this.versions[k]!.from <= date)) k--;
|
|
250
|
+
return this.versions[k]!;
|
|
251
|
+
}
|
|
252
|
+
|
|
207
253
|
isTradingDay(date: DateNum): boolean {
|
|
208
254
|
return this.periods(date).length > 0;
|
|
209
255
|
}
|
|
@@ -215,7 +261,7 @@ export class TradingCalendar {
|
|
|
215
261
|
afterEarlyClose(time: number): boolean {
|
|
216
262
|
const at = this.locate(time);
|
|
217
263
|
if (!at || at.inside || !this.corrections.get(at.date)) return false;
|
|
218
|
-
const version =
|
|
264
|
+
const version = this.versionOf(at.date);
|
|
219
265
|
const usual = version.byDay ? version.byDay[weekdayOf(at.date)]! : version.days[weekdayOf(at.date)] ? version.periods : [];
|
|
220
266
|
const y = Math.floor(at.date / 10000);
|
|
221
267
|
const m = Math.floor(at.date / 100) % 100;
|
|
@@ -231,26 +277,43 @@ export class TradingCalendar {
|
|
|
231
277
|
* Trading day and period of a time: the period that contains it, else the last period that started
|
|
232
278
|
* before it. Null when no trading day is found near the time.
|
|
233
279
|
*/
|
|
234
|
-
locate(time: number):
|
|
235
|
-
|
|
236
|
-
|
|
280
|
+
locate(time: number): Located | null {
|
|
281
|
+
if (time !== this.lastTime) {
|
|
282
|
+
this.lastAt = this.search(time);
|
|
283
|
+
this.lastTime = time;
|
|
284
|
+
}
|
|
285
|
+
const at = this.lastAt;
|
|
286
|
+
return at && { date: at.date, index: at.index, inside: at.inside };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private search(time: number): Located | null {
|
|
290
|
+
// the local date of the time, as `Date` reads the wall clock (whole ms, NaN outside its range)
|
|
291
|
+
const wall = time + zoneOffset(time, this.timezone);
|
|
292
|
+
const local = dayOf(dateOf(Math.abs(wall) <= MAX_DAY * MS_PER_DAY ? Math.floor(Math.trunc(wall) / MS_PER_DAY) : NaN));
|
|
237
293
|
for (let k = 0; k <= 5; k++) {
|
|
238
|
-
const date =
|
|
239
|
-
const index =
|
|
294
|
+
const { date, periods } = this.dayAt(local + k);
|
|
295
|
+
const index = periods.findIndex(([s, e]) => s <= time && time < e);
|
|
240
296
|
if (index !== -1) return { date, index, inside: true };
|
|
241
297
|
}
|
|
242
298
|
for (let k = 5; k >= -7; k--) {
|
|
243
|
-
const date =
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
periods.forEach(([s], i) => {
|
|
247
|
-
if (s <= time) index = i;
|
|
248
|
-
});
|
|
299
|
+
const { date, periods } = this.dayAt(local + k);
|
|
300
|
+
let index = periods.length - 1;
|
|
301
|
+
while (index >= 0 && !(periods[index]![0] <= time)) index--;
|
|
249
302
|
if (index !== -1) return { date, index, inside: false };
|
|
250
303
|
}
|
|
251
304
|
return null;
|
|
252
305
|
}
|
|
253
306
|
|
|
307
|
+
private dayAt(day: number): { date: DateNum; periods: Array<[number, number]> } {
|
|
308
|
+
let at = this.days.get(day);
|
|
309
|
+
if (!at) {
|
|
310
|
+
const date = dateOf(day);
|
|
311
|
+
at = { date, periods: this.periods(date) };
|
|
312
|
+
this.days.set(day, at);
|
|
313
|
+
}
|
|
314
|
+
return at;
|
|
315
|
+
}
|
|
316
|
+
|
|
254
317
|
/** First trading day in [from, to] (dates), or null. */
|
|
255
318
|
firstTradingDay(from: DateNum, to: DateNum): DateNum | null {
|
|
256
319
|
for (let date = from; date <= to; date = addDays(date, 1)) if (this.isTradingDay(date)) return date;
|
package/src/session/context.ts
CHANGED
|
@@ -68,6 +68,26 @@ export interface SessionPeriods {
|
|
|
68
68
|
close: (time: number) => number;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** Calendars of the `session` / `timezone` arguments before the cache restarts */
|
|
72
|
+
const MAX_ARGUMENT_CALENDARS = 64;
|
|
73
|
+
const argumentCalendars = new Map<string, TradingCalendar>();
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Calendar of a session string read in a time zone, without corrections and holidays: it depends on these only, so
|
|
77
|
+
* the calls with other timeframes or other chart contexts share it and its periods (a script often reads
|
|
78
|
+
* `time(timeframe.period, s)` and `time("D", s)` on every bar).
|
|
79
|
+
*/
|
|
80
|
+
function argumentCalendar(timezone: string, session: string, rule: 'symbol' | 'pine', dayPlacement: boolean): TradingCalendar {
|
|
81
|
+
const key = `${rule}|${dayPlacement}|${timezone}|${session}`;
|
|
82
|
+
let calendar = argumentCalendars.get(key);
|
|
83
|
+
if (!calendar) {
|
|
84
|
+
calendar = new TradingCalendar(timezone, { session }, rule, dayPlacement);
|
|
85
|
+
if (argumentCalendars.size >= MAX_ARGUMENT_CALENDARS) argumentCalendars.clear();
|
|
86
|
+
argumentCalendars.set(key, calendar);
|
|
87
|
+
}
|
|
88
|
+
return calendar;
|
|
89
|
+
}
|
|
90
|
+
|
|
71
91
|
/**
|
|
72
92
|
* Calendar for the `session` / `timezone` arguments of `time(tf, session, timezone)`. Without both: the loaded
|
|
73
93
|
* session. As in PineScript, an explicit time zone reads the symbol session hours in that zone without corrections
|
|
@@ -87,11 +107,13 @@ function calendarFor(
|
|
|
87
107
|
if (session !== undefined && parsePineSession(session) === null) {
|
|
88
108
|
const name = session.trim();
|
|
89
109
|
const symbol = name === 'regular' && chart.regularSession ? chart.regularSession : chart.session!;
|
|
90
|
-
return { bars: new SessionBars(
|
|
110
|
+
return { bars: new SessionBars(argumentCalendar(tz, toSpec(symbol).session, 'symbol', false), bars.chart.period), test: true };
|
|
91
111
|
}
|
|
92
|
-
const spec = session ? { session } : { session: toSpec(chart.session!).session };
|
|
93
112
|
return {
|
|
94
|
-
bars: new SessionBars(
|
|
113
|
+
bars: new SessionBars(
|
|
114
|
+
argumentCalendar(tz, session || toSpec(chart.session!).session, session ? 'pine' : 'symbol', dayPlacement),
|
|
115
|
+
bars.chart.period
|
|
116
|
+
),
|
|
95
117
|
test: true,
|
|
96
118
|
// a PineScript session string: the rules of time.inSession (runs ending at 0000, days around a clock change)
|
|
97
119
|
inside: session ? (time) => sessionRun(time, session, tz, in_seconds(bars.chart.period) * 1000) !== null : undefined,
|
|
@@ -138,15 +160,20 @@ export function sessionPeriods(
|
|
|
138
160
|
return { open, close };
|
|
139
161
|
}
|
|
140
162
|
|
|
141
|
-
|
|
163
|
+
/** Periods per chart, timeframe, session and time zone (no session / time zone is ""): maps of the given strings, no
|
|
164
|
+
* key to build on every bar */
|
|
165
|
+
const periodCache = new WeakMap<object, Map<string, Map<string, Map<string, SessionPeriods>>>>();
|
|
142
166
|
|
|
143
167
|
function periodsOfChart(chart: SessionContext, timeframe: string, session?: string, timezone?: string): { s: ChartSessions; p: SessionPeriods } {
|
|
144
168
|
const s = chartSessions(chart);
|
|
145
|
-
let
|
|
146
|
-
if (!
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
169
|
+
let byTimeframe = periodCache.get(chart);
|
|
170
|
+
if (!byTimeframe) periodCache.set(chart, (byTimeframe = new Map()));
|
|
171
|
+
let bySession = byTimeframe.get(timeframe);
|
|
172
|
+
if (!bySession) byTimeframe.set(timeframe, (bySession = new Map()));
|
|
173
|
+
let byZone = bySession.get(session ?? '');
|
|
174
|
+
if (!byZone) bySession.set(session ?? '', (byZone = new Map()));
|
|
175
|
+
let p = byZone.get(timezone ?? '');
|
|
176
|
+
if (!p) byZone.set(timezone ?? '', (p = sessionPeriods(s.bars, chart, timeframe, session, timezone)));
|
|
150
177
|
return { s, p };
|
|
151
178
|
}
|
|
152
179
|
|
|
@@ -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. */
|
package/src/ta/index.ts
CHANGED
|
@@ -1871,6 +1871,9 @@ type PivotLength = simple_int | ArrayLike<number>;
|
|
|
1871
1871
|
*/
|
|
1872
1872
|
function pivot(source: Source, leftbars: PivotLength, rightbars: PivotLength, isHigh: boolean): series_float {
|
|
1873
1873
|
const { values, bars, last } = keptValues(source);
|
|
1874
|
+
if (typeof leftbars === 'number' && typeof rightbars === 'number') {
|
|
1875
|
+
return pivotConstant(values, bars, last, Math.trunc(leftbars), Math.trunc(rightbars), isHigh);
|
|
1876
|
+
}
|
|
1874
1877
|
return Array.from(source, (_, i) => {
|
|
1875
1878
|
// a series length: the lengths of the current bar
|
|
1876
1879
|
const left = Math.trunc(typeof leftbars === 'number' ? leftbars : leftbars[i]!);
|
|
@@ -1885,6 +1888,44 @@ function pivot(source: Source, leftbars: PivotLength, rightbars: PivotLength, is
|
|
|
1885
1888
|
});
|
|
1886
1889
|
}
|
|
1887
1890
|
|
|
1891
|
+
/**
|
|
1892
|
+
* {@link pivot} with constant lengths: the window of kept values only moves forward, so the best of it is the front of
|
|
1893
|
+
* a deque of candidates (positions of strictly decreasing values for a high, the newest of equal values), instead of a
|
|
1894
|
+
* scan of the window on every bar.
|
|
1895
|
+
*/
|
|
1896
|
+
function pivotConstant(
|
|
1897
|
+
values: readonly number[],
|
|
1898
|
+
bars: readonly number[],
|
|
1899
|
+
last: Int32Array,
|
|
1900
|
+
left: number,
|
|
1901
|
+
right: number,
|
|
1902
|
+
isHigh: boolean
|
|
1903
|
+
): series_float {
|
|
1904
|
+
const out: series_float = new Array<number>(last.length);
|
|
1905
|
+
const deque = new Int32Array(values.length);
|
|
1906
|
+
let head = 0;
|
|
1907
|
+
let tail = 0;
|
|
1908
|
+
let added = 0;
|
|
1909
|
+
for (let i = 0; i < last.length; i++) {
|
|
1910
|
+
const k = last[i]!;
|
|
1911
|
+
for (; added <= k; added++) {
|
|
1912
|
+
const v = values[added]!;
|
|
1913
|
+
while (tail > head && (isHigh ? values[deque[tail - 1]!]! <= v : values[deque[tail - 1]!]! >= v)) tail--;
|
|
1914
|
+
deque[tail++] = added;
|
|
1915
|
+
}
|
|
1916
|
+
if (!(i >= left + right) || k < 0) {
|
|
1917
|
+
out[i] = NaN;
|
|
1918
|
+
continue;
|
|
1919
|
+
}
|
|
1920
|
+
// as the scan: the window is the kept value of this bar and the `left + right` before it
|
|
1921
|
+
const first = k - Math.max(0, left + right);
|
|
1922
|
+
while (deque[head]! < first) head++;
|
|
1923
|
+
const best = deque[head]!;
|
|
1924
|
+
out[i] = bars[best] === i - right ? values[best]! : NaN;
|
|
1925
|
+
}
|
|
1926
|
+
return out;
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1888
1929
|
/**
|
|
1889
1930
|
* Pivot High - detects pivot high points in the price series.
|
|
1890
1931
|
*
|
package/src/ta/running-sum.ts
CHANGED
|
@@ -74,9 +74,18 @@ export class RunningSum {
|
|
|
74
74
|
private stepLength = 0;
|
|
75
75
|
private stepX = NaN;
|
|
76
76
|
private stepPartials: number[] = [];
|
|
77
|
+
/**
|
|
78
|
+
* {@link resized} partials of the kept state by length, until the next {@link commit}: the calls of a loop (several
|
|
79
|
+
* steps on one bar) move the same kept sum to the same lengths. Kept from the second step on, so that one step per
|
|
80
|
+
* bar costs nothing more.
|
|
81
|
+
*/
|
|
82
|
+
private readonly resizedByLength = new Map<number, readonly number[]>();
|
|
83
|
+
private resizedSinceCommit = 0;
|
|
77
84
|
|
|
78
|
-
/** The partials of the last `len` values (kept state, nothing added). */
|
|
85
|
+
/** The partials of the last `len` values (kept state, nothing added); a new array. */
|
|
79
86
|
private resized(len: number): number[] {
|
|
87
|
+
const cached = this.resizedByLength.get(len);
|
|
88
|
+
if (cached) return cached.slice();
|
|
80
89
|
const values = this.values;
|
|
81
90
|
const n = values.length;
|
|
82
91
|
const p = this.partials.slice();
|
|
@@ -84,6 +93,7 @@ export class RunningSum {
|
|
|
84
93
|
const want = Math.min(len, n);
|
|
85
94
|
if (want < kept) for (let k = n - kept; k < n - want; k++) addExact(p, -values[k]!);
|
|
86
95
|
else for (let k = n - kept - 1; k >= n - want; k--) addExact(p, values[k]!);
|
|
96
|
+
if (this.resizedSinceCommit++ > 0) this.resizedByLength.set(len, p.slice());
|
|
87
97
|
return p;
|
|
88
98
|
}
|
|
89
99
|
|
|
@@ -123,6 +133,8 @@ export class RunningSum {
|
|
|
123
133
|
if (!Number.isNaN(this.stepX)) this.values.push(this.stepX);
|
|
124
134
|
this.keptLength = this.stepLength;
|
|
125
135
|
this.partials = this.stepPartials;
|
|
136
|
+
this.resizedSinceCommit = 0;
|
|
137
|
+
if (this.resizedByLength.size > 0) this.resizedByLength.clear();
|
|
126
138
|
}
|
|
127
139
|
}
|
|
128
140
|
|