Sidechair

array.percentile_nearest_rank()

Browse the reference

Returns the value at a given percentage of the sorted numbers, always one of the items in the array.

Try it in the editor

The numbers are ranked from smallest to largest and the item at position ceil(percentage / 100 x count) is returned. The 50th percentile of five numbers is the third, and 100 returns the largest.

A percentage outside 0 to 100 is clamped to that range. na items are skipped, and an empty set returns na.

Syntax

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

Arguments

NameTypeDefaultDescription
idarray<int/float> or array<int>requiredThe numeric array to rank. na items are skipped.
percentageseries int/floatrequiredA percentage from 0 to 100.

Returns

series float or series int: The item found at that rank.

Remarks

Example

Pine Script90th percentile of recent volumeOpen in editor
//@version=6
indicator("array.percentile_nearest_rank: busy level", overlay = false)
var vol = array.new_float()
array.push(vol, volume)
if array.size(vol) > 50
    array.shift(vol)
plot(volume, "Volume", style = plot.style_columns)
plot(array.percentile_nearest_rank(vol, 90), "90th percentile", color.red)

See also

Runs on Sidechair