Skip to content

Commit

Permalink
three_candles
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 1, 2023
1 parent d199b0a commit 7dee50f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions ta_lib/patterns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pub mod shrinking;
pub mod slingshot;
pub mod split;
pub mod tasuki;
pub mod three_candles;
58 changes: 58 additions & 0 deletions ta_lib/patterns/src/three_candles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use core::series::Series;

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

let body = (open - &close).abs();

(close.gt(&close.shift(1))
& close.shift(1).gt(&close.shift(2))
& close.shift(2).gt(&close.shift(3))
& body.gte(&body.highest(5))
& body.shift(1).gte(&body.shift(1).highest(5))
& body.shift(2).gte(&body.shift(2).highest(5)))
.into()
}

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

let body = (open - &close).abs();

(close.lt(&close.shift(1))
& close.shift(1).lt(&close.shift(2))
& close.shift(2).lt(&close.shift(3))
& body.gte(&body.highest(5))
& body.shift(1).gte(&body.shift(1).highest(5))
& body.shift(2).gte(&body.shift(2).highest(5)))
.into()
}

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

#[test]
fn test_three_candles_bullish() {
let open = vec![4.0, 4.0, 4.0, 4.0, 4.0];
let close = vec![2.0, 2.5, 2.0, 1.5, 2.0];
let expected = vec![false, false, false, false, false];

let result = bullish(&open, &close);

assert_eq!(result, expected);
}

#[test]
fn test_three_candles_bearish() {
let open = vec![4.0, 4.0, 4.0, 4.0, 4.0];
let close = vec![2.0, 2.5, 2.0, 1.5, 2.0];
let expected = vec![false, false, false, false, false];

let result = bearish(&open, &close);

assert_eq!(result, expected);
}
}

0 comments on commit 7dee50f

Please sign in to comment.