Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marubozu #15

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ta_lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ta_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"core",
"momentum",
"patterns",
"price",
"trend",
"volatility",
Expand Down
4 changes: 2 additions & 2 deletions ta_lib/core/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ impl<T> IntoIterator for Series<T> {

impl Into<Vec<f64>> for Series<f64> {
fn into(self) -> Vec<f64> {
self.into_iter().filter_map(|x| x).collect()
self.data.into_iter().map(|x| x.unwrap_or(0.0)).collect()
}
}

impl Into<Vec<bool>> for Series<bool> {
fn into(self) -> Vec<bool> {
self.into_iter().filter_map(|x| x).collect()
self.data.into_iter().map(|x| x.unwrap_or(false)).collect()
}
}

Expand Down
1 change: 1 addition & 0 deletions ta_lib/patterns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../core" }
1 change: 1 addition & 0 deletions ta_lib/patterns/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod marubozu;
56 changes: 56 additions & 0 deletions ta_lib/patterns/src/marubozu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use core::series::Series;

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

(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<bool> {
let open = Series::from(open);
let high = Series::from(high);
let low = Series::from(low);
let close = Series::from(close);

(close.shift(1).lt_series(&open.shift(1))
& high.shift(1).eq_series(&open.shift(1))
& low.shift(1).eq_series(&close.shift(1)))
.into()
}

#[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![false, false, false, false, false];

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![false, false, false, false, false];

let result = bearish(&open, &high, &low, &close);

assert_eq!(result, expected);
}
}
Loading