StatisticalPine Script v6

ta.percentile_nearest_rank()Percentile (Nearest Rank)

Calculates the percentile using the nearest rank method.

Syntax

Syntax
ta.percentile_nearest_rank(source, length, percentage) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries of values to process.
lengthseries intNumber of bars in the rolling window.
percentagesimple int/floatTarget percentile in the 0-100 range.

Returns

P-th percentile of source for length bars back using nearest rank method.

Remarks

na values in the source series are ignored.

Code Examples

Pine Script v6 Example
//@version=6
indicator("percentile_nearest_rank")
plot(ta.percentile_nearest_rank(high, 50, 90), title="90th pct high")

Trading Applications

Calculate percentile levels using nearest rank method

Build percentile-based channel indicators

Identify extreme price levels statistically

Create robust outlier detection systems

Frequently Asked Questions

It returns a value from the window that corresponds to the requested percentile using the nearest-rank rule, so the output is always an actual observed level in the sample (after na handling).

Nearest rank snaps to an order statistic; linear interpolation can return values between two sorted observations. Nearest rank is stepwise; interpolation is smoother.

Use nearest rank when you want band levels tied to real prints in the window; use linear interpolation when you want smoother, more continuous percentile estimates.

Generate ta.percentile_nearest_rank() Code with AI

Skip the manual coding. Use Pineify's AI Coding Agent to generate Pine Script code using ta.percentile_nearest_rank() and other built-in functions instantly.