Sidechair

array.median()

Browse the reference

Returns the middle value of the numbers in an array once they are sorted.

Try it in the editor

With an odd count it is the center item. With an even count it is the mean of the two center items. na items are skipped before ranking.

The median ignores outliers that would drag array.avg() away from the typical value, which makes it useful on spiky data such as volume.

Syntax

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

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to take the middle of. na items are skipped.

Returns

series float or series int: The median, or na when the array holds no numbers.

Example

Pine ScriptMedian volume vs mean volumeOpen in editor
//@version=6
indicator("array.median: robust volume", overlay = false)
var vol = array.new_float()
array.push(vol, volume)
if array.size(vol) > 30
    array.shift(vol)
plot(array.median(vol), "Median", color.blue)
plot(array.avg(vol), "Mean", color.orange)

See also

Runs on Sidechair