diff --git a/ta_lib/price/src/lib.rs b/ta_lib/price/src/lib.rs index 9be3540a..34e66b50 100644 --- a/ta_lib/price/src/lib.rs +++ b/ta_lib/price/src/lib.rs @@ -1,3 +1,4 @@ pub mod average; pub mod median; pub mod typical; +pub mod wcl; diff --git a/ta_lib/price/src/wcl.rs b/ta_lib/price/src/wcl.rs new file mode 100644 index 00000000..905c9ac6 --- /dev/null +++ b/ta_lib/price/src/wcl.rs @@ -0,0 +1,30 @@ +pub fn wcl(high: &[f64], low: &[f64], close: &[f64]) -> Vec { + let len = high.len(); + + if len != low.len() || len != close.len() { + return vec![0.0; len]; + } + + high.iter() + .zip(low) + .zip(close) + .map(|((&h, &l), &c)| (h + l + (c * 2.0)) / 4.0) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_weighted_close_price() { + let high = vec![1.0, 2.0, 3.0]; + let low = vec![0.5, 1.0, 1.5]; + let close = vec![0.75, 1.5, 2.25]; + let expected = vec![0.75, 1.5, 2.25]; + + let result = wcl(&high, &low, &close); + + assert_eq!(result, expected); + } +}