Instructions/probuilder · probacktest · proorder · proscreener

IF

IF in ProBuilder runs a block of code only when its boolean condition is true. The foundation of conditional logic in every ProRealTime program.

Syntax

probuilder
IF (condition) THEN
  // code to execute when condition is true
ENDIF

With optional branches:

probuilder
IF (condition1) THEN
  // runs if condition1 is true
ELSIF (condition2) THEN
  // runs if condition2 is true
ELSE
  // runs if neither is true
ENDIF

How it works

ProBuilder evaluates the expression in parentheses on every bar of the chart. When the result is true, the code between THEN and ENDIF executes. When the result is false, the block is skipped entirely and execution continues with whatever follows ENDIF.

Conditions can be simple comparisons such as a < b or close > open, or compound expressions built with the logical operators AND, OR, NOT, and XOR. Parentheses control precedence the way they do in mathematics.

IF blocks may be nested arbitrarily deep, but readability degrades quickly past two or three levels. Prefer flat structures with combined conditions, or refactor deep nesting into named intermediate variables.

Examples

Example 1, Basic numeric comparison (Indicator)

probuilder
a = 10
b = 20
// Compute c only when a is smaller
IF (a < b) THEN
  c = a + b
ENDIF
RETURN c

The IF block executes because 10 is less than 20, so c becomes 30 and that value is returned.

Example 2, Compound condition for a screener (ProScreener)

probuilder
myRSI = RSI[14](close)
myATR = AverageTrueRange[14](close)
IF (myRSI < 30) AND (myATR > 5) THEN
  screener[myRSI] = 1
ENDIF

The screener fires only when RSI is oversold AND the ATR confirms enough volatility, a common quality filter to suppress signals in dead markets.

Example 3, Three-state regime detection (ProOrder)

probuilder
// Classify the current trend as up, down, or flat
fastMA = Average[20](close)
slowMA = Average[50](close)

IF (fastMA > slowMA) THEN
  regime = 1
ELSIF (fastMA < slowMA) THEN
  regime = -1
ELSE
  regime = 0
ENDIF

ELSIF allows multi-way branching without nested IFs. The variable regime then carries the signal forward through the rest of the strategy.

Common errors and gotchas

  • Missing ENDIF. Every IF needs a matching ENDIF. The compiler often reports the error several lines below the actual mistake. When debugging, count opening and closing pairs from the top of the script down.
  • Equality with =. ProBuilder uses a single = for both assignment and comparison. Inside an IF condition = acts as comparison. Outside it assigns. Mixing both meanings on one line is a frequent source of confusing bugs.
  • Forgetting THEN. The keyword THEN is mandatory between the condition and the block body. IF (a < b) c = a + b ENDIF produces a syntax error.
  • Conditions that return non-boolean values. Mathematical expressions such as IF (a + b) are invalid. The condition must reduce to true or false.
  • ELSE, fallback branch when no IF or ELSIF condition matches.
  • ELSIF, chained alternative conditions inside the same block.
  • ENDIF, closes an IF block.
  • THEN, required between the condition and the block body.
  • AND, logical conjunction inside compound conditions.
  • OR, logical disjunction inside compound conditions.
  • NOT, logical negation.
  • WHILE, loop that runs while a condition holds.