ta.percentrank()Percent Rank
ta.percentrank(source, length) returns the percentage of previous source values at or below the current value. Pine Script v5 and v6 use the same signature. The result describes relative position, not a price change or a probability of a profitable trade.
Pine Script v6 reference verified: · TradingView reference
Syntax
ta.percentrank(source, length) → series floatArguments
| Parameter | Type | Description |
|---|---|---|
| source | series int/float | Series of values to process (e.g. close, high, volume, or indicator series). If any value in the lookback window is na, the function returns na. |
| length | series int | Number of bars in the rolling lookback window. Must be an integer greater than 0; accepts dynamic series lengths. |
Returns
A series float from 0 to 100 representing the percentage of lookback window values less than or equal to the current source value.
Remarks
For length = 5, compare the current value with five preceding values. If those values are [98, 100, 102, 104, 108] and the current value is 105, four comparisons satisfy previous <= current, so the hand calculation is 100 * 4 / 5 = 80. Equal values count too. This is an illustrative calculation, not a measured chart result. Missing source values produce na; keep that state visible rather than silently replacing missing data with zero. The examples are educational indicator code, not investment advice.
Hand calculation: five previous values
Using [98, 100, 102, 104, 108] as the comparison set. These rows illustrate the documented less-than-or-equal rule.
| Current value | Previous values at or below it | Rank |
|---|---|---|
| 97 | 0 of 5 | 0% |
| 102 | 3 of 5, including the equal value 102 | 60% |
| 105 | 4 of 5 | 80% |
| 108 | 5 of 5, including the equal value 108 | 100% |
Reference sources and next steps
TradingView documents the function in its Pine Script v5 reference and Pine Script v6 reference. The reference describes comparison with previous values and propagation of missing values. Syntax validation does not prove trading performance; inspect the plots on your intended chart.
Code Examples
//@version=5
indicator("Pine Script v5 ta.percentrank Example", overlay=false)
// Inputs for lookback length and extreme thresholds
lengthInput = input.int(50, title="Percent Rank Length", minval=1)
upperThreshold = input.float(80.0, title="Upper Rank Threshold")
lowerThreshold = input.float(20.0, title="Lower Rank Threshold")
// Calculate the percent rank of the closing price over the rolling window
rankClose = ta.percentrank(close, lengthInput)
// Plot the percent rank oscillator (0 to 100 scale)
plot(rankClose, title="Close Percent Rank", color=color.blue, linewidth=2)
h1 = hline(upperThreshold, title="Upper Threshold", color=color.red, linestyle=hline.style_dotted)
h2 = hline(lowerThreshold, title="Lower Threshold", color=color.green, linestyle=hline.style_dotted)
hline(50.0, title="Midline (50%)", color=color.gray, linestyle=hline.style_dashed)
fill(h1, h2, color=color.new(color.blue, 95), title="Threshold Band")//@version=6
indicator("Pine Script v6 ta.percentrank Signals", overlay=false)
// Parameters
int lengthInput = input.int(50, "Lookback Window Length", minval=1)
float extremeHigh = input.float(90.0, "Upper Rank Threshold")
float extremeLow = input.float(10.0, "Lower Rank Threshold")
// Calculate percent rank of close across historical bars
float closeRank = ta.percentrank(close, lengthInput)
// Evaluate conditions only after the chart bar closes
bool isLowRank = barstate.isconfirmed and not na(closeRank) and closeRank <= extremeLow
bool isHighRank = barstate.isconfirmed and not na(closeRank) and closeRank >= extremeHigh
// Dynamic oscillator color based on percentile extremes
color oscColor = closeRank >= extremeHigh ? color.red : closeRank <= extremeLow ? color.green : color.teal
plot(closeRank, "Percent Rank", color=oscColor, linewidth=2)
// Reference threshold levels
hline(extremeHigh, "Upper Rank", color=color.red, linestyle=hline.style_dashed)
hline(extremeLow, "Lower Rank", color=color.green, linestyle=hline.style_dashed)
hline(50.0, "Midline (50%)", color=color.gray, linestyle=hline.style_dotted)
// Highlight background when extreme conditions occur
bgcolor(isLowRank ? color.new(color.green, 90) : isHighRank ? color.new(color.red, 90) : na)Trading Applications
Plot how the current close ranks against recent prices.
Rank volume or another numeric indicator against its own history.
Use a confirmed rank condition as one input to an indicator or alert.
Compare different lookback lengths before choosing a threshold; high rank alone does not imply a reversal.
Related Functions
Frequently Asked Questions
Generate ta.percentrank() Code with AI
Draft Pine Script using ta.percentrank() with Pineify's AI Coding Agent, then review the arguments and test the result on your chart. Usage depends on your plan and available credits.
Related Pine Script Functions
ta.percentile_linear_interpolation()
Learn how to use ta.percentile_linear_interpolation() in Pine Script. Calculate percentiles using linear interpolation.
ta.percentile_nearest_rank()
Learn how to use ta.percentile_nearest_rank() in Pine Script. Calculate percentiles using nearest rank method.
ta.median() - Median
Learn how to use ta.median() in Pine Script. Calculate the median value for robust price analysis.
ta.stdev() - Standard Deviation
Learn how to use ta.stdev() in Pine Script. Syntax, parameters, code examples for statistical standard deviation calculation.
ta.rsi() - Relative Strength Index
Learn how to use ta.rsi() in Pine Script. Syntax, parameters, code examples for the RSI momentum oscillator.