Category: Quantitative Finance

SMA 200 + RSI 2 w/ATR Position Sizing Strategy Analysis

Hello all! For my second post I decided to analyze an aggressive short-term strategy. While researching, I found a good amount of literature supporting the potential of SMA 200 and RSI 2 strategies, so it seemed like a solid place to begin. I was also reading through previous posts on the Quantstrat Trader blog and ATR position sizing caught my attention so I decided to add it to the strategy ( Parts of the code and analysis later in this post was influenced by the Quantstrat Trader blog and Ilya Kipnis himself, so a big thank you to him! Though the tested strategies proved to not be as successful as I had originally hoped, I was able to draw valuable conclusions from the data while improving my backtesting skills.

To start, I analyzed 2 variations of a strategy that used 4 indicators. In my previous post, the Bollinger Bands and RSI strategy used a 14-period RSI indicator. This strategy uses a 2-period RSI, which makes it a much more sensitive momentum indicator. Two simple moving averages, 5 and 200, were used to track short and long term market trends respectively. Finally, a lagging average true range (ATR) indicator, provided by the IKTrading package, was used to monitor market volatility. This allowed me to adjust trade sizes and normalize risk. The first version of the strategy, V1, uses the 200-day SMA to determine the market trend direction (closing price above/below) and the 2-period RSI to identify overbought/oversold situations that could mean revert. It then makes quick exits, hopefully closing a profitable trade on the long or short side, when the mean reversion is done, indicated by the closing price and 5-day SMA relationship. The signals are shown below:

  • Enter long: Close > SMA 200 & RSI 2 < 20
  • Enter short: Close < SMA 200 & RSI 2 > 80
  • Exit long: Close > SMA 5
  • Exit short Close < SMA 5

The second version of the strategy, V2, is similar to V1 except it only enters long side positions and has an additional exit signal based on the 2-period RSI. The signals are shown below:

  • Enter long: Close > SMA 200 & RSI 2 < 20
  • Exit long: Close > SMA 5
  • Exit long: RSI 2 > 80

Both of these strategies were ran with an ATR position sizing function, using a 20-period moving average and risk percentage of 2%. They were backtested on the following ETFs from 2003-2016, and SPY from 2010-2016/2000-2016. The metrics primarily used to analyze the results were profit factor (gross profits/gross losses), percent positive, and annualized Sharpe ratio. Both strategies made about ~10,000 trades across the ETFs 13 year period. Together these metrics give a fairly complete view of the strategy’s profitability and risk factor.


Strategy V1’s Performance

ETFs w/o ATR Position Sizing
ETFs w/ ATR Position Sizing
SPY 2010-2016 2000-2016
ATR? No Yes No Yes
Percent Positive 63.5% 62.8% 64.6% 64.5%
Profit Factor 1.59 1.31 1.72 1.44
Annual Sharpe 1.99 1.51 2.27 2.08

Overall, with or without ATR position sizing, this was not a winning strategy as implemented. Most surprisingly, position sizing negatively impacted the profit factor, percent positive, and annualized Sharpe. The strategy did display its ability to adjust to market volatility.


The above figure shows the strategy’s performance on IYR, the US Real Estate ETF. Looking at the ETF price, position fill (blue columns), and ATR indicator (bottom blue line), you can see the gradual reduction in position sizing leading up to the 2008 recession and the transition to short side positions. The implementation of the ATR position sizing was a definite success in appropriately adjusting the trade sizes based on market volatility which is shown nicely in this figure.

After analyzing the position fill on this ETF as well as others, a common flaw of this strategy seems to be holding long positions too long into a downturn and consequently not entering short positions soon enough. As visible in the figure above, these transitions are quite mistimed. If this strategy could improve these timings I believe it would drastically improve its profitability.

Strategy V2’s Performance

ETFs w/o ATR Position Sizing
ETFs w/ ATR Position Sizing
SPY 2010-2016 2000-2016
ATR? No Yes No Yes
Percent Positive 60.7% 64.8% 61.4% 63.7%
Profit Factor 1.55 1.48 1.40 1.33
Annual Sharpe 1.59 2.15 1.20 1.66

The results of this strategy told a slightly different story. Across the ETFs and SPY, the profit factor was negatively affected by ATR position sizing, but the Sharpe ratio was improved. Initially I hypothesized that the ATR position sizing should increase the Sharpe ratio since it prevents huge losses by decreasing risk volatility, so this was exciting to see.


Once again, visible in the above IYR figure above, the strategy does a fairly good job at sitting out severe downtrends. Strategy V1’s trade-side distribution was about 80% long and 20% short so this strategy was essentially just the long trades with the additional exit signal that may or may not have been beneficial.

I also tested these strategies with more sensitive signals by changing the RSI thresholds to 90/10, but that did not improve their performance. Overall, these strategies let me experiment with a short term trading strategy and the metrics used to evaluate them, which will be useful in future projects. With regard to the ATR position sizing, I strongly believe that it would be much more helpful when applied to a long term trading strategy. In the case of a short term trading strategy, the trade sizes become essentially irrelevant as the number of trades increases making the strategy’s percent positive, win-loss ratio, etc. more important. In a long term trading strategy where positions are potentially held for months, the trade size is much more important. Based on the qualitative impact ATR position sizing had on this strategy, I now understand how much of an essential risk management tool it is.

Another tool used to help adjust for risk that I want to incorporate into my next strategy is trailing stops. The figure below is the MAE plot for strategy V1 which can be used to determine an appropriate trailing stop. For example, a trailing stop of 4% added to this strategy would cut a significant portion of the losses.


Now that I have a basic understanding of Quantstrat and evaluating strategy performance, I plan to focus on finding more profitable strategies in my next projects. I still want to dive into something a little more statistically/mathematically dense. Currently I am taking an advanced applied mathematics course, so maybe I’ll get some ideas in there! Additionally, I eventually want to incorporate machine learning into a strategy. Thanks for reading!

Bollinger Bands and RSI Analysis with Quantstrat

Hello, my name is Colton Smith and this is my first hands on experience backtesting quantitative trading strategies. For the last 6 months quantitative trading has been a personal topic of interest that I have dedicated a lot of time researching to.  I’m currently a senior at the University of Washington, majoring in Industrial and Systems Engineering with a minor in Applied Mathematics. Outside of my major, I have taken computer sceince courses and been heavily involved in the UW Math Club which I am now the President of. With this background and experience I have become very familiar with statistics, probability, and R. This upcoming year I will be taking more computer science courses and continuing quant projects to further prepare myself for internship and job oppurtunites.

The most logical place to begin was the Quanstrat package in R. I wanted to start with a simple strategy that would let me explore the different functions of Quanstrat and play around with some data. Being already familiar with Bollinger Bands, a volatility indicator, I thought it would be interesting to analyze them combined with RSI, a momentum indicator.

Therefore, I chose to analyze this strategy from 01/01/2010 to 12/31/2015 because no combination handled 2008 very well and I wanted consistent data to work with. The Bollinger Bands indicator used a simple moving average with n = 20 and RSI used a weighted moving average with n = 14. The buy signal was when the close was below the lower Bollinger Band and the RSI was below the lower threshold which would indicate the stock may be oversold and could mean revert upwards. The sell signal was when the close was above the upper Bollinger Band and the RSI was above the upper threshold which would indiacte the stock may be overbought and could mean revert downwards. I decided to gather data for the 150 different combinations of Bollinger Band standard deviations (1,1.25,1.5,1.75,2,2.25), upper RSI thresholds (60,65,70,75,80), and lower RSI thresholds (20,25,30,35,40). These ranges were chosen to ensure that all combinations executed a decent amount of trades during this period. Some combinations (<5%) still didn’t execute very many trades, which may have skewed some of the data. These strategies were tested on SPY with a starting account equity of $100,000 and each buy signal making a purchase of 500 shares. I chose to analyze the combination’s performance based on its CAGR, Compound Annual Growth Rate. For reference, the SPY’s CAGR in this time period was 10.6%.

Figure 1
Figure 2

Figure 1 displays the results of each combination. The best performing combination was with Bollinger Bands standard deviations of 1 and the upper/lower RSI thresholds set at 80/40 which had nearly a 40% CAGR. Looking at figure 1 and 2 together you can see how the standard deviation of the strategy’s performance shrinks as larger Bollinger Bands standard deviations are used. This is because when starting at the beginning of figure 1, where lower standard deviations are used, the RSI thresholds are controlling the strategy. However, when the Bollinger Bands standard deviations increase, they dominate the strategy. Additionally, as displayed in figure 2, the convention that using Bollinger Bands with standard deviations of 2 for the best performance, is supported.

As visible from the highlighted combinations in figure 1 and from the table below, strategies with higher upper and lower RSI thresholds performed better because they bought more often and sold less frequently. I believe this worked well because of the upward market trend. The 3d surface plot below helps visualize the influence of the upper and lower RSI thresholds. This plot is with Bollinger Bands standard deviations of 2 and nicely shows how increasing the lower RSI threshold has a drastic effect on the CAGR, much more than the upper RSI threshold does. For reference, the red dots were combinations that didn’t beat the SPY CAGR, the yellow dots were ones that outperformed the SPY CAGR but were below 25%, and the green dots were combinations that had CAGR of above 25%.

Figure 3

Overall, this project was very helpful to learn how to work with multiple signals and paramsets in Quanstrat, as well as some data visualization techniques. Next, I plan to explore a strategy where I can utilize my statistical knowledge with one that makes much more frequent trades so I can analyze risk and profit factors.

Acknowledgements: Thank you to Ernest Chan, Ilya Kipnis, and Brian Peterson for answering my elementary questions about Quantstrat and backtesting.