How I Used TradingView To Create And Backtest An RSI Trading Strategy (Step By Step)

Last Updated on August 26, 2023

Let’s see how to build a trading strategy based on the popular RSI indicator using TradingView (step by step) in the crypto market, from building the layout to implementing its native Pine Script language.

Our RSI-based trading strategy will seek reversals in the overbought and oversold zones. We will use DOT on a m5 timeframe with a 14-period RSI and 65/35 for overbought and oversold levels, respectively. Our Long signal will be when the RSI exits the oversold zone. Our Short signal will be when the RSI exits the overbought zone. We will exit the market at the mid level of the RSI (50).

Related reading: Free TradingView trading strategies

What is TradingView?

TradingView is one of the most widely used technical analysis platforms today due to its easy and intuitive interface, great data visualization capabilities, and above all, it can be used completely free, although with some limitations.

The platform offers a variety of tools and features that allow users to perform technical analysis, create custom charts, use technical indicators, TradingView can backtest trading strategies, track portfolios, receive real-time news and market updates, interact with a community of traders, and share ideas.

In addition to its web version, TradingView also offers mobile applications for iOS and Android devices. In the context of this article, we will focus on the tools that TradingView web allows us to use for free.

How to turn on RSI in TradingView?

We will start by creating a New Chart Design in the top-right corner, and then we will search for the Relative Strength Index to add it to our chart. To do this, click on Indicators and type “RSI” in the search bar.

The indicator will appear at the bottom of our chart with its default parameters. If you want to modify it, simply hover over the indicator’s name, and the Settings icon will appear. Once the window is open, we can modify the indicator’s parameters as desired.

How I Used Tradingview To Create And Backtest An RSI Trading Strategy

How to use RSI strategy in TradingView?

The RSI is used to measure the strength and speed of price movements of an asset, whether it’s upward or downward. It provides information about whether an asset is overbought or oversold, which can help investors and traders make informed decisions.

The levels of overbought and oversold are subjective to each trader and the asset they are analyzing, and they can represent extended price movements in a particular direction.

How to set up RSI in TradingView?

The RSI is calculated using a mathematical formula that compares the magnitude of upward and downward movements over a specific period of time.

The indicator oscillates between 0 and 100, and a value above 70 is generally considered to indicate that an asset is overbought, which may imply a potential downward price reversal. On the other hand, a value below 30 indicates that an asset is oversold, suggesting a potential upward reversal.

This is what its Settings window looks like.

Best RSI settings in TradingView

TradingView RSI strategy script

We can start by manually recognizing our trading rules on the chart using lines to mark the points where our buy and sell operations should be executed, and then begin using Pine Script to plot the results on our chart.

We will select the Pine Editor tab at the bottom of our screen, which will open the editor for us to start writing our strategy.

By default, TradingView provides us with a script for indicators, but we will be writing/coding a strategy. To do this, in the top right corner of the editor, we will click on “Open” and select “Strategy” in the Templates section.

The example code shows rules for a moving average crossover strategy, but we will be using RSI. Therefore, we will start by renaming the script to “RSI Simple Strategy,” and even in the first line of code strategy(), we can assign it a short name. Then, we will delete everything below the script header so that we only have the following remaining:

Coding an RSI strategy in TradingView

TradingView has a library that includes many technical indicators. For the RSI, it is enough to write “ta.rsi” for Pine Script to automatically provide us with the built-in function as an option. In order for our function to return the corresponding values, it needs two parameters to perform the calculations, the first one being the Source and the second one being the Length. For our strategy, the RSI function will be as follows:

RSI settings in TradingView

How do you use RSI to buy and sell in TradingView?

Now we need to declare our conditions to execute our orders. Let’s remember that we want to enter the market with a Long operation when the RSI is coming out of the oversold zone, so our code will be written as follows:

longCondition = rsi > 35 and rsi[1] < 35

And for our Shorts, we want to exit the overbought zone:

shortCondition = rsi < 65 and rsi[1] > 65

Perfect! We already have our RSI declared with the necessary parameters for its calculation and the conditions that determine the entry and exit of our trading strategy in TradingView.

In Pine Script, to initiate a market operation, a command called strategy.entry is used, which will later require arguments for the direction of the operation, as well as many other useful parameters for all kinds of scenarios one can imagine.

But for our example, we only need a few of these arguments: the ID and the direction.

Pine Script has several ways to close positions, in our case, we will use strategy.close, to which we must indicate the order we want to close. Like strategy.entry, it has several arguments for all kinds of situations, but simply providing the order ID we want to close is enough.

This is how we build a simple trading strategy with RSI in Tradingview and Pine Script, and we will soon see its trading performance and statistics. However, we must effectively instruct the code to enter and exit the market only when our conditions are valid. To achieve this, we use a conditional statement, known as IF, which is present in every programming language.

RSI trading strategy backtest in TradingView

From this point on, we have our script ready to be compiled by TradingView, but we will make one final addition to avoid touching the code of our strategy if we want to change the parameters of the RSI.

This is achieved by adding user inputs to our code so that they are available in the strategy’s settings window.

First, the option to change the period of the RSI:

Second, the overbought and oversold values of our strategy:

We will replace the previously declared numerical values with our user inputs in the code. This way, when we want to modify values in the settings window of our strategy, the changes will be immediately applied to the calculations in the script.

To close positions, we will simply look for the RSI to cross the middle level of 50.

Tradingview RSI strategy backtest

We are ready to test our RSI strategy on TradingView!

It’s time to click on “Add to chart” in the Pine Script editor and voila! Our strategy is already giving us results. We have useful data such as the equity curve, net profit, drawdown, and win rate at our fingertips.

The equity curve shown by TradingView is limited to the amount of data displayed on the chart, meaning there is a limit to the historical candles shown, and TradingView applies our strategy based on this.

In our chart, we have two months of 5-minute candles, and its equity curve looks like this:

Tradingview RSI strategy backtest

As we have already shown in previous articles, the RSI alone does not perform very well or consistently, which is why it is just one tool among many in a trader’s toolkit that benefits from complementing it with other indicators or criteria.

How do you use deep backtesting in TradingView?

If we have a Pro or better account, we can run a “deep backtest”, which is limited to the historical data available on TradingView for a particular asset. For DOT, we have data since 2019 and can see the performance historically. We can use specific date intervals or simply run our logic from the first available day with data for the asset. The curve of our strategy is much more promising with a larger amount of historical data, which allows us to model and optimize our strategies better.

Deep backtesting TradingView

The green line is the equity curve, while the purple is showing the drawdowns.

In the Performance Summary tab, we will find statistics about our strategy, such as Sharpe Ratio, Average R:R, and much more. In the List of Trades, we can see all the trades performed by the backtest and even export all this data in CSV format to analyze it separately.

Here is a screenshot of how it provides us with some metrics:

Statistics and performance metrics for RSI strategy in TradingView

This is how our operations look like:

Trading rules and setup RSI strategy in TradingView

For free users, the Export data option is not available. To solve this inconvenience, we recommend checking out a post by QuantNomad that provides a solution for analyzing our data in an Excel spreadsheet.

TradingView RSI trading strategy – final thoughts and conclusion

As you could see in this step-by-step guide, building an RSI trading strategy in TradingView doesn’t have to be complicated to start getting familiar with the platform and its native Pine Script language. It allows us to have a clear visualization of the conditions we define and presents data in a clear and practical manner.

With this, along with the extensive content from Quantified Strategies on how to analyze strategies, you should have robust tools to make informed decisions about your systems and strategies.

Similar Posts