IsSet
IsSet in ProBuilder returns 1 when a given array element has been assigned a value and 0 when it has not, preventing reads of uninitialized array entries.
Syntax
ISSET($MyArray[index])Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| $MyArray | array | required | The array whose element is being tested. Array names start with $. |
| index | integer | required | The element position to check. |
Formula
ISSET($a[i]) = 1 if element i of $a has been assigned
= 0 otherwiseHow it works
ProBuilder arrays do not need to be declared with a size, and elements can be written at arbitrary indices. That flexibility means an array can contain gaps: index 0 and index 2 may hold values while index 1 was never touched. ISSET reports, per element, whether an assignment has happened.
Reading an element that was never set does not raise an error, it simply yields a default value, which makes bugs of this kind quiet and hard to trace. Guarding reads with ISSET converts the silent default into an explicit branch, so the script can skip the element, substitute a fallback, or search elsewhere.
The check is per index, not per array. Testing ISSET($a[0]) says nothing about $a[1]. An element stays set once assigned, unless it is explicitly cleared with UNSET, after which ISSET returns 0 for it again. LASTSET complements ISSET by returning the highest index that has been assigned, which is useful for iterating over a sparsely filled array.
Examples
Example 1, Testing sparse array elements (Indicator)
// Assign two of four elements
$levels[0] = 10
$levels[2] = 30
// ISSET returns 1 for assigned indices and 0 for the gaps
a = ISSET($levels[0]) // 1
b = ISSET($levels[1]) // 0
c = ISSET($levels[2]) // 1
d = ISSET($levels[3]) // 0
RETURN a + b + c + d AS "elements set"Only indices 0 and 2 were written, so the four checks return 1, 0, 1, 0 and the indicator plots 2, the number of populated elements.
Example 2, Reading a per-bar value only when it exists (Indicator)
// Store the high of each bar that sets a new 20-bar peak
IF high >= Highest[20](high) THEN
$peaks[BarIndex] = high
ENDIF
// Fall back to 0 on bars where no peak was recorded
val = 0
IF ISSET($peaks[BarIndex]) THEN
val = $peaks[BarIndex]
ENDIF
RETURN val AS "latest peak"The array is only written on peak bars, leaving gaps everywhere else. ISSET distinguishes recorded bars from empty ones before the element is read.
Example 3, Guarding stored entry prices (ProBacktest)
// Record each entry price in an array as trades open
IF NOT ONMARKET AND close CROSSES OVER Average[50](close) THEN
BUY 1 CONTRACT AT MARKET
$entryPrices[n] = close
n = n + 1
ENDIF
// Exit 2 percent above the last stored entry, if one exists
IF LONGONMARKET AND ISSET($entryPrices[n - 1]) THEN
IF close > $entryPrices[n - 1] * 1.02 THEN
SELL AT MARKET
ENDIF
ENDIFBefore the exit rule reads the most recent stored entry price, ISSET confirms that the element exists, which protects the first bars where no trade has been recorded yet.
Common errors and gotchas
- Unset reads fail silently. Accessing an element that was never assigned returns a default value instead of an error, so a missing ISSET guard produces wrong numbers rather than a crash.
- Zero is a set value. An element assigned the value 0 still reports ISSET = 1. Testing
$a[i] = 0is not a substitute for ISSET, since it cannot distinguish "assigned zero" from "never assigned". - Per-element scope. ISSET checks one index at a time. Verifying that an array "has data" requires checking the specific indices the code will read, or using LASTSET to find the populated range.
- UNSET clears the flag. After UNSET removes an element, ISSET returns 0 for that index again. Code holding on to an index across bars should re-check before reading.
Related instructions
Unset, clears an array element or an entire array.LastSet, returns the highest index assigned in an array.ArrayMax, largest value stored in an array.ArrayMin, smallest value stored in an array.ArraySort, sorts array contents in place.ONCE, one-time variable initialization on the first bar.BarIndex, current bar number, a common array index.
