From 248d3d77c576c919c30c672151c55bd503a1caa8 Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Sat, 22 Jul 2023 16:54:20 +0300 Subject: [PATCH 1/3] marubozu --- ta_lib/patterns/src/marubozu.rs | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ta_lib/patterns/src/marubozu.rs diff --git a/ta_lib/patterns/src/marubozu.rs b/ta_lib/patterns/src/marubozu.rs new file mode 100644 index 00000000..a1936ccd --- /dev/null +++ b/ta_lib/patterns/src/marubozu.rs @@ -0,0 +1,54 @@ +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.shift(1) > open.shift(1) + && high.shift(1) == close.shift(1) + && low.shift(1) == open.shift(1) +} + +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.shift(1) < open.shift(1) + && high.shift(1) == open.shift(1) + && low.shift(1) == close.shift(1) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_marubozu_bullish() { + let open = vec![4.0, 4.0, 4.0, 4.0, 4.0]; + let high = vec![3.0, 3.0, 3.0, 3.0, 3.0]; + let low = vec![1.0, 1.0, 1.0, 1.0, 1.0]; + let close = vec![2.0, 2.5, 2.0, 1.5, 2.0]; + let expected = vec![true, true, true, true, true]; + + let result = bullish(&open, &high, &low, &close); + + assert_eq!(result, expected); + } + + #[test] + fn test_marubozu_bearish() { + let open = vec![4.0, 4.0, 4.0, 4.0, 4.0]; + let high = vec![3.0, 3.0, 3.0, 3.0, 3.0]; + let low = vec![1.0, 1.0, 1.0, 1.0, 1.0]; + let close = vec![2.0, 2.5, 2.0, 1.5, 2.0]; + let expected = vec![true, true, true, true, true]; + + let result = bearish(&open, &high, &low, &close); + + assert_eq!(result, expected); + } +} From 67ae7e780ad5e840e46cd4cc3b91d0f62e1d66bc Mon Sep 17 00:00:00 2001 From: m5l14i11 Date: Thu, 27 Jul 2023 15:49:05 +0300 Subject: [PATCH 2/3] marubozu --- ta_lib/Cargo.lock | 7 +++++++ ta_lib/Cargo.toml | 1 + ta_lib/core/src/series.rs | 4 ++-- ta_lib/patterns/Cargo.toml | 1 + ta_lib/patterns/src/lib.rs | 1 + ta_lib/patterns/src/marubozu.rs | 16 ++++++++-------- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ta_lib/Cargo.lock b/ta_lib/Cargo.lock index 73f1bb39..911a6679 100644 --- a/ta_lib/Cargo.lock +++ b/ta_lib/Cargo.lock @@ -15,6 +15,13 @@ dependencies = [ "utils", ] +[[package]] +name = "patterns" +version = "0.1.0" +dependencies = [ + "core", +] + [[package]] name = "price" version = "0.1.0" diff --git a/ta_lib/Cargo.toml b/ta_lib/Cargo.toml index 2e20ee45..4cfedc42 100644 --- a/ta_lib/Cargo.toml +++ b/ta_lib/Cargo.toml @@ -2,6 +2,7 @@ members = [ "core", "momentum", +"patterns", "price", "trend", "volatility", diff --git a/ta_lib/core/src/series.rs b/ta_lib/core/src/series.rs index 96d15bdd..eba3bb8e 100644 --- a/ta_lib/core/src/series.rs +++ b/ta_lib/core/src/series.rs @@ -148,13 +148,13 @@ impl IntoIterator for Series { impl Into> for Series { fn into(self) -> Vec { - self.into_iter().filter_map(|x| x).collect() + self.data.into_iter().map(|x| x.unwrap_or(0.0)).collect() } } impl Into> for Series { fn into(self) -> Vec { - self.into_iter().filter_map(|x| x).collect() + self.data.into_iter().map(|x| x.unwrap_or(false)).collect() } } diff --git a/ta_lib/patterns/Cargo.toml b/ta_lib/patterns/Cargo.toml index 82fe057b..4412ab16 100644 --- a/ta_lib/patterns/Cargo.toml +++ b/ta_lib/patterns/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +core = { path = "../core" } diff --git a/ta_lib/patterns/src/lib.rs b/ta_lib/patterns/src/lib.rs index e69de29b..017374f5 100644 --- a/ta_lib/patterns/src/lib.rs +++ b/ta_lib/patterns/src/lib.rs @@ -0,0 +1 @@ +pub mod marubozu; diff --git a/ta_lib/patterns/src/marubozu.rs b/ta_lib/patterns/src/marubozu.rs index a1936ccd..20cc4191 100644 --- a/ta_lib/patterns/src/marubozu.rs +++ b/ta_lib/patterns/src/marubozu.rs @@ -6,9 +6,9 @@ pub fn bullish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec open.shift(1) - && high.shift(1) == close.shift(1) - && low.shift(1) == open.shift(1) + (close.shift(1).gt_series(&open.shift(1)) + & high.shift(1).eq_series(&close.shift(1)) + & low.shift(1).eq_series(&open.shift(1))).into() } pub fn bearish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec { @@ -17,9 +17,9 @@ pub fn bearish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec Date: Thu, 27 Jul 2023 15:50:14 +0300 Subject: [PATCH 3/3] marubozu --- ta_lib/patterns/src/marubozu.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ta_lib/patterns/src/marubozu.rs b/ta_lib/patterns/src/marubozu.rs index 20cc4191..7f6ec287 100644 --- a/ta_lib/patterns/src/marubozu.rs +++ b/ta_lib/patterns/src/marubozu.rs @@ -8,7 +8,8 @@ pub fn bullish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec Vec { @@ -19,7 +20,8 @@ pub fn bearish(open: &[f64], high: &[f64], low: &[f64], close: &[f64]) -> Vec