Repeating programming code multiple times up to a certain value is done with the for-to loop in PowerLanguage. But what if you wanted to loop over historical price bars starting with the oldest price bar down to the current bar?

In this article:

The down-to loop in MultiCharts PowerLanguage

The down-to loop executes programming code repeatedly for a certain amount of loop cycles, starting from a begin value down to an end value (MultiCharts Wiki, 2012a).

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


for counter = beginValue downto endValue begin 

    // Code to execute repeatedly

end;

A down-to loop has three parts:

  • The counter variable contains the loop count. When the loop begins, this variable is given the value of beginValue;
  • The beginValue sets the starting value of the loop. Because the down-to loop decreases the loop count on each cycle, this value needs to be bigger than endValue;
  • And the endValue sets the value at which the loop stops.

So a down-to loop starts at beginValue and continues as long as counter is not less than endValue. At the end of each loop cycle, this counter variable is decreased by 1.

Characteristics of the PowerLanguage down-to loop

The down-to loop has the following rules and features (MultiCharts Wiki, 2012a):

  • The counter variable is automatically decreased at the end of each loop cycle – this doesn’t need to be done manually;
  • The begin and end keywords are not optional: they need to be included with every down-to loop;
  • The counter variable always needs to be included and assigned the beginValue, even if this variable already has a value.

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

Down-to PowerLanguage loop example

A basic down-to loop looks as follows:


Variables:
    counter(0);

for counter = 4 downto 0 begin 

    Print("The value of 'counter' is ", counter);

end;

//> The value of 'counter' is    4.00
//> The value of 'counter' is    3.00
//> The value of 'counter' is    2.00
//> The value of 'counter' is    1.00
//> The value of 'counter' is    0.00

This loop starts at 4 and repeats the programming code inside the loop as long as the loop count is greater than or equal to 0. This makes the loop run 5 times.

The loop count variable (counter) is decreased automatically with 1 at the end of each loop cycle. This variable can be used inside the loop to access the current loop count, which is done here by printing its value.

Counting the number of higher closes with the down-to loop

Loops are often used to cycle through historical price bars, such as when calculating how many of the last 10 price bars closed higher:


Variables:
    y(0), higherClose(0);

higherClose = 0;  
    
for y = 9 downto 0 begin 

    if (Close[y] > Close[y + 1]) then begin
        
        Print("This close (", NumToStr(Close[y], 5),
            ") is higher than the previous close (",
            NumToStr(Close[y + 1], 5), ")");
            
        higherClose = higherClose + 1;
        
    end;

end;

Print("A total of ", NumToStr(higherClose, 0),
    " bars closed higher.");

//> This close (1.29387) is higher than the previous close (1.29384)
//> This close (1.29461) is higher than the previous close (1.29387)
//> This close (1.29470) is higher than the previous close (1.29461)
//> This close (1.29474) is higher than the previous close (1.29426)
//> A total of 4 bars closed higher.

This down-to loop begins with a loop count of 9 and ends a 0 for a total of 10 cycles. The loop count variable (y) is used inside the loop to access the closing prices of historical price bars. This is done by placing this variable between the square brackets ([ and ]) following the Close keyword.

The if statement checks if the closing price from y bars ago is higher than the close before that (Close[y+1]). Text is outputted with Print() when that is the case and the higherClose variable is increased with one. After the loop has been completed, the higherClose variable holds the total number of higher closes.

Summary

Down-to loops repeat all programming code between the begin and end keywords from a begin value down to a certain end value. At the end of each loop cycle, the loop count variable is automatically decreased by 1.


References

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

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