The RSI
calculated in R is different than that I see in Yahoo-Finance. Yahoo-Finance documentation says it uses SMA
and close
. I done the same in R but the values don't match. I am not sure which one to trust. I tried both close & Adjusted close.
library(quantmod)
getSymbols("AAPL", from = '2021-05-01', to = '2021-06-16')
price <- Cl(AAPL) #Close Price
RSI(price, SMA, n = 5)
R output of RSI(5, Close):
AdjClose <- Ad(AAPL) # Adjusted close
RSI(AdjClose, SMA, n = 5)
RSI calculations use an EMA, not an SMA. Not sure where you got the yahoo description, but if it says it uses an sma, it is incorrect. The up and down calculations of the rsi are a typical ema calculation. Most packages / software use the Wilder's smoothing, but a few do not. By default quantmod does use the Wilder's smoothing in the RSI calculation. But if you specify EMA and no wilder = TRUE
in the RSI function, the smoothing is not done.
To get the same figures as yahoo, you need to use the basic RSI function from quantmond (TTR).
tail(RSI(price, n = 5))
rsi
2021-06-08 63.28863
2021-06-09 66.53765
2021-06-10 51.60627
2021-06-11 63.91243
2021-06-14 79.97755
2021-06-15 69.58576
or if you want to fill in all the details:
tail(RSI(price, EMA, n = 5, wilder = TRUE))
rsi
2021-06-08 63.28863
2021-06-09 66.53765
2021-06-10 51.60627
2021-06-11 63.91243
2021-06-14 79.97755
2021-06-15 69.58576