Sidechair

ta.percentile_nearest_rank()

Browse the reference

Returns the value at the given percentile of the last length bars using the nearest-rank method, always an actual value from the window.

Try it in the editor

ta.percentile_nearest_rank sorts the window and picks the value at rank ceil(percentage / 100 * length). A percentage of 0 gives the smallest value and 100 the largest, and the result is always one of the window's own values.

Output is na until the window holds length values.

Syntax

Pine Script
ta.percentile_nearest_rank(source: series int/float, length: series int, percentage: simple int/float) -> series float

Arguments

NameTypeDefaultDescription
sourceseries int/floatrequiredThe series whose last length values are ranked; the result is one of them.
lengthseries intrequiredNumber of bars in the window. A whole number of 1 or more.
percentagesimple int/floatrequiredPercentile to find, from 0 to 100.

Returns

series float: the window value at the requested percentile, or na while the window is not full.

Remarks

  • Percentile 50 is close to ta.median but never averages two values.

Example

Pine ScriptRolling 90th percentileOpen in editor
//@version=6
indicator("90th percentile", overlay = true)
plot(ta.percentile_nearest_rank(high, 50, 90), "P90 of highs", color = color.red)
plot(ta.percentile_nearest_rank(low, 50, 10), "P10 of lows", color = color.green)

See also

Runs on Sidechair