Instructions/probuilder · probacktest · proorder · proscreener

FOR

The FOR loop in ProBuilder repeats a block of code a fixed number of times, counting up with TO or down with DOWNTO. Every FOR block is closed by NEXT.

Syntax

probuilder
FOR variable = start TO end DO
  // commands repeated on each pass
NEXT

Counting downward:

probuilder
FOR variable = start DOWNTO end DO
  // commands repeated on each pass
NEXT

How it works

On each bar, the loop initializes the counter variable to the start value, executes the block, then increments the counter by 1 (TO) or decrements it by 1 (DOWNTO). When the counter passes the end value, execution continues after NEXT. The DO keyword is optional in current ProRealTime versions but keeping it improves readability.

The counter is a normal variable and is commonly used as a bar offset, so close[i] inside a loop reads the close price i bars back. This makes FOR the standard tool for summing, scanning, or drawing over a range of historical bars.

Loops can be nested. The whole loop runs again on every bar of the chart, so a loop of 100 passes on a 10,000-bar chart executes one million iterations. Keep ranges as small as the calculation allows, and use BREAK to exit early once the result is known.

Examples

Example 1, Sum of opening prices over a bar range (Indicator)

probuilder
a = 0
FOR i = 10 TO 20 DO
  // add the open of each bar in the range to the running total
  a = open[i] + a
NEXT
RETURN a AS "Sum of Opens"

The counter i walks from bar offset 10 to bar offset 20, accumulating each opening price into a, which is returned once the loop finishes.

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

probuilder
count = 0
FOR i = 0 TO 19 DO
  // count the bars that closed above their open
  IF close[i] > open[i] THEN
    count = count + 1
  ENDIF
NEXT
SCREENER[count >= 15] (count AS "Bullish bars")

The loop inspects the last 20 bars and the screener lists instruments where at least 15 of them closed higher than they opened.

Example 3, Backward scan with DOWNTO (ProOrder)

probuilder
// find the most recent bar that matches the 50-bar high
hh = highest[50](high)
found = 50
FOR i = 50 DOWNTO 0 DO
  IF high[i] = hh THEN
    found = i
  ENDIF
NEXT
// enter only if the high was printed within the last 5 bars
IF found <= 5 AND NOT onmarket THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

DOWNTO scans from the oldest offset toward the current bar, leaving found at the most recent offset where the 50-bar high was set.

Common errors and gotchas

  • Missing NEXT. Every FOR needs a matching NEXT. With nested loops the compiler may flag the error far from the loop that is actually unclosed, so match pairs from the inside out.
  • Start beyond end. A FOR i = 10 TO 5 loop never executes, and neither does FOR i = 5 DOWNTO 10. The direction keyword must match the direction of the range, otherwise the block is silently skipped.
  • Modifying the counter inside the loop. Reassigning the loop variable inside the block interferes with the automatic increment and can produce skipped passes or unexpected exits. Use a separate variable for intermediate math.
  • Performance on large ranges. The loop reruns on every bar, so wide ranges multiply quickly. Prefer built-in functions such as summation or highest when one exists, and exit early with BREAK when the answer is found.
  • TO, upward range separator inside a FOR header.
  • DOWNTO, downward range separator inside a FOR header.
  • NEXT, closes a FOR block and triggers the next pass.
  • DO, optional keyword ending a FOR or WHILE header.
  • BREAK, exits the current loop before the range is exhausted.
  • WHILE, condition-driven loop for an unknown number of passes.
  • WEND, closes a WHILE block.
  • BarIndex, bar counter useful for bounding loop ranges.