MultiCharts | PowerLanguage Tutorials | CH5: Loop

Loop

The for and while loop allows the program to repeatedly execute the instructions until a certain number of times or the logical conditions are not met.

The break and continue make program can jump out of the loop in the middle.

The #return make program jump out of script execution this time.

For loop

The for loop uses a numeric variable to count, and is given an initial and final value.

Used with To

When for used with To, the value of Counter starts from IValue.
Each time a loop is executed, the value of Counter is incremented by one.
The loop executes until Counter is greater than FValue, and then program exits loops.

For Counter=IValue To FValue Begin
  I1;
  I2;
End;

Used with DownTo

When for used with DownTo, the value of Counter starts from IValue.
Each time a loop is executed, the value of Counter is decreased by one.
The loop executes until Counter is less than FValue, and then program exits loops.

For Counter=IValue DownTo FValue Begin
  I1;
  I2;
End;

Example

For BarBackNo = 0 To 9 Begin
    HighPriceSum = HighPriceSum + High[BarBackNo];
End;
For BarBackNo = 9 DownTo 0 Begin
    HighPriceSum = HighPriceSum + High[BarBackNo];
End;

While loop

The while judges Logical_Expression before each execution of the loop.
If Logical_Expression is true, the instructions in the Begin and End block will be executed, and if it is false, it will exit the loop.

While Logical_Expression Begin
  I1;
  I2;
  I3;
End;

Example

BarBackNo = 0;
While BarBackNo < 10 Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];

  BarBackNo = BarBackNo + 1;
End;

Jump out loop

The for and while loops will continue to execute until the specified conditions are not met, but sometimes there are situations where you want to jump out of the loop in the middle.
The break and continue instructions make you can jump out of the loop midway.

Break instruction

When the program executes to break, jump out and end the entire loop.

Usage

break;

Examples

var: dayCount(0);
For dayCount = 0 to 6 Begin
    If (Open < 16000) then break;
End;
var: dayCount(0);
dayCount = 0;
While dayCount < 7 Begin
    If (Open < 16000) then break;

    dayCount = dayCount + 1;
End;

Continue instruction

When the program executes to continue, it jumps out of loop this round and continue executing the next one.
Continue is different from break. Continue just jumps out of the loop this round, and does not end the entire loop.

Usage

continue;

Examples

var: dayCount(0);
For dayCount = 0 to 6 Begin
    If (Open < 16000) then continue;
End;
var: dayCount(0);
dayCount = 0;
While dayCount < 7 Begin
    If (Open < 16000) then continue;
    
    dayCount = dayCount + 1;
End;

Return instruction

The #return instruction is similar to break, but #return ends the execution of the script, not just the loop.
When the program executes to #return, the execution of script is ended, and according to the script execution mechanism, the script is re-executed when the next execution condition is satisfied (ex: upon bar is completed).

Usage

#return;

Reference

https://www.multicharts.com/trading-software/index.php?title=Category:Comparisons_and_Loops

https://www.multicharts.com/trading-software/index.php?title=Return

https://www.multicharts.com/trading-software/index.php?title=While

https://www.multicharts.com/trading-software/index.php?title=For

Leave a Reply