Discover more from All About Trading!
Coding One of the Best Trading Strategies in TradingView
Coding a Powerful Reversal Strategy on the RSI Using Pine Script
Simple strategies can be as powerful as complex strategies. This article presents one of my favorite strategies used with the RSI.
For a complete collection of candlestick patterns in detail with back-tests and technical strategies, you can check out my newest book with O’Reilly Media. The book features a huge number of classic and modern candlestick patterns as it dwelves into the realm of technical analysis with different trading strategies. The book comes with its own GitHub and is dynamic in nature as it is continuously updated and questions are answered on the O’Reilly platform promptly.
The RSI Simple Reversal Strategy
The RSI is known as the number one technical bounded indicator. With the huge number of techniques and strategies applied on it, one particular technique always arises. The pull-back technique and more specifically the simple pull-back technique awaits the confirmation of the RSI’s exit from the oversold and overbought levels before initiating a signal.
The trading conditions of the simple pull-back strategy are as follows:
A long (buy) signal is generated whenever the RSI surpasses the oversold level and shapes a comeback to it without surpassing a threshold level. For example, the 5-period RSI surpasses 20, does not surpass 33, and then comes back to a level between 20 and 33.
A short (sell) signal is generated whenever the RSI breaks the overbought level and shapes a comeback to it without breaking a threshold level. For example, the 5-period RSI breaks 80, does not break 67, and then comes back to a level between 80 and 67.
The next Figure shows an example of two bullish trades generated using the strategy.
The next Figure shows an example of a bearish trade generated using the strategy.
The levels in red are the overbought and oversold levels while the levels in orange are the threshold levels.
Coding the Strategy in TradingView
Coding the strategy in TradingView is straightforward and simple. The way we will do this is by marking the signals with green or red arrows as the following chart shows.
The code in Pine Script is as follows:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Sofien-Kaabar //@version=5 indicator("Simple Pull-Back", overlay = true) lookback = input(defval = 5, title = 'Lookback') lower_barrier = input(defval = 20, title = 'Lower Barrier') lower_threshold = input(defval = 33, title = 'Lower Threshold') upper_barrier = input(defval = 80, title = 'Upper Barrier') upper_threshold = input(defval = 67, title = 'Upper Threshold') rsi = ta.rsi(close, lookback) buy_signal = rsi >= lower_barrier and rsi < rsi and rsi > lower_barrier and rsi < lower_threshold and rsi < lower_barrier sell_signal = rsi <= upper_barrier and rsi > rsi and rsi < upper_barrier and rsi > upper_threshold and rsi > upper_barrier plotshape(buy_signal, style = shape.triangleup, color = color.green, location = location.belowbar, size = size.small) plotshape(sell_signal, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.small)
The following Figure shows another signal chart.
If you want to see how to create all sorts of algorithms yourself, feel free to check out Lumiwealth. From algorithmic trading to blockchain and machine learning, they have hands-on detailed courses that I highly recommend.
Learn Algorithmic Trading with Python Lumiwealth
Learn how to create your own trading algorithms for stocks, options, crypto and more from the experts at Lumiwealth. Click to learn more
To sum up, what I am trying to do is to simply contribute to the world of objective technical analysis which is promoting more transparent techniques and strategies that need to be back-tested before being implemented. This way, technical analysis will get rid of the bad reputation of being subjective and scientifically unfounded.
I recommend you always follow the the below steps whenever you come across a trading technique or strategy:
Have a critical mindset and get rid of any emotions.
Back-test it using real life simulation and conditions.
If you find potential, try optimizing it and running a forward test.
Always include transaction costs and any slippage simulation in your tests.
Always include risk management and position sizing in your tests.
Finally, even after making sure of the above, stay careful and monitor the strategy because market dynamics may shift and make the strategy unprofitable.