Skip to content

Commit

Permalink
aosc
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Jun 29, 2023
1 parent 652d760 commit b05a58e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions ta_lib/momentum/src/aosc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use overlap::sma::sma;
use price::median::median_price;

pub fn aosc(
high: &[f64],
low: &[f64],
short_period: usize,
long_period: usize,
) -> Vec<Option<f64>> {
let median_price = median_price(high, low);
let median_price_values = median_price
.iter()
.map(|&x| x.unwrap_or(0.0))
.collect::<Vec<_>>();

let ao_short = sma(&median_price_values, short_period);
let ao_long = sma(&median_price_values, long_period);

ao_short
.iter()
.zip(&ao_long)
.map(|(&short, &long)| match (short, long) {
(Some(short), Some(long)) => Some(short - long),
_ => None,
})
.collect()
}

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

#[test]
fn test_aosc() {
let high = &[3.0, 4.0, 5.0, 6.0, 7.0];
let low = &[1.0, 2.0, 3.0, 4.0, 5.0];

let short_period = 2;
let long_period = 4;

let result = aosc(high, low, short_period, long_period);

let expected_result = vec![None, None, None, Some(1.0), Some(1.0)];

assert_eq!(result, expected_result);
}
}
1 change: 1 addition & 0 deletions ta_lib/momentum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod aosc;
pub mod macd;
pub mod rsi;

0 comments on commit b05a58e

Please sign in to comment.