volas-compute 3.0.4

Numeric kernels and technical indicators for volas (pure functions over slices)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Stochastic oscillator batch fast paths.

use super::oscillators::rsi;
use crate::kernels;

/// TA-Lib stochastic raw %K: `100·(close − LL) / (HH − LL)` over `period` (HH/LL the
/// highest high / lowest low). A flat range yields 0, but — unlike [`rsv`] — the
/// warm-up is NaN, so the smoothing MAs in `stoch`/`stochf` begin at the right row.
/// Lookback `period-1`.
pub fn stoch_fastk(high: &[f64], low: &[f64], close: &[f64], period: usize) -> Vec<f64> {
    // The TA-Lib-style tracker wins on short and medium default stochastic windows
    // by avoiding van-Herk scratch buffers. On long arrays the O(n) van-Herk path has
    // better streaming behavior, so keep this as a fixed-cost fast path only.
    if close.len() <= 4096 && period > 0 && period <= 16 && period <= close.len() {
        if let Some(out) = stoch_fastk_small_window(high, low, close, period) {
            return out;
        }
    }
    // One fused pass for max(high) and min(low) instead of two separate van Herk sweeps.
    let (hh, ll) = kernels::rolling_max_min(high, low, period);
    let n = close.len();
    let mut out = vec![f64::NAN; n];
    for i in 0..n {
        if !hh[i].is_nan() {
            let diff = hh[i] - ll[i];
            out[i] = if diff != 0.0 {
                100.0 * (close[i] - ll[i]) / diff
            } else {
                0.0
            };
        }
    }
    out
}

fn stoch_fastk_small_window(
    high: &[f64],
    low: &[f64],
    close: &[f64],
    period: usize,
) -> Option<Vec<f64>> {
    let n = close.len();
    if high.len() != n || low.len() != n || period == 0 || period > n {
        return None;
    }
    let lookback = period - 1;
    // NaN-prefilled buffer: the warm-up stays NaN and the loop overwrites the
    // valid region (D2 2026-06-12 — replaces the with_capacity + set_len pattern;
    // the prefill is a vectorized splat, measured at parity by make perf-ab).
    let mut out = vec![f64::NAN; n];

    let high_ptr = high.as_ptr();
    let low_ptr = low.as_ptr();
    let close_ptr = close.as_ptr();
    let out_ptr = out.as_mut_ptr();
    let mut today = lookback;
    let mut trailing = 0usize;
    let mut highest_idx = usize::MAX;
    let mut lowest_idx = usize::MAX;
    let mut highest = 0.0;
    let mut lowest = 0.0;

    while today < n {
        // For the NaN-free OHLC hot path this mirrors TA-Lib's small-window tracker:
        // keep current extrema and rescan only when they fall out of the window.
        // If any input contains NaN, decline to the generic rolling kernel so the
        // existing NaN semantics stay centralized there.
        let low_today = unsafe { *low_ptr.add(today) };
        if low_today.is_nan() {
            return None;
        }
        if lowest_idx == usize::MAX || lowest_idx < trailing {
            lowest_idx = trailing;
            lowest = unsafe { *low_ptr.add(trailing) };
            if lowest.is_nan() {
                return None;
            }
            let mut idx = trailing + 1;
            while idx <= today {
                let value = unsafe { *low_ptr.add(idx) };
                if value.is_nan() {
                    return None;
                }
                if value < lowest {
                    lowest_idx = idx;
                    lowest = value;
                }
                idx += 1;
            }
        } else if low_today <= lowest {
            lowest_idx = today;
            lowest = low_today;
        }

        let high_today = unsafe { *high_ptr.add(today) };
        if high_today.is_nan() {
            return None;
        }
        if highest_idx == usize::MAX || highest_idx < trailing {
            highest_idx = trailing;
            highest = unsafe { *high_ptr.add(trailing) };
            if highest.is_nan() {
                return None;
            }
            let mut idx = trailing + 1;
            while idx <= today {
                let value = unsafe { *high_ptr.add(idx) };
                if value.is_nan() {
                    return None;
                }
                if value > highest {
                    highest_idx = idx;
                    highest = value;
                }
                idx += 1;
            }
        } else if high_today >= highest {
            highest_idx = today;
            highest = high_today;
        }

        let c = unsafe { *close_ptr.add(today) };
        if c.is_nan() {
            return None;
        }
        let diff = highest - lowest;
        unsafe {
            *out_ptr.add(today) = if diff != 0.0 {
                100.0 * (c - lowest) / diff
            } else {
                0.0
            };
        }
        trailing += 1;
        today += 1;
    }
    Some(out)
}

/// Default STOCHF `.d` (`fastk=5, fastd=3, SMA`) in one pass. This keeps the
/// TA-Lib-style 5-bar extremum tracker and maintains the 3-bar D sum as raw `%K`
/// values are produced, avoiding the full `fastk` buffer plus a second SMA pass.
/// Returns `None` on NaN or shape mismatch so callers can preserve the generic path.
pub fn stochf_d_default_sma(high: &[f64], low: &[f64], close: &[f64]) -> Option<Vec<f64>> {
    stoch_d_sma_fused::<1>(high, low, close)
}

/// Default STOCH `.d` (`fastk=5, slowk=3 SMA, slowd=3 SMA`) in one pass. The raw
/// `%K`, slowK sum, and slowD sum advance together; only the requested slowD line is
/// materialized. Falls back through `None` for NaN-bearing inputs.
pub fn stoch_d_default_sma(high: &[f64], low: &[f64], close: &[f64]) -> Option<Vec<f64>> {
    stoch_d_sma_fused::<2>(high, low, close)
}

fn stoch_d_sma_fused<const STAGES: usize>(
    high: &[f64],
    low: &[f64],
    close: &[f64],
) -> Option<Vec<f64>> {
    let n = close.len();
    if high.len() != n || low.len() != n || n < 5 {
        return None;
    }
    // NaN-prefilled buffer: the warm-up stays NaN and the loop overwrites the
    // valid region (D2 2026-06-12 — replaces the with_capacity + set_len pattern;
    // the prefill is a vectorized splat, measured at parity by make perf-ab).
    let mut out = vec![f64::NAN; n];

    let high_ptr = high.as_ptr();
    let low_ptr = low.as_ptr();
    let close_ptr = close.as_ptr();
    let out_ptr = out.as_mut_ptr();
    let mut today = 4usize;
    let mut trailing = 0usize;
    let mut highest_idx = usize::MAX;
    let mut lowest_idx = usize::MAX;
    let mut highest = 0.0;
    let mut lowest = 0.0;
    let mut k_window = [0.0f64; 3];
    let mut d_window = [0.0f64; 3];
    let mut k_sum = 0.0;
    let mut d_sum = 0.0;
    let mut k_count = 0usize;
    let mut d_count = 0usize;

    while today < n {
        let low_today = unsafe { *low_ptr.add(today) };
        if low_today.is_nan() {
            return None;
        }
        if lowest_idx == usize::MAX || lowest_idx < trailing {
            lowest_idx = trailing;
            lowest = unsafe { *low_ptr.add(trailing) };
            if lowest.is_nan() {
                return None;
            }
            let mut idx = trailing + 1;
            while idx <= today {
                let value = unsafe { *low_ptr.add(idx) };
                if value.is_nan() {
                    return None;
                }
                if value < lowest {
                    lowest_idx = idx;
                    lowest = value;
                }
                idx += 1;
            }
        } else if low_today <= lowest {
            lowest_idx = today;
            lowest = low_today;
        }

        let high_today = unsafe { *high_ptr.add(today) };
        if high_today.is_nan() {
            return None;
        }
        if highest_idx == usize::MAX || highest_idx < trailing {
            highest_idx = trailing;
            highest = unsafe { *high_ptr.add(trailing) };
            if highest.is_nan() {
                return None;
            }
            let mut idx = trailing + 1;
            while idx <= today {
                let value = unsafe { *high_ptr.add(idx) };
                if value.is_nan() {
                    return None;
                }
                if value > highest {
                    highest_idx = idx;
                    highest = value;
                }
                idx += 1;
            }
        } else if high_today >= highest {
            highest_idx = today;
            highest = high_today;
        }

        let c = unsafe { *close_ptr.add(today) };
        if c.is_nan() {
            return None;
        }
        let diff = highest - lowest;
        let fastk = if diff != 0.0 {
            100.0 * (c - lowest) / diff
        } else {
            0.0
        };

        let k_slot = k_count % 3;
        k_sum += fastk;
        if k_count >= 3 {
            k_sum -= k_window[k_slot];
        }
        k_window[k_slot] = fastk;
        k_count += 1;

        if k_count >= 3 {
            let slowk = k_sum / 3.0;
            if STAGES == 1 {
                unsafe {
                    *out_ptr.add(today) = slowk;
                }
            } else {
                let d_slot = d_count % 3;
                d_sum += slowk;
                if d_count >= 3 {
                    d_sum -= d_window[d_slot];
                }
                d_window[d_slot] = slowk;
                d_count += 1;
                if d_count >= 3 {
                    unsafe {
                        *out_ptr.add(today) = d_sum / 3.0;
                    }
                }
            }
        }

        trailing += 1;
        today += 1;
    }
    Some(out)
}

fn stoch_fastk_same_series_small_window(data: &[f64], period: usize) -> Option<Vec<f64>> {
    let n = data.len();
    let mut out = vec![f64::NAN; n];
    let mut today = period - 1;
    let mut trailing = 0usize;
    let mut highest_idx = usize::MAX;
    let mut lowest_idx = usize::MAX;
    let mut highest = 0.0;
    let mut lowest = 0.0;
    while today < n {
        let x = data[today];
        if x.is_nan() {
            return None;
        }
        if lowest_idx == usize::MAX
            || highest_idx == usize::MAX
            || lowest_idx < trailing
            || highest_idx < trailing
        {
            lowest_idx = trailing;
            lowest = data[trailing];
            highest_idx = trailing;
            highest = lowest;
            if lowest.is_nan() {
                return None;
            }
            for (off, &value) in data[(trailing + 1)..=today].iter().enumerate() {
                if value.is_nan() {
                    return None;
                }
                let idx = trailing + 1 + off;
                if value <= lowest {
                    lowest_idx = idx;
                    lowest = value;
                }
                if value >= highest {
                    highest_idx = idx;
                    highest = value;
                }
            }
        } else {
            if x <= lowest {
                lowest_idx = today;
                lowest = x;
            }
            if x >= highest {
                highest_idx = today;
                highest = x;
            }
        }

        let diff = highest - lowest;
        out[today] = if diff != 0.0 {
            100.0 * (x - lowest) / diff
        } else {
            0.0
        };
        trailing += 1;
        today += 1;
    }
    Some(out)
}

/// TA-Lib StochRSI raw %K: the stochastic %K of the RSI line — `stoch_fastk` applied
/// to `rsi(close, rsi_period)` over `fastk_period`. Because RSI itself warms up, the
/// %K is masked to NaN until a full `fastk_period` window of finite RSI is available
/// (index `rsi_period + fastk_period - 1`). The `fastd` line is then `ma_typed` of this.
pub fn stochrsi_fastk(close: &[f64], rsi_period: usize, fastk_period: usize) -> Vec<f64> {
    let rsi = rsi(close, rsi_period);
    let n = rsi.len();
    let mut fk = vec![f64::NAN; n];
    // RSI warms up with a leading-NaN prefix, so a rolling min/max over the whole line
    // fails the NaN-free check and drops to the slow monotonic-deque path — StochRSI's
    // hot spot. Run the stochastic %K on RSI's *finite tail* instead (NaN-free → the
    // van Herk fast path) and place it back at `start`. The first valid value then
    // lands at the first fully-finite RSI window — exactly the rows the old explicit
    // mask kept — and the result is bit-identical (min/max over a finite window is
    // order-independent and exact), so the separate masking pass is no longer needed.
    let start = rsi.iter().position(|x| !x.is_nan()).unwrap_or(n);
    let tail = &rsi[start..];
    if tail.len() >= fastk_period {
        // This StochRSI-specific call passes the same RSI buffer as high/low/close.
        // Track one rolling window here so ordinary STOCH/STOCHF keep the lean generic
        // `stoch_fastk` path with no extra same-buffer branch.
        let fk_tail = if fastk_period > 0 && fastk_period <= 8 {
            stoch_fastk_same_series_small_window(tail, fastk_period)
                .unwrap_or_else(|| stoch_fastk(tail, tail, tail, fastk_period))
        } else {
            stoch_fastk(tail, tail, tail, fastk_period)
        };
        fk[start..].copy_from_slice(&fk_tail);
    }
    fk
}

/// Default STOCHRSI `.d` (`rsi=14, fastk=5, fastd=3, SMA`) with the stochastic
/// and D-smoothing stages fused over the finite RSI tail. RSI itself remains the
/// single recursive source of truth; this only removes the intermediate fastK Vec.
pub fn stochrsi_d_default_sma(close: &[f64]) -> Option<Vec<f64>> {
    let rsi = rsi(close, 14);
    let n = rsi.len();
    let start = rsi.iter().position(|x| !x.is_nan()).unwrap_or(n);
    let tail = &rsi[start..];
    if tail.len() < 7 {
        return Some(vec![f64::NAN; n]);
    }

    // NaN-prefilled buffer: the warm-up stays NaN and the loop overwrites the
    // valid region (D2 2026-06-12 — replaces the with_capacity + set_len pattern;
    // the prefill is a vectorized splat, measured at parity by make perf-ab).
    let mut out = vec![f64::NAN; n];
    let out_ptr = out.as_mut_ptr();
    let src = tail.as_ptr();
    let mut today = 4usize;
    let mut trailing = 0usize;
    let mut highest_idx = usize::MAX;
    let mut lowest_idx = usize::MAX;
    let mut highest = 0.0;
    let mut lowest = 0.0;
    let mut k_window = [0.0f64; 3];
    let mut k_sum = 0.0;
    let mut k_count = 0usize;

    while today < tail.len() {
        let x = unsafe { *src.add(today) };
        if x.is_nan() {
            return None;
        }
        if lowest_idx == usize::MAX
            || highest_idx == usize::MAX
            || lowest_idx < trailing
            || highest_idx < trailing
        {
            lowest_idx = trailing;
            lowest = unsafe { *src.add(trailing) };
            highest_idx = trailing;
            highest = lowest;
            if lowest.is_nan() {
                return None; // LCOV_EXCL_LINE
            }
            let mut idx = trailing + 1;
            while idx <= today {
                let value = unsafe { *src.add(idx) };
                if value.is_nan() {
                    return None; // LCOV_EXCL_LINE
                }
                if value <= lowest {
                    lowest_idx = idx;
                    lowest = value;
                }
                if value >= highest {
                    highest_idx = idx;
                    highest = value;
                }
                idx += 1;
            }
        } else {
            if x <= lowest {
                lowest_idx = today;
                lowest = x;
            }
            if x >= highest {
                highest_idx = today;
                highest = x;
            }
        }

        let diff = highest - lowest;
        let fastk = if diff != 0.0 {
            100.0 * (x - lowest) / diff
        } else {
            0.0
        };
        let slot = k_count % 3;
        k_sum += fastk;
        if k_count >= 3 {
            k_sum -= k_window[slot];
        }
        k_window[slot] = fastk;
        k_count += 1;
        if k_count >= 3 {
            unsafe {
                *out_ptr.add(start + today) = k_sum / 3.0;
            }
        }

        trailing += 1;
        today += 1;
    }
    Some(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ohlc(n: usize) -> (Vec<f64>, Vec<f64>, Vec<f64>) {
        let close: Vec<f64> = (0..n).map(|i| 100.0 + i as f64).collect();
        let high: Vec<f64> = close.iter().map(|v| v + 2.0).collect();
        let low: Vec<f64> = close.iter().map(|v| v - 2.0).collect();
        (high, low, close)
    }

    #[test]
    fn stochastic_small_window_paths_cover_nan_declines() {
        let (high, low, close) = ohlc(8);
        let out = stoch_fastk(&high, &low, &close, 5);
        assert!(out[3].is_nan());
        assert!(out[4].is_finite());

        let flat = vec![3.0; 8];
        let out = stoch_fastk_small_window(&flat, &flat, &flat, 5).unwrap();
        assert_eq!(out[4], 0.0);

        assert!(stoch_fastk_small_window(&high[..7], &low, &close, 5).is_none());

        let mut x = low.clone();
        x[4] = f64::NAN;
        assert!(stoch_fastk_small_window(&high, &x, &close, 5).is_none());
        let mut x = low.clone();
        x[0] = f64::NAN;
        assert!(stoch_fastk_small_window(&high, &x, &close, 5).is_none());
        let mut x = low.clone();
        x[2] = f64::NAN;
        assert!(stoch_fastk_small_window(&high, &x, &close, 5).is_none());

        let mut x = high.clone();
        x[4] = f64::NAN;
        assert!(stoch_fastk_small_window(&x, &low, &close, 5).is_none());
        let mut x = high.clone();
        x[0] = f64::NAN;
        assert!(stoch_fastk_small_window(&x, &low, &close, 5).is_none());
        let mut x = high.clone();
        x[2] = f64::NAN;
        assert!(stoch_fastk_small_window(&x, &low, &close, 5).is_none());

        let mut x = close.clone();
        x[4] = f64::NAN;
        assert!(stoch_fastk_small_window(&high, &low, &x, 5).is_none());
    }

    #[test]
    fn stochastic_fused_default_paths_cover_nan_declines() {
        let (high, low, close) = ohlc(12);
        assert!(stoch_d_sma_fused::<1>(&high[..4], &low[..4], &close[..4]).is_none());

        let out = stochf_d_default_sma(&high, &low, &close).unwrap();
        assert!(out[5].is_nan());
        assert!(out[6].is_finite());
        let out = stoch_d_default_sma(&high, &low, &close).unwrap();
        assert!(out[7].is_nan());
        assert!(out[8].is_finite());

        let flat = vec![3.0; 12];
        let out = stoch_d_sma_fused::<2>(&flat, &flat, &flat).unwrap();
        assert_eq!(out[8], 0.0);

        let mut x = low.clone();
        x[4] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&high, &x, &close).is_none());
        let mut x = low.clone();
        x[0] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&high, &x, &close).is_none());
        let mut x = low.clone();
        x[2] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&high, &x, &close).is_none());

        let mut x = high.clone();
        x[4] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&x, &low, &close).is_none());
        let mut x = high.clone();
        x[0] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&x, &low, &close).is_none());
        let mut x = high.clone();
        x[2] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&x, &low, &close).is_none());

        let mut x = close.clone();
        x[4] = f64::NAN;
        assert!(stoch_d_sma_fused::<2>(&high, &low, &x).is_none());
    }

    #[test]
    fn same_series_and_stochrsi_paths_cover_guard_branches() {
        let flat = vec![7.0; 8];
        let out = stoch_fastk_same_series_small_window(&flat, 5).unwrap();
        assert_eq!(out[4], 0.0);

        let mut x = (0..8).map(|i| 10.0 + i as f64).collect::<Vec<_>>();
        x[4] = f64::NAN;
        assert!(stoch_fastk_same_series_small_window(&x, 5).is_none());
        let mut x = (0..8).map(|i| 10.0 + i as f64).collect::<Vec<_>>();
        x[0] = f64::NAN;
        assert!(stoch_fastk_same_series_small_window(&x, 5).is_none());
        let mut x = (0..8).map(|i| 10.0 + i as f64).collect::<Vec<_>>();
        x[2] = f64::NAN;
        assert!(stoch_fastk_same_series_small_window(&x, 5).is_none());

        let short = (0..20).map(|i| 100.0 + i as f64).collect::<Vec<_>>();
        let out = stochrsi_d_default_sma(&short).unwrap();
        assert!(out.iter().all(|v| v.is_nan()));

        let long = (0..80)
            .map(|i| 100.0 + (i as f64 * 0.17).sin() * 4.0 + i as f64 * 0.1)
            .collect::<Vec<_>>();
        let out = stochrsi_d_default_sma(&long).unwrap();
        assert!(out.iter().any(|v| v.is_finite()));

        let mut with_nan = long;
        with_nan[20] = f64::NAN;
        assert!(stochrsi_d_default_sma(&with_nan).is_none());
    }
}