Sidechair

array.mode()

Browse the reference

Returns the number that occurs most often in an array.

Try it in the editor

Items are matched exactly, so it suits whole numbers and rounded prices. When two or more values occur equally often, the smallest of them is returned.

na items are skipped. An array without numbers returns na.

Syntax

Pine Script
array.mode(id: array<int/float>) -> series float
array.mode(id: array<int>) -> series int
array.mode() -> series float
array.mode() -> series int

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to count. na items are skipped; a tie goes to the smaller value.

Returns

series float or series int: The most frequent value, or na.

Example

Pine ScriptMost common rounded closeOpen in editor
//@version=6
indicator("array.mode: common level", overlay = true)
var seen = array.new_float()
array.push(seen, math.round(close))
if array.size(seen) > 60
    array.shift(seen)
plot(array.mode(seen), "Most visited whole price")

See also

Runs on Sidechair