quantedge-ta 0.15.1

A streaming technical analysis library for Rust
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
use std::{fmt::Display, num::NonZero};

use crate::{
    Indicator, IndicatorConfig, IndicatorConfigBuilder, PriceSource, internals::PriceWindow,
};

/// Configuration for the Commodity Channel Index ([`Cci`]) indicator.
///
/// CCI measures the deviation of a price from its statistical mean.
/// Uses the typical price (HLC3) by default. Output begins after
/// `length` bars.
///
/// # Example
///
/// ```
/// use quantedge_ta::CciConfig;
/// use std::num::NonZero;
///
/// let config = CciConfig::close(NonZero::new(20).unwrap());
/// assert_eq!(config.length(), 20);
/// ```
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct CciConfig {
    length: usize,
    source: PriceSource,
}

impl CciConfig {
    /// Window length (number of bars).
    #[must_use]
    pub fn length(&self) -> usize {
        self.length
    }

    /// CCI on closing price.
    #[must_use]
    pub fn close(length: NonZero<usize>) -> Self {
        Self::builder()
            .length(length)
            .source(PriceSource::Close)
            .build()
    }

    /// CCI on typical price: `(high + low + close) / 3`.
    #[must_use]
    pub fn hlc3(length: NonZero<usize>) -> Self {
        Self::builder().length(length).build()
    }
}

impl IndicatorConfig for CciConfig {
    type Builder = CciConfigBuilder;

    fn builder() -> Self::Builder {
        CciConfigBuilder::new()
    }

    fn source(&self) -> PriceSource {
        self.source
    }

    fn convergence(&self) -> usize {
        self.length
    }

    fn to_builder(&self) -> Self::Builder {
        CciConfigBuilder {
            length: Some(self.length),
            source: self.source,
        }
    }
}

impl Default for CciConfig {
    /// Default: length=20, source=HLC3 (Lambert's original Typical Price, `TradingView` default).
    fn default() -> Self {
        Self {
            length: 20,
            source: PriceSource::HLC3,
        }
    }
}

impl Display for CciConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CciConfig({}, {})", self.length, self.source)
    }
}

/// Builder for [`CciConfig`].
///
/// Defaults: source = [`PriceSource::HLC3`].
/// Length must be set before calling
/// [`build`](IndicatorConfigBuilder::build).
pub struct CciConfigBuilder {
    length: Option<usize>,
    source: PriceSource,
}

impl CciConfigBuilder {
    fn new() -> Self {
        CciConfigBuilder {
            length: None,
            source: PriceSource::HLC3,
        }
    }

    /// Sets the indicator window length.
    #[must_use]
    pub fn length(mut self, length: NonZero<usize>) -> Self {
        self.length.replace(length.get());
        self
    }
}

impl IndicatorConfigBuilder<CciConfig> for CciConfigBuilder {
    fn source(mut self, source: PriceSource) -> Self {
        self.source = source;
        self
    }

    fn build(self) -> CciConfig {
        CciConfig {
            length: self.length.expect("length is required"),
            source: self.source,
        }
    }
}

/// Commodity Channel Index (CCI).
///
/// Measures the deviation of the current price from its simple
/// moving average, scaled by the mean absolute deviation. Positive
/// values indicate the price is above its average; negative values
/// indicate it is below.
///
/// ```text
/// CCI = (price − SMA) / (0.015 × mean_deviation)
/// ```
///
/// The constant 0.015 scales CCI so that roughly 70–80% of values
/// fall between −100 and +100. Values outside that range suggest
/// strong trend or overbought/oversold conditions.
///
/// When the mean deviation is near zero (flat market), returns 0.0.
///
/// Returns `None` until the window is full (after `length` bars).
///
/// Supports live repainting: feeding a bar with the same `open_time`
/// recomputes from the previous state without advancing.
///
/// # Example
///
/// ```
/// use quantedge_ta::{Cci, CciConfig};
/// use std::num::NonZero;
/// # use quantedge_ta::{Ohlcv, Price, Timestamp};
/// #
/// # struct Bar(f64, u64);
/// # impl Ohlcv for Bar {
/// #     fn open(&self) -> Price { self.0 }
/// #     fn high(&self) -> Price { self.0 }
/// #     fn low(&self) -> Price { self.0 }
/// #     fn close(&self) -> Price { self.0 }
/// #     fn open_time(&self) -> Timestamp { self.1 }
/// # }
///
/// let mut cci = Cci::new(CciConfig::close(NonZero::new(3).unwrap()));
///
/// assert_eq!(cci.compute(&Bar(10.0, 1)), None);
/// assert_eq!(cci.compute(&Bar(20.0, 2)), None);
///
/// // SMA = 20, mean_dev = 20/3, CCI = (30 − 20) / (0.015 × 20/3) = 100
/// let value = cci.compute(&Bar(30.0, 3)).unwrap();
/// assert!((value - 100.0).abs() < 1e-6);
/// ```
#[derive(Clone, Debug)]
pub struct Cci {
    config: CciConfig,
    window: PriceWindow,
    current: Option<f64>,
    length_reciprocal: f64,
}

impl Indicator for Cci {
    type Config = CciConfig;
    type Output = f64;

    fn new(config: Self::Config) -> Self {
        Cci {
            config,
            window: PriceWindow::new(config.length, config.source),
            current: None,
            #[allow(clippy::cast_precision_loss)]
            length_reciprocal: 1.0 / config.length as f64,
        }
    }

    fn compute(&mut self, ohlcv: &impl crate::Ohlcv) -> Option<Self::Output> {
        let price = self.window.add(ohlcv);

        self.current = self.window.sum().map(|sum| {
            let sma = sum * self.length_reciprocal;
            let mean_dev = self
                .window
                .fold(0.0, |acc, price| acc + (price - sma).abs())
                * self.length_reciprocal;

            if mean_dev < f64::EPSILON {
                0.0
            } else {
                (price - sma) / (0.015 * mean_dev)
            }
        });

        self.current
    }

    #[inline]
    fn value(&self) -> Option<Self::Output> {
        self.current
    }
}

impl Display for Cci {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CCI({}, {})", self.config.length, self.config.source)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_util::{Bar, assert_approx, bar, nz};

    fn make_cci(length: usize) -> Cci {
        Cci::new(CciConfig::close(nz(length)))
    }

    mod filling {
        use super::*;

        #[test]
        fn none_until_window_full() {
            let mut cci = make_cci(3);
            assert_eq!(cci.compute(&bar(10.0, 1)), None);
            assert_eq!(cci.compute(&bar(20.0, 2)), None);
        }

        #[test]
        fn returns_value_when_full() {
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            assert!(cci.compute(&bar(30.0, 3)).is_some());
        }
    }

    mod computation {
        use super::*;

        #[test]
        fn basic_cci_value() {
            // prices: 10, 20, 30 → SMA = 20
            // deviations: |10-20|=10, |20-20|=0, |30-20|=10
            // mean_dev = 20/3
            // CCI = (30 - 20) / (0.015 * 20/3) = 10 / 0.1 = 100
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            let value = cci.compute(&bar(30.0, 3)).unwrap();
            assert_approx!(value, 100.0);
        }

        #[test]
        fn negative_cci() {
            // prices: 30, 20, 10 → SMA = 20
            // deviations: |30-20|=10, |20-20|=0, |10-20|=10
            // mean_dev = 20/3
            // CCI = (10 - 20) / (0.015 * 20/3) = -10 / 0.1 = -100
            let mut cci = make_cci(3);
            cci.compute(&bar(30.0, 1));
            cci.compute(&bar(20.0, 2));
            let value = cci.compute(&bar(10.0, 3)).unwrap();
            assert_approx!(value, -100.0);
        }

        #[test]
        fn price_at_mean_gives_zero() {
            // prices: 10, 30, 20 → SMA = 20
            // CCI = (20 - 20) / ... = 0
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(30.0, 2));
            assert_eq!(cci.compute(&bar(20.0, 3)), Some(0.0));
        }

        #[test]
        fn flat_market_gives_zero() {
            let mut cci = make_cci(3);
            for t in 1..=5 {
                cci.compute(&bar(10.0, t));
            }
            assert_eq!(cci.value(), Some(0.0));
        }
    }

    mod sliding {
        use super::*;

        #[test]
        fn drops_oldest_on_advance() {
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            cci.compute(&bar(15.0, 3)); // window [10,20,15], SMA=15
            let v1 = cci.value().unwrap();
            cci.compute(&bar(25.0, 4)); // window [20,15,25], SMA=20
            let v2 = cci.value().unwrap();
            // Different windows produce different CCI values
            assert!((v1 - v2).abs() > 1e-10);
        }

        #[test]
        fn slides_across_many_bars() {
            let mut cci = make_cci(2);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            cci.compute(&bar(30.0, 3));
            cci.compute(&bar(40.0, 4));
            // Window = [40, 50], SMA = 45
            // mean_dev = (|40-45| + |50-45|) / 2 = 5
            // CCI = (50 - 45) / (0.015 * 5) = 5 / 0.075 = 66.666...
            let value = cci.compute(&bar(50.0, 5)).unwrap();
            assert_approx!(value, 5.0 / 0.075);
        }
    }

    mod repaint {
        use super::*;

        #[test]
        fn updates_current_bar() {
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            let v1 = cci.compute(&bar(25.0, 3)).unwrap();
            let v2 = cci.compute(&bar(30.0, 3)).unwrap();
            assert!(v2 > v1, "higher price should give higher CCI");
        }

        #[test]
        fn multiple_repaints_match_single() {
            let mut cci = make_cci(2);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            cci.compute(&bar(25.0, 2)); // repaint
            let final_val = cci.compute(&bar(30.0, 2)).unwrap();

            let mut clean = make_cci(2);
            clean.compute(&bar(10.0, 1));
            let expected = clean.compute(&bar(30.0, 2)).unwrap();

            assert!((final_val - expected).abs() < 1e-10);
        }

        #[test]
        fn repaint_during_filling() {
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(15.0, 1)); // repaint
            assert_eq!(cci.compute(&bar(20.0, 2)), None); // still filling
            assert!(cci.compute(&bar(30.0, 3)).is_some());
        }

        #[test]
        fn advance_after_repaint() {
            let mut cci = make_cci(2);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            cci.compute(&bar(25.0, 2)); // repaint
            let after = cci.compute(&bar(30.0, 3)).unwrap();

            // Clean path with repainted bar
            let mut clean = make_cci(2);
            clean.compute(&bar(25.0, 1));
            let expected = clean.compute(&bar(30.0, 2)).unwrap();

            assert!((after - expected).abs() < 1e-10);
        }
    }

    mod live_data {
        use super::*;

        #[test]
        fn mixed_open_and_closed_bars() {
            let mut cci = make_cci(3);

            // Bar 1: open then close
            assert_eq!(cci.compute(&bar(5.0, 1)), None);
            assert_eq!(cci.compute(&bar(3.0, 1)), None); // repaint

            // Bar 2: open then close
            assert_eq!(cci.compute(&bar(6.0, 2)), None);
            assert_eq!(cci.compute(&bar(8.0, 2)), None); // repaint

            // Bar 3: open
            let v1 = cci.compute(&bar(4.0, 3)).unwrap();

            // Bar 3: close (repaint)
            let v2 = cci.compute(&bar(7.0, 3)).unwrap();
            assert!((v1 - v2).abs() > 1e-10, "repaint should change value");

            // Bar 4
            let v3 = cci.compute(&bar(9.0, 4)).unwrap();
            assert!(v3.is_finite());
        }
    }

    mod clone {
        use super::*;

        #[test]
        fn produces_independent_state() {
            let mut cci = make_cci(3);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            cci.compute(&bar(30.0, 3)); // converged

            let mut cloned = cci.clone();

            // Advance original
            let orig_val = cci.compute(&bar(25.0, 4)).unwrap();

            // Clone still at previous value
            assert_eq!(
                cloned.value(),
                Some(orig_val).map(|_| cloned.value().unwrap())
            );

            // Clone advances independently with different price
            let clone_val = cloned.compute(&bar(50.0, 4)).unwrap();
            assert!(
                (orig_val - clone_val).abs() > 1e-10,
                "divergent inputs should give different CCI"
            );
        }
    }

    mod config {
        use super::*;
        use std::collections::HashSet;

        #[test]
        fn close_helper_uses_close_source() {
            let config = CciConfig::close(nz(20));
            assert_eq!(config.source(), PriceSource::Close);
        }

        #[test]
        fn hlc3_helper_uses_hlc3_source() {
            let config = CciConfig::hlc3(nz(20));
            assert_eq!(config.source(), PriceSource::HLC3);
        }

        #[test]
        fn length_accessor() {
            let config = CciConfig::close(nz(20));
            assert_eq!(config.length(), 20);
        }

        #[test]
        fn convergence_equals_length() {
            let config = CciConfig::close(nz(20));
            assert_eq!(config.convergence(), 20);

            let config = CciConfig::close(nz(200));
            assert_eq!(config.convergence(), 200);
        }

        #[test]
        fn default_source_is_hlc3() {
            let config = CciConfig::builder().length(nz(20)).build();
            assert_eq!(config.source(), PriceSource::HLC3);
        }

        #[test]
        fn custom_source() {
            let config = CciConfig::builder()
                .length(nz(20))
                .source(PriceSource::HL2)
                .build();
            assert_eq!(config.source(), PriceSource::HL2);
        }

        #[test]
        #[should_panic(expected = "length is required")]
        fn panics_without_length() {
            let _ = CciConfig::builder().build();
        }

        #[test]
        fn eq_and_hash() {
            let a = CciConfig::close(nz(20));
            let b = CciConfig::close(nz(20));
            let c = CciConfig::close(nz(10));

            let mut set = HashSet::new();
            set.insert(a);

            assert!(set.contains(&b));
            assert!(!set.contains(&c));
        }

        #[test]
        fn to_builder_roundtrip() {
            let config = CciConfig::hlc3(nz(20));
            assert_eq!(config.to_builder().build(), config);
        }
    }

    mod display {
        use super::*;

        #[test]
        fn formats_correctly() {
            let cci = make_cci(20);
            assert_eq!(cci.to_string(), "CCI(20, Close)");
        }

        #[test]
        fn config_formats_correctly() {
            let config = CciConfig::close(nz(20));
            assert_eq!(config.to_string(), "CciConfig(20, Close)");
        }
    }

    mod value_accessor {
        use super::*;

        #[test]
        fn none_before_convergence() {
            let cci = make_cci(3);
            assert_eq!(cci.value(), None);
        }

        #[test]
        fn returns_current_value() {
            let mut cci = make_cci(2);
            cci.compute(&bar(10.0, 1));
            cci.compute(&bar(20.0, 2));
            assert!(cci.value().is_some());
        }

        #[test]
        fn matches_last_compute() {
            let mut cci = make_cci(2);
            cci.compute(&bar(10.0, 1));
            let computed = cci.compute(&bar(20.0, 2));
            assert_eq!(cci.value(), computed);
        }
    }

    mod price_source {
        use super::*;

        #[test]
        fn uses_configured_source() {
            // HL2 = (high + low) / 2
            let mut cci = Cci::new(
                CciConfig::builder()
                    .length(nz(2))
                    .source(PriceSource::HL2)
                    .build(),
            );
            // HL2 bar 1: (20 + 10) / 2 = 15
            // HL2 bar 2: (30 + 20) / 2 = 25
            // SMA = 20, mean_dev = 5
            // CCI = (25 - 20) / (0.015 * 5)
            let b1 = Bar::new(0.0, 20.0, 10.0, 0.0).at(1);
            let b2 = Bar::new(0.0, 30.0, 20.0, 0.0).at(2);
            cci.compute(&b1);
            let value = cci.compute(&b2).unwrap();
            let expected = 5.0 / (0.015 * 5.0);
            assert_approx!(value, expected);
        }
    }
}