Instructions/probuilder · probacktest · proorder · proscreener

NEXT

NEXT closes a FOR loop block in ProBuilder, sending execution back to the loop header until the counter passes its end value. Every FOR requires one NEXT.

Syntax

probuilder
FOR variable = start TO end DO
  // statements executed on each iteration
NEXT

How it works

A FOR loop in ProBuilder consists of a header, a body, and the closing keyword NEXT. The header assigns a start value to the counter variable and declares the end value with TO for ascending loops or DOWNTO for descending ones. Everything between DO and NEXT forms the body.

Each time execution reaches NEXT, the counter is incremented (or decremented with DOWNTO) and compared against the end value. When the counter is still inside the range, execution jumps back to the top of the body. When it has passed the end value, execution continues with the first statement after NEXT. Both boundaries are inclusive, so FOR i = 10 TO 20 runs eleven iterations.

NEXT is purely structural, it takes no arguments and produces no value. Every FOR must be closed by exactly one NEXT, and nested loops require one NEXT per FOR, closing from the innermost loop outward. To leave a loop before the counter is exhausted, use BREAK, which transfers control to the statement following the matching NEXT.

Since the whole script already runs once per bar, loops delimited by NEXT are typically used for calculations across a range of periods or array indices within a single bar, not for stepping through time.

Examples

Example 1, Summing averages over a range of periods (Indicator)

probuilder
a = 0
FOR i = 10 TO 20 DO
  // accumulate the moving average of the open for each period length
  a = average[i](open) + a
NEXT
RETURN a AS "compound averages direction"

The loop runs with i at 10 through 20. NEXT increments the counter and repeats the body, and after the final iteration the accumulated sum is returned.

Example 2, Counting bullish bars in a window (ProOrder)

probuilder
// Count how many of the last 10 bars closed up
bullCount = 0
FOR k = 0 TO 9 DO
  IF close[k] > open[k] THEN
    bullCount = bullCount + 1
  ENDIF
NEXT

IF bullCount >= 7 AND NOT onmarket THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

The body between DO and NEXT inspects one historical bar per iteration. After NEXT completes the tenth pass, the count feeds the entry condition.

Example 3, Averaging a range of closes (ProScreener)

probuilder
// Mean close of the last 15 bars, computed manually
total = 0
FOR n = 0 TO 14 DO
  total = total + close[n]
NEXT
meanClose = total / 15

SCREENER[close > meanClose * 1.02] (meanClose AS "15-bar mean")

NEXT closes the accumulation loop, after which the mean is compared against the current close to filter instruments.

Common errors and gotchas

  • Missing or extra NEXT. Every FOR needs exactly one NEXT. In nested loops the compiler pairs each NEXT with the nearest open FOR, so a missing one often produces an error several lines away from the real mistake.
  • Modifying the counter inside the body. Reassigning the loop variable between DO and NEXT changes how many iterations remain and can create loops that never terminate within the bar. Prefer BREAK for early exit.
  • Wrong direction keyword. FOR i = 20 TO 10 never enters the body because the counter already exceeds the end value. Descending ranges require DOWNTO instead of TO.
  • Heavy loops on every bar. The entire FOR...NEXT block runs once per bar. Large ranges combined with expensive calls such as average[i] multiply calculation time across the whole chart.
  • FOR, opens the counted loop that NEXT closes.
  • TO, declares the ascending end value of the loop counter.
  • DOWNTO, declares a descending loop range.
  • DO, separates the loop header from the body.
  • BREAK, exits the loop before the counter is exhausted.
  • WHILE, condition-driven loop alternative.
  • WEND, closes a WHILE loop, the counterpart of NEXT.