# How to code a loop with defined number of passes?

I've read through a number of examples of code from the forum regarding loops where some operation is done for each bar of code. I'm looking for something different (I think) but can't find an example to follow.

I have an indicator with an inputs for the number of moving averages to review. In the example, let's assume num_avg = 5. Each moving average is 5 bars longer than the previous one and the beginning moving average length is 7. So moving averages would be 7, 12, 17, 22, 27 in this example. I want a loop that will take the user input (num_avg = 5) and calculate each of the moving average values as described here and sum them.

Any guidance about how to structure this would be much appreciated. I know exactly how to do this in VBA but no idea how in AFL.

1 Like
``````start = 7;
end = 27;

sum_them = 0;
for ( i = start; i <= end; i += 5 ) {
sum_them += MA(C, i);
}

Plot( sum_them, "sum_them", colorRed );
``````
3 Likes

This is incredibly helpful! Thanks so much for taking the time to share what you know.