Repeating programming code for a fixed number of times can be done with the for-to loop and down-to loop. But what if you don’t know beforehand how many loop cycles you need?

In this article:

The while loop in MultiCharts PowerLanguage

The while loop repeats programming code as long as a true/false expression is true (MultiCharts Wiki, 2012a).

Because the while loop has no fixed end point, the programmer needs to make sure that the true/false expression becomes false at some point in order to stop the loop. This is opposed to the for-to and down-to loop, which automatically change the counter variable and so ensure that the loop ends (see MultiCharts Wiki, 2012b).

The while loop has the following default pattern (MultiCharts Wiki, 2012a):


while trueFalseExpression = true begin

    // Code that needs to be executed repeatedly
    // and that makes the 'trueFalseExpression'
    // false at some point.

end;

Before a while loop is executed for the first time, its true/false expression needs to be true. The loop is then executed as long as the expression remains true. A while loop can therefore run forever unless the true/false expression becomes false or the break keyword is executed.

Tip: A loop that runs forever is called an infinite loop (Liberty & MacDonald, 2009). To prevent this common while loop error, double-check the loop’s code before executing the script. See troubleshooting infinite loops for fixing infinite loop errors.

Programming example of the while loop in PowerLanguage

A basic while loop is the following:


Variables:
    y(0);

y = 0;

while y < 5 begin

    Print("y = ", y);
    y = y + 1;

end;

//> y =    0.00
//> y =    1.00
//> y =    2.00
//> y =    3.00
//> y =    4.00

This while loop executes as long as y is less than 5. Inside the loop the value of y is printed and incremented with 1 (y = y + 1). Increasing this variable during each loop cycle makes sure that the true/false expression (y < 5) becomes false after 5 loop cycles, at which point the loop stops.

Calculating the average price with a while loop

The following example calculates the average closing price of the 10 bars before the current bar:


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

x = 10;
avgClose = 0;

while x > 0 begin 

    avgClose = avgClose + Close[x];  
    x = x - 1;

end;

avgClose = avgClose / 10;

Print("Average close of the previous 10 bars: ",
    NumToStr(avgClose, 8));

//> Average close of the previous 10 bars: 1.29173700

Before the loop begins, the x variable is set to 10 and the avgClose variable reset to 0. Then the while loop checks to see if x is greater than 0 (which it is), after which it begins looping.

Inside the loop, the avgClose variable is assigned the sum of its current value plus a previous closing price. This closing price is retrieved by placing the x variable inside the square brackets following the Close keyword. To prevent a never-ending loop, the x variable is decreased by 1 with each loop cycle. That way the while loop stops after 10 loop cycles.

When the while loop has stopped, the avgClose variable is divided by 10 to calculate the average. This value is subsequently converted to a string with 8 decimals with the NumToStr() keyword and outputted with Print().

Summary

While loops repeat all programming code between the begin and end keywords as long as a true/false expression is true. It’s up to the programmer to make sure that this expression becomes false at some point; otherwise, an infinite loop is the result.


References

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

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

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