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
// local imports

use charts::Chart;
use charts::SourceSeries;
use charts::utils::{highest, lowest};

// Ichimoku Kinkō Hyō

pub struct Ichimoku<'chart> {
    chart: &'chart Chart,

    // params
    turning_line_period: usize,
    standard_line_period: usize,
    span_b_period: usize,
    lagging_span_displacement: usize,
}

impl<'chart> Ichimoku<'chart> {
    pub fn new(
        chart: &'chart Chart,

        // params
        turning_line_period: usize,
        standard_line_period: usize,
        span_b_period: usize,
        lagging_span_displacement: usize,
    ) -> Self {
        Ichimoku {
            chart: chart,

            // params
            turning_line_period: turning_line_period,
            standard_line_period: standard_line_period,
            span_b_period: span_b_period,
            lagging_span_displacement: lagging_span_displacement,
        }
    }

    /// Return Ichimoku chart with default settings.
    pub fn default(chart: &'chart Chart) -> Self {
        // reference:
        // http://www.ichimokutrader.com/elements.html
        // http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:ichimoku_cloud

        Ichimoku {
            chart: chart,

            // default params
            turning_line_period: 9,
            standard_line_period: 26,
            span_b_period: 52,
            lagging_span_displacement: 26,
        }
    }

    /// A moving average of the highest high and lowest low over the last
    /// `turning_line_period` data points.
    pub fn turning_line(&self, index: usize) -> Option<f64> {
        // aka Tenkan-sen, or conversion-line

        let lowest_val = match lowest(self.chart.low().offset(index), self.turning_line_period) {
            None => {
                return None;
            }
            Some(lowest_val) => lowest_val,
        };

        let highest_val = match highest(self.chart.high().offset(index), self.turning_line_period) {
            None => {
                return None;
            }
            Some(highest_val) => highest_val,
        };

        // get the average

        let result = (lowest_val + highest_val) / 2.0;

        Some(result)
    }

    /// A moving average of the highest high and lowest low over the last
    /// `standard_line_period` data points.
    pub fn standard_line(&self, index: usize) -> Option<f64> {
        // aka Kijun-sen or base-line

        let lowest_val = match lowest(self.chart.low().offset(index), self.standard_line_period) {
            None => {
                return None;
            }
            Some(lowest_val) => lowest_val,
        };

        let highest_val = match highest(self.chart.high().offset(index), self.standard_line_period)
        {
            None => {
                return None;
            }
            Some(highest_val) => highest_val,
        };

        let result = (lowest_val + highest_val) / 2.0;

        Some(result)
    }

    /// The average of the turning line (i.e Tenkan Sen) and
    /// standard line (i.e. Kijun Sen), plotted `lagging_span_displacement`
    /// data points ahead.
    pub fn span_a(&self, index: i64) -> Option<f64> {
        // Senkou Span A (Leading Span A)

        // Span A plotted at `index` relies on data that is self.lagging_span_displacement
        // data points behind.

        let normalized_index = index + (self.lagging_span_displacement as i64);

        if normalized_index < 0 {
            return None;
        }

        let normalized_index = normalized_index as usize;

        let turning_line_val = match self.turning_line(normalized_index) {
            None => {
                return None;
            }
            Some(x) => x,
        };

        let standard_line_val = match self.standard_line(normalized_index) {
            None => {
                return None;
            }
            Some(x) => x,
        };

        // get the average

        let result = (turning_line_val + standard_line_val) / 2.0;

        Some(result)
    }

    /// The average of the highest high and lowest low over the last `span_b_period`
    /// data points, plotted `lagging_span_displacement` data points ahead.
    pub fn span_b(&self, index: i64) -> Option<f64> {
        // Senkou Span B (Leading Span B)

        // Span B plotted at `index` relies on data that is self.lagging_span_displacement
        // data points behind.

        let normalized_index = index + (self.lagging_span_displacement as i64);

        if normalized_index < 0 {
            return None;
        }

        let normalized_index = normalized_index as usize;

        let lowest_val = match lowest(
            self.chart.low().offset(normalized_index),
            self.span_b_period,
        ) {
            None => {
                return None;
            }
            Some(lowest_val) => lowest_val,
        };

        let highest_val = match highest(
            self.chart.high().offset(normalized_index),
            self.span_b_period,
        ) {
            None => {
                return None;
            }
            Some(highest_val) => highest_val,
        };

        // get the average

        let result = (lowest_val + highest_val) / 2.0;

        Some(result)
    }

    /// The closing price plotted `lagging_span_displacement` data points behind.
    pub fn lagging_line(&self, index: i64) -> Option<f64> {
        // The ladding line plotted at `index` relies on data that is self.lagging_span_displacement
        // data points ahead.

        let normalized_index = index - (self.lagging_span_displacement as i64);

        if normalized_index < 0 {
            return None;
        }

        let normalized_index = normalized_index as usize;

        self.chart.close().get(normalized_index)
    }
}