Cannot push_back new value

void exponential_moving_average(vector<double> series, int periods, vector<double>& EMA)
{
double alpha;
alpha=(2/(periods+1));
double new_EMA;
new_EMA=series[0];
EMA.push_back(new_EMA);
for (int i=1; i<series.size(); i++)
{
new_EMA=(alpha*series[i]+(1-alpha)*new_EMA);
EMA.push_back(new_EMA);
};
}

Hello guys,
Something must be wrong on the above code. I have firstly initialized EMA[0] to be series[0], then I use a loop to generate the other following EMA values and push back the new_EMA to the vector (or so I thought I was doing). However, it turns out that I got a whole vector of the value of series[0] instead of a new series of EMA that is supposed to emerge after the calculation of the for loop. Can anyone spot what's wrong in the code? Many thanks.
Regards,
Bosco
It looks like alpha=(2/(periods+1)); is equal to zero. or is so little that
alpha*series[i] is close to zero.
It can't be... I use 20 for periods, so alpha should not be zero...
You can insert outputting of your variables in your function and look what values do they have.
Of course I did. Like I said, turns out all the values in the EMA vector = new_EMA = series[0], i.e. the value that I initialized to the first value of the vector.

So it seems that the for loop was not doing what it is supposed to do, and the initial new_EMA value was kept being pushed back to the vector. That is my guess. But exactly what is wrong, I don't know...

Bosco
One more you could insert printing all you variables in your function and see what values they have and how many times the loop is executed. What is the problem?!
Integer divisions returns an integer
alpha is 0.
you are exactly right! its working now! many thanks!
Topic archived. No new replies allowed.