Pine Script – Lesson 4: Generating RSI Signals
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”.
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:
//@version=4 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) plot(na)
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:
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:
rsiOB = rsiValue >= rsiOverbought rsiOS = 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(rsiOB, title= "Overbought", location=location.abovebar, color=color.red, transp=0, style=shape.triangledown, text="OB") plotshape(rsiOS, title= "Oversold", location=location.belowbar, color=color.green, 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:
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.
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!
//@version=4 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, color=color.red, transp=0, style=shape.triangledown, text="OB") plotshape(isRsiOS, title="Oversold", location=location.belowbar, color=color.green, transp=0, style=shape.triangleup, text="OS")