0

Hi,

I'm trying to compute a moving average of length 20. I've got a vector of structs that's read into my function and I'm using an iterator to read through as follows:

    int length = a;
    int count = 0;
    double sum = 0;

    while (itr!= priceinput.end()) 

    {   
            sum += itr->Close;
            count ++;
            if (count >= length)
                {
                    itr->MovingAve = (sum / (double) length);
                    sum -= (itr-length)->Close; // this is the problem line
                }

So I'm trying to find out what the right syntax is for the sum-= line. When count gets to 21, I have to subtract the 1st number from the sum, so that's (count-length). I've been googling for the right syntax to do this with the iterator, in this case, itr, but no luck so far.

Any ideas?
Thanks,
TR

1
Contributor
1
Reply
13
Views
2 Years
Discussion Span
Last Post by toneranger
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.