df_maths/
ma.rs

1use json::{array, JsonValue};
2
3/// 简单移动平均 MA
4/// 移动平均
5///
6/// * data 数据列表
7/// * n 统计周期
8pub fn ma(data: JsonValue, n: f64, value: bool) -> JsonValue
9{
10    let mut index = 0.0;
11    let mut y = 0.0;
12    let mut list = array![];
13    for item in data.members() {
14        index += 1.0;
15        let x: f64 = item.as_f64().unwrap();
16        let i = {
17            if index > n {
18                let i = data[(index - n - 1.0) as usize].as_f64().unwrap();
19                i
20            } else {
21                0.0
22            }
23        };
24        y = (x + y * n - i) / n;
25        if index < n {
26            list.push(0.0).unwrap();
27        } else {
28            list.push(y).unwrap();
29        }
30    }
31    if value {
32        return list[(list.len() - 1) as usize].clone();
33    }
34    return list;
35}
36
37/// 指数平滑移动平均 EXPMEMA
38///
39/// * data 数据列表
40/// * n 阻尼系数 区间 [0,1]
41pub fn expmema(data: JsonValue, n: f64) -> JsonValue {
42    let mut index = 0.0;
43    let mut y = 0.0;
44    let mut list = array![];
45    for item in data.members() {
46        let x = item.as_f64().unwrap();
47        if index == 0.0 {
48            y = x * (1.0 - n) + 0.0 * n;
49            list.push(y).unwrap();
50        } else {
51            let x = data[(index) as usize].as_f64().unwrap();
52            y = x * (1.0 - n) + y * n;
53            list.push(y).unwrap();
54        }
55        index += 1.0;
56    }
57    return list;
58}
59
60/// 修正指数移动平均 XEXPMEMA
61///
62/// * data 数据列表
63/// * n 阻尼系数 区间 [0,1]
64pub fn xexpmema(data: JsonValue, n: f64) -> JsonValue {
65    let mut index = 0.0;
66    let mut list = array![];
67    for item in data.members() {
68        let x = item.as_f64().unwrap();
69        if index == 0.0 {
70            list.push(x).unwrap();
71        } else {
72            let y = x / (1.0 - n.powf(index + 1.0));
73            list.push(y).unwrap();
74        }
75        index += 1.0;
76    }
77    return list;
78}
79
80/// 指数移动平均 EMA
81///
82/// * data 数据列表
83/// * n 统计周期
84pub fn ema(data: JsonValue, n: f64) -> JsonValue {
85    let mut y = 0.0;
86    let mut list = array![];
87    for item in data.members() {
88        let x = item.as_f64().unwrap();
89        y = (x + y * (n - 1.0)) / n;
90        list.push(y).unwrap();
91    }
92    return list;
93}
94
95/// 加权移动平均法
96///
97/// * data 真实值
98/// * n 移动周期 3.0
99/// * value 返回值还是列表
100/// ````rust
101/// use json::array;
102/// use df_maths::ma;
103/// let data = array![38,45,35,49,70,43,46,55,45,68,64];
104/// let res = ma::wma(data, 3, true);
105/// ````
106pub fn wma(mut data: JsonValue, n: i32, value: bool) -> JsonValue {
107    let mut list = array![];
108    let mut index = 0;
109    data.push(0.0).unwrap();
110    for _ in data.members() {
111        let mut x = 0.0;
112        let mut a = 0.0;
113        let y = {
114            if index >= n {
115                for i in 0..n as usize {
116                    x = x + data[(index - n) as usize + i].as_f64().unwrap() * (i as f64 + 1.0);
117                    a += i as f64 + 1.0;
118                }
119                x / a
120            } else {
121                0.0
122            }
123        };
124        index += 1;
125        list.push(y).unwrap();
126    }
127    if value {
128        return list[(list.len() - 1) as usize].clone();
129    }
130    return list;
131}