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
use crate::{CandleComponent, Trade};

/// This 'CandleComponent' keeps track of the low price
#[derive(Debug, Clone)]
pub struct Low {
    low: f64,
}

impl Default for Low {
    fn default() -> Self {
        // ensure the initial value is maximal,
        // so any subsequent calls to 'update' will set the proper low value
        Self { low: f64::MAX }
    }
}

impl CandleComponent for Low {
    #[inline(always)]
    fn value(&self) -> f64 {
        self.low
    }

    #[inline(always)]
    fn update(&mut self, trade: &Trade) {
        if trade.price < self.low {
            self.low = trade.price;
        }
    }

    #[inline(always)]
    fn reset(&mut self) {
        self.low = f64::MAX;
    }
}

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

    #[test]
    fn low() {
        let mut m = Low::default();
        for t in &crate::candle_components::tests::TRADES {
            m.update(t);
        }
        assert_eq!(m.value(), 100.0);
    }
}