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
IF (condition) THEN
// code to execute when condition is true
ENDIFWith optional branches:
IF (condition1) THEN
// runs if condition1 is true
ELSIF (condition2) THEN
// runs if condition2 is true
ELSE
// runs if neither is true
ENDIFHow 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)
a = 10
b = 20
// Compute c only when a is smaller
IF (a < b) THEN
c = a + b
ENDIF
RETURN cThe 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)
myRSI = RSI[14](close)
myATR = AverageTrueRange[14](close)
IF (myRSI < 30) AND (myATR > 5) THEN
screener[myRSI] = 1
ENDIFThe 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)
// 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
ENDIFELSIF 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 ENDIFproduces 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.
Related instructions
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.
