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
#[derive(Clone)]
pub enum Source {
    Open,
    High,
    Low,
    Close,
    Volume,
}

impl AsRef<Source> for Source {
    fn as_ref(&self) -> &Source {
        self
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct DataPoint {
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub volume: f64,
}

impl DataPoint {
    pub fn get<T: AsRef<Source>>(&self, source_type: T) -> f64 {
        match *source_type.as_ref() {
            Source::Open => self.open,
            Source::High => self.high,
            Source::Low => self.low,
            Source::Close => self.close,
            Source::Volume => self.volume,
        }
    }
}

#[derive(Clone)]
pub struct SourceSeries<'chart> {
    chart: &'chart Chart,
    series_type: Source,
    offset: usize,
}

impl<'chart> SourceSeries<'chart> {
    pub fn new(chart: &'chart Chart, series_type: Source) -> Self {
        SourceSeries {
            chart: chart,
            series_type: series_type,
            offset: 0,
        }
    }

    pub fn offset(&self, index: usize) -> Self {
        SourceSeries {
            chart: self.chart.clone(),
            series_type: self.series_type.clone(),
            offset: index,
        }
    }

    /// Get the value of this `SourceSeries` at `index`.
    pub fn get(&self, index: usize) -> Option<f64> {
        match self.chart.get(index) {
            None => None,
            Some(data_point) => Some(data_point.get(&self.series_type)),
        }
    }

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

pub trait Chart {
    fn as_chart(&self) -> &Chart;

    /// Get value of this `Chart` at `index`.
    fn get(&self, index: usize) -> Option<&DataPoint>;

    /// Get number of data points in this chart.
    fn len(&self) -> usize;

    /// Add newest data point to the chart.
    fn push(&mut self, data_point: &DataPoint);

    /// Remove oldest data point from the chart.
    fn pop_front(&mut self);

    fn open<'chart>(&'chart self) -> SourceSeries {
        let chart: &'chart Chart = self.as_chart();
        SourceSeries::new(chart, Source::Open)
    }

    fn high<'chart>(&'chart self) -> SourceSeries {
        let chart: &'chart Chart = self.as_chart();
        SourceSeries::new(chart, Source::High)
    }

    fn low<'chart>(&'chart self) -> SourceSeries {
        let chart: &'chart Chart = self.as_chart();
        SourceSeries::new(chart, Source::Low)
    }

    fn close<'chart>(&'chart self) -> SourceSeries {
        let chart: &'chart Chart = self.as_chart();
        SourceSeries::new(chart, Source::Close)
    }

    fn volume<'chart>(&'chart self) -> SourceSeries {
        let chart: &'chart Chart = self.as_chart();
        SourceSeries::new(chart, Source::Volume)
    }
}

pub mod utils;

// types of charts

pub mod candlestick;