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/ta/index.ts
CHANGED
|
@@ -24,10 +24,9 @@ import { checkPositiveLength, checkSumLength, checkWindowLength } from './limits
|
|
|
24
24
|
* - `na` values (also +/-Infinity) in the source series are ignored: the mean of the last `length` non-`na`
|
|
25
25
|
* values, and a bar with an `na` source keeps the previous result (as in PineScript)
|
|
26
26
|
* - Returns NaN until `length` non-`na` values are available
|
|
27
|
-
* - The
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* (see {@link math.sum}); a bar with an `na` source gives the mean of the last `length` earlier values
|
|
27
|
+
* - The correctly rounded window sum divided by the length, bit for bit with PineScript (#188)
|
|
28
|
+
* - With a series length, the window is the last `length` values of each bar (see {@link math.sum}); a bar with an
|
|
29
|
+
* `na` source gives the mean of the last `length` earlier values
|
|
31
30
|
*
|
|
32
31
|
* @example
|
|
33
32
|
* ```typescript
|
|
@@ -86,7 +85,7 @@ export function ema(source: Source, length: simple_int): series_float {
|
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
const emaInitialized = firstValidIndex >= 0;
|
|
89
|
-
// The seed is ta.sma on that bar: the
|
|
88
|
+
// The seed is ta.sma on that bar: the window sum of math.sum (PineScript bits), not a plain loop sum
|
|
90
89
|
let emaValue = emaInitialized ? runningSum(source.slice(0, firstValidIndex + 1), len)[firstValidIndex]! / len : NaN;
|
|
91
90
|
|
|
92
91
|
for (let i = 0; i < source.length; i++) {
|
|
@@ -282,7 +281,7 @@ export function bb(
|
|
|
282
281
|
* As in PineScript: `na` values (also +/-Infinity) are skipped, the window holds the last
|
|
283
282
|
* `length` non-na values, and a bar whose own value is `na` keeps the previous result.
|
|
284
283
|
* The result is the square root of {@link variance} (0 when the variance is not positive), computed from
|
|
285
|
-
*
|
|
284
|
+
* the correctly rounded window sums of the values and of their squares, bit for bit as PineScript (#188).
|
|
286
285
|
*/
|
|
287
286
|
export function stdev(
|
|
288
287
|
source: Source,
|
|
@@ -769,7 +768,7 @@ export function supertrend(
|
|
|
769
768
|
* - Moving average used in RSI calculation
|
|
770
769
|
* - Alpha = 1 / length (different from EMA which uses alpha = 2 / (length + 1))
|
|
771
770
|
* - First value is initialized with SMA, then uses exponential smoothing. As in PineScript, the seed is the
|
|
772
|
-
* `ta.sma` value (
|
|
771
|
+
* `ta.sma` value (the correctly rounded window sum divided by the length)
|
|
773
772
|
* - Formula: `RMA = (source + (length - 1) * RMA[1]) / length`, evaluated in this order as in PineScript
|
|
774
773
|
* (equal to `alpha * source + (1 - alpha) * RMA[1]`, but bit for bit)
|
|
775
774
|
* - `na` values (also +/-Infinity) in the source series are ignored
|
|
@@ -803,7 +802,7 @@ export function rma(source: Source, length: simple_int): series_float {
|
|
|
803
802
|
}
|
|
804
803
|
|
|
805
804
|
const rmaInitialized = firstValidIndex >= 0;
|
|
806
|
-
// The seed is ta.sma on that bar: the
|
|
805
|
+
// The seed is ta.sma on that bar: the window sum of math.sum (PineScript bits), not a plain loop sum
|
|
807
806
|
let rmaValue = rmaInitialized ? runningSum(source.slice(0, firstValidIndex + 1), len)[firstValidIndex]! / len : NaN;
|
|
808
807
|
|
|
809
808
|
for (let i = 0; i < source.length; i++) {
|
|
@@ -1170,8 +1169,7 @@ export function mom(source: Source, length: simple_int): series_float {
|
|
|
1170
1169
|
* - Measures average absolute distance from the mean
|
|
1171
1170
|
* - Less sensitive to outliers than standard deviation
|
|
1172
1171
|
* - PineScript rule (bit for bit): `sum(abs(source[i] - mean)) / length` for `i = 0 .. length - 1`, added newest
|
|
1173
|
-
* first, with `mean = ta.sma(source, length)`
|
|
1174
|
-
* non-zero deviation)
|
|
1172
|
+
* first, with `mean = ta.sma(source, length)`
|
|
1175
1173
|
* - The loop reads the last `length` bars: an `na` among them gives `na`
|
|
1176
1174
|
*
|
|
1177
1175
|
* @example
|
|
@@ -1208,7 +1206,7 @@ export function dev(source: Source, length: simple_int): series_float {
|
|
|
1208
1206
|
* - `na` values in the source series are ignored
|
|
1209
1207
|
* - The function calculates on the `length` quantity of non-`na` values
|
|
1210
1208
|
* - Relationship: `stdev = sqrt(variance)`
|
|
1211
|
-
* - Computed as PineScript, bit for bit, from
|
|
1209
|
+
* - Computed as PineScript, bit for bit, from the correctly rounded window sums of the values and of their squares:
|
|
1212
1210
|
* biased `sumSq / length - mean * mean`, unbiased `sumSq / (length - 1) - mean * sum / (length - 1)`
|
|
1213
1211
|
*
|
|
1214
1212
|
* @example
|
|
@@ -1467,7 +1465,7 @@ export function linreg(source: Source, length: simple_int, offset: simple_int =
|
|
|
1467
1465
|
* - 0 = no correlation
|
|
1468
1466
|
* - Measures linear relationship between two series
|
|
1469
1467
|
* - As PineScript: `cov = sma(x * y) - sma(x) * sma(y)`, `r = cov / sqrt(variance(x) * variance(y))` with the
|
|
1470
|
-
*
|
|
1468
|
+
* the correctly rounded window sums of `ta.sma` / `ta.variance` (each skips its own `na` values); `|cov| <= 1e-10` gives 0, a
|
|
1471
1469
|
* zero variance gives `na`
|
|
1472
1470
|
*
|
|
1473
1471
|
* @example
|
|
@@ -1478,7 +1476,7 @@ export function linreg(source: Source, length: simple_int, offset: simple_int =
|
|
|
1478
1476
|
*/
|
|
1479
1477
|
export function correlation(source1: Source, source2: Source, length: simple_int): series_float {
|
|
1480
1478
|
checkPositiveLength('correlation', length, 0);
|
|
1481
|
-
// PineScript: from
|
|
1479
|
+
// PineScript: from the correctly rounded window sums, cov = sma(x * y) - sma(x) * sma(y), r = cov / sqrt(var(x) * var(y))
|
|
1482
1480
|
// (biased variances); each running sum skips its own na values
|
|
1483
1481
|
const product = Array.from({ length: source1.length }, (_, i) => {
|
|
1484
1482
|
const v1 = source1[i];
|
|
@@ -1689,7 +1687,7 @@ export function mfi(source: Source, length: simple_int, volume?: Source): series
|
|
|
1689
1687
|
upperFlow.push(le(change, 0) ? 0 : flow);
|
|
1690
1688
|
lowerFlow.push(ge(change, 0) ? 0 : flow);
|
|
1691
1689
|
}
|
|
1692
|
-
// math.sum: the
|
|
1690
|
+
// math.sum: the correctly rounded sum of the last `length` non-na values (as PineScript, bit for bit)
|
|
1693
1691
|
const upper = runningSum(upperFlow, length);
|
|
1694
1692
|
const lower = runningSum(lowerFlow, length);
|
|
1695
1693
|
|
|
@@ -1873,6 +1871,9 @@ type PivotLength = simple_int | ArrayLike<number>;
|
|
|
1873
1871
|
*/
|
|
1874
1872
|
function pivot(source: Source, leftbars: PivotLength, rightbars: PivotLength, isHigh: boolean): series_float {
|
|
1875
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
|
+
}
|
|
1876
1877
|
return Array.from(source, (_, i) => {
|
|
1877
1878
|
// a series length: the lengths of the current bar
|
|
1878
1879
|
const left = Math.trunc(typeof leftbars === 'number' ? leftbars : leftbars[i]!);
|
|
@@ -1887,6 +1888,44 @@ function pivot(source: Source, leftbars: PivotLength, rightbars: PivotLength, is
|
|
|
1887
1888
|
});
|
|
1888
1889
|
}
|
|
1889
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
|
+
|
|
1890
1929
|
/**
|
|
1891
1930
|
* Pivot High - detects pivot high points in the price series.
|
|
1892
1931
|
*
|
|
@@ -2612,7 +2651,7 @@ export function min(source: Source): series_float {
|
|
|
2612
2651
|
* @remarks
|
|
2613
2652
|
* PineScript rule (bit for bit): `-num / math.sum(source, length)`, with `num` the sum of `source[i] * (i + 1)`
|
|
2614
2653
|
* for `i = 0 .. length - 1`, added newest first.
|
|
2615
|
-
* - The denominator is the
|
|
2654
|
+
* - The denominator is the correctly rounded window sum of `math.sum` (the last `length` non-`na` values)
|
|
2616
2655
|
* - The numerator reads the last `length` bars, so an `na` among them gives `na`
|
|
2617
2656
|
* - A zero denominator gives `na` (PineScript division by zero)
|
|
2618
2657
|
*
|
package/src/ta/running-sum.ts
CHANGED
|
@@ -1,145 +1,140 @@
|
|
|
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
17
|
|
|
19
|
-
/**
|
|
20
|
-
* Whether the compensation `c` is (partly) lost when it is applied to `x`: `x + |c|` in floating point moves `x` by
|
|
21
|
-
* less than `|c|`. Moving toward +infinity uses the grid above `x` (the coarser binade just below a power of two, the
|
|
22
|
-
* finer binade for a negative power of two), as PineScript (measured bit for bit, #142).
|
|
23
|
-
*/
|
|
24
|
-
function compensationLost(x: number, c: number): boolean {
|
|
25
|
-
if (c === 0 || x === 0) return false;
|
|
26
|
-
const size = Math.abs(c);
|
|
27
|
-
return Math.abs(x + size - x) < size;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
18
|
/** A value PineScript treats as na in window functions (also +/-Infinity). */
|
|
31
19
|
export function isNa(v: number | null | undefined): boolean {
|
|
32
20
|
return v === undefined || v === null || !Number.isFinite(v);
|
|
33
21
|
}
|
|
34
22
|
|
|
23
|
+
/** Adds `x` to the partials `p` (non-overlapping, increasing magnitude), exactly. */
|
|
24
|
+
function addExact(p: number[], x: number): void {
|
|
25
|
+
let i = 0;
|
|
26
|
+
for (let j = 0; j < p.length; j++) {
|
|
27
|
+
let y = p[j]!;
|
|
28
|
+
if (Math.abs(x) < Math.abs(y)) [x, y] = [y, x];
|
|
29
|
+
const hi = x + y;
|
|
30
|
+
const lo = y - (hi - x);
|
|
31
|
+
if (lo !== 0) p[i++] = lo;
|
|
32
|
+
x = hi;
|
|
33
|
+
}
|
|
34
|
+
p.length = i;
|
|
35
|
+
p.push(x);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The partials' exact sum rounded once to a double (round half to even), as Python `math.fsum`. */
|
|
39
|
+
function roundExact(p: readonly number[]): number {
|
|
40
|
+
let n = p.length;
|
|
41
|
+
if (n === 0) 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) hi = x2;
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return hi;
|
|
59
|
+
}
|
|
60
|
+
|
|
35
61
|
/**
|
|
36
|
-
*
|
|
62
|
+
* Window sum state, one value at a time. {@link step} computes the sum with a new value from the kept state without
|
|
37
63
|
* changing it; {@link commit} keeps the last step. So several candidate values can be tried on one bar and only the
|
|
38
64
|
* last one kept (a ta.* call in a loop).
|
|
39
65
|
*
|
|
40
|
-
* The length can change from one step to the next (a series length)
|
|
41
|
-
*
|
|
42
|
-
* values that leave the window, oldest first; a larger length adds back the values from the one before the old window
|
|
43
|
-
* down to the one `length` values back, newest first (one more than the window needs). Then the usual step runs with
|
|
44
|
-
* 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.
|
|
45
68
|
*/
|
|
46
69
|
export class RunningSum {
|
|
47
70
|
private readonly values: number[] = [];
|
|
48
|
-
|
|
49
|
-
private
|
|
50
|
-
private
|
|
51
|
-
private
|
|
52
|
-
private stepLength = NaN;
|
|
71
|
+
/** Exact sum of the kept window (the last `keptLength` values). */
|
|
72
|
+
private partials: number[] = [];
|
|
73
|
+
private keptLength = 0;
|
|
74
|
+
private stepLength = 0;
|
|
53
75
|
private stepX = NaN;
|
|
54
|
-
private
|
|
55
|
-
|
|
56
|
-
|
|
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;
|
|
57
84
|
|
|
58
|
-
/**
|
|
59
|
-
private
|
|
85
|
+
/** The partials of the last `len` values (kept state, nothing added); a new array. */
|
|
86
|
+
private resized(len: number): number[] {
|
|
87
|
+
const cached = this.resizedByLength.get(len);
|
|
88
|
+
if (cached) return cached.slice();
|
|
60
89
|
const values = this.values;
|
|
61
90
|
const n = values.length;
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
s = t;
|
|
70
|
-
};
|
|
71
|
-
if (len < kept) {
|
|
72
|
-
for (let k = Math.max(0, n - kept); k <= n - len - 1; k++) add(-values[k]!);
|
|
73
|
-
} else if (len > kept) {
|
|
74
|
-
for (let k = n - kept - 1; k >= Math.max(0, n - len); k--) 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) for (let k = n - kept; k < n - want; k++) addExact(p, -values[k]!);
|
|
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());
|
|
97
|
+
return p;
|
|
78
98
|
}
|
|
79
99
|
|
|
80
100
|
/**
|
|
81
|
-
* The
|
|
101
|
+
* The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
|
|
82
102
|
* unchanged until {@link commit}.
|
|
83
103
|
*
|
|
84
104
|
* @returns The sum, or NaN while fewer than `length` values exist (with `x`)
|
|
85
105
|
*/
|
|
86
106
|
step(x: number, length: number): number {
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const c0 = c;
|
|
93
|
-
const n = values.length;
|
|
94
|
-
if (n >= len) {
|
|
95
|
-
const y = -this.stored[n - len]! - c;
|
|
96
|
-
const t = s + y;
|
|
97
|
-
c = (t - s) - y;
|
|
98
|
-
s = t;
|
|
99
|
-
}
|
|
100
|
-
if (compensationLost(x, c0)) {
|
|
101
|
-
let fresh = x;
|
|
102
|
-
for (let k = n - 1; k >= Math.max(0, n + 1 - len); k--) fresh += values[k]!;
|
|
103
|
-
s = fresh;
|
|
104
|
-
c = 0;
|
|
105
|
-
this.stepY = x;
|
|
106
|
-
} else {
|
|
107
|
-
const y = x - c;
|
|
108
|
-
const t = s + y;
|
|
109
|
-
c = (t - s) - y;
|
|
110
|
-
s = t;
|
|
111
|
-
this.stepY = y;
|
|
112
|
-
}
|
|
113
|
-
this.stepLength = len;
|
|
107
|
+
const n = this.values.length;
|
|
108
|
+
const p = this.resized(length - 1);
|
|
109
|
+
addExact(p, x);
|
|
110
|
+
this.stepPartials = p;
|
|
111
|
+
this.stepLength = Math.min(length, n + 1);
|
|
114
112
|
this.stepX = x;
|
|
115
|
-
|
|
116
|
-
this.stepC = c;
|
|
117
|
-
return n + 1 >= len ? s : NaN;
|
|
113
|
+
return n + 1 >= length ? roundExact(p) : NaN;
|
|
118
114
|
}
|
|
119
115
|
|
|
120
116
|
/**
|
|
121
|
-
* The
|
|
122
|
-
*
|
|
117
|
+
* The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
|
|
118
|
+
* unchanged until {@link commit}. With an unchanged length, it is the previous result.
|
|
123
119
|
*
|
|
124
120
|
* @returns The sum, or NaN while fewer than `length` values exist
|
|
125
121
|
*/
|
|
126
122
|
stepNa(length: number): number {
|
|
127
|
-
this.resize(length);
|
|
128
|
-
this.stepLength = length;
|
|
129
|
-
this.stepX = NaN;
|
|
130
123
|
const n = this.values.length;
|
|
131
|
-
|
|
124
|
+
const p = this.resized(length);
|
|
125
|
+
this.stepPartials = p;
|
|
126
|
+
this.stepLength = Math.min(length, n);
|
|
127
|
+
this.stepX = NaN;
|
|
128
|
+
return n > 0 && n >= length ? roundExact(p) : NaN;
|
|
132
129
|
}
|
|
133
130
|
|
|
134
131
|
/** Keeps the state of the last {@link step} or {@link stepNa}. */
|
|
135
132
|
commit(): void {
|
|
136
|
-
if (!Number.isNaN(this.stepX))
|
|
137
|
-
this.values.push(this.stepX);
|
|
138
|
-
this.stored.push(this.stepY);
|
|
139
|
-
}
|
|
133
|
+
if (!Number.isNaN(this.stepX)) this.values.push(this.stepX);
|
|
140
134
|
this.keptLength = this.stepLength;
|
|
141
|
-
this.
|
|
142
|
-
this.
|
|
135
|
+
this.partials = this.stepPartials;
|
|
136
|
+
this.resizedSinceCommit = 0;
|
|
137
|
+
if (this.resizedByLength.size > 0) this.resizedByLength.clear();
|
|
143
138
|
}
|
|
144
139
|
}
|
|
145
140
|
|
|
@@ -157,7 +152,7 @@ function sumWindows(source: ArrayLike<number | null | undefined>, lengthAt: (i:
|
|
|
157
152
|
}
|
|
158
153
|
|
|
159
154
|
/**
|
|
160
|
-
* The sum of the last `length` non-na values on each bar, as PineScript
|
|
155
|
+
* The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.
|
|
161
156
|
*
|
|
162
157
|
* @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)
|
|
163
158
|
* @param length - Number of non-na values in the window
|
|
@@ -198,7 +193,7 @@ export function runningSumSeries(
|
|
|
198
193
|
return sumWindows(source, (i) => lens[i]!);
|
|
199
194
|
}
|
|
200
195
|
|
|
201
|
-
/** Variance from the
|
|
196
|
+
/** Variance from the window sums of the values and of their squares; rules in {@link runningVariance}. */
|
|
202
197
|
function varianceFromSums(
|
|
203
198
|
source: ArrayLike<number | null | undefined>,
|
|
204
199
|
sumOf: (src: ArrayLike<number | null | undefined>) => number[],
|
|
@@ -220,7 +215,7 @@ function varianceFromSums(
|
|
|
220
215
|
}
|
|
221
216
|
|
|
222
217
|
/**
|
|
223
|
-
* Window variance from the
|
|
218
|
+
* Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):
|
|
224
219
|
* biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.
|
|
225
220
|
*/
|
|
226
221
|
export function runningVariance(
|
package/src/time/timezone.ts
CHANGED
|
@@ -36,13 +36,18 @@ type Zone =
|
|
|
36
36
|
| {
|
|
37
37
|
readonly kind: 'iana';
|
|
38
38
|
readonly format: Intl.DateTimeFormat;
|
|
39
|
-
/** Offset of each UTC
|
|
39
|
+
/** Offset at the start of each UTC day (`floor(time / 1 day)`) */
|
|
40
|
+
readonly dayStarts: Map<number, number>;
|
|
41
|
+
/** Offset of each UTC hour (`floor(time / 1 h)`) of a day with an offset change; null for an hour with the
|
|
42
|
+
* change inside */
|
|
40
43
|
readonly hours: Map<number, number | null>;
|
|
41
44
|
};
|
|
42
45
|
|
|
43
46
|
const MS_PER_HOUR = 3_600_000;
|
|
44
47
|
/** Cached hours per zone before the cache restarts (about 11 years of hours) */
|
|
45
48
|
const MAX_CACHED_HOURS = 100_000;
|
|
49
|
+
/** Cached days per zone before the cache restarts (about 270 years) */
|
|
50
|
+
const MAX_CACHED_DAYS = 100_000;
|
|
46
51
|
|
|
47
52
|
const zones = new Map<string, Zone>();
|
|
48
53
|
|
|
@@ -80,19 +85,36 @@ function resolveZone(timezone: string): Zone {
|
|
|
80
85
|
} catch {
|
|
81
86
|
throw invalid();
|
|
82
87
|
}
|
|
83
|
-
zone = { kind: 'iana', format, hours: new Map() };
|
|
88
|
+
zone = { kind: 'iana', format, dayStarts: new Map(), hours: new Map() };
|
|
84
89
|
}
|
|
85
90
|
zones.set(timezone, zone);
|
|
86
91
|
return zone;
|
|
87
92
|
}
|
|
88
93
|
|
|
94
|
+
/** Offset of the zone at the start of UTC day `day`, cached. */
|
|
95
|
+
function dayStartOffset(zone: Zone & { kind: 'iana' }, day: number): number {
|
|
96
|
+
let offset = zone.dayStarts.get(day);
|
|
97
|
+
if (offset === undefined) {
|
|
98
|
+
offset = formattedOffset(zone.format, day * MS_PER_DAY);
|
|
99
|
+
if (zone.dayStarts.size >= MAX_CACHED_DAYS) zone.dayStarts.clear();
|
|
100
|
+
zone.dayStarts.set(day, offset);
|
|
101
|
+
}
|
|
102
|
+
return offset;
|
|
103
|
+
}
|
|
104
|
+
|
|
89
105
|
/**
|
|
90
106
|
* Offset of the zone from UTC at instant `time`, in milliseconds (local = UTC + offset).
|
|
91
|
-
*
|
|
92
|
-
*
|
|
107
|
+
*
|
|
108
|
+
* Two offset changes of a zone are always more than a day apart (the closest ones of the time zone database are 167
|
|
109
|
+
* hours apart), so when a UTC day starts with the offset of the next day, the whole day has it: daily bars cost one
|
|
110
|
+
* lookup per day. In a day with a change, an hour whose first and last second have the same offset has it (an offset
|
|
111
|
+
* change happens at most once in an hour): it is computed once and cached.
|
|
93
112
|
*/
|
|
94
113
|
function offsetAt(zone: Zone, time: number): number {
|
|
95
114
|
if (zone.kind === 'fixed') return zone.offsetMs;
|
|
115
|
+
const day = Math.floor(time / MS_PER_DAY);
|
|
116
|
+
const start = dayStartOffset(zone, day);
|
|
117
|
+
if (start === dayStartOffset(zone, day + 1)) return start;
|
|
96
118
|
const hour = Math.floor(time / MS_PER_HOUR);
|
|
97
119
|
let offset = zone.hours.get(hour);
|
|
98
120
|
if (offset === undefined) {
|