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
|
@@ -1,144 +1,144 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* `ta.variance` (bit for bit).
|
|
2
|
+
* Window sums of PineScript `math.sum` / `ta.sma` / `ta.stdev` / `ta.variance` / `ta.mfi` (bit for bit).
|
|
4
3
|
*
|
|
5
|
-
* PineScript
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* +infinity by the size of the compensation, in floating point (#136, #142).
|
|
4
|
+
* PineScript gives the correctly rounded sum of the window values (the exact sum of the doubles, rounded once), on
|
|
5
|
+
* every bar: `ta.sma` is that sum divided by the length (measured on the reference 05/11/2026, #188: built-in and
|
|
6
|
+
* expression sources, with or without a `ta.stdev` of the same source, constant and series lengths, na values; 0
|
|
7
|
+
* differing bars of 16,550 on BTCUSD 1D and 34,665 on AAPL 1D). The running sum of earlier versions (Kahan steps,
|
|
8
|
+
* #114) gave a small residue on a window of zeros; the reference no longer does.
|
|
11
9
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
10
|
+
* The exact sum is kept as non-overlapping partials (Shewchuk's algorithm, as Python `math.fsum`): adding a value and
|
|
11
|
+
* removing the one that leaves the window (adding its negation) are exact, so a bar costs the values that enter and
|
|
12
|
+
* leave, not the whole window.
|
|
14
13
|
*
|
|
15
14
|
* @internal
|
|
16
15
|
*/
|
|
17
16
|
import { checkSumLength } from './limits.js';
|
|
18
|
-
/**
|
|
19
|
-
* Whether the compensation `c` is (partly) lost when it is applied to `x`: `x + |c|` in floating point moves `x` by
|
|
20
|
-
* less than `|c|`. Moving toward +infinity uses the grid above `x` (the coarser binade just below a power of two, the
|
|
21
|
-
* finer binade for a negative power of two), as PineScript (measured bit for bit, #142).
|
|
22
|
-
*/
|
|
23
|
-
function compensationLost(x, c) {
|
|
24
|
-
if (c === 0 || x === 0)
|
|
25
|
-
return false;
|
|
26
|
-
const size = Math.abs(c);
|
|
27
|
-
return Math.abs(x + size - x) < size;
|
|
28
|
-
}
|
|
29
17
|
/** A value PineScript treats as na in window functions (also +/-Infinity). */
|
|
30
18
|
export function isNa(v) {
|
|
31
19
|
return v === undefined || v === null || !Number.isFinite(v);
|
|
32
20
|
}
|
|
21
|
+
/** Adds `x` to the partials `p` (non-overlapping, increasing magnitude), exactly. */
|
|
22
|
+
function addExact(p, x) {
|
|
23
|
+
let i = 0;
|
|
24
|
+
for (let j = 0; j < p.length; j++) {
|
|
25
|
+
let y = p[j];
|
|
26
|
+
if (Math.abs(x) < Math.abs(y))
|
|
27
|
+
[x, y] = [y, x];
|
|
28
|
+
const hi = x + y;
|
|
29
|
+
const lo = y - (hi - x);
|
|
30
|
+
if (lo !== 0)
|
|
31
|
+
p[i++] = lo;
|
|
32
|
+
x = hi;
|
|
33
|
+
}
|
|
34
|
+
p.length = i;
|
|
35
|
+
p.push(x);
|
|
36
|
+
}
|
|
37
|
+
/** The partials' exact sum rounded once to a double (round half to even), as Python `math.fsum`. */
|
|
38
|
+
function roundExact(p) {
|
|
39
|
+
let n = p.length;
|
|
40
|
+
if (n === 0)
|
|
41
|
+
return 0;
|
|
42
|
+
let hi = p[--n];
|
|
43
|
+
while (n > 0) {
|
|
44
|
+
const x = hi;
|
|
45
|
+
const y = p[--n];
|
|
46
|
+
hi = x + y;
|
|
47
|
+
const lo = y - (hi - x);
|
|
48
|
+
if (lo !== 0) {
|
|
49
|
+
// half-way case: the rest has the same sign, so round away from the tie
|
|
50
|
+
if (n > 0 && ((lo < 0 && p[n - 1] < 0) || (lo > 0 && p[n - 1] > 0))) {
|
|
51
|
+
const y2 = lo * 2;
|
|
52
|
+
const x2 = hi + y2;
|
|
53
|
+
if (y2 === x2 - hi)
|
|
54
|
+
hi = x2;
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return hi;
|
|
60
|
+
}
|
|
33
61
|
/**
|
|
34
|
-
*
|
|
62
|
+
* Window sum state, one value at a time. {@link step} computes the sum with a new value from the kept state without
|
|
35
63
|
* changing it; {@link commit} keeps the last step. So several candidate values can be tried on one bar and only the
|
|
36
64
|
* last one kept (a ta.* call in a loop).
|
|
37
65
|
*
|
|
38
|
-
* The length can change from one step to the next (a series length)
|
|
39
|
-
*
|
|
40
|
-
* values that leave the window, oldest first; a larger length adds back the values from the one before the old window
|
|
41
|
-
* down to the one `length` values back, newest first (one more than the window needs). Then the usual step runs with
|
|
42
|
-
* the new length: it removes the stored value of the one `length` values back and adds the new value.
|
|
66
|
+
* The length can change from one step to the next (a series length): the window is then the last `length` values,
|
|
67
|
+
* the values that leave or come back are removed or added exactly.
|
|
43
68
|
*/
|
|
44
69
|
export class RunningSum {
|
|
45
70
|
values = [];
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
stepLength = NaN;
|
|
71
|
+
/** Exact sum of the kept window (the last `keptLength` values). */
|
|
72
|
+
partials = [];
|
|
73
|
+
keptLength = 0;
|
|
74
|
+
stepLength = 0;
|
|
51
75
|
stepX = NaN;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
76
|
+
stepPartials = [];
|
|
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
|
+
resizedByLength = new Map();
|
|
83
|
+
resizedSinceCommit = 0;
|
|
84
|
+
/** The partials of the last `len` values (kept state, nothing added); a new array. */
|
|
85
|
+
resized(len) {
|
|
86
|
+
const cached = this.resizedByLength.get(len);
|
|
87
|
+
if (cached)
|
|
88
|
+
return cached.slice();
|
|
57
89
|
const values = this.values;
|
|
58
90
|
const n = values.length;
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
else if (len > kept) {
|
|
73
|
-
for (let k = n - kept - 1; k >= Math.max(0, n - len); k--)
|
|
74
|
-
add(values[k]);
|
|
75
|
-
}
|
|
76
|
-
this.stepS = s;
|
|
77
|
-
this.stepC = c;
|
|
91
|
+
const p = this.partials.slice();
|
|
92
|
+
const kept = Math.min(this.keptLength, n);
|
|
93
|
+
const want = Math.min(len, n);
|
|
94
|
+
if (want < kept)
|
|
95
|
+
for (let k = n - kept; k < n - want; k++)
|
|
96
|
+
addExact(p, -values[k]);
|
|
97
|
+
else
|
|
98
|
+
for (let k = n - kept - 1; k >= n - want; k--)
|
|
99
|
+
addExact(p, values[k]);
|
|
100
|
+
if (this.resizedSinceCommit++ > 0)
|
|
101
|
+
this.resizedByLength.set(len, p.slice());
|
|
102
|
+
return p;
|
|
78
103
|
}
|
|
79
104
|
/**
|
|
80
|
-
* The
|
|
105
|
+
* The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
|
|
81
106
|
* unchanged until {@link commit}.
|
|
82
107
|
*
|
|
83
108
|
* @returns The sum, or NaN while fewer than `length` values exist (with `x`)
|
|
84
109
|
*/
|
|
85
110
|
step(x, length) {
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const c0 = c;
|
|
92
|
-
const n = values.length;
|
|
93
|
-
if (n >= len) {
|
|
94
|
-
const y = -this.stored[n - len] - c;
|
|
95
|
-
const t = s + y;
|
|
96
|
-
c = (t - s) - y;
|
|
97
|
-
s = t;
|
|
98
|
-
}
|
|
99
|
-
if (compensationLost(x, c0)) {
|
|
100
|
-
let fresh = x;
|
|
101
|
-
for (let k = n - 1; k >= Math.max(0, n + 1 - len); k--)
|
|
102
|
-
fresh += values[k];
|
|
103
|
-
s = fresh;
|
|
104
|
-
c = 0;
|
|
105
|
-
this.stepY = x;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
const y = x - c;
|
|
109
|
-
const t = s + y;
|
|
110
|
-
c = (t - s) - y;
|
|
111
|
-
s = t;
|
|
112
|
-
this.stepY = y;
|
|
113
|
-
}
|
|
114
|
-
this.stepLength = len;
|
|
111
|
+
const n = this.values.length;
|
|
112
|
+
const p = this.resized(length - 1);
|
|
113
|
+
addExact(p, x);
|
|
114
|
+
this.stepPartials = p;
|
|
115
|
+
this.stepLength = Math.min(length, n + 1);
|
|
115
116
|
this.stepX = x;
|
|
116
|
-
|
|
117
|
-
this.stepC = c;
|
|
118
|
-
return n + 1 >= len ? s : NaN;
|
|
117
|
+
return n + 1 >= length ? roundExact(p) : NaN;
|
|
119
118
|
}
|
|
120
119
|
/**
|
|
121
|
-
* The
|
|
122
|
-
*
|
|
120
|
+
* The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
|
|
121
|
+
* unchanged until {@link commit}. With an unchanged length, it is the previous result.
|
|
123
122
|
*
|
|
124
123
|
* @returns The sum, or NaN while fewer than `length` values exist
|
|
125
124
|
*/
|
|
126
125
|
stepNa(length) {
|
|
127
|
-
this.resize(length);
|
|
128
|
-
this.stepLength = length;
|
|
129
|
-
this.stepX = NaN;
|
|
130
126
|
const n = this.values.length;
|
|
131
|
-
|
|
127
|
+
const p = this.resized(length);
|
|
128
|
+
this.stepPartials = p;
|
|
129
|
+
this.stepLength = Math.min(length, n);
|
|
130
|
+
this.stepX = NaN;
|
|
131
|
+
return n > 0 && n >= length ? roundExact(p) : NaN;
|
|
132
132
|
}
|
|
133
133
|
/** Keeps the state of the last {@link step} or {@link stepNa}. */
|
|
134
134
|
commit() {
|
|
135
|
-
if (!Number.isNaN(this.stepX))
|
|
135
|
+
if (!Number.isNaN(this.stepX))
|
|
136
136
|
this.values.push(this.stepX);
|
|
137
|
-
this.stored.push(this.stepY);
|
|
138
|
-
}
|
|
139
137
|
this.keptLength = this.stepLength;
|
|
140
|
-
this.
|
|
141
|
-
this.
|
|
138
|
+
this.partials = this.stepPartials;
|
|
139
|
+
this.resizedSinceCommit = 0;
|
|
140
|
+
if (this.resizedByLength.size > 0)
|
|
141
|
+
this.resizedByLength.clear();
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
/** The running sums of `source` with the length `lengthAt(i)` on bar `i`. */
|
|
@@ -154,7 +154,7 @@ function sumWindows(source, lengthAt) {
|
|
|
154
154
|
return out;
|
|
155
155
|
}
|
|
156
156
|
/**
|
|
157
|
-
* The sum of the last `length` non-na values on each bar, as PineScript
|
|
157
|
+
* The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.
|
|
158
158
|
*
|
|
159
159
|
* @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)
|
|
160
160
|
* @param length - Number of non-na values in the window
|
|
@@ -190,7 +190,7 @@ export function runningSumSeries(source, lengths, name = 'math.sum') {
|
|
|
190
190
|
const lens = seriesLengths(lengths, source.length, name);
|
|
191
191
|
return sumWindows(source, (i) => lens[i]);
|
|
192
192
|
}
|
|
193
|
-
/** Variance from the
|
|
193
|
+
/** Variance from the window sums of the values and of their squares; rules in {@link runningVariance}. */
|
|
194
194
|
function varianceFromSums(source, sumOf, lengthAt, biased) {
|
|
195
195
|
const squares = Array.from({ length: source.length }, (_, i) => {
|
|
196
196
|
const v = source[i];
|
|
@@ -206,7 +206,7 @@ function varianceFromSums(source, sumOf, lengthAt, biased) {
|
|
|
206
206
|
});
|
|
207
207
|
}
|
|
208
208
|
/**
|
|
209
|
-
* Window variance from the
|
|
209
|
+
* Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):
|
|
210
210
|
* biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.
|
|
211
211
|
*/
|
|
212
212
|
export function runningVariance(source, length, biased) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"running-sum.js","sourceRoot":"","sources":["../../../src/ta/running-sum.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"running-sum.js","sourceRoot":"","sources":["../../../src/ta/running-sum.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,8EAA8E;AAC9E,MAAM,UAAU,IAAI,CAAC,CAA4B;IAC/C,OAAO,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED,qFAAqF;AACrF,SAAS,QAAQ,CAAC,CAAW,EAAE,CAAS;IACtC,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,CAAC;QACd,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;YAAE,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC/C,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;QACxB,IAAI,EAAE,KAAK,CAAC;YAAE,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,CA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,MAA4C,EAC5C,OAA0B,EAC1B,MAAe,EACf,IAAI,GAAG,aAAa;IAEpB,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzD,OAAO,gBAAgB,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAE,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,CAAE,EAAE,MAAM,CAAC,CAAC;AACtG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timezone.d.ts","sourceRoot":"","sources":["../../../src/time/timezone.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;
|
|
1
|
+
{"version":3,"file":"timezone.d.ts","sourceRoot":"","sources":["../../../src/time/timezone.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAgIH,qDAAqD;AACrD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW;IACX,KAAK,EAAE,MAAM,CAAC;IACd,WAAW;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,mEAAmE;IACnE,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,uFAAuF;AACvF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEjE;AAED,kEAAkE;AAClE,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,WAAW,CAWvE;AAED;;;;;;;GAOG;AACH;;;GAGG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,SAAI,EAAE,MAAM,SAAI,EAAE,MAAM,SAAI,GAAG,MAAM,CAc9G;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,IAAI,GAAE,MAAU,EAChB,MAAM,GAAE,MAAU,EAClB,MAAM,GAAE,MAAU,EAClB,OAAO,GAAE,OAAe,GACvB,MAAM,CAaR"}
|
|
@@ -29,6 +29,8 @@ function hasTimeZoneCase(name) {
|
|
|
29
29
|
const MS_PER_HOUR = 3_600_000;
|
|
30
30
|
/** Cached hours per zone before the cache restarts (about 11 years of hours) */
|
|
31
31
|
const MAX_CACHED_HOURS = 100_000;
|
|
32
|
+
/** Cached days per zone before the cache restarts (about 270 years) */
|
|
33
|
+
const MAX_CACHED_DAYS = 100_000;
|
|
32
34
|
const zones = new Map();
|
|
33
35
|
function resolveZone(timezone) {
|
|
34
36
|
let zone = zones.get(timezone);
|
|
@@ -66,19 +68,37 @@ function resolveZone(timezone) {
|
|
|
66
68
|
catch {
|
|
67
69
|
throw invalid();
|
|
68
70
|
}
|
|
69
|
-
zone = { kind: 'iana', format, hours: new Map() };
|
|
71
|
+
zone = { kind: 'iana', format, dayStarts: new Map(), hours: new Map() };
|
|
70
72
|
}
|
|
71
73
|
zones.set(timezone, zone);
|
|
72
74
|
return zone;
|
|
73
75
|
}
|
|
76
|
+
/** Offset of the zone at the start of UTC day `day`, cached. */
|
|
77
|
+
function dayStartOffset(zone, day) {
|
|
78
|
+
let offset = zone.dayStarts.get(day);
|
|
79
|
+
if (offset === undefined) {
|
|
80
|
+
offset = formattedOffset(zone.format, day * MS_PER_DAY);
|
|
81
|
+
if (zone.dayStarts.size >= MAX_CACHED_DAYS)
|
|
82
|
+
zone.dayStarts.clear();
|
|
83
|
+
zone.dayStarts.set(day, offset);
|
|
84
|
+
}
|
|
85
|
+
return offset;
|
|
86
|
+
}
|
|
74
87
|
/**
|
|
75
88
|
* Offset of the zone from UTC at instant `time`, in milliseconds (local = UTC + offset).
|
|
76
|
-
*
|
|
77
|
-
*
|
|
89
|
+
*
|
|
90
|
+
* Two offset changes of a zone are always more than a day apart (the closest ones of the time zone database are 167
|
|
91
|
+
* hours apart), so when a UTC day starts with the offset of the next day, the whole day has it: daily bars cost one
|
|
92
|
+
* lookup per day. In a day with a change, an hour whose first and last second have the same offset has it (an offset
|
|
93
|
+
* change happens at most once in an hour): it is computed once and cached.
|
|
78
94
|
*/
|
|
79
95
|
function offsetAt(zone, time) {
|
|
80
96
|
if (zone.kind === 'fixed')
|
|
81
97
|
return zone.offsetMs;
|
|
98
|
+
const day = Math.floor(time / MS_PER_DAY);
|
|
99
|
+
const start = dayStartOffset(zone, day);
|
|
100
|
+
if (start === dayStartOffset(zone, day + 1))
|
|
101
|
+
return start;
|
|
82
102
|
const hour = Math.floor(time / MS_PER_HOUR);
|
|
83
103
|
let offset = zone.hours.get(hour);
|
|
84
104
|
if (offset === undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timezone.js","sourceRoot":"","sources":["../../../src/time/timezone.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,UAAU,GAAG,UAAU,CAAC;AAC9B,MAAM,kBAAkB,GAAG,EAAE,GAAG,IAAI,CAAC;AAErC,MAAM,eAAe,GAAG,+DAA+D,CAAC;AAExF,qFAAqF;AACrF,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAEtD,IAAI,cAA+C,CAAC;AAEpD,gHAAgH;AAChH,SAAS,eAAe,CAAC,IAAY;IACnC,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,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AC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAClG,sFAAsF;IACtF,IAAI,UAAU,CAAC,MAAM;QAAE,OAAO,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,CAAC;IAC1F,OAAO,KAAK,GAAG,MAAM,CAAC;AACxB,CAAC"}
|
package/package.json
CHANGED
package/src/callsite/index.ts
CHANGED
|
@@ -190,10 +190,22 @@ export function cross(): (a: number, b: number) => boolean {
|
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
+
/** Windows up to this length are scanned; longer ones are read from the tree of {@link KeptCalls}. */
|
|
194
|
+
const SCANNED_WINDOW = 32;
|
|
195
|
+
|
|
193
196
|
/** The values a ta.highest / ta.lowest / ta.pivothigh call site keeps: the non-na values of its calls, one per bar. */
|
|
194
197
|
class KeptCalls {
|
|
195
198
|
readonly values: number[] = [];
|
|
196
199
|
readonly bars: number[] = [];
|
|
200
|
+
/**
|
|
201
|
+
* Segment tree of the best position of each range of kept values (node 1 is the root, leaf `leaves + k` is value
|
|
202
|
+
* `k`, -1 for no value), built on the first long window: a call site whose length grows with the bars (the bars
|
|
203
|
+
* since a session start) would otherwise scan the whole history on every call.
|
|
204
|
+
*/
|
|
205
|
+
private tree: Int32Array | null = null;
|
|
206
|
+
private leaves = 0;
|
|
207
|
+
|
|
208
|
+
constructor(private readonly isLowest: boolean) {}
|
|
197
209
|
|
|
198
210
|
/**
|
|
199
211
|
* Keeps a non-na value (+/-Infinity is skipped like na, #183); a later call on the same bar (a loop) replaces the
|
|
@@ -207,18 +219,63 @@ class KeptCalls {
|
|
|
207
219
|
this.values.push(value);
|
|
208
220
|
this.bars.push(barIndex);
|
|
209
221
|
}
|
|
222
|
+
if (this.tree) this.update(this.values.length - 1);
|
|
210
223
|
}
|
|
211
224
|
|
|
212
225
|
/** Position of the best of the last `count` kept values (the newest one on ties), -1 when there is none. */
|
|
213
|
-
best(count: number
|
|
226
|
+
best(count: number): number {
|
|
214
227
|
const last = this.values.length - 1;
|
|
215
228
|
if (last < 0 || !(count >= 1)) return -1;
|
|
229
|
+
if (count > SCANNED_WINDOW) return this.bestOf(Math.max(0, Math.floor(last - count) + 1), last);
|
|
216
230
|
let best = last;
|
|
217
231
|
for (let k = last - 1; k >= 0 && k > last - count; k--) {
|
|
218
|
-
if (isLowest ? this.values[k]! < this.values[best]! : this.values[k]! > this.values[best]!) best = k;
|
|
232
|
+
if (this.isLowest ? this.values[k]! < this.values[best]! : this.values[k]! > this.values[best]!) best = k;
|
|
233
|
+
}
|
|
234
|
+
return best;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The better of two positions (-1: none): the lower (higher) value, the newest one on equal values. */
|
|
238
|
+
private better(a: number, b: number): number {
|
|
239
|
+
if (a < 0) return b;
|
|
240
|
+
if (b < 0) return a;
|
|
241
|
+
const va = this.values[a]!;
|
|
242
|
+
const vb = this.values[b]!;
|
|
243
|
+
if (this.isLowest ? va < vb : va > vb) return a;
|
|
244
|
+
if (this.isLowest ? vb < va : vb > va) return b;
|
|
245
|
+
return a > b ? a : b;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Best position of the kept values `first` to `last`, from the tree. */
|
|
249
|
+
private bestOf(first: number, last: number): number {
|
|
250
|
+
if (!this.tree) this.build();
|
|
251
|
+
const tree = this.tree!;
|
|
252
|
+
let best = -1;
|
|
253
|
+
for (let l = first + this.leaves, r = last + this.leaves + 1; l < r; l >>= 1, r >>= 1) {
|
|
254
|
+
if (l & 1) best = this.better(best, tree[l++]!);
|
|
255
|
+
if (r & 1) best = this.better(best, tree[--r]!);
|
|
219
256
|
}
|
|
220
257
|
return best;
|
|
221
258
|
}
|
|
259
|
+
|
|
260
|
+
/** (Re)builds the tree, with room for as many values again. */
|
|
261
|
+
private build(): void {
|
|
262
|
+
const n = this.values.length;
|
|
263
|
+
let leaves = 1;
|
|
264
|
+
while (leaves < 2 * n) leaves *= 2;
|
|
265
|
+
const tree = new Int32Array(2 * leaves).fill(-1);
|
|
266
|
+
for (let k = 0; k < n; k++) tree[leaves + k] = k;
|
|
267
|
+
for (let i = leaves - 1; i >= 1; i--) tree[i] = this.better(tree[2 * i]!, tree[2 * i + 1]!);
|
|
268
|
+
this.tree = tree;
|
|
269
|
+
this.leaves = leaves;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Value `k` was added or replaced. */
|
|
273
|
+
private update(k: number): void {
|
|
274
|
+
if (k >= this.leaves) return this.build();
|
|
275
|
+
const tree = this.tree!;
|
|
276
|
+
tree[this.leaves + k] = k;
|
|
277
|
+
for (let i = (this.leaves + k) >> 1; i >= 1; i >>= 1) tree[i] = this.better(tree[2 * i]!, tree[2 * i + 1]!);
|
|
278
|
+
}
|
|
222
279
|
}
|
|
223
280
|
|
|
224
281
|
/** One `ta.lowest` / `ta.highest` / `ta.lowestbars` / `ta.highestbars` call site; rules in {@link lowestByBar}. */
|
|
@@ -226,14 +283,14 @@ function extremeByBarSite(
|
|
|
226
283
|
isLowest: boolean,
|
|
227
284
|
offsets: boolean
|
|
228
285
|
): (barIndex: number, value: number, length: number) => number {
|
|
229
|
-
const kept = new KeptCalls();
|
|
286
|
+
const kept = new KeptCalls(isLowest);
|
|
230
287
|
const name = (isLowest ? 'lowest' : 'highest') + (offsets ? 'bars' : '');
|
|
231
288
|
return (barIndex, value, length) => {
|
|
232
289
|
checkWindowLength(name, length, barIndex);
|
|
233
290
|
kept.add(barIndex, value);
|
|
234
291
|
const len = Math.trunc(length);
|
|
235
292
|
if (barIndex < len - 1) return NaN;
|
|
236
|
-
const best = kept.best(len
|
|
293
|
+
const best = kept.best(len);
|
|
237
294
|
if (best < 0) return NaN;
|
|
238
295
|
return offsets ? -(barIndex - kept.bars[best]!) : kept.values[best]!;
|
|
239
296
|
};
|
|
@@ -333,13 +390,13 @@ export function highestbars(): (barIndex: number, value: number, length: number)
|
|
|
333
390
|
|
|
334
391
|
/** One `ta.pivothigh` / `ta.pivotlow` call site; rules in {@link pivothigh}. */
|
|
335
392
|
function pivotSite(isLowest: boolean): (barIndex: number, value: number, leftbars: number, rightbars: number) => number {
|
|
336
|
-
const kept = new KeptCalls();
|
|
393
|
+
const kept = new KeptCalls(isLowest);
|
|
337
394
|
return (barIndex, value, leftbars, rightbars) => {
|
|
338
395
|
kept.add(barIndex, value);
|
|
339
396
|
const left = Math.trunc(leftbars);
|
|
340
397
|
const right = Math.trunc(rightbars);
|
|
341
398
|
if (!(barIndex >= left + right)) return NaN;
|
|
342
|
-
const best = kept.best(left + right + 1
|
|
399
|
+
const best = kept.best(left + right + 1);
|
|
343
400
|
return best >= 0 && kept.bars[best] === barIndex - right ? kept.values[best]! : NaN;
|
|
344
401
|
};
|
|
345
402
|
}
|
|
@@ -418,11 +475,12 @@ function loopSumSite(
|
|
|
418
475
|
): (barIndex: number, value: number, length: number) => [number, number] {
|
|
419
476
|
const running = new RunningSum();
|
|
420
477
|
const runningSq = new RunningSum();
|
|
478
|
+
const fn = name === 'sum' ? 'math.sum' : `ta.${name}`;
|
|
421
479
|
let lastBar = NaN;
|
|
422
480
|
let pending = false;
|
|
423
481
|
return (barIndex, value, length) => {
|
|
424
482
|
const len = Math.floor(length);
|
|
425
|
-
checkSumLength(
|
|
483
|
+
checkSumLength(fn, len, barIndex);
|
|
426
484
|
if (!(len >= 1)) throw new Error(`callsite.${name}: length must be at least 1 (got ${length})`);
|
|
427
485
|
if (barIndex !== lastBar) {
|
|
428
486
|
if (pending) {
|
|
@@ -452,9 +510,9 @@ function loopSumSite(
|
|
|
452
510
|
* - every call on a bar starts from the state of the earlier bars, so it gives the sum of its value and the last
|
|
453
511
|
* `length - 1` kept values; the last call of the bar is the one kept for the later bars
|
|
454
512
|
* - the length can change from one call to the next (also on the same bar): each call first moves the kept sum to
|
|
455
|
-
* its own length as `math.sum` with a series length
|
|
513
|
+
* its own length, as `math.sum` with a series length
|
|
456
514
|
* - a bar without a call is not in the history (as a call in an `if` block)
|
|
457
|
-
* - the sum is the
|
|
515
|
+
* - the sum is the correctly rounded window sum, as `math.sum` (bit for bit, #188)
|
|
458
516
|
*
|
|
459
517
|
* An na value is skipped as in `math.sum` (it returns the sum of the last `length` earlier values); when it is the
|
|
460
518
|
* last call of a bar, the bar is not in the history (not measured).
|
|
@@ -498,7 +556,7 @@ export function sma(): (barIndex: number, value: number, length: number) => numb
|
|
|
498
556
|
|
|
499
557
|
/**
|
|
500
558
|
* One `ta.stdev(source, length, biased)` call site inside a `for` / `while` loop, called with
|
|
501
|
-
* `(barIndex, value, length, biased?)` on each call: the
|
|
559
|
+
* `(barIndex, value, length, biased?)` on each call: the window sums of the values and of their squares follow the
|
|
502
560
|
* rules of {@link sum}; the result is computed from them as `ta.stdev` (bit for bit).
|
|
503
561
|
*
|
|
504
562
|
* @example
|