df-maths 0.1.1

This is an maths
Documentation
use json::{array, JsonValue};

/// 简单移动平均 MA
/// 移动平均
///
/// * data 数据列表
/// * n 统计周期
pub fn ma(data: JsonValue, n: f64, value: bool) -> JsonValue
{
    let mut index = 0.0;
    let mut y = 0.0;
    let mut list = array![];
    for item in data.members() {
        index += 1.0;
        let x: f64 = item.as_f64().unwrap();
        let i = {
            if index > n {
                let i = data[(index - n - 1.0) as usize].as_f64().unwrap();
                i
            } else {
                0.0
            }
        };
        y = (x + y * n - i) / n;
        if index < n {
            list.push(0.0).unwrap();
        } else {
            list.push(y).unwrap();
        }
    }
    if value {
        return list[(list.len() - 1) as usize].clone();
    }
    return list;
}

/// 指数平滑移动平均 EXPMEMA
///
/// * data 数据列表
/// * n 阻尼系数 区间 [0,1]
pub fn expmema(data: JsonValue, n: f64) -> JsonValue {
    let mut index = 0.0;
    let mut y = 0.0;
    let mut list = array![];
    for item in data.members() {
        let x = item.as_f64().unwrap();
        if index == 0.0 {
            y = x * (1.0 - n) + 0.0 * n;
            list.push(y).unwrap();
        } else {
            let x = data[(index) as usize].as_f64().unwrap();
            y = x * (1.0 - n) + y * n;
            list.push(y).unwrap();
        }
        index += 1.0;
    }
    return list;
}

/// 修正指数移动平均 XEXPMEMA
///
/// * data 数据列表
/// * n 阻尼系数 区间 [0,1]
pub fn xexpmema(data: JsonValue, n: f64) -> JsonValue {
    let mut index = 0.0;
    let mut list = array![];
    for item in data.members() {
        let x = item.as_f64().unwrap();
        if index == 0.0 {
            list.push(x).unwrap();
        } else {
            let y = x / (1.0 - n.powf(index + 1.0));
            list.push(y).unwrap();
        }
        index += 1.0;
    }
    return list;
}

/// 指数移动平均 EMA
///
/// * data 数据列表
/// * n 统计周期
pub fn ema(data: JsonValue, n: f64) -> JsonValue {
    let mut y = 0.0;
    let mut list = array![];
    for item in data.members() {
        let x = item.as_f64().unwrap();
        y = (x + y * (n - 1.0)) / n;
        list.push(y).unwrap();
    }
    return list;
}

/// 加权移动平均法
///
/// * data 真实值
/// * n 移动周期 3.0
/// * value 返回值还是列表
/// ````rust
/// use json::array;
/// use df_maths::ma;
/// let data = array![38,45,35,49,70,43,46,55,45,68,64];
/// let res = ma::wma(data, 3, true);
/// ````
pub fn wma(mut data: JsonValue, n: i32, value: bool) -> JsonValue {
    let mut list = array![];
    let mut index = 0;
    data.push(0.0).unwrap();
    for _ in data.members() {
        let mut x = 0.0;
        let mut a = 0.0;
        let y = {
            if index >= n {
                for i in 0..n as usize {
                    x = x + data[(index - n) as usize + i].as_f64().unwrap() * (i as f64 + 1.0);
                    a += i as f64 + 1.0;
                }
                x / a
            } else {
                0.0
            }
        };
        index += 1;
        list.push(y).unwrap();
    }
    if value {
        return list[(list.len() - 1) as usize].clone();
    }
    return list;
}