Pine Script – Lesson 4: Generating RSI Signals

Pine Script Basics Course Pine Script Mastery Course

How to Make the RSI Indicator Generate Trading Signals

In this lesson I will begin teaching you how to work with other existing indicators. We will focus on the RSI indicator for now as it is one of the simplest oscillators we can work with.

Pine Script allows you to fetch data from built-in indicators such as EMAs and SMAs, RSI, Stochastics, MACD, Volume, etc.

In this lesson we will build on the knowledge from the previous lessons and create an RSI signal indicator that draws icons onto the chart whenever price is “overbought” or “oversold”.

Video Lesson

If you prefer to learn in a visual/audio manner, then here’s a video version of this lesson:

Getting the RSI Value

The code for getting the RSI indicator value is this:

rsi = rsi(source, length)

So to build on the past lessons, let’s generate an RSI value and allow the user to change the settings in the script interface:

study(title="Lesson 4", shorttitle="RSI Signal", overlay=true)
rsiSource = input(title="RSI Source", type=input.source, defval=close)
rsiLength = input(title="RSI Length", type=input.integer, defval=14)
rsiValue = rsi(rsiSource, rsiLength)

You should notice some differences in this code to the last few lessons.

This time, we have a study with two titles. The first title is the official name of the script and will be what you see in the indicator’s interface when you view your indicator list.

The second title shorttitle is what shows up on your actual chart. This is useful for reducing chart clutter if you have a long indicator name.

The other major difference is that instead of plotting the close, this time we are plotting a variable named na.

This variable is what we use when we want to assign a totally empty value to something. This does not tell the script to plot zero, or to plot a variable named “na”. It tells the script, “don’t plot anything at all to this chart”.

This line is only necessary temporarily in order to make the script compile. If you do not include an “output function” (ie. a line of code that plots something) somewhere in your script then you will get a compilation error like this:

Pine Script Error Message

If you place the line plot(na) into your script then you will not get this error but the script will also not plot anything to your screen. This may seem useless to you now, but down the line you will find the na variable very useful so I think it is best that you are introduced to it now.

Checking if Price is Overbought or Oversold

Moving on, let’s add some more code to this script so that we can modify the “overbought” and “oversold” thresholds with ease. Add these two lines below the rsiLength input:

rsiOverbought = input(title="RSI Overbought Level", type=input.integer, defval=70)
rsiOversold = input(title="RSI Oversold Level", type=input.integer, defval=30)

Now when we get the RSI value we can compare it to these two user-set values to determine if price is overbought or oversold according to the specified levels.

By default the levels will be 70 for overbought and 30 for oversold. This is for demonstration purposes only. In reality, you would probably want to set it to 80 and 20 so that it only detects the most extreme price conditions.

Add these two lines of code to create “boolean” (true/false) variables we can use to detect whether or not the RSI is above or equal to (>=) the Overbought level, or below or equal to (<=) the Oversold level:

isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold

Plotting Shapes to the Chart

Now we can use these variables to tell Pine Script when and where to draw our overbought/oversold signals. Drawing icons and symbols and even text to your chart is quite simple. All you need to do is add these two lines of code:

plotshape(isRsiOB,  title= "Overbought", location=location.abovebar,, transp=0, style=shape.triangledown, text="OB")
plotshape(isRsiOS, title= "Oversold", location=location.belowbar,, transp=0, style=shape.triangleup, text="OS")

The first line of code is telling Pine Script to plot a shape onto the chart with the style of shape.triangledown, with the text OB, at the location of abovebar, with a transparency value of zero (transp=0), and in the color red. But it will plot this shape only when the variable isRsiOB is true.

The second line is almost identical to the first line, except that we have changed location=location.abovebar to location=location.belowbar, the color to green, the shape to shape.triangleup instead of down, and the text to OS. And just like the first line of code, this shape will only plot when isRsiOS is true.

I should mention that when you are plotting shapes to the chart they will plot above the bar by default so the location=location.abovebar setting can be left out when plotting shapes above candles. I left it in for demonstration purposes.

Now if you save your script and add it to the chart, you will end up with RSI signals being drawn to the screen like this:

Pine Script Lesson 4 Example

And there you have it. We now have a companion indicator to the RSI which will draw visual signals whenever price enters an extreme market condition!

You will notice that I have included some comments in the final source code. From now on I would strongly suggest doing this with your scripts as they are going to become a lot more complicated from here on out.

In the next lesson I will demonstrate how to add alerts to this script.

Pine Script Coding Resources

Now that you have a basic understanding of what composes a script, you may need to consult the Pine Script reference manual quite often to find examples and explanations of what certain operators and functions do.

You can find the Pine Script Language Reference Manual here.

There is also a number of other valuable Pine Script teaching resources available online. TradingView have their own Wiki page with tutorial examples which you can find here.

And there is one other resource that I used a lot when I first began learning Pine Script which you can find here. There are a lot of useful articles covering all kinds of topics.

My goal with these lessons is not to teach you everything about Pine Script, but to give you practical examples and tutorials on how to make tools that will help make your backtesting and live trading easier.

Advanced Course

If you want to take your Pine Script coding to the next level, then I think you’ll be interested in my Pine Script Mastery Course.

If you liked this free content then I promise that you’ll love my premium content where I am able to go into much greater detail and help answer students’ questions!

Source Code

study(title="Lesson 4", shorttitle="RSI Signal", overlay=true)

// Get user input
rsiSource = input(title="RSI Source", type=input.source, defval=close)
rsiLength = input(title="RSI Length", type=input.integer, defval=14)
rsiOverbought = input(title="RSI Overbought Level", type=input.integer, defval=80)
rsiOversold = input(title="RSI Oversold Level", type=input.integer, defval=20)

// Get RSI value
rsiValue = rsi(rsiSource, rsiLength)
isRsiOB = rsiValue >= rsiOverbought
isRsiOS = rsiValue <= rsiOversold

// Plot signals to chart
plotshape(isRsiOB, title="Overbought", location=location.abovebar,, transp=0, style=shape.triangledown, text="OB")
plotshape(isRsiOS, title="Oversold", location=location.belowbar,, transp=0, style=shape.triangleup, text="OS")


Free Premium Charts!

5 2 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
graham newlands
graham newlands
3 years ago

Hi, I’m looking at doing something a bit tricky so hoping for some help on how I can script. I am using 2 HMA indicators on my chart and I want to be able set alerts when the property of both HMA lines match the same colour e.g. If I have a 200 HMA then I want an alert when the 50 HMA matches the same colour as the 200 HMA *slower) moving average. Is this possible as I have the code for the HMA (see below) and I think this would help a lot of folkes when trying to… Read more »

3 years ago

Thanks mate, I have already coded and works a treat :-)

3 years ago

amazing lessons! but there’s an issue with your code:
this block:
“plotshape(isRsiOB, title= “Overbought”, location=location.abovebar,, transp=0, style=shape.triangledown, text=”OB”)
plotshape(isRsiOS, title= “Oversold”, location=location.belowbar,, transp=0, style=shape.triangleup, text=”OS”)”
contains faulty variables: “isRsiOS” and “isRsiOB” when you meant to write: “rsiOB” “rsiOS”, which are the previously declared variables here. Cheers!

2 years ago

great resource! i am new to coding and appreciate greatly if someone could provide some guidance.

I am trying to code the following
1) plot the highest and lowest RSI value in the previous day and
2) an alert to ping me when today’s RSI crosses the previous days high/low

Taking what you have already coded for RSI, i tried adding the following code to do 1)plot RSI value high/low of previous day but it comes back as error. Kindly point out if i have done anything wrong. Thank you.

RSIHighest = highest(rsi,”D”)
RSILowest = lowest(rsi,”D”)


2 years ago

Thanks for the content very useful i was wondering if you could help would be extension of this script i’m guessing… how would i pull RSI data from multiple time frames 5,15,1hr etc and have alert trigger when everything up to Hrly “stacked signals ” say is OB or OS

2 years ago

The indicator gives several signals in a row. What can i do so that only one signal is displayed.

1 year ago

I have placed this on my chart but it is not indicating OS, OB mark on the chart…What coul be mistake? It is not working.

1 year ago

lesson 2 was easy……he he