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
/**
 ** Channel Breakout
 *
 *  Support and resistance lines are drawn at the high and low of the historic price
 *  .. and a recent high above or low below is a channel breakout
 */
use super::Trend;

pub fn has_channel_breakout(prices: &[f32], window: usize, target_trend: &Trend) -> bool {
    if prices.len() < window {
        panic!("Window is Larger Than Prices Length")
    }

    let mut high_line_price = 0.0;
    let mut low_line_price = 0.0;

    let past_prices = &prices[..prices.len() - window];
    for &price in past_prices {
        if price > high_line_price {
            high_line_price = price
        }
        if price < low_line_price {
            low_line_price = price
        }
    }

    let mut window_high_price = 0.0;
    let mut window_low_price = 0.0;

    let recent_prices = &prices[prices.len() - window..];
    for &price in recent_prices {
        if price > window_high_price {
            window_high_price = price
        }
        if price < window_low_price {
            window_low_price = price
        }
    }
    match target_trend {
        Trend::Bullish => {
            if window_high_price > high_line_price {
                return true;
            } else {
                return false;
            }
        }
        Trend::Bearish => {
            if window_low_price < low_line_price {
                return true;
            } else {
                return false;
            }
        }
    }
}