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.
Files changed (48) hide show
  1. package/dist/cjs/callsite/index.d.ts +3 -3
  2. package/dist/cjs/callsite/index.js.map +1 -1
  3. package/dist/cjs/drawing/registry.d.ts +2 -2
  4. package/dist/cjs/drawing/registry.d.ts.map +1 -1
  5. package/dist/cjs/drawing/registry.js +17 -8
  6. package/dist/cjs/drawing/registry.js.map +2 -2
  7. package/dist/cjs/index.d.ts +1 -1
  8. package/dist/cjs/index.js +1 -1
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/math/index.d.ts +2 -6
  11. package/dist/cjs/math/index.d.ts.map +1 -1
  12. package/dist/cjs/math/index.js.map +2 -2
  13. package/dist/cjs/script/index.d.ts.map +1 -1
  14. package/dist/cjs/ta/index.d.ts +9 -11
  15. package/dist/cjs/ta/index.d.ts.map +1 -1
  16. package/dist/cjs/ta/index.js.map +2 -2
  17. package/dist/cjs/ta/running-sum.d.ts +13 -19
  18. package/dist/cjs/ta/running-sum.d.ts.map +1 -1
  19. package/dist/cjs/ta/running-sum.js +62 -74
  20. package/dist/cjs/ta/running-sum.js.map +2 -2
  21. package/dist/esm/callsite/index.d.ts +3 -3
  22. package/dist/esm/callsite/index.js +3 -3
  23. package/dist/esm/drawing/registry.d.ts +2 -2
  24. package/dist/esm/drawing/registry.d.ts.map +1 -1
  25. package/dist/esm/drawing/registry.js +28 -12
  26. package/dist/esm/drawing/registry.js.map +1 -1
  27. package/dist/esm/index.d.ts +1 -1
  28. package/dist/esm/index.js +1 -1
  29. package/dist/esm/math/index.d.ts +2 -6
  30. package/dist/esm/math/index.d.ts.map +1 -1
  31. package/dist/esm/math/index.js +1 -1
  32. package/dist/esm/math/index.js.map +1 -1
  33. package/dist/esm/script/index.d.ts.map +1 -1
  34. package/dist/esm/ta/index.d.ts +9 -11
  35. package/dist/esm/ta/index.d.ts.map +1 -1
  36. package/dist/esm/ta/index.js +13 -15
  37. package/dist/esm/ta/index.js.map +1 -1
  38. package/dist/esm/ta/running-sum.d.ts +13 -19
  39. package/dist/esm/ta/running-sum.d.ts.map +1 -1
  40. package/dist/esm/ta/running-sum.js +88 -103
  41. package/dist/esm/ta/running-sum.js.map +1 -1
  42. package/package.json +1 -1
  43. package/src/callsite/index.ts +3 -3
  44. package/src/drawing/registry.ts +29 -11
  45. package/src/index.ts +1 -1
  46. package/src/math/index.ts +3 -7
  47. package/src/ta/index.ts +13 -15
  48. package/src/ta/running-sum.ts +82 -99
@@ -1,145 +1,128 @@
1
1
  /**
2
- * Running window sum with the floating-point behaviour of PineScript `math.sum` / `ta.sma` / `ta.stdev` /
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 does not sum the window again on each bar. It keeps a compensated (Kahan) running sum:
6
- * - each bar first removes, with a Kahan step, the compensated value that was added for the value that leaves the
7
- * window, then adds the new value with a Kahan step and keeps its compensated value;
8
- * - when the compensation of the previous step would be lost when applied to the new value, the sum is computed
9
- * again from the window values (newest first) and the compensation is reset. The test moves the new value toward
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
- * So the result can differ from the exact window sum in the last bits (e.g. a small residue on a window of zeros),
13
- * as in PineScript.
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
- * Running sum state, one value at a time. {@link step} computes the sum with a new value from the kept state without
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). PineScript then first moves the kept sum to the
41
- * new length with Kahan steps of the raw values (not the stored compensated values): a smaller length removes the
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
- private readonly stored: number[] = [];
49
- private keptLength = NaN;
50
- private s = 0;
51
- private c = 0;
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 stepY = NaN;
55
- private stepS = 0;
56
- private stepC = 0;
76
+ private stepPartials: number[] = [];
57
77
 
58
- /** Kahan steps that move the kept sum from the kept length to `len`; the result goes to stepS / stepC. */
59
- private resize(len: number): void {
78
+ /** The partials of the last `len` values (kept state, nothing added). */
79
+ private resized(len: number): number[] {
60
80
  const values = this.values;
61
81
  const n = values.length;
62
- const kept = this.keptLength;
63
- let s = this.s;
64
- let c = this.c;
65
- const add = (v: number) => {
66
- const y = v - c;
67
- const t = s + y;
68
- c = (t - s) - y;
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;
82
+ const p = this.partials.slice();
83
+ const kept = Math.min(this.keptLength, n);
84
+ const want = Math.min(len, n);
85
+ if (want < kept) for (let k = n - kept; k < n - want; k++) addExact(p, -values[k]!);
86
+ else for (let k = n - kept - 1; k >= n - want; k--) addExact(p, values[k]!);
87
+ return p;
78
88
  }
79
89
 
80
90
  /**
81
- * The running sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
91
+ * The sum of the last `length` values with the (not na) value `x` added, from the kept state; the state is
82
92
  * unchanged until {@link commit}.
83
93
  *
84
94
  * @returns The sum, or NaN while fewer than `length` values exist (with `x`)
85
95
  */
86
96
  step(x: number, length: number): number {
87
- const len = length;
88
- const values = this.values;
89
- this.resize(len);
90
- let s = this.stepS;
91
- let c = this.stepC;
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;
97
+ const n = this.values.length;
98
+ const p = this.resized(length - 1);
99
+ addExact(p, x);
100
+ this.stepPartials = p;
101
+ this.stepLength = Math.min(length, n + 1);
114
102
  this.stepX = x;
115
- this.stepS = s;
116
- this.stepC = c;
117
- return n + 1 >= len ? s : NaN;
103
+ return n + 1 >= length ? roundExact(p) : NaN;
118
104
  }
119
105
 
120
106
  /**
121
- * The running sum of the last `length` values on a bar with an na value (no value added), from the kept state; the
122
- * state is unchanged until {@link commit}. With an unchanged length, it is the previous result.
107
+ * The sum of the last `length` values on a bar with an na value (no value added), from the kept state; the state is
108
+ * unchanged until {@link commit}. With an unchanged length, it is the previous result.
123
109
  *
124
110
  * @returns The sum, or NaN while fewer than `length` values exist
125
111
  */
126
112
  stepNa(length: number): number {
127
- this.resize(length);
128
- this.stepLength = length;
129
- this.stepX = NaN;
130
113
  const n = this.values.length;
131
- return n > 0 && n >= length ? this.stepS : NaN;
114
+ const p = this.resized(length);
115
+ this.stepPartials = p;
116
+ this.stepLength = Math.min(length, n);
117
+ this.stepX = NaN;
118
+ return n > 0 && n >= length ? roundExact(p) : NaN;
132
119
  }
133
120
 
134
121
  /** Keeps the state of the last {@link step} or {@link stepNa}. */
135
122
  commit(): void {
136
- if (!Number.isNaN(this.stepX)) {
137
- this.values.push(this.stepX);
138
- this.stored.push(this.stepY);
139
- }
123
+ if (!Number.isNaN(this.stepX)) this.values.push(this.stepX);
140
124
  this.keptLength = this.stepLength;
141
- this.s = this.stepS;
142
- this.c = this.stepC;
125
+ this.partials = this.stepPartials;
143
126
  }
144
127
  }
145
128
 
@@ -157,7 +140,7 @@ function sumWindows(source: ArrayLike<number | null | undefined>, lengthAt: (i:
157
140
  }
158
141
 
159
142
  /**
160
- * The sum of the last `length` non-na values on each bar, as PineScript computes it.
143
+ * The sum of the last `length` non-na values on each bar, correctly rounded, as PineScript.
161
144
  *
162
145
  * @param source - Values (na: NaN, null, undefined or +/-Infinity, skipped)
163
146
  * @param length - Number of non-na values in the window
@@ -198,7 +181,7 @@ export function runningSumSeries(
198
181
  return sumWindows(source, (i) => lens[i]!);
199
182
  }
200
183
 
201
- /** Variance from the running sums of the values and of their squares; rules in {@link runningVariance}. */
184
+ /** Variance from the window sums of the values and of their squares; rules in {@link runningVariance}. */
202
185
  function varianceFromSums(
203
186
  source: ArrayLike<number | null | undefined>,
204
187
  sumOf: (src: ArrayLike<number | null | undefined>) => number[],
@@ -220,7 +203,7 @@ function varianceFromSums(
220
203
  }
221
204
 
222
205
  /**
223
- * Window variance from the running sums of the values and of their squares, as PineScript computes it:
206
+ * Window variance from the window sums of the values and of their squares, as PineScript computes it (#188):
224
207
  * biased `sumSq / n - mean * mean`, unbiased `sumSq / (n - 1) - mean * sum / (n - 1)`, with `mean = sum / n`.
225
208
  */
226
209
  export function runningVariance(