LastSet
LastSet returns the highest index of a ProBuilder array that has been assigned a value, or -1 when no element is set. Useful for sparsely filled arrays.
Syntax
lastIndex = lastset($array)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$array | array | required | The array variable to inspect. Array names always start with $. |
How it works
Arrays in ProBuilder are sparse. Assigning a value to $prices[7] does not create or initialize elements 0 through 6, it only marks index 7 as set. LastSet scans the array and returns the largest index that has ever been assigned, regardless of any gaps below it.
When the array has never been written to, or has been cleared with Unset, LastSet returns -1. That sentinel value distinguishes an empty array from an array whose element 0 holds a value.
The most common pattern is treating LastSet as an effective length counter. When an array is filled sequentially from index 0, lastset($array) + 1 equals the number of stored elements, and $array[lastset($array)] reads the most recently appended value. Together with IsSet, which tests a single index, LastSet allows safe iteration over arrays whose size is not known in advance.
Because the value reflects assignments made on earlier bars as well, LastSet is also useful for detecting whether an accumulation buffer has been populated at all before reading from it.
Examples
Example 1, Finding the last assigned index (Indicator)
// Assign values at two non-contiguous indices
$myArray[3] = 50
$myArray[7] = 100
lastIndex = lastset($myArray) // returns 7
RETURN lastIndexOnly indices 3 and 7 are set. LastSet ignores the gap and returns 7, the highest assigned position.
Example 2, Reading the most recent stored entry price (ProBacktest)
// Append each entry price to an array
IF longtriggered THEN
$entries[lastset($entries) + 1] = tradeprice
ENDIF
// Exit when price falls 2% below the latest recorded entry
IF lastset($entries) >= 0 THEN
IF close < $entries[lastset($entries)] * 0.98 THEN
SELL AT MARKET
ENDIF
ENDIF
IF close crosses over average[50](close) THEN
BUY 1 CONTRACT AT MARKET
ENDIFThe array grows by one slot per entry. LastSet locates the last appended price, and the guard against -1 prevents reading from an empty array.
Example 3, Averaging only the values collected so far (ProScreener)
// Store the close of every bullish bar
IF close > open THEN
$bullCloses[lastset($bullCloses) + 1] = close
ENDIF
// Average the collected values once at least five exist
count = lastset($bullCloses) + 1
avgBull = 0
IF count >= 5 THEN
FOR i = 0 TO lastset($bullCloses) DO
avgBull = avgBull + $bullCloses[i]
NEXT
avgBull = avgBull / count
ENDIF
SCREENER[close > avgBull AND count >= 5] (avgBull AS "Avg bull close")LastSet supplies both the append position and the loop bound, so the code never iterates past the filled portion of the array.
Common errors and gotchas
- Index, not count. LastSet returns a position, so an array filled from index 0 holds
lastset($array) + 1elements. Forgetting the offset produces off-by-one loop bounds. - -1 used as an index. On an empty array the result is -1, and
$array[lastset($array)]then reads a negative index. Test forlastset($array) >= 0before any read. - Gaps are invisible. LastSet only reports the highest assigned index. Elements below it may still be unassigned, so a loop from 0 to LastSet can read slots that were never written. Use
IsSeton each index when gaps are possible. - Cleared by Unset. After
Unset($array)the function returns -1 again, even though the array variable itself still exists and can be refilled.
Related instructions
IsSet, tests whether one specific array index holds a value.Unset, clears all stored values from an array.ArrayMax, returns the largest value stored in an array.ArrayMin, returns the smallest value stored in an array.ArraySort, sorts array contents in ascending or descending order.BarIndex, zero-based position of the current bar, often used as an array index.ONCE, initializes a variable on the first bar only.
