Instructions/probuilder · probacktest · proorder · proscreener

DOWNTO

DOWNTO in ProBuilder makes a FOR loop count downward from a higher start value to a lower end value, decrementing by one on each pass through the loop.

Syntax

probuilder
FOR variable = startValue DOWNTO endValue DO
  // loop body
NEXT

How it works

A FOR loop with DOWNTO starts the counter at startValue and decrements it by one after each pass, running the body for every value down to and including endValue. It is the descending counterpart of TO, which increments.

Direction matters whenever iteration order affects the result. Bar offsets in ProBuilder grow with age, so close[20] is older than close[0]. A loop written FOR i = 20 DOWNTO 0 therefore visits bars from oldest to newest, which is the natural order for calculations that build up state chronologically, such as tracking the most recent occurrence of an event or replaying a sequence of bars in time order.

The start value must be greater than or equal to the end value. If startValue is smaller than endValue, the body never executes and the script continues after NEXT without any error. BREAK remains available inside the body to abandon the loop early.

Examples

Example 1, Counting down bars from oldest to newest (Indicator)

probuilder
Value = 0
// Scan from 20 bars back toward the current bar
FOR i = 20 DOWNTO 0 DO
  IF (Close[i] < Open[i]) THEN
    Value = Value + 1
  ELSE
    BREAK
  ENDIF
NEXT
RETURN Value

The counter runs from 20 down to 0, so the scan starts at the oldest bar in the window and moves toward the present, stopping at the first bar that closed at or above its open.

Example 2, Offset of the most recent up gap (Indicator)

probuilder
lastGap = -1
// Visit bars oldest first so the final assignment is the newest gap
FOR i = 50 DOWNTO 1 DO
  IF low[i - 1] > high[i] THEN
    lastGap = i - 1
  ENDIF
NEXT
RETURN lastGap AS "bars since last up gap"

Because DOWNTO processes older bars first, each detected gap overwrites the previous one, leaving the offset of the most recent gap in lastGap when the loop ends.

Example 3, Chronological momentum score (ProOrder)

probuilder
score = 0
// Compare each bar with the one before it, oldest pair first
FOR i = 10 DOWNTO 1 DO
  IF close[i] > close[i + 1] THEN
    score = score + 1
  ELSE
    score = score - 1
  ENDIF
NEXT

IF score >= 8 AND NOT ONMARKET THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

The loop walks the last ten bar-to-bar changes in time order and nets rises against falls. A score of 8 or more indicates a persistent advance and triggers a long entry.

Common errors and gotchas

  • Start smaller than end. FOR i = 0 DOWNTO 20 executes zero iterations. No error is raised, so downstream variables silently keep their initial values. Verify the bounds are in descending order.
  • Direction changes the result. Loops that overwrite a variable on match, or that BREAK on a condition, produce different answers with DOWNTO than with TO. Pick the direction that matches the intended chronology.
  • Off-by-one at the edges. Both bounds are inclusive. FOR i = 20 DOWNTO 0 runs 21 times. Referencing close[i + 1] inside the body also reaches one bar beyond the stated window.
  • No custom step size. The counter always moves by exactly one. Skipping every other value requires arithmetic on the counter inside the body rather than a step clause.
  • FOR, the counted loop that DOWNTO modifies.
  • TO, the ascending counterpart of DOWNTO.
  • NEXT, closes the FOR loop and moves the counter.
  • DO, optional marker between the loop header and the body.
  • BREAK, exits the loop before the counter reaches the end value.
  • WHILE, condition-driven alternative when the iteration count is unknown.
  • WEND, closes a WHILE loop.
  • BarIndex, current bar number, often paired with descending scans.