Instructions/probuilder · probacktest · proorder · proscreener

THEN

THEN links an IF condition to the block of ProBuilder code that runs when the condition is true. Mandatory in every IF statement, before ELSE or ELSIF.

Syntax

probuilder
IF condition THEN
  // code executed when the condition is true
ENDIF

How it works

THEN is the bridge between a condition and its consequences. The compiler reads everything between IF and THEN as the boolean expression to evaluate, and everything between THEN and the next branch keyword (ELSIF, ELSE, or ENDIF) as the block to execute when that expression is true.

The keyword is not optional. Omitting THEN after an IF condition is a syntax error, and the same applies after every ELSIF condition in a chained statement. ELSE, by contrast, takes no condition and therefore no THEN.

THEN carries no logic of its own, it is a structural marker. Complex conditions built with AND, OR, NOT, and parentheses all sit on the left side of THEN, and the block on the right side can contain any statements valid in the sub-language, including nested IF...THEN blocks. Every IF opened with THEN must eventually be closed with ENDIF.

The keyword also appears in the compact single-concept reading of ProBuilder conditionals: condition, THEN, action, ENDIF. Keeping that order in mind resolves most syntax errors around conditional blocks.

THEN behaves identically across all four sub-languages. Indicator code, strategy code, and screener code all use the same IF...THEN...ENDIF structure, so conditional logic written for one context ports directly to the others as long as the statements inside the block are valid there.

Examples

Example 1, Marking bullish bars (Indicator)

probuilder
// Draw a green up arrow under every bar that closed above its open
IF close > open THEN
  DRAWARROWUP(barindex, low) COLOURED(0, 255, 0)
ENDIF
RETURN

When the condition left of THEN is true, the drawing statement in the block executes and an arrow appears under the bar.

Example 2, Conditional entry with a compound condition (ProOrder)

probuilder
// Enter long when the fast average crosses over the slow one
// and the market is not already long
fastMA = average[20](close)
slowMA = average[100](close)

IF fastMA crosses over slowMA AND NOT longonmarket THEN
  BUY 1 CONTRACT AT MARKET
ENDIF

The whole compound expression sits before THEN. The order statement after THEN runs only on bars where both parts hold.

Example 3, Branching with ELSIF and THEN (ProScreener)

probuilder
// Classify momentum, THEN is required after IF and after ELSIF
mom = close - close[10]

IF mom > 0 THEN
  state = 1
ELSIF mom < 0 THEN
  state = -1
ELSE
  state = 0
ENDIF

SCREENER[state = 1] (mom AS "10-bar momentum")

Each conditional branch that carries a condition ends in THEN. The ELSE branch has no condition and therefore no THEN.

Common errors and gotchas

  • Missing THEN. IF close > open DRAWARROWUP(barindex, low) ENDIF fails to compile. Every IF and every ELSIF condition must be followed by THEN before the block starts.
  • THEN after ELSE. ELSE introduces the fallback block directly and takes neither a condition nor a THEN. Writing ELSE THEN is a syntax error.
  • Unclosed block. A THEN block runs until ELSIF, ELSE, or ENDIF. Forgetting ENDIF makes the compiler swallow following statements into the block and report an error far from the cause.
  • Statements on the condition side. Everything between IF and THEN must reduce to true or false. Assignments or bare arithmetic expressions in that position do not compile.
  • IF, opens the conditional statement that THEN belongs to.
  • ELSE, fallback branch executed when no condition matched.
  • ELSIF, additional condition in the same block, also followed by THEN.
  • ENDIF, closes the conditional statement.
  • AND, combines conditions on the left side of THEN.
  • OR, alternative conditions on the left side of THEN.
  • NOT, negates a condition before THEN.