Sidechair

array.max()

Browse the reference

Returns the largest number in an array, or the nth largest when you pass nth.

Try it in the editor

array.max(a) is the biggest value. array.max(a, 1) is the second biggest, array.max(a, 2) the third, counting from 0. Duplicates count separately, so two equal maxima give the same value for nth 0 and nth 1.

na items are skipped. If the array has no numbers, or nth is past the last ranked value, the result is na.

Syntax

Pine Script
array.max(id: array<int/float>, nth: series int) -> series float
array.max(id: array<int>, nth: series int) -> series int
array.max(nth: series int) -> series float
array.max(nth: series int) -> series int

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to search. na items are skipped.
nthseries int0Which value to return when the numbers are ranked, starting at 0 for the first.

Returns

series float or series int: The nth largest number, or na.

Example

Pine ScriptHighest and runner-up closeOpen in editor
//@version=6
indicator("array.max: top two", overlay = true)
var win = array.new_float()
array.push(win, close)
if array.size(win) > 20
    array.shift(win)
plot(array.max(win), "Highest", color.green)
plot(array.max(win, 1), "Second highest", color.lime)

See also

Runs on Sidechair