TO
TO sets the end value of a FOR loop counter in ProBuilder, so the loop runs from its start value up to that limit inclusively, one iteration per value.
Syntax
FOR variable = start_value TO end_value DO
// code executed on each iteration
NEXTHow it works
In a FOR loop header, TO separates the counter's start value from its end value. The loop assigns the start value to the counter, executes the body, and each pass through NEXT increments the counter by one. Iteration continues while the counter has not passed the value after TO, and both boundaries are included, so FOR i = 1 TO 20 executes twenty times.
TO always describes an ascending range. When the start value already exceeds the end value, the body never executes at all, the loop is simply skipped. Counting downward requires the companion keyword DOWNTO, which decrements the counter instead.
The end value can be a literal, a variable, or an expression, which makes TO useful for data-dependent ranges such as FOR i = 0 TO lastset($array) or window sizes taken from an optimization variable. The expression is evaluated when the loop starts.
Since scripts already execute once per bar, TO ranges typically sweep across period lengths, historical bar offsets, or array indices within the current bar rather than across time.
Examples
Example 1, Accumulating a ratio over 20 bars (Indicator)
// Sum the close/open ratio of the last 20 bars
total = 0
FOR x = 1 TO 20 DO
total = total + close[x] / open[x]
NEXT
RETURN total AS "close/open sum"TO fixes the upper bound at 20, so the body runs for x = 1 through 20 inclusively, reading one historical bar per iteration.
Example 2, Variable end value from stored data (ProBacktest)
// Record the range of each bar, then average all stored values
$ranges[max(0, lastset($ranges) + 1)] = high - low
sum = 0
n = lastset($ranges)
IF n >= 4 THEN
FOR i = 0 TO n DO
sum = sum + $ranges[i]
NEXT
avgRange = sum / (n + 1)
IF high - low > avgRange * 2 AND NOT onmarket THEN
BUY 1 CONTRACT AT MARKET
ENDIF
ENDIF
SET STOP %LOSS 1.5The value after TO is an expression, so the loop automatically covers exactly the filled portion of the array as it grows.
Example 3, Scanning several lookbacks in one pass (ProScreener)
// Count how many lookback windows show the close above its average
score = 0
FOR p = 10 TO 50 DO
IF close > average[p](close) THEN
score = score + 1
ENDIF
NEXT
SCREENER[score >= 35] (score AS "Trend score")TO ends the sweep at period 50, so the score aggregates 41 different moving average checks per instrument.
Common errors and gotchas
- Inclusive limit. The counter takes the end value itself, so
FOR i = 0 TO 9runs ten iterations, not nine. Off-by-one bounds are the most frequent loop bug. - Descending range with TO.
FOR i = 20 TO 10never enters the loop body because the start already exceeds the limit. Use DOWNTO to count downward. - End value depending on the loop body. The limit after TO is fixed when the loop starts. Changing the variables it was computed from inside the body does not extend or shorten the iteration.
- Large ranges on every bar. The full range set by TO runs once per bar across the whole chart. Wide ranges around costly calls such as
average[p]slow recalculation noticeably.
Related instructions
FOR, opens the counted loop whose range TO defines.DOWNTO, declares a descending range instead of an ascending one.DO, separates the loop header from the loop body.NEXT, closes the loop body and advances the counter.BREAK, exits the loop before the counter reaches the TO limit.WHILE, condition-driven loop for ranges that are not known in advance.
