When programming there are several situations where you want to repeat certain actions, like when calculating the average range. We could write code for each past price bar separately, but it would more efficient to repeatedly execute the same programming code. How can we do that in PowerLanguage?

In this article:

The for-to loop in MultiCharts PowerLanguage

The most common PowerLanguage loop is the for-to loop. This loop executes code repeatedly until the loop count reaches a certain value (MultiCharts Wiki, 2012a). This loop count is increased by one at the end of each loop cycle.

The for-to loop has the following default pattern (MultiCharts Wiki, 2012a):


for counter = beginValue to endValue begin 

    // Code to execute repeatedly

end;

A for-to loop has three parts:

  • A counter variable with the loop count value. This variable is given the value of beginValue when the loop begins.
  • The beginValue sets the starting value of the loop. To start the loop, this value needs to be less than the endValue.
  • And the endValue specifies the value at which the loop stops.

A for-to loop starts at beginValue and continues as long as counter is not greater than endValue.

Features of the PowerLanguage for-to loop

The for-to loop has a few important points (MultiCharts Wiki, 2012b):

  • The counter variable is automatically increased with 1 at the end of each loop cycle.
  • The counter variable always needs to be given a beginValue, even if this counter variable already has a value.

The for-to loop is similar to the down-to loop except for one important difference: the for-to loop increases the counter variable at the end of the cycle, while the down-to loop decreases it (MultiCharts Wiki, 2012b).

PowerLanguage for-to loop example

The following is a basic for-to loop:


Variables:
    counter(0);
    
for counter = 0 to 5 begin

    Print("Value of 'counter': ", counter);

end;

//> Value of 'counter':    0.00
//> Value of 'counter':    1.00
//> Value of 'counter':    2.00
//> Value of 'counter':    3.00
//> Value of 'counter':    4.00
//> Value of 'counter':    5.00

The counter variable is given here the value of 0 at the beginning of the loop. This variable is increased with 1 after each loop cycle. Inside the loop, we can use the counter variable to output the loop count. Because this for-to loop begins at 0 and continues till 5, it runs 6 times.

Looping over historical price bars with the for-to loop

A for-to loop is often used with things like arrays or price bars, like the following example that calculates the average range:


Variables:
    avgRange(0),
    x(0);

avgRange = 0;
    
for x = 0 to 4 begin 

    avgRange = avgRange + (High[x] - Low[x]);
    Print("High of bar = ", NumToStr(High[x], 5),
        ", and low of bar = ", NumToStr(Low[x], 5));

end;

avgRange = avgRange / x;
Print("The average range is: ", NumToStr(avgRange, 6));

//> High of bar = 1.28975, and low of bar = 1.28940
//> High of bar = 1.28978, and low of bar = 1.28950
//> High of bar = 1.28985, and low of bar = 1.28940
//> High of bar = 1.28979, and low of bar = 1.28937
//> High of bar = 1.28990, and low of bar = 1.28954
//> The average range is: 0.000372

The loop begins at 0 and loops to 4 for a total of 5 cycles. The avgRange variable holds the sum of bar ranges by adding a bar’s range to itself during each loop cycle.

Inside the loop, the x counter variable is used to access historical price bar data. This is done by placing this variable inside square brackets ([ and ]) following the High and Low keywords.

When the loop is done, the average range is calculated by dividing the sum of ranges stored in avgRange by x. Because x is increased with 1 at the end of each loop cycle and the loop only runs till 4, x has the value of 5 when the loop ends.

Summary

For-to loops repeat the programming code between the begin and end keywords from a begin value to an end value. The loop count variable is automatically increased with 1 at the end of each loop cycle.


References

MultiCharts Wiki (2012a, February 19). To. Retrieved on November 11, 2014, from http://www.multicharts.com/trading-software/index.php/To

MultiCharts Wiki (2012b, February 19). For. Retrieved on November 11, 2014, from http://www.multicharts.com/trading-software/index.php/For