# Percentile Nearest Rank Using Arrays

Percentile Nearest Rank Using Arrays is an indicator on [Vela](https://vela.luxalgo.com/chart), our next-generation charting platform, written in Pine Script®.

A rolling percentile engine using the nearest-rank method: sort the last N values and read the level below which a chosen percentage of them sit. One Percentage input turns the same engine into a rolling median, quartile bands, or the rolling extremes behind classic channel tools. A statistical building block for quantitatively minded traders.

- Indicator in the LuxAlgo Library, by LuxAlgo, published Sep 10, 2020.
- Access: free.
- Concepts: [Percentile Rank](https://www.luxalgo.com/library/concept/percentile-rank/) (primary), [Order-statistic Filters](https://www.luxalgo.com/library/concept/order-statistic-filters/)
- Open on Vela: https://vela.luxalgo.com/chart?pineScriptCodeId=cmpwn4gva0068vhsglcqu2wvl
- Page: https://www.luxalgo.com/library/indicator/percentile-nearest-rank-using-arrays/

**Percentile Nearest Rank Using Arrays** computes a rolling [percentile rank](https://www.luxalgo.com/library/concept/percentile-rank/) the direct way: gather the last *Length* values, sort them, and return the value below which *Percentage*% of the window sits. Created with Alex Grover, it is less a signal generator than a statistical primitive from the [order-statistic filters](https://www.luxalgo.com/library/concept/order-statistic-filters/) family: one input turns the same engine into a rolling median, a quartile envelope, or a rolling extreme.

## How to Trade the Percentile Nearest Rank Using Arrays?

- **Percentage 50**: plots the rolling median, a robust read of trend direction that a single outlier can't drag around.
- **Percentage 100**: returns the rolling maximum. For the rolling minimum, pick a percentage that rounds to the first rank (4 to 9 at the default Length of 15), because 0 wraps around to the maximum. These extremes are the statistics underneath stochastic normalization and Donchian-style channels.
- **Percentage 75 / 25**: the upper and lower rolling quartiles, bracketing the middle half of recent values.

Run two instances to frame a channel. The original release demonstrates a 75/25 interquartile band computed over a 100-bar window.

## Percentile Nearest Rank Using Arrays Settings

- **Length**: the rolling window: how many past data points are ranked.
- **Percentage**: the 0-100 threshold; the output is the value with Percentage% of the window below it.
- **Src**: the series the percentile is computed on.

## Frequently Asked Questions

### How exactly is the percentile computed?
The most recent Length values are sorted in ascending order and the element at index round(Percentage / 100 × Length) − 1 is returned, the subtraction adjusting for zero-based array indexing. That is the nearest-rank convention: the output is always an actual observed value, never an interpolation.

### Which classic tools can it reproduce?
Percentage 100 yields the rolling maximum and a percentage that rounds to the first rank (4 to 9 at the default Length of 15) yields the rolling minimum, the extremes that drive Donchian channels and the [Stochastic](https://www.luxalgo.com/library/indicator/stochastic/), while 50 gives the rolling median. That makes it a compact base layer for building or sanity-checking channel and normalization logic.

### How do I access this indicator?
This indicator is free in the LuxAlgo Library. From this page you can also drop it into Quant, our coding agent, and experiment with windows and thresholds. You can also copy the source code from this page and run it anywhere Pine Script® runs.

## Related indicators

- [Reversal Probability Zone & Levels](https://www.luxalgo.com/library/indicator/reversal-probability-zone-levels/) (Percentile Rank): markdown at https://www.luxalgo.com/library/indicator/reversal-probability-zone-levels.md
- [Consecutive Candle Streak Analysis](https://www.luxalgo.com/library/indicator/consecutive-candle-streak-analysis/) (Distribution-of-returns Profiling): markdown at https://www.luxalgo.com/library/indicator/consecutive-candle-streak-analysis.md
- [Probability Grid](https://www.luxalgo.com/library/indicator/probability-grid/) (Distribution-of-returns Profiling): markdown at https://www.luxalgo.com/library/indicator/probability-grid.md
- [Change-Point Detection (CUSUM)](https://www.luxalgo.com/library/indicator/change-point-detection-cusum/) (Outlier Detection): markdown at https://www.luxalgo.com/library/indicator/change-point-detection-cusum.md
- [Standardized Cumulative Deltas](https://www.luxalgo.com/library/indicator/standardized-cumulative-deltas/) (Z-score): markdown at https://www.luxalgo.com/library/indicator/standardized-cumulative-deltas.md
- [Sigmoid Transition Trailing Stop](https://www.luxalgo.com/library/indicator/sigmoid-transition-trailing-stop/) (Sigmoid/softmax Transforms): markdown at https://www.luxalgo.com/library/indicator/sigmoid-transition-trailing-stop.md
- [ATR Exceedance Probability Model](https://www.luxalgo.com/library/indicator/atr-exceedance-probability-model/) (Distribution-of-returns Profiling): markdown at https://www.luxalgo.com/library/indicator/atr-exceedance-probability-model.md
- [Day Of Week Price Distribution](https://www.luxalgo.com/library/indicator/day-of-week-price-distribution/) (Distribution-of-returns Profiling): markdown at https://www.luxalgo.com/library/indicator/day-of-week-price-distribution.md

---

Source: https://www.luxalgo.com/library/indicator/percentile-nearest-rank-using-arrays/ (LuxAlgo Library). Free to use with attribution: https://www.luxalgo.com/library/license/
