BREAK
BREAK exits a FOR or WHILE loop immediately in ProBuilder. Use it to stop iterating once a condition is met and skip the remaining passes through the loop.
Syntax
BREAKHow it works
When execution reaches a BREAK statement inside a loop, the loop ends at once. No further iterations run, the remainder of the current iteration is skipped, and control resumes at the first statement after the loop's closing keyword, NEXT for a FOR loop or WEND for a WHILE loop.
BREAK is almost always wrapped in an IF block, since an unconditional BREAK would make the loop body run at most once. The pattern is: iterate, test a stop condition, and bail out early when continuing would be pointless. This matters for performance in scripts that scan backwards over many bars, because the scan can stop at the first bar that answers the question instead of always walking the full range.
With nested loops, BREAK only exits the innermost loop containing it. Leaving an outer loop as well requires a flag variable that the outer loop checks, or a restructuring of the logic.
BREAK is only meaningful inside FOR and WHILE loops. Placing it anywhere else, for example directly inside a bare IF block at the top level of a script, is a compilation error.
Examples
Example 1, Counting a streak of down bars (Indicator)
Value = 0
FOR i = 1 TO 20 DO
IF (Close[i] < Open[i]) THEN
Value = Value + 1
ELSE
BREAK // stop at the first bar that is not a down bar
ENDIF
NEXT
RETURN ValueThe loop counts consecutive bars that closed below their open, scanning back up to 20 bars. The first bar that fails the test triggers BREAK, so the count reflects an unbroken streak rather than a total.
Example 2, Finding the most recent swing high (ProOrder)
DEFPARAM CumulateOrders = false
swingHigh = 0
FOR i = 2 TO 100 DO
// A local swing high: higher than its two neighbours
IF high[i] > high[i + 1] AND high[i] > high[i - 1] THEN
swingHigh = high[i]
BREAK // nearest swing found, no need to scan older bars
ENDIF
NEXT
// Enter long on a break of that level
IF swingHigh > 0 AND close CROSSES OVER swingHigh THEN
BUY 1 CONTRACT AT MARKET
ENDIF
SET STOP %LOSS 2The backward scan stops at the first qualifying swing high. Without BREAK the loop would continue and overwrite the result with older, less relevant swings.
Example 3, Limiting a search with WHILE (ProScreener)
// Walk back until a bar with double the average volume is found
avgVol = Average[20](volume)
i = 1
found = 0
WHILE i <= 50 DO
IF volume[i] > 2 * avgVol THEN
found = i
BREAK // record the distance and stop searching
ENDIF
i = i + 1
WEND
// Keep instruments with a volume spike in the last 10 bars
SCREENER[found > 0 AND found <= 10] (found AS "Bars since spike")The WHILE loop searches up to 50 bars back for a volume spike, and BREAK ends the search as soon as one is found.
Common errors and gotchas
- Only valid inside loops. BREAK outside a FOR or WHILE body does not compile. It cannot be used to skip the rest of a bar's calculation; restructure with IF blocks instead.
- Only the innermost loop exits. In nested loops, BREAK leaves the loop immediately surrounding it. Exiting multiple levels requires a flag checked by the outer loop.
- State after the loop. Variables keep whatever values they had when BREAK fired, and the FOR counter does not reach its end value. Do not assume the counter equals the loop bound after an early exit.
- Readability cost. Several BREAK statements scattered through one loop make control flow hard to follow. Prefer a single, clearly commented exit condition.
Related instructions
FOR, counted loop that BREAK can exit early.WHILE, conditional loop that BREAK can exit early.DO, optional keyword opening a loop body.NEXT, closes a FOR loop; execution resumes after it following a BREAK.TO, ascending range specifier in FOR loops.DOWNTO, descending range specifier in FOR loops.WEND, closes a WHILE loop; execution resumes after it following a BREAK.IF, conditional block that normally wraps a BREAK.
