Create a new moving average. this.periods = periods; this.data = new double[periods]; for (int i=0; i < periods; i++) { data[i] = 0.0; }
this.periods = periods; this.data = new double[periods]; for (int i=0; i < periods; i++) { data[i] = 0.0; }
double sum = 0.0; int lim = pos>periods?periods:pos; for (int i=0; i < lim; i++) { sum += data[i]; } return sum / lim;
Return average-so-far. return calculateAve();
return calculateAve();
pos = 0;
Update average and return average-so-far. data[pos++%periods] = newValue; if ( pos==Integer.MAX_VALUE){ pos = pos%periods; } return calculateAve();
data[pos++%periods] = newValue; if ( pos==Integer.MAX_VALUE){ pos = pos%periods; } return calculateAve();