use crate::core::Method;
use crate::core::{Error, PeriodType, ValueType, Window};
use crate::helpers::Peekable;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Integral {
value: ValueType,
window: Window<ValueType>,
}
pub type Sum = Integral;
impl Method for Integral {
type Params = PeriodType;
type Input = ValueType;
type Output = Self::Input;
fn new(length: Self::Params, &value: &Self::Input) -> Result<Self, Error> {
Ok(Self {
window: Window::new(length, value),
value: value * length as ValueType,
})
}
#[inline]
fn next(&mut self, &value: &Self::Input) -> Self::Output {
self.value += value;
if !self.window.is_empty() {
self.value -= self.window.push(value);
}
self.value
}
}
impl Default for Integral {
fn default() -> Self {
Self::new(0, &0.0).unwrap()
}
}
impl Peekable<<Self as Method>::Output> for Integral {
fn peek(&self) -> <Self as Method>::Output {
self.value
}
}
#[cfg(test)]
mod tests {
use super::{Integral as TestingMethod, Method};
use crate::core::ValueType;
use crate::helpers::{assert_eq_float, RandomCandles};
use crate::methods::tests::test_const;
#[test]
fn test_integral_const() {
for i in 1..255 {
let input = (i as ValueType + 56.0) / 16.3251;
let mut method = TestingMethod::new(i, &input).unwrap();
let output = method.next(&input);
test_const(&mut method, &input, &output);
}
}
#[test]
#[should_panic]
fn test_integral0_const() {
use crate::core::Method;
use crate::methods::tests::test_const;
let input = (5.0 + 56.0) / 16.3251;
let mut method = TestingMethod::new(0, &input).unwrap();
let output = method.next(&input);
test_const(&mut method, &input, &output);
}
#[test]
fn test_integral0() {
let src: Vec<ValueType> = RandomCandles::default()
.take(100)
.map(|x| x.close)
.collect();
let mut ma = TestingMethod::new(0, &src[0]).unwrap();
let mut q = Vec::new();
src.iter().enumerate().for_each(|(i, x)| {
let value1 = ma.next(x);
let value2 = src.iter().take(i + 1).fold(0.0, |s, &c| s + c);
q.push(x);
assert_eq_float(value2, value1);
});
}
#[test]
fn test_integral1() {
let mut candles = RandomCandles::default();
let mut ma = TestingMethod::new(1, &candles.first().close).unwrap();
candles.take(100).for_each(|x| {
assert_eq_float(x.close, ma.next(&x.close));
});
}
#[test]
fn test_integral() {
let candles = RandomCandles::default();
let src: Vec<ValueType> = candles.take(300).map(|x| x.close).collect();
(1..255).for_each(|length| {
let mut ma = TestingMethod::new(length, &src[0]).unwrap();
let length = length as usize;
src.iter().enumerate().for_each(|(i, x)| {
let value1 = ma.next(x);
let value2 = (0..length).fold(0.0, |s, j| s + src[i.saturating_sub(j)]);
assert_eq_float(value2, value1);
});
});
}
}