Rank Correlation Index: time ranks and price ranks
The Rank Correlation Index (RCI) compares the ordering of prices with the ordering of bars. It is Spearman rank correlation multiplied by 100: rising sequences approach +100, falling sequences approach -100. Equal prices share an average rank.
Method: TradingView RCI documentation. Sources checked October 17, 2026. The examples below are synthetic calculations, not market performance.
Five bars, oldest to newest
101, 103, 102, 104, 105 → RCI +90
Only the middle two price ranks exchange places. The calculation describes the window that just occurred; +90 is not a 90% chance of a rise on the next bar.
Calculate RCI without tied prices
Number time from 0 at the oldest bar to 4 at the newest. Rank prices from 0 for the lowest to 4 for the highest. For this five-bar example, d is the time rank minus the price rank.
| Time rank t | Close | Price rank r | (t - r)² |
|---|---|---|---|
| 0 | 101 | 0 | 0 |
| 1 | 103 | 2 | 1 |
| 2 | 102 | 1 | 1 |
| 3 | 104 | 3 | 0 |
| 4 | 105 | 4 | 0 |
RCI = 100 × [1 - 6 × Σd² / (n × (n² - 1))]
Σd² = 2; n = 5
RCI = 100 × [1 - 12 / 120] = +90
This shortcut requires distinct prices. Reversing the sequence to 105, 104, 102, 103, 101 gives -90. Strictly increasing 101, 102, 103, 104, 105 gives +100; strictly decreasing prices give -100. If you use Pine history offsets, remember that source[0] is the newest bar. Treating that offset as forward time reverses the sign.
Tied prices change the denominator
Change the sample to 101, 103, 103, 102, 105. The two closes of 103 occupy sorted positions 2 and 3, so both receive rank 2.5. The resulting price ranks are 0, 2.5, 2.5, 1, 4. Do not break the tie by bar order.
| t | r | (t - 2)(r - 2) | (t - 2)² | (r - 2)² |
|---|---|---|---|---|
| 0 | 0 | 4 | 4 | 4 |
| 1 | 2.5 | -0.5 | 1 | 0.25 |
| 2 | 2.5 | 0 | 0 | 0.25 |
| 3 | 1 | -1 | 1 | 1 |
| 4 | 4 | 4 | 4 | 4 |
| Totals | 6.5 | 10 | 9.5 | |
RCI = 100 × Σ[(t - mean(t))(r - mean(r))] / √(Σ(t - mean(t))² × Σ(r - mean(r))²)
= 100 × 6.5 / √(10 × 9.5) ≈ +66.69
The sum of squared rank deviations is now 9.5 instead of 10. Using the no-tie shortcut with Σd² = 6.5 would give +67.5, a different result. This is why the average-rank rule and the correlation denominator must both be correct. A window with all prices equal has zero rank variance, so the mathematical correlation is undefined. The sample script explicitly leaves it as na. Pearson formula and constant-input definition.
What the window and the ranks leave out
On the tied sample above, RCI(5) is +66.69. Its last three closes are 103, 102, 105, which receive fresh ranks 1, 0, 2: RCI(3) is +50 on the same final bar. Every window ranks its own prices. A shorter RCI is not a shortened average of the longer line.
Ranks discard move size. The sequence 1, 3, 2, 4, 1000 has the same ranks as the first example and still gives RCI +90. Pearson correlation between those raw prices and time is about 0.7085 instead of 0.9. RCI therefore cannot tell you the dollar risk, volatility or size of the last jump.
An RCI ribbon plots three lengths on the chart. Those lines share observations and are not three independent confirmations. Multiple lengths on one chart also differ from requesting multiple chart timeframes.
For magnitude-based momentum, compare the RSI guide. For the close within a high-low range, see the Stochastic oscillator. Neither answers the same ranking question. Cross-asset correlation requires two assets and should not be inferred from this single-price-series indicator.
Pine Script v6: RCI with confirmed crosses
Copy this indicator into TradingView's Pine Editor and add it to a chart. It uses ta.rci for the ranking calculation, starts with a configurable 10-bar window, and marks crosses back above -80 or below +80 only when the chart bar closes. It does not place orders or calculate strategy returns.
//@version=6
indicator("RCI: ranks and confirmed crosses", overlay = false)
length = input.int(10, "RCI length", minval = 2, maxval = 200)
source = input.source(close, "Source")
// ta.rci uses price ranks; equal prices receive average ranks.
rawRci = ta.rci(source, length)
// This example leaves incomplete and all-equal windows undefined.
hasWindow = bar_index >= length - 1
hasVariation = ta.highest(source, length) != ta.lowest(source, length)
rci = hasWindow and hasVariation ? rawRci : na
crossUp = ta.crossover(rci, -80)
crossDown = ta.crossunder(rci, 80)
confirmedUp = barstate.isconfirmed and crossUp
confirmedDown = barstate.isconfirmed and crossDown
plot(rci, "RCI", color = color.blue, linewidth = 2)
hline(80, "+80", color = color.gray, linestyle = hline.style_dashed)
hline(0, "Zero", color = color.gray)
hline(-80, "-80", color = color.gray, linestyle = hline.style_dashed)
plotshape(confirmedUp, "Closed above -80", shape.triangleup, location.bottom, color.blue, size = size.tiny)
plotshape(confirmedDown, "Closed below +80", shape.triangledown, location.top, color.orange, size = size.tiny)
alertcondition(confirmedUp, "RCI closed above -80", "RCI crossed above -80 on a closed chart bar.")
alertcondition(confirmedDown, "RCI closed below +80", "RCI crossed below +80 on a closed chart bar.")The complete source remains selectable if clipboard access is unavailable.
The line can move during an open bar; the markers and alert conditions wait for barstate.isconfirmed. Create an alert in TradingView using the named condition and select Once Per Bar Close. The script does not create a running alert for you. It leaves the initial incomplete window and a fully flat window blank.
Check a signal before treating it as a rule
- Match source, length, timeframe and smoothing when comparing this line with TradingView's built-in RCI. Use no smoothing for the direct comparison.
- For a cross above -80, the previous value must be at or below -80 and the current value strictly above it. Landing exactly on -80 does not trigger that cross.
- A sequence of strictly rising closes keeps RCI at +100 even after repeated new bars. An extreme reading alone supplies no reversal timing.
- Small tick changes can reorder prices with little economic movement. Transaction costs may exceed the move after a threshold crossing.
- Define entries, exits, position size, fees and slippage separately before backtesting. Test those fixed rules on a later period rather than choosing the best-looking length after seeing the result.
Educational examples only, not investment advice. Syntax validation does not establish signal accuracy or future returns.
Specify the RCI behavior you want to change
Pineify's Pine Script AI Coding Agent can help draft or modify a script from stated rules. Give it the source, lengths, tie convention and alert timing, then inspect the generated code and compare chart values yourself. Account access and usage limits depend on the current plan.
Modify this RCI indicator to plot lengths 5, 10 and 20 on the current chart timeframe. Retain average ranks for equal prices, keep flat windows undefined, and alert only when the 5-bar line crosses above -80 on a closed bar. Explain each change and keep it as an indicator.Open Pineify
Review the Pine Script AI Coding Agent workflow before adapting the example.
RCI questions
Sources and calculation notes
- TradingView: Rank Correlation Index for price ordering and average ties.
- TradingView: RCI Ribbon for the three-window application.
- SciPy: Pearson correlation for the normalized covariance formula and zero-variance boundary.
- TradingView: Bar states for closing-bar confirmation.
Checked October 17, 2026. Tables and numeric comparisons are original synthetic examples derived from the stated formulas. They are not observations from a live or historical strategy.