Skip to content

Commit

Permalink
vo
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jul 5, 2023
1 parent c08d9e1 commit 984a9a7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions ta_lib/volume/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod vo;
pub mod vwap;
39 changes: 39 additions & 0 deletions ta_lib/volume/src/vo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use overlap::ema::ema;

pub fn vo(source: &[f64], short_period: usize, long_period: usize) -> Vec<Option<f64>> {
let vo_short = ema(source, short_period);
let vo_long = ema(source, long_period);

vo_short
.iter()
.zip(&vo_long)
.map(|(&short, &long)| match (short, long) {
(Some(short), Some(long)) => Some(100.0 * (short - long) / long),
_ => None,
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_vo() {
let source = vec![1.0, 2.0, 3.0, 2.0, 1.0];
let expected = vec![None, None, Some(13.5802), Some(2.83224), Some(-10.71604)];
let epsilon = 0.001;

let result = vo(&source, 2, 3);

for i in 0..source.len() {
match (result[i], expected[i]) {
(Some(a), Some(b)) => {
assert!((a - b).abs() < epsilon, "at position {}: {} != {}", i, a, b)
}
(None, None) => {}
_ => panic!("at position {}: {:?} != {:?}", i, result[i], expected[i]),
}
}
}
}

0 comments on commit 984a9a7

Please sign in to comment.