Tutorial

Connors RSI in Pine Script: Build the Three-Part Indicator

Build Connors RSI in Pine Script v6: a short RSI, a streak RSI and a percent rank, with code for each part and checks.

•11 min read

You have seen the name on a hundred oscillator lists and you have probably seen it as a single line in a pane, wiggling between 0 and 100. Then you open the Pine Editor to build it yourself and realise the formula has three parts, one of which counts how many bars in a row a market has closed up or down, and that none of the built-in indicators in the library is the thing you want. This post builds Connors RSI from scratch, one component at a time, so that you can see what each part measures and change any of them without breaking the other two.

What Connors RSI actually is

Connors RSI was introduced by Larry Connors and his research team as a short-term momentum oscillator. It is not a new kind of calculation. It is the average of three existing ideas, each scaled to run from 0 to 100, so the average also stays between 0 and 100.

The first part is a very short RSI of the closing price, with a default length of 3. An ordinary 14-period RSI is smooth and slow. A 3-period RSI is jumpy, and that jumpiness is the point: it reacts to the last few bars and little else.

The second part is an RSI of the up and down streak. The streak counts consecutive closes in the same direction. Two up closes in a row make the streak 2, a third makes it 3, and a down close resets it to negative 1. A close equal to the previous close resets it to 0. That streak series is then fed into a 2-period RSI, which turns "how long has this run lasted" into a bounded number. A long run of up closes pushes this component toward 100, and a long run of down closes pushes it toward 0.

The third part is the percent rank of the one-bar rate of change over a lookback, with a default of 100 bars. It asks a different question from the other two: not how fast price moved, but how today's move compares with the last 100 one-bar moves. A bar that moved more than 97 of the previous 100 bars scores near 97, whatever its absolute size.

The final value is the plain average of the three. Each default (3, 2 and 100) is a convention, not a law, and later in the post you will see why the script exposes all three as inputs.

Component one: the three-period RSI

Pine Script v6 already has ta.rsi(source, length), so the first component is one line. The only decision is the source. Most people use the close, but exposing it as an input costs nothing and lets you test other prices later.

pine
//@version=6
indicator("CRSI part 1: short RSI", shorttitle="CRSI p1", overlay=false)

src = input.source(close, "Source")
rsiLen = input.int(3, "RSI length", minval=2)

priceRsi = ta.rsi(src, rsiLen)

plot(priceRsi, "Price RSI", color=color.new(color.blue, 0))
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)

If you want to check the numbers against something you can compute by hand, the free RSI calculator takes a price series and shows the same value along with the Pine Script that produces it. A short RSI like this one swings across the whole range in a few bars, which is exactly why the other two components exist: on its own it is too twitchy to read.

Component two: counting the streak

There is no built-in streak function, so you write one, and this is where most hand-built versions go wrong. The streak has to remember its value from the previous bar, which means a variable declared with var, updated with :=, and reset in three different situations.

pine
//@version=6
indicator("CRSI part 2: streak", shorttitle="CRSI p2", overlay=false)

src = input.source(close, "Source")
streakLen = input.int(2, "Streak RSI length", minval=2)

var int streak = 0
if src > src[1]
    streak := streak > 0 ? streak + 1 : 1
else if src < src[1]
    streak := streak < 0 ? streak - 1 : -1
else
    streak := 0

streakRsi = ta.rsi(streak, streakLen)

plot(streakRsi, "Streak RSI", color=color.new(color.orange, 0))
plot(streak, "Raw streak", color=color.new(color.gray, 60), display=display.data_window)
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)

Read the if block as a small state machine. When the close is above the previous close, the streak either continues (if it was already positive, add one) or starts fresh at 1 (if it was zero or negative). The mirror image handles down closes. A flat close sets the streak to 0.

Two details deserve attention. First, on the very first bar src[1] is na, so both comparisons are false and the streak stays at 0. That is the behaviour you want, and it is a good example of why it helps to know how na flows through comparisons before it surprises you elsewhere. Second, ta.rsi is called on every bar, outside the if block. RSI keeps an internal running average, so calling it conditionally would starve it of history. Keep ta.* functions at the top level of the script, a rule that matters just as much when conditions are chained with and and or, as the post on lazy evaluation in Pine Script v6 explains.

The raw streak plot, set to show only in the Data Window, is a debugging aid. When you hover a bar you can read the streak and compare it with what the candles show. If the streak says 4 and you can count only three green candles, the reset logic is wrong.

Component three: percent rank of the rate of change

The third part uses two built-ins. ta.roc(source, length) returns the percentage change over length bars, and with a length of 1 it is the one-bar return. ta.percentrank(source, length) returns the percentage of the previous length values that were less than or equal to the current value.

pine
//@version=6
indicator("CRSI part 3: percent rank", shorttitle="CRSI p3", overlay=false)

src = input.source(close, "Source")
rankLen = input.int(100, "Percent rank lookback", minval=2)

oneBarReturn = ta.roc(src, 1)
rank = ta.percentrank(oneBarReturn, rankLen)

plot(rank, "ROC percent rank", color=color.new(color.teal, 0))
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)

This component behaves differently from the other two, and it helps to know how. The RSI parts care about the direction of recent moves. The rank part cares about size relative to history, and a quiet market with one unusually large bar will score the large bar near 100 regardless of how small that bar is in dollar terms. That is useful, because it adapts to the instrument's own volatility, but it also means the component is noisy when the lookback is short. The 100-bar default exists to give it a stable reference sample.

Putting the three together

Now the full indicator. The code is the three parts joined by math.avg, with the thresholds as inputs, a coloured fill between the overbought and oversold levels, and a marker when the line crosses back through an extreme.

pine
//@version=6
indicator("Connors RSI", shorttitle="CRSI", overlay=false)

src = input.source(close, "Source")
rsiLen = input.int(3, "Price RSI length", minval=2)
streakLen = input.int(2, "Streak RSI length", minval=2)
rankLen = input.int(100, "Percent rank lookback", minval=2)
upper = input.float(90, "Upper level", minval=50, maxval=100)
lower = input.float(10, "Lower level", minval=0, maxval=50)

// Component 1: short RSI of price
priceRsi = ta.rsi(src, rsiLen)

// Component 2: RSI of the up/down streak
var int streak = 0
if src > src[1]
    streak := streak > 0 ? streak + 1 : 1
else if src < src[1]
    streak := streak < 0 ? streak - 1 : -1
else
    streak := 0
streakRsi = ta.rsi(streak, streakLen)

// Component 3: percent rank of the one-bar rate of change
rank = ta.percentrank(ta.roc(src, 1), rankLen)

crsi = math.avg(priceRsi, streakRsi, rank)

// Crossings computed on every bar, then combined
backBelowUpper = ta.crossunder(crsi, upper)
backAboveLower = ta.crossover(crsi, lower)
confirmedExit = barstate.isconfirmed and backBelowUpper
confirmedEntry = barstate.isconfirmed and backAboveLower

upperLine = hline(upper, "Upper level", color=color.new(color.red, 40))
lowerLine = hline(lower, "Lower level", color=color.new(color.green, 40))
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)
fill(upperLine, lowerLine, color=color.new(color.blue, 94), title="Neutral zone")

plot(crsi, "Connors RSI", color=color.new(color.purple, 0), linewidth=2)
plotshape(confirmedExit, "Back below upper", shape.triangledown, location.top, color.red, size=size.tiny)
plotshape(confirmedEntry, "Back above lower", shape.triangleup, location.bottom, color.green, size=size.tiny)

alertcondition(confirmedExit, "CRSI back below upper", "Connors RSI crossed back below the upper level")
alertcondition(confirmedEntry, "CRSI back above lower", "Connors RSI crossed back above the lower level")

A few choices in that code are worth explaining. The crossings are computed in their own variables on every bar and only then combined with barstate.isconfirmed. If you wrote barstate.isconfirmed and ta.crossunder(crsi, upper) inline, the right-hand function would be skipped on unconfirmed bars and ta.crossunder would compare against stale history. The barstate.isconfirmed guide covers why the confirmation guard is the difference between a marker that stays and one that vanishes when the bar closes.

The markers fire when the line crosses back through a level, not when it touches the level. That is a deliberate choice: an oscillator can sit above 90 for several bars, and a marker on every one of them is noise. A crossing is a single firing, which makes it both cleaner on the chart and more sensible as an alert. If an alert ever appears on a bar that does not match what you saw, the cause is almost always one of the issues in why a Pine Script alert fires on the wrong bar.

Why the defaults are 3, 2 and 100

Because the inputs are exposed, you can change all three, and it is worth understanding what each change does before you do.

Lengthening the price RSI from 3 to, say, 5 slows the first component and makes the whole indicator spend less time pinned at the extremes. Lengthening the streak RSI changes the streak component less than you might expect, because streaks are usually short. A streak of 5 or more consecutive closes in one direction is rare on most instruments and timeframes, so a longer RSI over it spends most of its time near the middle and contributes little. The percent rank lookback has the most direct effect on how often the line reaches 90 or 10, since a shorter sample makes extreme ranks easier to hit.

The honest summary is that these parameters shape how sensitive the oscillator is, not whether it is right. Moving the levels from 90 and 10 to 80 and 20 gives you more markers and each one means less. You should pick the thresholds by looking at how the oscillator behaves on the symbol and timeframe you care about, and a good way to do that is to run the script, scroll through history, and count how often each level is reached.

Checking it before you trust it

An indicator you built yourself can be wrong in ways that look plausible. Three checks catch most mistakes.

The first is the component check. Add the three parts from the earlier sections to the chart one at a time, along with the combined indicator, and confirm that the combined line is always the average of the other three. If you hover over a bar, the Data Window should show a combined value within rounding of the mean of the three components. If it does not, you have a bracket or a variable name wrong.

The second is the range check. Every component and the final value must stay between 0 and 100. If the line ever leaves that range, one component is not scaled. This is easy to cause when you swap a component, for example by replacing ta.percentrank with a raw rate of change, which is unbounded.

The third is the bar-by-bar check, and it is the one that catches the subtle bugs. Step through a few dozen bars and, at each step, compare the streak with what the candles do, and confirm that the markers only appear on the bar where the line crosses a level. This is slow when you do it by hand on a live chart, which is why a bar-at-a-time view is more useful than scrolling.

Running it in PineScripter

In the PineScripter editor you can describe this indicator in plain English, something like "build Connors RSI with three parts: a 3-period RSI of the close, a 2-period RSI of the up and down streak, and the percent rank of the one-bar rate of change over 100 bars; show 90 and 10 as levels and mark when the line crosses back through them". PineScripter writes Pine Script v6, checks it with its built-in linting engine, and fixes any compile error it finds itself, so you do not have to copy errors between TradingView and a chat window.

The part that fits this particular indicator is the chart. PineScripter's chart runs your script on real market data, 5,000 bars at most, across 308 crypto spot markets and 14 timeframes, and draws the plots and shapes so that you see what it produces before you paste it into TradingView. Bar replay then steps through the run one bar at a time, so you can see the exact bar on which a marker appears and ask the AI about that bar, which is the bar-by-bar check from the previous section done in a tool built for it. When you want a change, such as swapping the average for a weighted one, the edit touches only the affected lines, and the Code Changes view shows you exactly what moved. Plans start at $9.99 a month on the pricing page, with credits that reset each billing month.

Indicator Analysis can then show how the markers clustered and how price moved 1, 5, 10 and 20 bars after each one on the data you ran. That describes the past only. It is a way to see how your markers behaved, not a forecast of what comes next.

Common mistakes when building Connors RSI

A few errors come up often enough to list.

Calling ta.rsi on the streak inside the if block is the most frequent one. It compiles, and often produces something that looks right on the last few hundred bars, but the internal state is wrong and the first values drift.

Declaring the streak without var is the second. Without it the variable resets every bar and the streak never exceeds 1.

Mixing percentage and fraction scales is the third. ta.roc returns a percentage and ta.percentrank returns a percentage, so both are already on a 0 to 100 scale at the end. Dividing or multiplying by 100 in the combination step puts the average out of range.

Treating the first 100 bars as reliable is the fourth. The percent rank needs a full lookback before it means anything, and the streak RSI needs a few bars to settle, so the early part of any chart should be read with that in mind.

What to do next

The version above is a complete indicator, but it is a starting point. You might add a smoothing option, a second timeframe via request.security (read how request.security works first, since the repainting rules apply), or a table showing the three components for the latest bar. Each of these is a small, local change, and each is easier to make and verify when you can see the effect on a chart immediately.

If you want the three-part structure in a form you can reuse, keep the component code in separate sections as shown and resist the urge to collapse it into one expression. When one component misbehaves, you will want to be able to plot it by itself.

Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.

Back to Blog

Frequently asked questions

What is Connors RSI?

It is the average of three components: a 3-period RSI of the close, a 2-period RSI of the up and down streak, and the percent rank of the one-bar rate of change over 100 bars. Each is scaled 0 to 100, so the average is too.

Is there a built-in Connors RSI function in Pine Script v6?

No. You build it from ta.rsi, ta.roc, ta.percentrank and math.avg, plus a small var-based streak counter, as shown in this post.

Why does my streak never get above 1?

The streak variable is probably declared without var, so it resets on every bar. Declare it with var int streak = 0 and update it with :=.

Can I change the 3, 2 and 100 defaults?

Yes, the script exposes all three as inputs. They change how sensitive the oscillator is, not whether it is correct, so check the behaviour on your own symbol and timeframe.

Does the indicator repaint?

The line follows the live bar until it closes, like any indicator. The markers in this script only fire when barstate.isconfirmed is true, so they do not disappear after the bar closes.

Describe your indicator in plain English and get Pine Script that compiles →


Disclaimer: PineScripter is a coding tool for Pine Script development. It does not provide financial advice and does not guarantee trading profits. Always backtest strategies thoroughly and understand the risks before live trading.