Skip to content

Pine Script

ta.percentrank() in Pine Script: Formula, Ties and Examples

ta.percentrank() in Pine Script returns the percentage of comparison values over a specified length that are less than or equal to the current source value. Its syntax is ta.percentrank(source, length), returning a series float on a 0–100 scale. A rank describes relative position in a lookback, not percentage price change, a forecast probability or a trading instruction. Equal values count in the at-or-below comparison.

Illustrative percent-rank cover showing three of five observations at or below a horizontal comparison level.
A rank counts relative position in a comparison set; it does not predict the next outcome.

Part of the Pine Script guide and function reference. The function and percentile functions discussed here use TradingView's Pine Script v6 reference as of 25 September 2026.[1]

ta.percentrank() at a glance

Scroll horizontally to read every column.

Item Meaning
Signature ta.percentrank(source, length)
Source The numeric series whose current value is ranked
Length The lookback length in bars; use a positive integer
Return A series float expressed as a percentage on a 0–100 scale
Ties Equal values count with lower values
Illustrative use Compare a bar-range measure with its own recent observations
Different operation A percentile function takes a percentage and returns a source value

TradingView's strategies documentation includes ta.percentrank(close, lengthInput) and plots its result in a separate pane.[2]

What does the percent-rank calculation mean?

Percent rank = 100 × count of comparison values ≤ current source value ÷ number of comparison values

For a complete comparison set of length N, the denominator is N. TradingView describes the comparison in terms of previous values at or below the current source value.[1] The arithmetic examples here use explicit, fully available comparison sets; they do not model missing-data or startup behaviour.

A percentage output does not change the underlying question into a probability forecast. A rank of 60 means that 60% of the comparison observations meet the at-or-below test. It does not mean a 60% chance that the next price move will be positive.

Worked example: ties count toward a rank of 60

Illustrative arithmetic, not recommended settings. Take a comparison set of five observations: 10, 20, 20, 30 and 40. The value being ranked is 20. The set is displayed in sorted order to make the comparison visible.

Comparison observation At or below 20? Contribution to the count
10 Yes 1
20 Yes, equality counts 1
20 Yes, equality counts 1
30 No 0
40 No 0

The count is 1 + 1 + 1 + 0 + 0 = 3. Percent rank = 100 × 3 ÷ 5 = 60. Counting only values strictly below 20 would give 100 × 1 ÷ 5 = 20, which uses a different tie convention.

With a complete five-observation set, one observation represents 100 ÷ 5 = 20 percentage points. A short comparison set therefore produces coarse rank increments. Neither five observations nor a rank of 60 is a suggested trading parameter.

Illustrative ta.percentrank calculation: three of five comparison values, 10, 20 and 20, are at or below the value 20, giving a rank of 60.
Illustrative comparison set. Equality counts, so both observations equal to 20 belong in the numerator.

Can the rank change when the source value stays the same?

Yes. The comparison set can change independently of the current value. Suppose the value being ranked is still 20, but a later comparison set is 20, 20, 30, 40 and 50. Only two of those five observations are at or below 20, giving 100 × 2 ÷ 5 = 40.

The current value is unchanged between these two illustrations, while its rank falls from 60 to 40. The difference comes from the comparison set. A rolling-window boundary can therefore change a rank-based condition without the same change in the source itself.

Equality also matters on flat data. Under the at-or-below definition, if every comparison value equals the current value, every comparison passes. A high rank is therefore not proof that the source has increased or that a market is moving strongly.

How can Pine Script rank a bar-range measure?

Illustrative sketch; not compiled or tested. Paste into TradingView's Pine Editor. This indicator ranks each bar's high-minus-low range and plots the percentage. The five-bar input is only an example, and the script creates no alerts or orders.[1]

//@version=6
indicator("Bar range percent rank")
length = input.int(5, "Illustrative lookback", minval = 1)
barRange = high - low
rank = ta.percentrank(barRange, length)
plot(rank, "Range rank")

A range-based regime filter could test this rank against a threshold defined in a separate rule specification. The rank would describe how large the current bar's range is relative to its comparison ranges. It would not decide whether to buy or sell.

The source choice still matters. A high-minus-low range does not include a gap from the preceding close to the current open when that gap lies outside the current bar. Ranking this measure does not turn it into a complete account of price variation.

Compute the rank on each bar, then use the result in the condition. History-dependent calculations can become inconsistent when their execution is skipped on some bars; TradingView explains this in its execution-model documentation.[3]

How is percent rank different from a percentile?

The two operations reverse the question. A rank starts with a value and asks where it stands. A percentile starts with a percentage and asks for the corresponding value.[1]

Function Question Result units
ta.percentrank(source, length) How many comparison values are at or below the current value? Percentage
ta.percentile_nearest_rank(source, length, percentage) Which observed value corresponds to the requested percentile using nearest rank? Source units
ta.percentile_linear_interpolation(source, length, percentage) What value corresponds to the requested percentile using interpolation? Source units; the value need not be an observed sample

A percentile of a price series returns a price. A rank of that series returns a percentage. Ties and the percentile method also matter, so the operations should not be assumed to undo one another exactly.

Clipping a value between two bounds is another distinct operation, covered in clamping with math.min() and math.max(). A common output range does not make the calculations interchangeable.

What changes before a rank becomes an alert or MT5 order?

A rank is a measurement. “Rank is above a threshold” is a state that can persist across bars; “rank crosses above a threshold” is an event using current and previous observations. See ta.crossover() and ta.crossunder() for the boundary rules.

On an open realtime bar, the high, low and close can change, so a rank based on them can change too.[3] Define whether the rule acts intrabar or on the closing update. For an alert() call, alert.freq_once_per_bar_close allows the alert only when the realtime bar closes.[4]

PineConnector's alert integration guide connects explicit script events and messages to an execution workflow.[5] The sequence remains measurement, condition, alert trigger, webhook delivery, PineConnector processing, EA request and broker outcome. The numeric rank itself is not an instruction to trade.

The demo verification procedure checks the actual alert message, processing result and intended broker account.[6] Check acceptance, any deal and the resulting position separately from the chart calculation.

What are the main limitations and mistakes?

  • Missing history: an unavailable calculation is not a rank of zero. Check availability before using it as an input to an alert.
  • Replacing missing output: nz(rank) can substitute zero and make missing information appear to be a low rank. The nz() reference explains the distinction.
  • Ignoring ties: strictly-below counting is a different formula.
  • Comparing unlike samples: changing the source, lookback, symbol or timeframe changes the comparison population.
  • Inferring direction: a high range rank identifies a relative range, not a preferred order direction.
  • Forgetting alert snapshots: recreate affected alerts after changing code or inputs; existing alerts keep their saved context.[4]

Frequently asked questions

What does ta.percentrank() return in Pine Script?

ta.percentrank(source, length) returns a series float representing the percentage of comparison values at or below the current source value over the specified length. The scale is 0–100, and ties count. The result describes relative position in a sample; it is not a price-change percentage or a future-outcome probability.

Is ta.percentrank() available in Pine Script v5?

Yes. Pine Script v5 documents ta.percentrank(source, length) in the ta namespace. Pine Script v6 uses the same function name and argument order. Match the reference to the script's version and distinguish the percent-rank calculation from any surrounding alert or strategy logic.[7]

What is the difference between percent rank and percentile?

Percent rank takes a source value and returns its relative position as a percentage. A percentile function takes a requested percentage and returns a value in the source's units. Pine Script provides nearest-rank and linear-interpolation percentile functions; their outputs can differ, and interpolation can return a value not present in the sample.

Does a high percent rank mean buy or sell?

No. A percent rank measures where a value stands relative to its comparison observations. A high rank of bar ranges says nothing by itself about order direction, and equal observations can also produce a high rank. Any entry, exit or filter requires a separate rule with defined timing and data inputs.

Reviewed 25 September 2026. Facts were checked against the linked sources on that date. Nothing in this article was tested on a trading account and no code was compiled.

Related reading

Sources

  1. TradingView – Pine Script v6 Reference Manual: percent rank and percentiles, accessed 25 September 2026.
  2. TradingView – Strategies: using strategy information, accessed 25 September 2026.
  3. TradingView – Execution model, accessed 25 September 2026.
  4. TradingView – Alerts, accessed 25 September 2026.
  5. PineConnector – Add alerts to your script, accessed 25 September 2026.
  6. PineConnector – Test your setup, accessed 25 September 2026.
  7. TradingView – Pine Script v5 Reference Manual, accessed 25 September 2026.

PineConnector executes the instructions you send it. It does not select trades, manage money, or hold funds. Trading carries risk, and past performance of any strategy does not indicate future results.


Leave a comment

Back To PiCo Blog

Ready when your strategy is

You bring the strategy.We bring the infrastructure.

Connect TradingView to MetaTrader, choose where MT5 runs and put the full PineConnector workflow through its paces from your first month.

Strategy and trading decisions remain yours. The MT5 environment can be ours.

PineConnector Edge

Run the full PineConnector workflow.

$59/mo at launch

Core plan · 1 connection · 1 hosted MT5 environment

Try Core for $7

7 days of Core for $7, then $59/month at the launch price unless you cancel.