In this post, we discuss a straightforward trading strategy based on the Relative Strength Index (RSI) and demonstrate how to implement it using the QuantConnect platform. We also share insights about the strategy's performance, including a detailed analysis of key metrics.

The Strategy

The strategy is based on two simple rules:

1. **Entry:** If the RSI(2) of SPY falls below 15, it signals an oversold market condition. The strategy takes this as a buying opportunity and enters a long position at the close of the day.
2. **Exit:** The strategy closes the position if today's closing price is higher than yesterday's high.

This approach uses the RSI, a popular momentum oscillator, to identify potential buy opportunities in oversold market conditions. The RSI is calculated based on recent price changes, and an RSI period of 2 makes it very sensitive to the latest movements. The RSI lower threshold is set aggressively at 15 (compared to the commonly used level of 30), allowing the strategy to react to more extreme oversold conditions.

Implementation on QuantConnect

QuantConnect provides a robust platform for backtesting and live trading of such strategies. For our strategy, we particularly utilize the QuantConnect's Roll∈gW∈dow and TradeBar features.

Roll∈gW∈dow is used to store the trailing data, keeping the strategy's memory footprint low and providing efficient access to historical data points. It is used in this strategy to maintain the high prices of the last two days.

TradeBar is a data type in QuantConnect that represents a period in a financial security's trading session. In this strategy, we use daily TradeBar data of SPY for calculating the RSI and determining entry and exit points.

Performance Analysis

The backtest results of the strategy from 2010 to 2022 show promising returns with a net profit of 158.251% and a compounding annual return of 7.568%. The strategy executed a total of 852 trades during this period, with a win rate of 62%.

81860_1690075039.jpgSimple RSI Equity Curve

However, the strategy experienced a significant drawdown of 23.5%. In particular, the years between 2018 and 2021 saw a downturn in the portfolio value. This could be due to a combination of factors, including adverse market conditions during that period (including the COVID-19 market crash), limitations in the strategy's model, high transaction costs, or the strategy being overfitted to past market conditions.

  1. import numpy as np
  2. from QuantConnect.Algorithm import QCAlgorithm
  3. from QuantConnect.Indicators import *
  4. from QuantConnect import Resolution
  5. from QuantConnect.Data.Market import TradeBar
  6. class RsiCloseAlgorithm(QCAlgorithm):
  7. def Initialize(self):
  8. self.SetStartDate(2010, 1, 1) # Set the start date for backtesting
  9. self.SetEndDate(2023, 1, 1) # Set the end date for backtesting
  10. self.SetCash(100000) # Set the initial cash balance for backtesting
  11. # Define the symbol we want to trade
  12. self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
  13. # RSI parameters
  14. self.rsi_period = 2
  15. self.rsi_lower = 15
  16. # Set up the RSI indicator
  17. self.rsi = self.RSI(self.symbol, self.rsi_period, MovingAverageType.Simple, Resolution.Daily)
  18. # Initialize a RollingWindow to keep track of past TradeBars
  19. self.barWindow = RollingWindow[TradeBar](2)
  20. def OnData(self, data):
  21. # Add the current TradeBar to the RollingWindow
  22. self.barWindow.Add(data[self.symbol])
  23. # Skip if the RollingWindow is not ready
  24. if not self.barWindow.IsReady:
  25. return
  26. # Get the past high price from the RollingWindow
  27. pastHigh = self.barWindow[1].High
  28. # Check if RSI is lower than the defined threshold
  29. if self.rsi.Current.Value < self.rsi_lower:
  30. self.SetHoldings(self.symbol, 1.0)
  31. # Check if today's close is higher than yesterday's high
  32. if self.Portfolio[self.symbol].Invested and self.Securities[self.symbol].Close > pastHigh:
  33. self.Liquidate(self.symbol)
  34. # Plot RSI on chart
  35. self.Plot("Indicators", "RSI", self.rsi.Current.Value)
  36. self.Plot("Indicators", "Oversold Level", self.rsi_lower)
+ Expand

 

Conclusion

This simple RSI-based trading strategy shows potential for profitable trades, but like all strategies, it has periods of drawdowns and may not perform well in all market conditions. Regular performance monitoring, tweaking the parameters, and considering other factors such as transaction costs and market impact can help improve the strategy's performance.

QuantConnect provides a powerful and flexible platform for developing, backtesting, and deploying algorithmic trading strategies. With features like Roll∈gW∈dow for efficient data handling and TradeBar for capturing market data, QuantConnect makes it easier to implement and test a wide range of trading strategies.

Author

OkiTrader

July 2023