Skip to content

Commit

Permalink
piercing
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Aug 1, 2023
1 parent 7dee50f commit 331f2ce
Show file tree
Hide file tree
Showing 2 changed files with 53 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 @@ -10,6 +10,7 @@ pub mod h;
pub mod hexad;
pub mod marubozu;
pub mod on_neck;
pub mod piercing;
pub mod shrinking;
pub mod slingshot;
pub mod split;
Expand Down
52 changes: 52 additions & 0 deletions ta_lib/patterns/src/piercing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use core::series::Series;

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

(close.gt(&open)
& close.shift(1).lt(&open.shift(1))
& open.lt(&close.shift(1))
& close.lt(&open.shift(1))
& close.gte(&((close.shift(1) + open.shift(1)) / 2.0)))
.into()
}

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

(close.lt(&open)
& close.shift(1).gt(&open.shift(1))
& open.gt(&close.shift(1))
& close.gt(&open.shift(1))
& close.lte(&((close.shift(1) + open.shift(1)) / 2.0)))
.into()
}

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

#[test]
fn test_piercing_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_piercing_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 331f2ce

Please sign in to comment.