-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod vo; | ||
pub mod vwap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]), | ||
} | ||
} | ||
} | ||
} |