Constants/probuilder · probacktest · proorder · proscreener

Undefined

Undefined is a ProBuilder value that clears a variable's default of 0, marking it as not yet set. Assign Undefined once so a variable is not drawn or treated as zero.

Syntax

probuilder
myVariable = Undefined

Undefined is assigned to a variable and takes no index. It can be given to a variable only once, normally at the start of the code.

How it works

Variables in ProBuilder are not explicitly initialised and start at 0. That default can cause trouble when 0 is a meaningful price or the variable should stay hidden until a condition assigns it. Setting the variable to Undefined clears that default so it is neither drawn on the chart nor treated as a real value.

A variable can be set to Undefined only once, and only before it receives a numeric value. After a real number is assigned, it cannot be returned to the undefined state.

There is no way to test directly whether a variable equals Undefined. In boolean and numeric contexts an undefined variable behaves as 0, so the usual pattern is to check whether it still equals 0 to decide if it has been set. Undefined values are also excluded from indicator scaling, which keeps an unset series from distorting the chart's axis.

Examples

Example 1, Hiding a level until it is set (Indicator)

probuilder
// Start undefined so no flat line at 0 is drawn before a signal
myLevel = Undefined

// Capture the close only when price crosses its average
IF Close CROSSES OVER Average[20](Close) THEN
  myLevel = Close
ENDIF

RETURN myLevel AS "Signal level"

Because the variable starts as Undefined, no line appears until the crossover assigns a value.

Example 2, Detecting an unset value (ProScreener)

probuilder
// Undefined behaves as 0, so a 0 test reveals whether it was set
target = Undefined
IF High > High[1] THEN
  target = High
ENDIF

// Keep only instruments where the target was assigned
SCREENER[target > 0] (target AS "Target")

The screener uses the 0 comparison to keep instruments where target received a real value.

Example 3, Optional stop level in a strategy (ProOrder)

probuilder
DEFPARAM CumulateOrders = false

stopLevel = Undefined

IF Close CROSSES OVER Average[20](Close) THEN
  BUY 1 CONTRACT AT MARKET
  stopLevel = Lowest[10](Low)
ENDIF

// Apply the stop only once it has been set
IF LONGONMARKET AND stopLevel > 0 THEN
  SELL AT stopLevel STOP
ENDIF

Starting stopLevel as Undefined keeps it inert until an entry assigns a real stop.

Common errors and gotchas

  • Cannot be tested directly. There is no IF x = Undefined comparison. Since an undefined variable reads as 0, test IF x = 0 to check whether it was set.
  • Assign only once. A variable accepts Undefined a single time, before it holds a number. It cannot be reset to undefined later.
  • Collides with a real zero. If 0 is a valid value for the variable, the 0 test cannot distinguish unset from a genuine zero. Use a separate flag in that case.
  • Close, the closing price of a bar.
  • Open, the opening price of a bar.
  • High, the highest price of a bar.
  • Low, the lowest price of a bar.
  • Decimals, the number of decimal places of the instrument.
  • Range, the high minus low of a bar.
  • Volume, the traded volume of a bar.
  • TypicalPrice, the average of high, low, and close.