Take the 2-minute tour ×

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?

share|improve this question
Reformatted code; please revert if incorrect. –  trashgod Aug 7 '10 at 19:08
Do you have any reason to prefer one implementation to the other? –  trashgod Aug 7 '10 at 19:21

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
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 Aug 8 '10 at 2:43

Your Answer


