market-rs 0.1.2

A library which provides a simple interface for interacting with a market
Documentation
pub fn sma(input: Vec<f64>, period: usize) -> Vec<f64> {
    let mut output = Vec::new();
    let mut sum = 0.0;
    for i in 0..input.len() {
        sum += input[i];
        if i >= period {
            sum -= input[i - period];
        }
        output.push(sum / period as f64);
    }
    output
}

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

    #[test]
    fn sma_test() {
        let input = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let period = 2;
        let expected = vec![0.5, 1.5, 2.5, 3.5, 4.5];
        let output = sma(input, period);
        assert_eq!(output, expected);
    }
}