oakscriptjs 0.15.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/callsite/index.d.ts +3 -3
- package/dist/cjs/callsite/index.js.map +1 -1
- package/dist/cjs/drawing/registry.d.ts +2 -2
- package/dist/cjs/drawing/registry.d.ts.map +1 -1
- package/dist/cjs/drawing/registry.js +17 -8
- 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/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/ta/index.d.ts +9 -11
- package/dist/cjs/ta/index.d.ts.map +1 -1
- package/dist/cjs/ta/index.js.map +2 -2
- package/dist/cjs/ta/running-sum.d.ts +13 -19
- package/dist/cjs/ta/running-sum.d.ts.map +1 -1
- package/dist/cjs/ta/running-sum.js +62 -74
- package/dist/cjs/ta/running-sum.js.map +2 -2
- package/dist/esm/callsite/index.d.ts +3 -3
- package/dist/esm/callsite/index.js +3 -3
- package/dist/esm/drawing/registry.d.ts +2 -2
- package/dist/esm/drawing/registry.d.ts.map +1 -1
- package/dist/esm/drawing/registry.js +28 -12
- 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/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/ta/index.d.ts +9 -11
- package/dist/esm/ta/index.d.ts.map +1 -1
- package/dist/esm/ta/index.js +13 -15
- package/dist/esm/ta/index.js.map +1 -1
- package/dist/esm/ta/running-sum.d.ts +13 -19
- package/dist/esm/ta/running-sum.d.ts.map +1 -1
- package/dist/esm/ta/running-sum.js +88 -103
- package/dist/esm/ta/running-sum.js.map +1 -1
- package/package.json +1 -1
- package/src/callsite/index.ts +3 -3
- package/src/drawing/registry.ts +29 -11
- package/src/index.ts +1 -1
- package/src/math/index.ts +3 -7
- package/src/ta/index.ts +13 -15
- package/src/ta/running-sum.ts +82 -99
|
@@ -1,144 +1,129 @@
|
|
|
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
|
-
/** Kahan steps that move the kept sum from the kept length to `len`; the result goes to stepS / stepC. */
|
|
56
|
-
resize(len) {
|
|
76
|
+
stepPartials = [];
|
|
77
|
+
/** The partials of the last `len` values (kept state, nothing added). */
|
|
78
|
+
resized(len) {
|
|
57
79
|
const values = this.values;
|
|
58
80
|
const n = values.length;
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
for (let k = Math.max(0, n - kept); k <= n - len - 1; k++)
|
|
70
|
-
add(-values[k]);
|
|
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;
|
|
81
|
+
const p = this.partials.slice();
|
|
82
|
+
const kept = Math.min(this.keptLength, n);
|
|
83
|
+
const want = Math.min(len, n);
|
|
84
|
+
if (want < kept)
|
|
85
|
+
for (let k = n - kept; k < n - want; k++)
|
|
86
|
+
addExact(p, -values[k]);
|
|
87
|
+
else
|
|
88
|
+
for (let k = n - kept - 1; k >= n - want; k--)
|
|
89
|
+
addExact(p, values[k]);
|
|
90
|
+
return p;
|
|
78
91
|
}
|
|
79
92
|
/**
|
|
80
|
-
* The
|
|
93
|
+
* The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
|
|
81
94
|
* unchanged until {@link commit}.
|
|
82
95
|
*
|
|
83
96
|
* @returns The sum, or NaN while fewer than `length` values exist (with `x`)
|
|
84
97
|
*/
|
|
85
98
|
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;
|
|
99
|
+
const n = this.values.length;
|
|
100
|
+
const p = this.resized(length - 1);
|
|
101
|
+
addExact(p, x);
|
|
102
|
+
this.stepPartials = p;
|
|
103
|
+
this.stepLength = Math.min(length, n + 1);
|
|
115
104
|
this.stepX = x;
|
|
116
|
-
|
|
117
|
-
this.stepC = c;
|
|
118
|
-
return n + 1 >= len ? s : NaN;
|
|
105
|
+
return n + 1 >= length ? roundExact(p) : NaN;
|
|
119
106
|
}
|
|
120
107
|
/**
|
|
121
|
-
* The
|
|
122
|
-
*
|
|
108
|
+
* The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
|
|
109
|
+
* unchanged until {@link commit}. With an unchanged length, it is the previous result.
|
|
123
110
|
*
|
|
124
111
|
* @returns The sum, or NaN while fewer than `length` values exist
|
|
125
112
|
*/
|
|
126
113
|
stepNa(length) {
|
|
127
|
-
this.resize(length);
|
|
128
|
-
this.stepLength = length;
|
|
129
|
-
this.stepX = NaN;
|
|
130
114
|
const n = this.values.length;
|
|
131
|
-
|
|
115
|
+
const p = this.resized(length);
|
|
116
|
+
this.stepPartials = p;
|
|
117
|
+
this.stepLength = Math.min(length, n);
|
|
118
|
+
this.stepX = NaN;
|
|
119
|
+
return n > 0 && n >= length ? roundExact(p) : NaN;
|
|
132
120
|
}
|
|
133
121
|
/** Keeps the state of the last {@link step} or {@link stepNa}. */
|
|
134
122
|
commit() {
|
|
135
|
-
if (!Number.isNaN(this.stepX))
|
|
123
|
+
if (!Number.isNaN(this.stepX))
|
|
136
124
|
this.values.push(this.stepX);
|
|
137
|
-
this.stored.push(this.stepY);
|
|
138
|
-
}
|
|
139
125
|
this.keptLength = this.stepLength;
|
|
140
|
-
this.
|
|
141
|
-
this.c = this.stepC;
|
|
126
|
+
this.partials = this.stepPartials;
|
|
142
127
|
}
|
|
143
128
|
}
|
|
144
129
|
/** The running sums of `source` with the length `lengthAt(i)` on bar `i`. */
|
|
@@ -154,7 +139,7 @@ function sumWindows(source, lengthAt) {
|
|
|
154
139
|
return out;
|
|
155
140
|
}
|
|
156
141
|
/**
|
|
157
|
-
* The sum of the last `length` non-na values on each bar, as PineScript
|
|
142
|
+
* The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.
|
|
158
143
|
*
|
|
159
144
|
* @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)
|
|
160
145
|
* @param length - Number of non-na values in the window
|
|
@@ -190,7 +175,7 @@ export function runningSumSeries(source, lengths, name = 'math.sum') {
|
|
|
190
175
|
const lens = seriesLengths(lengths, source.length, name);
|
|
191
176
|
return sumWindows(source, (i) => lens[i]);
|
|
192
177
|
}
|
|
193
|
-
/** Variance from the
|
|
178
|
+
/** Variance from the window sums of the values and of their squares; rules in {@link runningVariance}. */
|
|
194
179
|
function varianceFromSums(source, sumOf, lengthAt, biased) {
|
|
195
180
|
const squares = Array.from({ length: source.length }, (_, i) => {
|
|
196
181
|
const v = source[i];
|
|
@@ -206,7 +191,7 @@ function varianceFromSums(source, sumOf, lengthAt, biased) {
|
|
|
206
191
|
});
|
|
207
192
|
}
|
|
208
193
|
/**
|
|
209
|
-
* Window variance from the
|
|
194
|
+
* Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):
|
|
210
195
|
* biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.
|
|
211
196
|
*/
|
|
212
197
|
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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C5C,MAAc,EACd,MAAe;IAEf,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAC/B,OAAO,gBAAgB,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AACpF,CAAC;AAED,4FAA4F;AAC5F,MAAM,UAAU,qBAAqB,CACnC,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"}
|
package/package.json
CHANGED
package/src/callsite/index.ts
CHANGED
|
@@ -452,9 +452,9 @@ function loopSumSite(
|
|
|
452
452
|
* - 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
453
|
* `length - 1` kept values; the last call of the bar is the one kept for the later bars
|
|
454
454
|
* - 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
|
|
455
|
+
* its own length, as `math.sum` with a series length
|
|
456
456
|
* - a bar without a call is not in the history (as a call in an `if` block)
|
|
457
|
-
* - the sum is the
|
|
457
|
+
* - the sum is the correctly rounded window sum, as `math.sum` (bit for bit, #188)
|
|
458
458
|
*
|
|
459
459
|
* An na value is skipped as in `math.sum` (it returns the sum of the last `length` earlier values); when it is the
|
|
460
460
|
* last call of a bar, the bar is not in the history (not measured).
|
|
@@ -498,7 +498,7 @@ export function sma(): (barIndex: number, value: number, length: number) => numb
|
|
|
498
498
|
|
|
499
499
|
/**
|
|
500
500
|
* One `ta.stdev(source, length, biased)` call site inside a `for` / `while` loop, called with
|
|
501
|
-
* `(barIndex, value, length, biased?)` on each call: the
|
|
501
|
+
* `(barIndex, value, length, biased?)` on each call: the window sums of the values and of their squares follow the
|
|
502
502
|
* rules of {@link sum}; the result is computed from them as `ta.stdev` (bit for bit).
|
|
503
503
|
*
|
|
504
504
|
* @example
|
package/src/drawing/registry.ts
CHANGED
|
@@ -92,7 +92,8 @@ let held = new WeakMap<object, number>();
|
|
|
92
92
|
let releasedAt = new WeakMap<object, number>();
|
|
93
93
|
/** Bars during which a released linefill is still skipped by the maximum count. */
|
|
94
94
|
const LINEFILL_RELEASE_BARS = 5000;
|
|
95
|
-
/** Bar on which a `var` variable got an object: the maximum count
|
|
95
|
+
/** Bar on which a `var` variable got an object: on that bar the maximum count deletes it only when no other object can
|
|
96
|
+
* go; from the next bar on, never. */
|
|
96
97
|
let heldAt = new WeakMap<object, number>();
|
|
97
98
|
/** Kinds with a maximum count of their own. */
|
|
98
99
|
type CountedKind = 'line' | 'label' | 'box' | 'polyline';
|
|
@@ -131,6 +132,16 @@ function heldBefore(obj: object): boolean {
|
|
|
131
132
|
return bar === undefined || currentBar === null || bar < currentBar;
|
|
132
133
|
}
|
|
133
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Held by a `var` variable since the current bar, or released by its last variable on the current bar: the maximum
|
|
137
|
+
* count deletes such an object only when the objects it may delete are not enough (#193; 12 `var` lines made on the
|
|
138
|
+
* first bar with a maximum of 5 lose the first 6, #158).
|
|
139
|
+
*/
|
|
140
|
+
function heldOnThisBar(obj: object): boolean {
|
|
141
|
+
if (currentBar === null) return false;
|
|
142
|
+
return held.has(obj) ? heldAt.get(obj) === currentBar : releasedAt.get(obj) === currentBar;
|
|
143
|
+
}
|
|
144
|
+
|
|
134
145
|
/** Linefill rule: held, or released less than 5,000 bars ago. */
|
|
135
146
|
function fillProtected(obj: object): boolean {
|
|
136
147
|
if (held.has(obj)) return true;
|
|
@@ -201,8 +212,8 @@ let lastBar: LastBar | null = null;
|
|
|
201
212
|
* Tells the drawing functions which bar is being calculated (`bar_index`), so that a bar index more than 10,000
|
|
202
213
|
* bars back throws as in PineScript; `null` turns the check off. `eachBar` of the script API calls it.
|
|
203
214
|
*
|
|
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
|
|
215
|
+
* The bar also dates the objects held by a `var` variable (on that bar deleted only when no other object can go,
|
|
216
|
+
* 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
217
|
* last bar of the data (PineScript `last_bar_index`): with it, a bar index more than 500 bars after it throws, also
|
|
207
218
|
* on the bars before the last one (RE10020).
|
|
208
219
|
*/
|
|
@@ -287,15 +298,22 @@ function cutOldest<K extends DrawingKind>(
|
|
|
287
298
|
if (kind === 'table') return []; // tables have no maximum count
|
|
288
299
|
const max = limits[kind === 'linefill' ? 'line' : (kind as keyof DrawingLimits)] + allowance(kind);
|
|
289
300
|
if (list.length <= max + SLACK) return [];
|
|
290
|
-
const
|
|
291
|
-
const cut: Array<DrawingTypes[K]> = [];
|
|
301
|
+
const gone = new Set<object>();
|
|
292
302
|
let excess = list.length - max;
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
303
|
+
const candidate = (old: DrawingTypes[K]): boolean =>
|
|
304
|
+
old !== obj && (!only || only(old)) && !(kind === 'linefill' ? fillProtected(old) : heldBefore(old));
|
|
305
|
+
// the oldest first; the objects a `var` variable got (or released) on this bar only when the others are not enough
|
|
306
|
+
for (const weak of kind === 'linefill' ? [false] : [false, true]) {
|
|
307
|
+
for (const old of list) {
|
|
308
|
+
if (excess <= 0) break;
|
|
309
|
+
if (!gone.has(old) && candidate(old) && (kind === 'linefill' || heldOnThisBar(old) === weak)) {
|
|
310
|
+
excess--;
|
|
311
|
+
gone.add(old);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
298
314
|
}
|
|
315
|
+
const kept = list.filter((old) => !gone.has(old));
|
|
316
|
+
const cut = list.filter((old) => gone.has(old));
|
|
299
317
|
// linefills: still above the maximum + 5, the oldest go, the held ones too
|
|
300
318
|
if (kind === 'linefill' && kept.length > max + SLACK) cut.push(...kept.splice(0, kept.length - max));
|
|
301
319
|
list.length = 0;
|
|
@@ -334,7 +352,7 @@ function registerLast<K extends DrawingKind>(bar: LastBar, kind: K, obj: Drawing
|
|
|
334
352
|
const list = live[kind];
|
|
335
353
|
list.push(obj);
|
|
336
354
|
const isOld = (old: object): boolean => bar.old.has(old);
|
|
337
|
-
if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old))) {
|
|
355
|
+
if (list.some((old) => old !== obj && isOld(old) && !heldBefore(old) && !heldOnThisBar(old))) {
|
|
338
356
|
// the objects of the bars before only; a line takes its linefills with it
|
|
339
357
|
const cut = cutOldest(kind, list, obj, isOld);
|
|
340
358
|
if (kind === 'polyline') {
|
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.1';
|
|
229
229
|
|
|
230
230
|
/**
|
|
231
231
|
* Library information
|
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)
|
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
|
|
|
@@ -2612,7 +2610,7 @@ export function min(source: Source): series_float {
|
|
|
2612
2610
|
* @remarks
|
|
2613
2611
|
* PineScript rule (bit for bit): `-num / math.sum(source, length)`, with `num` the sum of `source[i] * (i + 1)`
|
|
2614
2612
|
* for `i = 0 .. length - 1`, added newest first.
|
|
2615
|
-
* - The denominator is the
|
|
2613
|
+
* - The denominator is the correctly rounded window sum of `math.sum` (the last `length` non-`na` values)
|
|
2616
2614
|
* - The numerator reads the last `length` bars, so an `na` among them gives `na`
|
|
2617
2615
|
* - A zero denominator gives `na` (PineScript division by zero)
|
|
2618
2616
|
*
|