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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use std::fmt::Display;

use crate::{
    Indicator, IndicatorConfig, IndicatorConfigBuilder, Price, PriceSource, StdDev, Timestamp,
};

/// Anchor period for VWAP session resets.
///
/// Determines when the cumulative sums reset. Fixed anchors
/// (e.g. [`Day`](Self::Day)) reset when `open_time % period == 0`.
/// [`User`](Self::User) disables automatic resets — call
/// [`Vwap::reset()`] manually.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VwapAnchor {
    Hour1,
    Hour2,
    Hour4,
    Hour8,
    Hour12,
    Day,
    User,
}

impl VwapAnchor {
    #[allow(clippy::trivially_copy_pass_by_ref)]
    fn period_us(&self) -> Option<u64> {
        match self {
            Self::Hour1 => Some(3_600_000_000),
            Self::Hour2 => Some(7_200_000_000),
            Self::Hour4 => Some(14_400_000_000),
            Self::Hour8 => Some(28_800_000_000),
            Self::Hour12 => Some(43_200_000_000),
            Self::Day => Some(86_400_000_000),
            Self::User => None,
        }
    }
}

impl Display for VwapAnchor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "VwapAnchor::{}",
            match self {
                Self::Hour1 => "Hour1",
                Self::Hour2 => "Hour2",
                Self::Hour4 => "Hour4",
                Self::Hour8 => "Hour8",
                Self::Hour12 => "Hour12",
                Self::Day => "Day",
                Self::User => "User",
            }
        )
    }
}

/// Configuration for the Volume Weighted Average Price ([`Vwap`])
/// indicator.
///
/// VWAP weights price by volume to produce a running average
/// that resets on a configurable anchor period. Up to three
/// standard-deviation bands can be enabled.
///
/// # Example
///
/// ```
/// use quantedge_ta::{Vwap, VwapConfig, VwapAnchor, Ohlcv, Price, Timestamp};
///
/// struct Bar(f64, 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 volume(&self) -> f64 { self.1 }
///     fn open_time(&self) -> Timestamp { self.2 }
/// }
///
/// let mut vwap = Vwap::new(VwapConfig::builder().anchor(VwapAnchor::User).build());
///
/// let v = vwap.compute(&Bar(10.0, 100.0, 1)).unwrap();
/// assert_eq!(v.vwap(), 10.0);
/// ```
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct VwapConfig {
    band_1: Option<StdDev>,
    band_2: Option<StdDev>,
    band_3: Option<StdDev>,
    source: PriceSource,
    anchor: VwapAnchor,
}

impl VwapConfig {
    /// First standard-deviation band multiplier, if configured.
    #[must_use]
    pub fn band_1(&self) -> Option<StdDev> {
        self.band_1
    }

    /// Second standard-deviation band multiplier, if configured.
    #[must_use]
    pub fn band_2(&self) -> Option<StdDev> {
        self.band_2
    }

    /// Third standard-deviation band multiplier, if configured.
    #[must_use]
    pub fn band_3(&self) -> Option<StdDev> {
        self.band_3
    }

    /// Anchor period for session resets.
    #[must_use]
    pub fn anchor(&self) -> VwapAnchor {
        self.anchor
    }

    fn band_to_str(band: Option<StdDev>) -> String {
        band.map_or("-".to_string(), |v| v.to_string())
    }
}

impl IndicatorConfig for VwapConfig {
    type Builder = VwapConfigBuilder;

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

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

    fn convergence(&self) -> usize {
        1
    }

    fn to_builder(&self) -> Self::Builder {
        VwapConfigBuilder {
            band_1: self.band_1,
            band_2: self.band_2,
            band_3: self.band_3,
            source: self.source,
            anchor: self.anchor,
        }
    }
}

impl Default for VwapConfig {
    fn default() -> Self {
        Self {
            band_1: None,
            band_2: None,
            band_3: None,
            source: PriceSource::HLC3,
            anchor: VwapAnchor::Day,
        }
    }
}

impl Display for VwapConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "VwapConfig(b1: {}, b2: {}, b3: {}, s: {}, a{})",
            VwapConfig::band_to_str(self.band_1),
            VwapConfig::band_to_str(self.band_2),
            VwapConfig::band_to_str(self.band_3),
            self.source,
            self.anchor,
        )
    }
}

/// Builder for [`VwapConfig`].
///
/// Defaults: source = [`PriceSource::HLC3`],
/// anchor = [`VwapAnchor::Day`], no bands.
pub struct VwapConfigBuilder {
    band_1: Option<StdDev>,
    band_2: Option<StdDev>,
    band_3: Option<StdDev>,
    source: PriceSource,
    anchor: VwapAnchor,
}

impl VwapConfigBuilder {
    fn new() -> Self {
        Self {
            band_1: None,
            band_2: None,
            band_3: None,
            source: PriceSource::HLC3,
            anchor: VwapAnchor::Day,
        }
    }

    /// Sets the first standard-deviation band multiplier.
    #[must_use]
    pub fn band_1(mut self, value: Option<StdDev>) -> Self {
        self.band_1 = value;
        self
    }

    /// Sets the second standard-deviation band multiplier.
    #[must_use]
    pub fn band_2(mut self, value: Option<StdDev>) -> Self {
        self.band_2 = value;
        self
    }

    /// Sets the third standard-deviation band multiplier.
    #[must_use]
    pub fn band_3(mut self, value: Option<StdDev>) -> Self {
        self.band_3 = value;
        self
    }

    /// Sets the anchor period for session resets.
    #[must_use]
    pub fn anchor(mut self, value: VwapAnchor) -> Self {
        self.anchor = value;
        self
    }
}

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

    fn build(self) -> VwapConfig {
        VwapConfig {
            band_1: self.band_1,
            band_2: self.band_2,
            band_3: self.band_3,
            source: self.source,
            anchor: self.anchor,
        }
    }
}

/// A single standard-deviation band around the VWAP line.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VwapBand {
    upper: Price,
    lower: Price,
}

impl VwapBand {
    /// Upper band: `vwap + multiplier × std_dev`.
    #[inline]
    #[must_use]
    pub fn upper(&self) -> Price {
        self.upper
    }

    /// Lower band: `vwap − multiplier × std_dev`.
    #[inline]
    #[must_use]
    pub fn lower(&self) -> Price {
        self.lower
    }
}

impl Display for VwapBand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "VwapBand(u: {}, l: {})", self.upper, self.lower)
    }
}

/// Output of [`Vwap::compute`]: the VWAP line and optional bands.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VwapValue {
    vwap: Price,
    band_1: Option<VwapBand>,
    band_2: Option<VwapBand>,
    band_3: Option<VwapBand>,
}

impl VwapValue {
    /// The volume-weighted average price.
    #[inline]
    #[must_use]
    pub fn vwap(&self) -> Price {
        self.vwap
    }

    /// First standard-deviation band, if configured.
    #[inline]
    #[must_use]
    pub fn band_1(&self) -> Option<VwapBand> {
        self.band_1
    }

    /// Second standard-deviation band, if configured.
    #[inline]
    #[must_use]
    pub fn band_2(&self) -> Option<VwapBand> {
        self.band_2
    }

    /// Third standard-deviation band, if configured.
    #[inline]
    #[must_use]
    pub fn band_3(&self) -> Option<VwapBand> {
        self.band_3
    }
}

impl Display for VwapValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "VwapValue(vwap: {}, b1: {}, b2: {}, b3: {})",
            self.vwap,
            VwapValue::band_to_str(self.band_1),
            VwapValue::band_to_str(self.band_2),
            VwapValue::band_to_str(self.band_3),
        )
    }
}

impl VwapValue {
    fn band_to_str(band: Option<VwapBand>) -> String {
        band.map_or("-".to_string(), |v| v.to_string())
    }
}

/// Volume Weighted Average Price (VWAP).
///
/// Computes the cumulative average price weighted by volume,
/// resetting on a configurable anchor period. Optionally
/// produces up to three standard-deviation bands.
///
/// ```text
/// VWAP = Σ(price × volume) / Σ(volume)
/// ```
///
/// Supports live repainting: feeding a bar with the same
/// `open_time` recomputes from the previous state without
/// advancing.
///
/// # Example
///
/// ```
/// use quantedge_ta::{Vwap, VwapConfig, VwapAnchor, Ohlcv, Price, Timestamp};
///
/// struct Bar(f64, 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 volume(&self) -> f64 { self.1 }
///     fn open_time(&self) -> Timestamp { self.2 }
/// }
///
/// let config = VwapConfig::builder()
///     .anchor(VwapAnchor::User)
///     .build();
/// let mut vwap = Vwap::new(config);
///
/// let v1 = vwap.compute(&Bar(10.0, 100.0, 1)).unwrap();
/// assert_eq!(v1.vwap(), 10.0);
///
/// // (10×100 + 20×200) / (100+200) = 5000/300 ≈ 16.667
/// let v2 = vwap.compute(&Bar(20.0, 200.0, 2)).unwrap();
/// assert!((v2.vwap() - 16.666_666_666_666_668).abs() < 1e-10);
/// ```
#[derive(Clone, Debug)]
pub struct Vwap {
    config: VwapConfig,
    cumulative_tpv: f64,
    cumulative_tpv2: f64,
    cumulative_volume: f64,
    pending_price: Price,
    pending_volume: f64,
    current: Option<VwapValue>,
    last_open_time: Option<Timestamp>,
    prev_close: Option<Price>,
    cur_close: Price,
    has_bands: bool,
    anchor: Option<u64>,
}

impl Indicator for Vwap {
    type Config = VwapConfig;
    type Output = VwapValue;

    fn new(config: Self::Config) -> Self {
        Self {
            config,
            cumulative_tpv: 0.0,
            cumulative_tpv2: 0.0,
            cumulative_volume: 0.0,
            pending_price: 0.0,
            pending_volume: 0.0,
            current: None,
            last_open_time: None,
            prev_close: None,
            cur_close: -1.0,
            has_bands: config.band_1.is_some()
                || config.band_2.is_some()
                || config.band_3.is_some(),
            anchor: config.anchor.period_us(),
        }
    }

    fn compute(&mut self, ohlcv: &impl crate::Ohlcv) -> Option<Self::Output> {
        let volume = ohlcv.volume();
        let is_next_bar = self.last_open_time.is_none_or(|t| t < ohlcv.open_time());

        if is_next_bar {
            self.prev_close = Some(self.cur_close);
            self.last_open_time = Some(ohlcv.open_time());

            if self
                .anchor
                .is_some_and(|v| ohlcv.open_time().is_multiple_of(v))
            {
                self.do_reset();
            } else {
                self.cumulative_tpv += self.pending_price * self.pending_volume;
                self.cumulative_tpv2 += self.pending_price.powi(2) * self.pending_volume;
                self.cumulative_volume += self.pending_volume;
            }
        }

        self.cur_close = ohlcv.close();
        let price = self.config.source.extract(ohlcv, self.prev_close);

        self.pending_price = price;
        self.pending_volume = volume;

        let effective_volume = self.cumulative_volume + volume;

        self.current = if effective_volume < f64::EPSILON {
            None
        } else {
            let tpv = self.cumulative_tpv + price * volume;
            let vwap = tpv / effective_volume;

            if self.has_bands {
                let tpv2 = self.cumulative_tpv2 + price.powi(2) * volume;
                let variance = (tpv2 / effective_volume) - vwap.powi(2);
                let std_dev = variance.max(0.0).sqrt();

                Some(VwapValue {
                    vwap,
                    band_1: Self::compute_band(self.config.band_1, vwap, std_dev),
                    band_2: Self::compute_band(self.config.band_2, vwap, std_dev),
                    band_3: Self::compute_band(self.config.band_3, vwap, std_dev),
                })
            } else {
                Some(VwapValue {
                    vwap,
                    band_1: None,
                    band_2: None,
                    band_3: None,
                })
            }
        };

        self.current
    }

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

impl Vwap {
    /// Manually resets cumulative sums.
    ///
    /// # Panics
    ///
    /// Panics if the anchor is not [`VwapAnchor::User`].
    pub fn reset(&mut self) {
        debug_assert!(
            self.config.anchor == VwapAnchor::User,
            "Only user-controlled VWAP can be reset. Set anchor to VwapAnchor::User"
        );
        self.do_reset();
    }

    fn do_reset(&mut self) {
        self.cumulative_tpv = 0.0;
        self.cumulative_tpv2 = 0.0;
        self.cumulative_volume = 0.0;
        self.pending_price = 0.0;
        self.pending_volume = 0.0;
    }
}

impl Vwap {
    fn compute_band(band: Option<StdDev>, vwap: Price, std_dev: f64) -> Option<VwapBand> {
        band.map(|b| {
            let offset = b.value() * std_dev;

            VwapBand {
                upper: vwap + offset,
                lower: vwap - offset,
            }
        })
    }
}

impl Display for Vwap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Vwap(b1: {}, b2: {}, b3: {}, s: {})",
            VwapConfig::band_to_str(self.config.band_1),
            VwapConfig::band_to_str(self.config.band_2),
            VwapConfig::band_to_str(self.config.band_3),
            self.config.source,
        )
    }
}

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

    fn vbar(close: f64, volume: f64, time: u64) -> crate::test_util::Bar {
        bar(close, time).vol(volume)
    }

    fn user_vwap() -> Vwap {
        Vwap::new(VwapConfig::builder().anchor(VwapAnchor::User).build())
    }

    fn user_vwap_with_bands() -> Vwap {
        Vwap::new(
            VwapConfig::builder()
                .anchor(VwapAnchor::User)
                .band_1(Some(StdDev::new(1.0)))
                .band_2(Some(StdDev::new(2.0)))
                .build(),
        )
    }

    mod convergence {
        use super::*;

        #[test]
        fn returns_value_on_first_bar() {
            let mut vwap = user_vwap();
            assert!(vwap.compute(&vbar(10.0, 100.0, 1)).is_some());
        }

        #[test]
        fn convergence_is_one() {
            assert_eq!(VwapConfig::default().convergence(), 1);
        }

        #[test]
        fn none_with_zero_volume() {
            let mut vwap = user_vwap();
            assert_eq!(vwap.compute(&vbar(10.0, 0.0, 1)), None);
        }
    }

    mod computation {
        use super::*;

        #[test]
        fn single_bar_equals_price() {
            let mut vwap = user_vwap();
            let v = vwap.compute(&vbar(10.0, 100.0, 1)).unwrap();
            assert_eq!(v.vwap(), 10.0);
        }

        #[test]
        fn two_bars_weighted_average() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            let v = vwap.compute(&vbar(20.0, 200.0, 2)).unwrap();
            // (10*100 + 20*200) / (100+200) = 5000/300
            assert_approx!(v.vwap(), 5000.0 / 300.0);
        }

        #[test]
        fn three_bars_cumulative() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            vwap.compute(&vbar(20.0, 200.0, 2));
            let v = vwap.compute(&vbar(30.0, 300.0, 3)).unwrap();
            // (10*100 + 20*200 + 30*300) / (100+200+300) = 14000/600
            assert_approx!(v.vwap(), 14000.0 / 600.0);
        }

        #[test]
        fn equal_volume_gives_simple_average() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            let v = vwap.compute(&vbar(20.0, 100.0, 2)).unwrap();
            assert_approx!(v.vwap(), 15.0);
        }

        #[test]
        fn no_bands_when_not_configured() {
            let mut vwap = user_vwap();
            let v = vwap.compute(&vbar(10.0, 100.0, 1)).unwrap();
            assert!(v.band_1().is_none());
            assert!(v.band_2().is_none());
            assert!(v.band_3().is_none());
        }
    }

    mod bands {
        use super::*;

        #[test]
        fn bands_present_when_configured() {
            let mut vwap = user_vwap_with_bands();
            vwap.compute(&vbar(10.0, 100.0, 1));
            let v = vwap.compute(&vbar(20.0, 200.0, 2)).unwrap();
            assert!(v.band_1().is_some());
            assert!(v.band_2().is_some());
            assert!(v.band_3().is_none());
        }

        #[test]
        fn bands_symmetric_around_vwap() {
            let mut vwap = user_vwap_with_bands();
            vwap.compute(&vbar(10.0, 100.0, 1));
            let v = vwap.compute(&vbar(20.0, 200.0, 2)).unwrap();
            let b1 = v.band_1().unwrap();
            let midpoint = b1.upper().midpoint(b1.lower());
            assert_approx!(midpoint, v.vwap());
        }

        #[test]
        fn band_2_wider_than_band_1() {
            let mut vwap = user_vwap_with_bands();
            vwap.compute(&vbar(10.0, 100.0, 1));
            let v = vwap.compute(&vbar(20.0, 200.0, 2)).unwrap();
            let b1 = v.band_1().unwrap();
            let b2 = v.band_2().unwrap();
            assert!(b2.upper() > b1.upper());
            assert!(b2.lower() < b1.lower());
        }

        #[test]
        fn single_price_zero_std_dev() {
            let mut vwap = user_vwap_with_bands();
            let v = vwap.compute(&vbar(10.0, 100.0, 1)).unwrap();
            let b1 = v.band_1().unwrap();
            // All same price → std_dev = 0 → bands collapse to vwap
            assert_eq!(b1.upper(), v.vwap());
            assert_eq!(b1.lower(), v.vwap());
        }
    }

    mod anchor_reset {
        use super::*;

        #[test]
        fn user_reset_clears_state() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            vwap.compute(&vbar(20.0, 200.0, 2));
            vwap.reset();
            let v = vwap.compute(&vbar(30.0, 300.0, 3)).unwrap();
            assert_eq!(v.vwap(), 30.0);
        }

        #[test]
        #[should_panic(expected = "Only user-controlled VWAP can be reset")]
        fn reset_panics_for_non_user_anchor() {
            let mut vwap = Vwap::new(VwapConfig::default());
            vwap.reset();
        }

        #[test]
        fn day_anchor_resets_on_boundary() {
            let day_us: u64 = 86_400_000_000;
            let mut vwap = Vwap::new(VwapConfig::default()); // Day anchor

            // Bar in first day
            vwap.compute(&vbar(10.0, 100.0, day_us));
            vwap.compute(&vbar(20.0, 200.0, day_us + 1000));

            // Bar at next day boundary → reset
            let v = vwap.compute(&vbar(50.0, 500.0, day_us * 2)).unwrap();
            assert_eq!(v.vwap(), 50.0);
        }
    }

    mod repaint {
        use super::*;

        #[test]
        fn updates_current_bar() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            vwap.compute(&vbar(20.0, 200.0, 2));
            // Repaint bar 2 with different price
            let v = vwap.compute(&vbar(30.0, 200.0, 2)).unwrap();
            // (10*100 + 30*200) / 300 = 7000/300
            assert_approx!(v.vwap(), 7000.0 / 300.0);
        }

        #[test]
        fn multiple_repaints() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            vwap.compute(&vbar(20.0, 200.0, 2));
            vwap.compute(&vbar(25.0, 200.0, 2)); // repaint
            let v = vwap.compute(&vbar(30.0, 200.0, 2)).unwrap();
            assert_approx!(v.vwap(), 7000.0 / 300.0);
        }

        #[test]
        fn repaint_then_advance() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            vwap.compute(&vbar(20.0, 200.0, 2));
            vwap.compute(&vbar(30.0, 200.0, 2)); // repaint
            // Advance: cumulative includes repainted bar 2
            let v = vwap.compute(&vbar(40.0, 100.0, 3)).unwrap();
            // (10*100 + 30*200 + 40*100) / (100+200+100) = 11000/400
            assert_approx!(v.vwap(), 11000.0 / 400.0);
        }

        #[test]
        fn repaint_first_bar() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            let v = vwap.compute(&vbar(20.0, 200.0, 1)).unwrap();
            assert_eq!(v.vwap(), 20.0);
        }
    }

    mod live_data {
        use super::*;

        #[test]
        fn mixed_open_and_closed_bars() {
            let mut vwap = user_vwap();

            // Bar 1: open
            let v = vwap.compute(&vbar(10.0, 100.0, 1)).unwrap();
            assert_eq!(v.vwap(), 10.0);
            // Bar 1: close (repaint)
            let v = vwap.compute(&vbar(12.0, 150.0, 1)).unwrap();
            assert_eq!(v.vwap(), 12.0);

            // Bar 2: open
            let v = vwap.compute(&vbar(15.0, 200.0, 2)).unwrap();
            // (12*150 + 15*200) / (150+200) = 4800/350
            assert_approx!(v.vwap(), 4800.0 / 350.0);

            // Bar 2: close (repaint)
            let v = vwap.compute(&vbar(14.0, 200.0, 2)).unwrap();
            // (12*150 + 14*200) / (150+200) = 4600/350
            assert_approx!(v.vwap(), 4600.0 / 350.0);

            // Bar 3
            let v = vwap.compute(&vbar(20.0, 300.0, 3)).unwrap();
            // (12*150 + 14*200 + 20*300) / (150+200+300) = 10600/650
            assert_approx!(v.vwap(), 10600.0 / 650.0);
        }
    }

    mod clone {
        use super::*;

        #[test]
        fn produces_independent_state() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            vwap.compute(&vbar(20.0, 200.0, 2));

            let mut cloned = vwap.clone();

            let v_orig = vwap.compute(&vbar(30.0, 300.0, 3)).unwrap();
            let v_clone = cloned.compute(&vbar(50.0, 500.0, 3)).unwrap();

            assert!((v_orig.vwap() - v_clone.vwap()).abs() > 1e-10);
        }
    }

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

        #[test]
        fn default_values() {
            let config = VwapConfig::default();
            assert_eq!(config.source(), PriceSource::HLC3);
            assert_eq!(config.anchor(), VwapAnchor::Day);
            assert!(config.band_1().is_none());
            assert!(config.band_2().is_none());
            assert!(config.band_3().is_none());
        }

        #[test]
        fn eq_and_hash() {
            let a = VwapConfig::default();
            let b = VwapConfig::default();
            let c = VwapConfig::builder().anchor(VwapAnchor::User).build();

            let mut set = HashSet::new();
            set.insert(a);
            assert!(set.contains(&b));
            assert!(!set.contains(&c));
        }

        #[test]
        fn to_builder_roundtrip() {
            let config = VwapConfig::builder()
                .band_1(Some(StdDev::new(1.0)))
                .anchor(VwapAnchor::Hour4)
                .build();
            assert_eq!(config.to_builder().build(), config);
        }

        #[test]
        fn display_config() {
            let config = VwapConfig::default();
            let s = config.to_string();
            assert!(s.contains("VwapConfig"));
        }
    }

    mod display {
        use super::*;

        #[test]
        fn formats_correctly() {
            let vwap = user_vwap();
            let s = vwap.to_string();
            assert!(s.contains("Vwap"));
        }

        #[test]
        fn anchor_display() {
            assert_eq!(VwapAnchor::Day.to_string(), "VwapAnchor::Day");
            assert_eq!(VwapAnchor::User.to_string(), "VwapAnchor::User");
        }

        #[test]
        fn band_display() {
            let band = VwapBand {
                upper: 10.0,
                lower: 5.0,
            };
            assert!(band.to_string().contains("10"));
        }

        #[test]
        fn value_display() {
            let val = VwapValue {
                vwap: 10.0,
                band_1: None,
                band_2: None,
                band_3: None,
            };
            assert!(val.to_string().contains("10"));
        }
    }

    mod value_accessor {
        use super::*;

        #[test]
        fn none_before_any_bar() {
            let vwap = user_vwap();
            assert_eq!(vwap.value(), None);
        }

        #[test]
        fn returns_current_value() {
            let mut vwap = user_vwap();
            vwap.compute(&vbar(10.0, 100.0, 1));
            assert!(vwap.value().is_some());
        }

        #[test]
        fn matches_last_compute() {
            let mut vwap = user_vwap();
            let computed = vwap.compute(&vbar(10.0, 100.0, 1));
            assert_eq!(vwap.value(), computed);
        }
    }
}