StatisticalPine Script v6

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

Syntax
ta.percentrank(source, length) → series float

Arguments

ParameterTypeDescription
sourceseries int/floatSeries 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.
lengthseries intNumber 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 valuePrevious values at or below itRank
970 of 50%
1023 of 5, including the equal value 10260%
1054 of 580%
1085 of 5, including the equal value 108100%
ta.percentrank returns a percentage. ta.percentile_nearest_rank returns a source value at a requested percentile; ties and discrete samples mean the two are not exact mathematical inverses.

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

Pine Script v5: close percent rank
//@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")
Pine Script v6: confirmed high and low rank conditions
//@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.

Frequently Asked Questions

It means 80% of the previous values in the comparison window are less than or equal to the current source value. It does not mean price rose 80%, or that a trade has an 80% chance of success.

Yes. Both versions use ta.percentrank(source, length), with a series int/float source, a series int length, and a series float result. In v4, the function was called percentrank() without the ta. namespace. The two complete examples above use their stated version directives.

Percent rank returns a percentage for the current source value. ta.percentile_nearest_rank(source, length, percentage) returns a source value for a requested percentage. For a price series, its output is a price rather than a rank. They answer different questions and are not exact inverses for every sample.

ta.percentrank includes na values in its calculation and can return na when the required history or source values are unavailable. Check not na(rank) before using the result. A derived source such as a moving average may need additional warm-up. Using nz(source) inserts zeros and changes the ranking distribution, so only do that when zero is a deliberate missing-data assumption.

Yes. The length parameter accepts a series int in v5 and v6. Keep it positive and available on every bar. A changing length also changes the comparison sample, so a rank can change even when the current source value is unchanged.

Calculate the rank on every bar, then combine a missing-value check with your threshold. The v6 example also requires barstate.isconfirmed for its background conditions, because close and its rank can change before the bar closes. To extend the example in Pineify, give the Pine Script AI Coding Agent the source, lookback, threshold, and desired plot or alert behavior, then review the generated code in TradingView.

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.