Skip to content
Home » Scripts » The RSI Indicator-based Strategy

The RSI Indicator-based Strategy

Want to master the RSI Indicator-based Strategy? This essential tool, known as the Relative Strength Index (RSI), offers traders a robust method to gauge the power and direction of price swings. J. Welles Wilder Jr. created RSI. It tracks how fast and how much prices shift, with readings moving between 0 and 100.

Here’s how to read it: If the RSI value surpasses 70, the market might be overbought. It suggests that the asset’s price could be higher than its actual value, and a price correction may be imminent. On the flip side, if the RSI value is under 30, we could be looking at an oversold market. It suggests the asset could be undervalued and a price rise might be on the cards.

So how does one use this in an RSI Indicator-based Strategy? Simple. You enter or exit trades based on these RSI values. Traders often spot buying opportunities when the RSI moves above 30, signaling a potential upward price movement. Similarly, a move below 70 could be a signal to sell or exit long positions, hinting at a possible price drop.

Let’s take this concept further with a Python code example on QuantConnect. This demo shows how you can use the RSI indicator to trigger buy and sell signals in an RSI Indicator-based Strategy.

In the code example, we target the SPY ETF. We use a 14-day RSI coupled with Wilder’s Moving Average. When the RSI dips below 30, indicating an oversold market, the algorithm buys. When the RSI value climbs over 70, pointing to an overbought market, it sells.

But remember, before you run this algorithm on QuantConnect, adjust the start date, end date, and other parameters to match your needs. With the RSI Indicator-based Strategy, you can make more informed trading decisions and better navigate the financial markets.

Click on the code to copy it to your clipboard.
Click here for detailed instructions on how to use the scripts in QuantConnect.

Python
from AlgorithmImports import *

class RsiIndicatorStrategy(QCAlgorithm):

    def Initialize(self):
        # Set the start and end date for the backtest
        self.SetStartDate(2022, 1, 1)
        self.SetEndDate(2022, 12, 31)
        # Set the initial cash balance
        self.SetCash(10000)

        # Add the SPY ETF with daily resolution
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        # Create a 14-day RSI indicator with Wilder's Moving Average
        self.rsi = self.RSI(self.symbol, 14, MovingAverageType.Wilders, Resolution.Daily)

        # Set a warm-up period to ensure the RSI indicator is ready
        self.SetWarmUp(20)

    def OnData(self, data):
        # Skip processing during the warm-up period
        if self.IsWarmingUp:
            return

        # Skip processing if the RSI indicator is not ready
        if not self.rsi.IsReady:
            return

        # Buy when the RSI value goes below 30 (oversold condition)
        if not self.Portfolio.Invested and self.rsi.Current.Value < 30:
            self.SetHoldings(self.symbol, 1.0)
            self.Debug(f"Buy signal at {self.Time}")

        # Sell when the RSI value exceeds 70 (overbought condition)
        elif self.Portfolio.Invested and self.rsi.Current.Value > 70:
            self.Liquidate(self.symbol)
            self.Debug(f"Sell signal at {self.Time}")
C#
using System;
using QuantConnect.Data;
using QuantConnect.Indicators;
using QuantConnect.Algorithm;

namespace QuantConnect.Algorithms
{
    public class RsiIndicatorAlgorithm : QCAlgorithm
    {
        private Symbol _symbol;
        private RelativeStrengthIndex _rsi;

        public override void Initialize()
        {
            // Set the start and end date for the backtest
            SetStartDate(2022, 1, 1);
            SetEndDate(2022, 12, 31);
            // Set the initial cash balance
            SetCash(10000);

            // Add the SPY ETF with daily resolution
            _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
            // Create a 14-day RSI indicator with Wilder's Moving Average
            _rsi = RSI(_symbol, 14, MovingAverageType.Wilders, Resolution.Daily);

            // Set a warm-up period to ensure the RSI indicator is ready
            SetWarmUp(20);
        }

        public override void OnData(Slice data)
        {
            // Skip processing during the warm-up period
            if (IsWarmingUp) return;

            // Skip processing if the RSI indicator is not ready
            if (!_rsi.IsReady) return;

            // Buy when the RSI value goes below 30 (oversold condition)
            if (!Portfolio.Invested && _rsi.Current.Value < 30)
            {
                SetHoldings(_symbol, 1.0);
                Debug($"Buy signal at {Time}");
            }
            // Sell when the RSI value exceeds 70 (overbought condition)
            else if (Portfolio.Invested && _rsi.Current.Value > 70)
            {
                Liquidate(_symbol);
                Debug($"Sell signal at {Time}");
            }
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *