DO
DO in ProBuilder marks the start of the loop body in FOR and WHILE statements. It is optional, performs no operation, and exists to make loops readable.
Syntax
FOR variable = start TO end DO
// actions executed on each iteration
NEXTIn a WHILE loop:
WHILE condition DO
// actions executed while the condition is true
WENDHow it works
DO is a structural keyword. It sits between the loop declaration, either FOR variable = start TO end or WHILE condition, and the statements that run on each iteration. The compiler accepts loops with or without it; execution is identical either way. Its purpose is readability, marking exactly where the loop header ends and the body begins.
In a FOR loop, the body between DO and NEXT runs once for each value of the counter, stepping upward with TO or downward with DOWNTO. In a WHILE loop, the body between DO and WEND repeats for as long as the condition evaluates to true, with the condition re-tested before every pass.
Loops execute in full on every bar of the chart or backtest, so a loop that scans hundreds of bars runs that scan again on each new bar. BREAK exits the enclosing loop early, which keeps iteration counts down when the remaining passes cannot change the result.
Examples
Example 1, Counting consecutive down bars (Indicator)
Value = 0
FOR i = 1 TO 20 DO
// Stop counting at the first bar that is not a down bar
IF (Close[i] < Open[i]) THEN
Value = Value + 1
ELSE
BREAK
ENDIF
NEXT
RETURN ValueThe loop walks back through the previous 20 bars and counts how many closed below their open, stopping at the first bar that did not. DO separates the loop header from the counting logic.
Example 2, Consecutive up bars with a WHILE loop (ProScreener)
i = 0
count = 0
// Walk backward while each bar closed above its open
WHILE (close[i] > open[i]) AND (i < 100) DO
count = count + 1
i = i + 1
WEND
SCREENER[count >= 3] (count AS "up bars in a row")The WHILE loop counts the current streak of up bars, capped at 100 iterations, and the screener matches instruments with a streak of three or more.
Example 3, Volatility spike filter (ProBacktest)
total = 0
// Sum the range of the previous 10 completed bars
FOR i = 1 TO 10 DO
total = total + Range[i]
NEXT
avgRange = total / 10
// Enter when the current bar's range doubles the recent average
IF Range > 2 * avgRange AND NOT ONMARKET THEN
BUY 1 CONTRACT AT MARKET
ENDIFA FOR loop with DO accumulates the ranges of the last ten bars to build an average, which then gates the entry condition.
Common errors and gotchas
- Wrong closing keyword. FOR loops end with NEXT and WHILE loops end with WEND. Pairing FOR with WEND, or WHILE with NEXT, is a compile error even when DO is present.
- DO on its own line. DO belongs at the end of the loop header line. Placing it on a separate line, or duplicating it before every statement, does not compile.
- Infinite WHILE loops. DO does not guard against a condition that never becomes false. A WHILE body must change something the condition depends on, or include a hard iteration cap, otherwise the script hangs.
- Assuming DO is required. Omitting DO is legal, so its absence in existing code is not a bug. Mixed styles within one script compile fine but hurt readability.
Related instructions
FOR, counted loop that DO introduces the body of.NEXT, closes a FOR loop and advances the counter.TO, ascending counter direction in a FOR header.DOWNTO, descending counter direction in a FOR header.WHILE, condition-driven loop that also accepts DO.WEND, closes a WHILE loop.BREAK, exits the enclosing loop before it finishes.IF, conditional logic commonly nested inside loop bodies.
