Sidechair

array.lastindexof()

Browse the reference

Finds the last position holding a value and returns it, or -1 when the value is not in the array.

Try it in the editor

The search starts at the final item and walks backward, so when a value appears more than once the highest index comes back.

A result of -1 means the value is absent; any other result is a usable index for array.get() and array.set().

Syntax

Pine Script
array.lastindexof(id: any array type, value: series <type of the array's elements>) -> series int
array.lastindexof(value: series <type of the array's elements>) -> series int

Arguments

NameTypeDefaultDescription
idany array typerequiredThe array to search from the back for the value.
valueseries <type of the array's elements>requiredThe item to look for. It has to match the type of the array's items.

Returns

series int: The index of the last match, or -1.

Remarks

Example

Pine ScriptBars since the window low last appearedOpen in editor
//@version=6
indicator("array.lastindexof: low age", overlay = false)
var lows = array.new_float()
array.push(lows, low)
if array.size(lows) > 20
    array.shift(lows)
idx = array.lastindexof(lows, array.min(lows))
plot(array.size(lows) - 1 - idx, "Bars since lowest low")

See also

Runs on Sidechair