import java.util.ArrayList;
public class MovingAverage {
private ArrayList<Double> data;
private int period;
public MovingAverage(int period) {
this.period = period;
this.data = new ArrayList<Double>();
}
public void addData(double value) {
this.data.add(value);
if (this.data.size() > this.period) {
this.data.remove(0);
}
}
public double calculate() {
double sum = 0;
for (double value : this.data) {
sum += value;
}
return sum / this.data.size();
}
}
public class MovingAverage {
private ArrayList<Double> data;
private int period;
public MovingAverage(int period) {
this.period = period;
this.data = new ArrayList<Double>();
}
public void addData(double value) {
this.data.add(value);
if (this.data.size() > this.period) {
this.data.remove(0);
}
}
public double calculate() {
double sum = 0;
for (double value : this.data) {
sum += value;
}
return sum / this.data.size();
}
}
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.