use json::{array, JsonValue};
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;
}
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;
}
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;
}
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;
}
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;
}