percentrank
Percent Rank
percentrank(source, length)
Calculates the percentage of values in the lookback period that are less than the current value. This provides a relative ranking of the current value within the historical window, useful for identifying extreme or unusual values.
Parameters
- source (field): The data series to calculate rank on
- Valid inputs: close, open, high, low, volume, or any indicator output
- length (int): Number of periods for the lookback window
- Common values: 14, 20, 100
- Range: 1 to any positive integer
Formula
Percent Rank = (Count of values < current value) / (length - 1) * 100
Where:
- Count includes all values in the window except current
- Result ranges from 0 to 100
- 0 = current value is lowest in window
- 100 = current value is highest in window
- 50 = current value is at median
Examples
percentrank(close, 100); # 100-period percent rank
percentrank(volume, 20); # Volume percentile rank
percentrank(close, 252) > 90; # Price in top 10% of yearly range
percentrank(rsi(close, 14), 50) < 10; # RSI in bottom 10% of recent values
Returns
Percent rank value between 0 and 100
Notes
- Returns 0 when current value is the minimum in the window
- Returns 100 when current value is the maximum in the window
- Returns 50 when current value is at the median
- Useful for identifying overbought/oversold conditions
- Can be applied to any indicator output (RSI, volume, etc.)
- More robust than simple price comparisons
- Returns NULL when insufficient data (< length bars)