diff --git a/ta_lib/utils/src/change.rs b/ta_lib/utils/src/change.rs new file mode 100644 index 00000000..cab544c7 --- /dev/null +++ b/ta_lib/utils/src/change.rs @@ -0,0 +1,41 @@ +pub fn change(source: &[f64], length: usize) -> Vec { + let len = source.len(); + let mut change = vec![0.0; len]; + + if length > len { + return change; + } + + for i in length..len { + change[i] = source[i] - source[i - length]; + } + + change +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_change() { + let source = vec![1.0, 2.0, 3.0, 4.0, 5.0]; + let length = 2; + let expected = vec![0.0, 0.0, 2.0, 2.0, 2.0]; + + let result = change(&source, length); + + assert_eq!(result, expected); + } + + #[test] + fn test_change_zero_length() { + let source = vec![1.0, 2.0, 3.0, 4.0, 5.0]; + let length = 0; + let expected = vec![0.0, 0.0, 0.0, 0.0, 0.0]; + + let result = change(&source, length); + + assert_eq!(result, expected); + } +} diff --git a/ta_lib/utils/src/lib.rs b/ta_lib/utils/src/lib.rs index 8f0b2cee..1924a882 100644 --- a/ta_lib/utils/src/lib.rs +++ b/ta_lib/utils/src/lib.rs @@ -1,3 +1,4 @@ +pub mod change; pub mod highest; pub mod lowest; pub mod nz;