diff --git a/ta_lib/patterns/src/lib.rs b/ta_lib/patterns/src/lib.rs index 9ce8192e..e82b9114 100644 --- a/ta_lib/patterns/src/lib.rs +++ b/ta_lib/patterns/src/lib.rs @@ -6,4 +6,5 @@ pub mod counterattack; pub mod doji; pub mod double_doji; pub mod marubozu; +pub mod shrinking; pub mod tasuki; diff --git a/ta_lib/patterns/src/shrinking.rs b/ta_lib/patterns/src/shrinking.rs new file mode 100644 index 00000000..2046c9f0 --- /dev/null +++ b/ta_lib/patterns/src/shrinking.rs @@ -0,0 +1,68 @@ +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); + + (close.gt(&open) + & close.gt(&high.shift(1)) + & close.gt(&high.shift(2)) + & close.gt(&high.shift(3)) + & high.shift(1).lt(&high.shift(2)) + & high.shift(2).lt(&high.shift(3)) + & low.shift(1).eq(&low.shift(2)) + & low.shift(2).eq(&low.shift(3)) + & close.shift(4).lt(&open.shift(4))) + .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); + + (close.lt(&open) + & close.lt(&low.shift(1)) + & close.lt(&low.shift(2)) + & close.lt(&low.shift(3)) + & low.shift(1).lt(&low.shift(2)) + & low.shift(2).lt(&low.shift(3)) + & high.shift(1).eq(&high.shift(2)) + & high.shift(2).eq(&high.shift(3)) + & close.shift(4).gt(&open.shift(4))) + .into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_shrinking_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_shrinking_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); + } +}