Instructions/probuilder · probacktest · proorder · proscreener

ENDIF

ENDIF in ProBuilder closes a conditional block opened by IF. Every IF needs a matching ENDIF, including nested blocks and blocks with ELSE or ELSIF branches.

Syntax

probuilder
IF (condition) THEN
  // statements
ELSE
  // statements
ENDIF

How it works

ENDIF marks the point where conditional execution stops and unconditional execution resumes. Everything between IF and ENDIF belongs to the conditional block; everything after ENDIF runs on every bar regardless of the condition. One ENDIF closes one IF, no matter how many ELSIF branches sit in between, and the optional ELSE branch also falls inside the same block.

Nested conditions each need their own ENDIF. The compiler matches each ENDIF to the nearest unclosed IF above it, so the closing order is the reverse of the opening order. Indentation has no syntactic meaning in ProBuilder, but indenting each nesting level makes the pairing visible and is the main defense against mismatched blocks.

A missing or surplus ENDIF is a compile-time error. The reported line number frequently points at the end of the script or at an unrelated statement below the real mistake, because the compiler only notices the imbalance once it runs out of code.

Examples

Example 1, Direction from two moving averages (Indicator)

probuilder
a = average[10](close)
b = exponentialaverage[20](close)
// Compare the two averages and encode the result as +1 or -1
IF (a > b) THEN
  result = 1
ELSE
  result = -1
ENDIF
RETURN result AS "direction"

The block assigns 1 when the simple average sits above the exponential average and -1 otherwise. ENDIF closes the block so the RETURN below always executes.

Example 2, Nested conditions in a screener (ProScreener)

probuilder
match = 0
IF RSI[14](close) < 30 THEN
  // Inner block only runs when the outer condition passed
  IF volume > Average[20](volume) THEN
    match = 1
  ENDIF
ENDIF
SCREENER[match] (RSI[14](close) AS "RSI")

Two IF blocks nest, and each has its own ENDIF. The inner ENDIF closes the volume test, the outer one closes the RSI test.

Example 3, Entry and reversal logic (ProOrder)

probuilder
fast = ExponentialAverage[9](close)
slow = ExponentialAverage[21](close)
// One block handles both directions through ELSIF
IF fast CROSSES OVER slow THEN
  BUY 1 CONTRACT AT MARKET
ELSIF fast CROSSES UNDER slow THEN
  SELLSHORT 1 CONTRACT AT MARKET
ENDIF

A single IF...ELSIF...ENDIF block routes bullish crosses to a long entry and bearish crosses to a short entry. One ENDIF closes the whole structure.

Common errors and gotchas

  • Missing ENDIF. Every IF requires exactly one ENDIF. The compiler often flags a line far below the actual omission, so when the error appears, count IF and ENDIF pairs from the top of the script.
  • One ENDIF per ELSIF chain, not per branch. ELSIF branches do not take their own ENDIF. Writing an ENDIF after each ELSIF closes the block early and usually produces a cascade of errors.
  • Misplaced statements after ENDIF. Code intended to run only when the condition holds must sit above ENDIF. A statement accidentally moved below it executes on every bar, which changes behavior without any compile error.
  • Nesting confusion. With several nested blocks, each ENDIF binds to the nearest open IF. Consistent indentation is the practical way to keep the pairs aligned while editing.
  • IF, opens the conditional block that ENDIF closes.
  • THEN, required between the condition and the block body.
  • ELSE, fallback branch inside an IF block.
  • ELSIF, chained alternative conditions inside the same block.
  • AND, combines conditions in the IF header.
  • OR, accepts any of several conditions in the IF header.
  • NOT, negates a condition.
  • WHILE, loop construct with its own closing keyword, WEND.