From d199b0a47a77e3c1c20258aa7cb2261e61101a8b Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Tue, 1 Aug 2023 17:16:30 +0300 Subject: [PATCH] engulfing --- ta_lib/patterns/src/engulfing.rs | 64 ++++++++++++++++++++++++++++++++ ta_lib/patterns/src/lib.rs | 1 + 2 files changed, 65 insertions(+) create mode 100644 ta_lib/patterns/src/engulfing.rs diff --git a/ta_lib/patterns/src/engulfing.rs b/ta_lib/patterns/src/engulfing.rs new file mode 100644 index 00000000..28834a64 --- /dev/null +++ b/ta_lib/patterns/src/engulfing.rs @@ -0,0 +1,64 @@ +use core::series::Series; + +pub fn bullish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec { + let open = Series::from(open); + let high = Series::from(high); + let low = Series::from(low); + let close = Series::from(close); + + let body = (&close - &open).abs(); + + (close.gt(&open) + & close.shift(1).lt(&open.shift(1)) + & high.gt(&high.shift(1)) + & low.lt(&low.shift(1)) + & body.gte(&(2.0 * body.shift(1)))) + .into() +} + +pub fn bearish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec { + let open = Series::from(open); + let high = Series::from(high); + let low = Series::from(low); + let close = Series::from(close); + + let body = (&close - &open).abs(); + + (close.lt(&open) + & close.shift(1).gt(&open.shift(1)) + & high.gt(&high.shift(1)) + & low.lt(&low.shift(1)) + & body.gte(&(2.0 * body.shift(1)))) + .into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_engulfing_bullish() { + let open = vec![4.0, 3.0, 4.0, 3.0, 4.0]; + let high = vec![4.5, 3.5, 4.5, 3.5, 4.5]; + let low = vec![4.0, 3.0, 4.0, 3.0, 4.0]; + let close = vec![4.5, 3.5, 4.5, 3.5, 4.5]; + let expected = vec![false, false, false, false, false]; + + let result = bullish(&open, &high, &low, &close); + + assert_eq!(result, expected); + } + + #[test] + fn test_engulfing_bearish() { + let open = vec![4.0, 3.0, 4.0, 3.0, 4.0]; + let high = vec![4.0, 3.0, 4.0, 3.0, 4.0]; + let low = vec![3.5, 2.5, 3.5, 2.5, 3.5]; + let close = vec![3.5, 2.5, 3.5, 2.5, 3.5]; + let expected = vec![false, false, false, false, false]; + + let result = bearish(&open, &high, &low, &close); + + assert_eq!(result, expected); + } +} diff --git a/ta_lib/patterns/src/lib.rs b/ta_lib/patterns/src/lib.rs index 5cc852d0..d54aa706 100644 --- a/ta_lib/patterns/src/lib.rs +++ b/ta_lib/patterns/src/lib.rs @@ -5,6 +5,7 @@ pub mod breakaway; pub mod counterattack; pub mod doji; pub mod double_doji; +pub mod engulfing; pub mod h; pub mod hexad; pub mod marubozu;