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};
use std::collections::VecDeque;

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

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

impl CandleStick {
    pub fn new() -> Self {
        CandleStick {
            candles: VecDeque::new(),
        }
    }
}

impl Chart for CandleStick {
    fn as_chart(&self) -> &Chart {
        self
    }

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

        if len <= 0 {
            return None;
        }

        if index >= len {
            return None;
        }

        let len_index = len - 1;
        let normalized_index = len_index - index;

        self.candles.get(normalized_index)
    }

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

    fn pop_front(&mut self) {
        self.candles.pop_front();
    }

    fn len(&self) -> usize {
        self.candles.len()
    }
}