Skip to main content

How to Code RSI in Pine Script (And Actually Use It)

· 5 min read

So you want to code an RSI indicator in Pine Script? Cool! RSI (Relative Strength Index) is probably one of the most popular indicators out there, and for good reason - it's simple, useful, and actually works when you know how to use it properly.

Pine Script RSI

What's RSI All About?

The Best Pine Script Generator

The Basics

Think of RSI as a speedometer for price movements. It bounces between 0 and 100, telling you whether a stock (or crypto, or whatever you're trading) has been going up or down too fast lately.

The classic rules are pretty simple:

  • Above 70? Might be overbought (could drop soon)
  • Below 30? Might be oversold (could bounce back)

But here's the thing - don't just blindly follow these numbers. They're more like guidelines than hard rules.

How Does It Actually Work?

The math behind RSI isn't too crazy. Basically, it looks at your recent gains vs losses over a certain period (usually 14 days) and gives you a number.

RSI Formula

Don't worry if the formula looks intimidating - Pine Script handles all the heavy lifting for you.

Let's Code This Thing

Alright, time to get our hands dirty. Here's how to build an RSI indicator from scratch.

Step 1: Set Up Your Script

First things first, tell Pine Script what version you're using and give your indicator a name:

//@version=6
indicator("My RSI Indicator", overlay=false)

The overlay=false part means it'll show up in a separate pane below your chart, not on top of the price bars.

Step 2: Make It Customizable

Let people adjust the RSI period if they want:

rsi_length = input.int(14, title="RSI Period", minval=1)

Most people stick with 14, but some traders like to experiment with different numbers.

Step 3: Calculate the RSI

Here's where Pine Script makes your life easy:

rsi_value = ta.rsi(close, rsi_length)

Yep, that's it. Pine Script has a built-in RSI function that does all the math for you.

Making It Look Good

Now let's add some visual elements to make your RSI actually useful:

// Plot the RSI line
plot(rsi_value, title="RSI", color=color.blue, linewidth=2)

// Add the overbought and oversold lines
hline(70, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(30, "Oversold", color=color.green, linestyle=hline.style_dashed)
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)

The midline at 50 is handy for seeing the overall trend direction.

Want Alerts? Here You Go

If you want to get notified when RSI hits certain levels:

// Alert when crossing into overbought territory
alertcondition(ta.crossover(rsi_value, 70), title="RSI Overbought", message="RSI just crossed above 70!")

// Alert when crossing into oversold territory
alertcondition(ta.crossunder(rsi_value, 30), title="RSI Oversold", message="RSI just dropped below 30!")

The No-Code Way (If You're Feeling Lazy)

Pineify Platform

Look, I get it. Sometimes you just want to add RSI to your chart without writing any code. That's where Pineify comes in handy.

Pineify Interface

It's basically a visual editor where you can drag and drop indicators together. Plus, you can combine RSI with other indicators without hitting TradingView's limit of two indicators per chart (which is honestly pretty annoying).

The condition editor is pretty neat too - you can set up complex rules like "buy when RSI crosses above 30 AND price is above the 50-day moving average" without writing a single line of code.

How I Actually Use RSI

Here's the real talk about RSI - don't just buy every time it hits 30 or sell every time it hits 70. That's a quick way to lose money.

Instead, try these approaches:

Look for Divergences: When price makes a new high but RSI doesn't, or vice versa. That's often a sign things might reverse.

Use It with Trend: In an uptrend, look for RSI to bounce off 40-50 instead of waiting for 30. In a downtrend, it might top out at 50-60 instead of reaching 70.

Wait for Confirmation: Don't just act on RSI alone. Wait for price action to confirm what RSI is telling you.

Putting It All Together

Here's a complete, simple RSI script you can copy and paste:

//@version=6
indicator("Simple RSI", overlay=false)

// User inputs
rsi_length = input.int(14, title="RSI Length", minval=1)

// Calculate RSI
rsi_value = ta.rsi(close, rsi_length)

// Plot everything
plot(rsi_value, title="RSI", color=color.blue, linewidth=2)
hline(70, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(30, "Oversold", color=color.green, linestyle=hline.style_dashed)
hline(50, "Midline", color=color.gray, linestyle=hline.style_dotted)

// Optional: Color the background when overbought/oversold
bgcolor(rsi_value > 70 ? color.new(color.red, 90) : rsi_value < 30 ? color.new(color.green, 90) : na)

Final Thoughts

RSI is a solid indicator, but remember - no single indicator is perfect. It works best when you understand what it's actually measuring (momentum) and use it alongside other tools and your own market knowledge.

The cool thing about coding your own RSI is that you can customize it however you want. Maybe you prefer different overbought/oversold levels, or you want to add some smoothing. Once you understand the basics, you can tweak it to fit your trading style.

Happy coding, and remember - the best indicator is the one you actually understand and use consistently!

Want to Learn More?

Check out these resources if you want to dive deeper: