ELSE
ELSE in ProBuilder runs an alternative block of code when the preceding IF condition is false. Each IF statement accepts a single ELSE branch before ENDIF.
Syntax
IF (condition) THEN
// statements when the condition is true
ELSE
// statements when the condition is false
ENDIFHow it works
An IF statement on its own either runs its block or does nothing. Adding ELSE turns it into a two-way branch: exactly one of the two blocks executes on every bar, never both and never neither. Execution then continues after ENDIF regardless of which branch ran.
ELSE cannot stand alone. It must appear inside an IF block, after the THEN block and any ELSIF branches, and before ENDIF. Each IF statement accepts at most one ELSE, and it must be the final branch. When several conditions need to be tested in order, chain them with ELSIF and reserve ELSE for the case where none of them matched.
Because one branch always runs, an IF/ELSE pair is the idiomatic way to guarantee that a variable receives a value on every bar, avoiding stale values carried over from previous bars.
Examples
Example 1, Classifying the current bar (Indicator)
// compare the current high with the previous bar's high
IF (High > High[1]) THEN
result = 1
ELSE
result = -1
ENDIF
RETURN resultWhen the current high exceeds the previous high, result is 1, in every other case the ELSE branch sets it to -1. The variable is guaranteed a fresh value on each bar.
Example 2, Scoring above or below a moving average (ProScreener)
ma = Average[200](close)
// score the instrument by its position relative to the average
IF close > ma THEN
score = (close - ma) / ma * 100
ELSE
score = 0
ENDIF
SCREENER[score > 5] (score AS "Percent above MA")Instruments trading above the 200-period average get a percentage score, the ELSE branch zeroes out everything else so the screener condition stays clean.
Example 3, Choosing a stop distance by regime (ProOrder)
DEFPARAM CumulateOrders = false
atr = AverageTrueRange[14](close)
// widen the stop in volatile conditions, tighten it otherwise
IF atr > Average[100](atr) THEN
stopDist = 3 * atr
ELSE
stopDist = 1.5 * atr
ENDIF
IF close CROSSES OVER Average[50](close) AND NOT onmarket THEN
BUY 1 CONTRACT AT MARKET
SET STOP pLOSS stopDist
ENDIFThe ELSE branch supplies the default stop distance whenever the volatility test fails, so stopDist is defined before the entry logic uses it.
Common errors and gotchas
- ELSE without a matching IF. ELSE is only valid between an IF ... THEN block and its ENDIF. A stray ELSE, or one separated from its IF by a misplaced ENDIF, fails to compile.
- Multiple ELSE branches. Each IF statement accepts one ELSE at most. Additional mutually exclusive cases belong in ELSIF branches placed before the ELSE.
- ELSE with its own condition. ELSE takes no condition and no THEN. Writing
ELSE (condition) THENis a syntax error, the conditional form is ELSIF. - Wrong branch pairing in nested blocks. In nested IF statements, ELSE binds to the nearest unclosed IF. Missing an inner ENDIF silently attaches the ELSE to the wrong condition, which compiles but branches incorrectly.
Related instructions
IF, opens the conditional block that ELSE extends.ELSIF, conditional alternative branches tested before ELSE.ENDIF, closes the IF/ELSE block.THEN, required after IF and ELSIF conditions, never after ELSE.AND, combines conditions inside the IF test.OR, alternative conditions inside the IF test.NOT, negates a condition.
