# RSI Strategy Indicator with R

Last Update: February 14, 2020

Strategy indicators consist of identifying trend-following or mean-reversion asset price patterns. Main indicators include single or multiple, lagging or leading technical indicators.

This topic is part of Advanced Trading Analysis with R course. Feel free to take a look at Course Curriculum.

This tutorial has an educational and informational purpose and doesn’t constitute any type of trading or investment advice. All content, including code and data, is presented for personal educational use exclusively and with no guarantee of exactness of completeness. Past performance doesn’t guarantee future results. Please read full Disclaimer.

An example of mean-reversion leading strategy indicator is relative strength index RSI  which consists of bounded oscillator that measures an asset prices trend strength or weakness. Fourteen days are commonly used for its calculation.

1. Strategy indicator calculation.

1.1. Average gain and loss calculation.

Where  = current period close prices fourteen days average gain, = current period close prices fourteen days average loss absolute value,  = initial close prices fourteen days average gain,  = initial close prices fourteen days average loss absolute value, = current period close prices gain,   = current period close prices loss absolute value,  = current period close prices gain or loss,  = current period close prices.

1.2. Relative strength calculation.

Where  = current period close prices fourteen days relative strength,  = current period close prices fourteen days average gain,  = current period close prices fourteen days average loss absolute value.

1.3. Relative strength index calculation.

Where = current period close prices fourteen days relative strength index,  = current period close prices fourteen days relative strength.

2. R script code example.

library('TTR')
library('quantmod')

2.2. RSI strategy indicator data reading.

• Data: S&P 500® index replicating ETF (ticker symbol: SPY) daily open, high, low, close, adjusted close prices and volume (2016).
SPY <- read.csv('RSI-Strategy-Indicator-Data.txt',header=T)
SPY <- xts(SPY[,2:7],order.by=as.Date(SPY[,1]))

2.3. RSI strategy indicator calculation and chart.

• Relative strength index RSI strategy indicator number of periods not fixed and only included for educational purposes.
rsi <- RSI(Cl(SPY),n=14)
barChart(SPY,theme=chartTheme("white"))
addRSI(n=14)

 J. Welles Wilder Jr. “New Concepts in Technical Trading Systems”. Commodities Magazine (now Futures Magazine). 1978.

 Joshua Ulrich. “TTR: Technical Trading Rules”. R package version 0.23-5. 2019.

Jeffrey A. Ryan and Joshua M. Ulrich. “quantmod: Quantitative Financial Modelling Framework”. R package version 0.4-15. 2019.

+