views:

56

answers:

1

I was playing with ActiveQuant FinancialLibrary SimpleMovingAverage function SMA() below:

Is there an error in the algo below, where it calculates the average by looking "into the future" ( as it does i < (period + skipdays) ) ?

public static double SMA(int period, double[] vals, int skipdays) {
    double value = 0.0;
    for (int i = skipdays; i < (period + skipdays); i++) {
        value += vals[i];
    }
    value /= (double) period;
    return value;
}

The for loop can be replaced with that below, where it looks backward.

    for (int i = skipdays - period; i < skipdays; i++) {
        value += vals[i];
    }

Am I missing something?

+1  A: 

I see no error. The algorithm correctly computes the mean value of the data array from index skipdays (inclusive) to index skipdays + period (exclusive) for period > 0.

Perhaps you need to rephrase the question.

Steve Emmerson
Yes there is no error, as ActiveQuant expects the first data or vals[0] is for the current date, vals[1] is the data for yesterday, etc. I was using it the other way around.
portoalet