Instructions/probuilder · probacktest · proorder · proscreener

ArrayMax

ArrayMax returns the largest value stored in a ProBuilder array. Use it to extract the maximum from collected data points in indicators and strategies.

Syntax

probuilder
result = ArrayMax($MyArray)

Parameters

NameTypeDefaultDescription
$MyArrayarrayrequiredThe array variable to scan. Array names always start with $.

How it works

ArrayMax examines every element that has been assigned in the array and returns the single largest value. It replaces the common pattern of looping over indices and comparing each element against a running maximum, which keeps scripts shorter and less error-prone.

The function operates on the array as it exists at the moment of the call. Arrays in ProBuilder persist from bar to bar, so an array that receives one new element per bar accumulates history, and ArrayMax reflects everything stored so far, not just the current bar. To restrict the result to a rolling window, either overwrite a fixed set of indices or clear the array with Unset before repopulating it.

If the array contains no elements at all, ArrayMax returns an undefined value. Guarding the call with IsSet or checking LastSet avoids feeding an undefined result into later calculations.

ArrayMax differs from Highest, which works on price series over a fixed number of bars, and from MAX, which compares exactly two scalar values. ArrayMax is the only one of the three that operates on an arbitrary user-filled collection.

Examples

Example 1, Highest of five stored prices (Indicator)

probuilder
// Store five closing prices in an array
$prices[1] = 130
$prices[2] = 135
$prices[3] = 128
$prices[4] = 140
$prices[5] = 138

// Extract the largest stored value
highestPrice = ArrayMax($prices)
RETURN highestPrice

The array holds five values and ArrayMax returns 140, the largest of them. This is the canonical usage: populate, then query.

Example 2, Breakout level from recorded swing highs (ProOrder)

probuilder
DEFPARAM CumulateOrders = false

// Record the high of each bar where RSI was above 60
IF RSI[14](close) > 60 THEN
  $swingHighs[LastSet($swingHighs) + 1] = high
ENDIF

// Enter long on a break above the strongest recorded high
IF IsSet($swingHighs[1]) THEN
  breakoutLevel = ArrayMax($swingHighs)
  IF close CROSSES OVER breakoutLevel THEN
    BUY 1 CONTRACT AT MARKET
  ENDIF
ENDIF

SET STOP %LOSS 2

The strategy appends a high to the array whenever momentum is strong, then uses ArrayMax to derive a breakout threshold from all recorded highs.

Example 3, Volume spike filter (ProScreener)

probuilder
// Collect the volume of the last 20 bars into an array
FOR i = 1 TO 20 DO
  $vols[i] = volume[i]
NEXT

// Flag instruments whose current volume exceeds every stored value
maxVol = ArrayMax($vols)
SCREENER[volume > maxVol] (volume AS "Volume")

The screener stores the previous 20 volume readings, then keeps only instruments where the current bar's volume tops the ArrayMax of that window.

Common errors and gotchas

  • Argument must be an array. Passing a plain variable, a price series, or an indicator result produces a compilation error. Only variables prefixed with $ and indexed with brackets qualify.
  • Empty array returns undefined. Calling ArrayMax before any element has been assigned yields an undefined value that silently propagates through arithmetic. Check IsSet($array[index]) or LastSet first.
  • Arrays accumulate across bars. An array appended to on every bar grows for the life of the chart, so ArrayMax covers the entire accumulated history. For a rolling maximum, reuse a fixed index range or clear the array with Unset.
  • Not a substitute for Highest. For the maximum of a built-in series over the last n bars, Highest[n](series) is simpler and faster than copying values into an array.
  • ArrayMin, returns the smallest value stored in an array.
  • ArraySort, orders array elements in ascending or descending order.
  • IsSet, tests whether a given array element has been assigned.
  • LastSet, returns the highest index assigned in an array.
  • Unset, clears an array or a single element.
  • Highest, highest value of a series over a fixed number of bars.
  • MAX, larger of two scalar values.
  • BarIndex, position of the current bar in the loaded data.