1use json::{array, JsonValue};
2
3pub 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
37pub 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
60pub 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
80pub 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
95pub 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}