-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
376 additions
and
366 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/// | ||
/// Calculates the absolute and relative difference between the beginning and ending of an f64 series. The relative difference is relative to the beginning. | ||
/// | ||
/// # Returns | ||
/// | ||
/// A tuple `(absolute, relative)` difference. | ||
/// | ||
pub fn price_diff(a: &[f64]) -> Option<(f64, f64)> { | ||
if !a.is_empty() { | ||
// unwrap is safe here even if first == last | ||
let (first, last) = (a.first().unwrap(), a.last().unwrap()); | ||
let abs_diff = last - first; | ||
let first = if *first == 0.0 { 1.0 } else { *first }; | ||
let rel_diff = abs_diff / first; | ||
Some((abs_diff, rel_diff)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// | ||
/// Window function to create a simple moving average | ||
/// | ||
pub fn n_window_sma(n: usize, series: &[f64]) -> Option<Vec<f64>> { | ||
if !series.is_empty() && n > 1 { | ||
Some( | ||
series | ||
.windows(n) | ||
.map(|w| w.iter().sum::<f64>() / w.len() as f64) | ||
.collect(), | ||
) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// | ||
/// Find the maximum in a series of f64 | ||
/// | ||
pub fn max(series: &[f64]) -> Option<f64> { | ||
if series.is_empty() { | ||
None | ||
} else { | ||
Some(series.iter().fold(f64::MIN, |acc, q| acc.max(*q))) | ||
} | ||
} | ||
|
||
/// | ||
/// Find the minimum in a series of f64 | ||
/// | ||
pub fn min(series: &[f64]) -> Option<f64> { | ||
if series.is_empty() { | ||
None | ||
} else { | ||
Some(series.iter().fold(f64::MAX, |acc, q| acc.min(*q))) | ||
} | ||
} |
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,6 @@ | ||
pub mod calculations; | ||
pub mod types; | ||
pub mod utils; | ||
|
||
#[cfg(test)] | ||
mod tests; |
Oops, something went wrong.