Commit f0c67dd
authored
Fix remaining indicator rolling-window bugs (#4351)
* Fix SpreadAnalyzer panic when quotes exceed capacity
`SpreadAnalyzer` stored spreads in an unbounded `Vec`, whereas the Cython
reference uses `deque(maxlen=capacity)`. Once more than `capacity` quotes
were processed, `fast_mean_iterated` received a slice longer than the
expected length, returned an error, and panicked on `unwrap` — crashing
live trading on the (capacity + 1)th quote.
Bound the rolling window to `capacity` by evicting the oldest spread,
matching the Cython behavior. Add a regression test feeding more than
`capacity` quotes; the existing tests stopped at exactly `capacity`, so
the panic went uncaught.
* Fix EfficiencyRatio unbounded window and reset
`EfficiencyRatio` stored prices and deltas in unbounded `Vec`s, whereas the
Cython reference uses `deque(maxlen=period)` for both. After `period` updates
this diverged from Cython on every bar:
- `net_diff` used the all-time-first price instead of the price `period` bars
back.
- `sum_deltas` summed every delta ever seen instead of the last `period` (also
an unbounded memory leak).
`reset()` also cleared `inputs` but left `deltas` populated, so stale deltas
leaked into the next run.
Bound both windows to `period` and clear `deltas` on reset, matching the Cython
behavior. Since `AdaptiveMovingAverage` embeds this indicator, its Kaufman
efficiency input is corrected too. Add regression tests feeding more than
`period` inputs and exercising reset; the existing tests stopped at exactly
`period`, so the divergence went uncaught.
* Fix RelativeVolatilityIndex std rolling window
`RelativeVolatilityIndex` stored prices in a fixed-capacity 1024-element
`ArrayDeque`, whereas the Cython reference uses `deque(maxlen=period)`. The
standard deviation was therefore computed over up to 1024 observations while
the mean came from the `period`-window SMA, diverging from Cython on every bar
once more than `period` prices were seen.
Bound the price window to `period`, matching Cython. Add a regression test
feeding more than `period` inputs; the existing tests stayed within the
buffer's growth range, so the divergence went uncaught.
* Fix FuzzyCandlesticks unbounded rolling windows
`FuzzyCandlesticks` stored candle lengths, body percents, and upper/lower wick
percents in four fixed-capacity 1024-element `ArrayDeque`s, whereas the Cython
reference uses `deque(maxlen=period)` for each. Once more than `period` candles
were processed, the means (`sum / period`) summed the entire buffer and the
standard deviations divided by the full buffer length, so every fuzzy
size/body/wick classification used statistics over far more than `period`
candles.
Bound all four windows to `period` by evicting the oldest entry, matching
Cython. Add a regression test feeding more than `period` candles; the existing
tests stayed within `period`, so the divergence went uncaught.
* Fix SpreadAnalyzer average freezing at capacity
Once the rolling window reaches `capacity`, the incremental
`fast_mean_iterated(..., drop_left=false)` update subtracts
`values[length - 1]` (the spread just pushed) instead of the evicted
oldest value, so the running average stops changing for non-constant
spreads. The Cython reference shares this quirk; the all-constant
regression test hid it.
Recompute the average from the bounded window after eviction, which is
O(capacity) given the existing `Vec::remove(0)` and always correct. Add
a regression with monotonically increasing spreads past capacity, and
update the constant-spread baked value to the recomputed float.1 parent 598a088 commit f0c67dd
4 files changed
Lines changed: 182 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
| |||
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
102 | 110 | | |
103 | 111 | | |
104 | 112 | | |
| |||
108 | 116 | | |
109 | 117 | | |
110 | 118 | | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
111 | 124 | | |
112 | 125 | | |
113 | 126 | | |
| |||
203 | 216 | | |
204 | 217 | | |
205 | 218 | | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
206 | 254 | | |
207 | 255 | | |
208 | 256 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
92 | | - | |
93 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
94 | 105 | | |
95 | 106 | | |
96 | 107 | | |
| |||
118 | 129 | | |
119 | 130 | | |
120 | 131 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | 132 | | |
148 | 133 | | |
149 | 134 | | |
| |||
212 | 197 | | |
213 | 198 | | |
214 | 199 | | |
215 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
216 | 249 | | |
217 | 250 | | |
218 | 251 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
327 | 339 | | |
328 | 340 | | |
329 | 341 | | |
| |||
617 | 629 | | |
618 | 630 | | |
619 | 631 | | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
620 | 668 | | |
621 | 669 | | |
622 | 670 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
134 | 142 | | |
135 | 143 | | |
136 | 144 | | |
| |||
223 | 231 | | |
224 | 232 | | |
225 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
226 | 249 | | |
227 | 250 | | |
228 | 251 | | |
| |||
0 commit comments