Skip to content

Commit

Permalink
re-organize code in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Parry-97 committed Jul 8, 2023
1 parent a20d16c commit 3899fb4
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 366 deletions.
57 changes: 57 additions & 0 deletions src/calculations/mod.rs
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)))
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
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;
Loading

0 comments on commit 3899fb4

Please sign in to comment.