Skip to content

Commit

Permalink
master_candle
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 8, 2023
1 parent 7d2a99c commit 583a9b4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions ta_lib/patterns/candlestick/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod harami_strict;
pub mod hexad;
pub mod kangaroo_tail;
pub mod marubozu;
pub mod master_candle;
pub mod on_neck;
pub mod piercing;
pub mod shrinking;
Expand Down
72 changes: 72 additions & 0 deletions ta_lib/patterns/candlestick/src/master_candle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use core::series::Series;

pub fn bullish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Series<bool> {
let open = Series::from(open);
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

close.gt(&high.shift(6))
& close.gt(&open)
& high.shift(1).lt(&high.shift(6))
& low.shift(1).gt(&low.shift(6))
& high.shift(2).lt(&high.shift(6))
& low.shift(2).gt(&low.shift(6))
& high.shift(3).lt(&high.shift(6))
& low.shift(3).gt(&low.shift(6))
& high.shift(4).lt(&high.shift(6))
& low.shift(4).gt(&low.shift(6))
& high.shift(5).lt(&high.shift(6))
& low.shift(5).gt(&low.shift(6))
}

pub fn bearish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Series<bool> {
let open = Series::from(open);
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

close.lt(&low.shift(6))
& close.lt(&open)
& high.shift(1).lt(&high.shift(6))
& low.shift(1).gt(&low.shift(6))
& high.shift(2).lt(&high.shift(6))
& low.shift(2).gt(&low.shift(6))
& high.shift(3).lt(&high.shift(6))
& low.shift(3).gt(&low.shift(6))
& high.shift(4).lt(&high.shift(6))
& low.shift(4).gt(&low.shift(6))
& high.shift(5).lt(&high.shift(6))
& low.shift(5).gt(&low.shift(6))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_master_candle_bullish() {
let open = vec![4.0, 3.0, 4.0, 3.0, 4.0, 6.0];
let high = vec![4.5, 3.5, 4.5, 3.5, 4.5, 5.0];
let low = vec![4.0, 3.0, 4.0, 3.0, 4.0, 3.6];
let close = vec![4.5, 3.5, 4.5, 3.5, 4.5, 4.5];
let expected = vec![false, false, false, false, false, false];

let result: Vec<bool> = bullish(&open, &high, &low, &close).into();

assert_eq!(result, expected);
}

#[test]
fn test_master_candle_bearish() {
let open = vec![4.0, 3.0, 4.0, 3.0, 4.0, 6.0];
let high = vec![4.0, 3.0, 4.0, 3.0, 4.0, 5.0];
let low = vec![3.5, 2.5, 3.5, 2.5, 3.5, 3.4];
let close = vec![3.5, 2.5, 3.5, 2.5, 3.5, 5.6];
let expected = vec![false, false, false, false, false, false];

let result: Vec<bool> = bearish(&open, &high, &low, &close).into();

assert_eq!(result, expected);
}
}

0 comments on commit 583a9b4

Please sign in to comment.