From c31de0938f33c89ef45c7510b7a2c8781a45a4a7 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Tue, 1 Aug 2023 19:38:32 +0300 Subject: [PATCH] harami --- ta_lib/patterns/src/harami_flexible.rs | 64 +++++++++++++++++++ .../src/{harami.rs => harami_strict.rs} | 4 +- ta_lib/patterns/src/lib.rs | 3 +- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 ta_lib/patterns/src/harami_flexible.rs rename ta_lib/patterns/src/{harami.rs => harami_strict.rs} (95%) diff --git a/ta_lib/patterns/src/harami_flexible.rs b/ta_lib/patterns/src/harami_flexible.rs new file mode 100644 index 00000000..8c41cf68 --- /dev/null +++ b/ta_lib/patterns/src/harami_flexible.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); + + (close.lt(&open.shift(1)) + & open.gte(&close.shift(1)) + & high.lt(&high.shift(1)) + & low.gt(&low.shift(1)) + & close.gt(&open) + & close.shift(1).lt(&open.shift(1)) + & close.shift(2).lt(&open.shift(2))) + .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.gt(&open.shift(1)) + & open.lte(&close.shift(1)) + & high.lt(&high.shift(1)) + & low.gt(&low.shift(1)) + & close.lt(&open) + & close.shift(1).gt(&open.shift(1)) + & close.shift(2).gt(&open.shift(2))) + .into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_harami_flexible_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_harami_flexible_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/harami.rs b/ta_lib/patterns/src/harami_strict.rs similarity index 95% rename from ta_lib/patterns/src/harami.rs rename to ta_lib/patterns/src/harami_strict.rs index 64d2e09b..9c240cf6 100644 --- a/ta_lib/patterns/src/harami.rs +++ b/ta_lib/patterns/src/harami_strict.rs @@ -33,7 +33,7 @@ mod tests { use super::*; #[test] - fn test_harami_bullish() { + fn test_harami_strict_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]; @@ -46,7 +46,7 @@ mod tests { } #[test] - fn test_harami_bearish() { + fn test_harami_strict_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]; diff --git a/ta_lib/patterns/src/lib.rs b/ta_lib/patterns/src/lib.rs index ead4e210..1f261628 100644 --- a/ta_lib/patterns/src/lib.rs +++ b/ta_lib/patterns/src/lib.rs @@ -7,7 +7,8 @@ pub mod doji; pub mod double_doji; pub mod engulfing; pub mod h; -pub mod harami; +pub mod harami_flexible; +pub mod harami_strict; pub mod hexad; pub mod marubozu; pub mod on_neck;