1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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 br_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;
}