Loop#
for and while loop syntax allows the program to repeatedly execute instructions inside the loop until a certain number of iterations is reached or the logical conditions are no longer met.
break and continue syntax allows the program to exit the loop midway.
#return syntax allows the program to exit the current script execution.
for Loop#
The for loop uses a numerical variable to count, and is given an initial value and a final value. The for loop is used with the reserved word To or DownTo.
Used with To#
When used with To, the value of Counter starts from IValue. Each time the loop executes, the value of Counter is incremented by one. When Counter is greater than FValue, the loop exits and no longer executes.
| |
| Syntax Element | Description |
|---|---|
Counter | Numerical variable, used as the loop counter |
IValue | Initial value of the counter |
FValue | Final value of the counter |
Used with DownTo#
When used with DownTo, the value of Counter starts from IValue. Each time the loop executes, the value of Counter is decremented by one. When Counter is less than FValue, the loop exits and no longer executes.
| |
Example#
Used with To: From bar 0 to bar 9, accumulate the highest price into HighPriceSum.
| |
Used with DownTo: From bar 9 to bar 0, accumulate the highest price into HighPriceSum.
| |
while Loop#
The while loop evaluates the logical expression before each execution. If the result is true, the instructions inside the begin/end block are executed; if the result is false, the loop exits.
Syntax#
| |
| Syntax Element | Description |
|---|---|
Logical_Expression | Logical expression, evaluated before each loop iteration |
Instruction | Instructions executed when the logical expression is true |
Example#
From bar 0 to bar 9, accumulate the highest price into HighPriceSum. This is equivalent to the for loop used with To example above.
| |
Exiting Loops and Scripts#
The for and while loops will continue to execute until the conditions are no longer met. However, sometimes you need to exit the loop midway. PowerLanguage provides the break and continue instructions to exit loops midway, and the #return instruction to exit the script.
break#
When the program executes to break, it exits and ends the entire loop.
Syntax#
| |
Example#
for loop with break: Iterate dayCount from 0 to 6. When dayCount equals 2, the entire loop ends. At this point, the value of total is 2.
| |
while loop with break: Equivalent to the for loop example above.
| |
continue#
When the program executes to continue, it skips the remaining instructions in the current iteration and proceeds to the next iteration. continue is different from break; continue only skips the current iteration and does not end the entire loop.
Syntax#
| |
Example#
for loop with continue: Iterate dayCount from 0 to 6. When dayCount equals 2, the accumulation of total is skipped. The final value of total is 6.
| |
When using
continuein awhileloop, pay special attention: if the counter increment instruction is aftercontinue,continuewill skip the increment, which may cause an infinite loop. The solution is to increment the counter beforecontinue.
while loop with continue: When dayCount equals 2, increment dayCount first and then execute continue to avoid an infinite loop. This is equivalent to the for loop example above. The final value of total is 6.
| |
#return#
The #return instruction is similar to break, but #return ends the current script execution, not just the loop.
When the program executes to #return, the current script execution ends, and according to the script execution mechanism, the script is re-executed when the next execution condition is met (Ex: bar completion).
Syntax#
| |
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