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
use charts::{Chart, DataPoint, Source, SourceSeries};

// candlestick chart
// Ref: https://en.wikipedia.org/wiki/Open-high-low-close_chart

pub struct CandleStick {
    candles: Vec<DataPoint>,
}

impl CandleStick {
    pub fn new() -> Self {
        CandleStick { candles: vec![] }
    }
}

impl Chart for CandleStick {
    fn get(&self, index: usize) -> Option<&DataPoint> {
        let len = self.candles.len();

        if index >= len {
            return None;
        }

        let normalized_index = (len - 1) - index;

        self.candles.get(normalized_index)
    }

    fn push(&mut self, data_point: &DataPoint) {
        self.candles.push(data_point.clone());
    }

    fn open(&self) -> SourceSeries {
        SourceSeries::new(Box::new(self), Source::Open)
    }

    fn high(&self) -> SourceSeries {
        SourceSeries::new(Box::new(self), Source::High)
    }

    fn low(&self) -> SourceSeries {
        SourceSeries::new(Box::new(self), Source::Low)
    }

    fn close(&self) -> SourceSeries {
        SourceSeries::new(Box::new(self), Source::Close)
    }

    fn volume(&self) -> SourceSeries {
        SourceSeries::new(Box::new(self), Source::Volume)
    }
}