package utils;


public class ExponentialMovingAverage {
    private double alpha;
    private Double oldValue;
    public ExponentialMovingAverage(double alpha) {
        this.alpha = alpha;
    }

    public double average(double value) {
        if (oldValue == null) {
            oldValue = value;
            return value;
        }
        //double newValue = oldValue + alpha * (value - oldValue);
        double k = 2 / (alpha +1);
        double newValue = (value * k) + (oldValue * (1 - k));
        oldValue = newValue;
        return newValue;
    }

    public double getAverage(){
        return oldValue;
    }
}
