WHILE
The WHILE loop in ProBuilder repeats a block of code as long as a boolean condition stays true. The condition is tested before each pass, WEND ends the loop.
Syntax
WHILE condition DO
// statements repeated while the condition is true
WENDHow it works
Before each pass, ProBuilder evaluates the condition. If it is true, the block between DO and WEND executes, then control returns to the WHILE line and the condition is tested again. If it is false, execution jumps past WEND. Because the test happens first, a condition that is false on entry means the block never runs at all.
Something inside the loop must eventually make the condition false. Unlike FOR, WHILE has no built-in counter, so the script is responsible for updating whatever the condition depends on. A condition that can never become false produces an infinite loop, which ProRealTime aborts with an execution error rather than letting the platform hang.
The entire loop runs again on every bar of the chart. WHILE suits open-ended scans, such as walking backward through bars until a pattern breaks, where a FOR range would either be wasteful or unknown.
Examples
Example 1, Counting iterations (Indicator)
i = 0
count = 0
WHILE i <> 11 DO
// advance the counter until it reaches 11
i = i + 1
count = count + 1
WEND
RETURN countThe loop runs while i differs from 11. Each pass increments both variables, so the loop exits after 11 iterations and the indicator returns 11.
Example 2, Measuring an up-close streak (ProScreener)
n = 0
// walk backward while each close is above the previous one
WHILE close[n] > close[n + 1] AND n < 100 DO
n = n + 1
WEND
SCREENER[n >= 3] (n AS "Up streak")The loop counts consecutive rising closes, stopping at the first bar that breaks the streak. The n < 100 bound guarantees termination even in an unusually long trend.
Example 3, Sizing a position within a risk budget (ProOrder)
DEFPARAM CumulateOrders = false
maxRisk = 400
size = 1
// double the position size while the risk budget allows it
WHILE (size * 2) * AverageTrueRange[14](close) <= maxRisk DO
size = size * 2
WEND
IF close CROSSES OVER Average[50](close) AND NOT onmarket THEN
BUY size CONTRACT AT MARKET
ENDIFThe loop grows the position size geometrically until one more doubling would exceed the risk budget, then the strategy trades that size.
Common errors and gotchas
- Infinite loops. If nothing inside the block changes the condition, the loop never terminates and ProRealTime stops the script with an error. Add an explicit safety bound, such as a maximum counter value, to any condition driven by market data.
- Zero iterations. The condition is evaluated before the first pass. When it is false on entry, the block is skipped entirely, and any variables assigned only inside the loop keep their prior values.
- Missing WEND. Every WHILE needs a closing WEND. The compiler error may point below the actual loop, so check pairs when the message seems misplaced.
- Equality-based exit conditions. A condition such as
i <> 11never becomes false ifisteps past 11 without landing on it, for example when incrementing by 2. Comparisons with<or>are safer exit tests than<>.
Related instructions
WEND, closes a WHILE block and returns control to the condition.DO, keyword ending a WHILE or FOR header.FOR, counter-driven loop for a known number of passes.NEXT, closes a FOR block.BREAK, exits the current loop immediately.IF, conditional execution without repetition.THEN, required between an IF condition and its block.ENDIF, closes an IF block.
