7.3 Technical Indicators
Technical trading rule (TTR) package is loaded when we load quantmod package.
If you want to use it separately, then just load the package as usual:
install.packages("TTR")
library(TTR)
7.3.1 Simple Moving Average
sma <-SMA(Cl(AAPL),n=20)
tail(sma,n=5)
## SMA
## 2019-04-09 190.7990
## 2019-04-10 191.7445
## 2019-04-11 192.5055
## 2019-04-12 193.1430
## 2019-04-15 193.7035
7.3.2 Exponential moving average
ema <-EMA(Cl(AAPL),n=20)
tail(ema,n=5)
## EMA
## 2019-04-09 190.5737
## 2019-04-10 191.5305
## 2019-04-11 192.2371
## 2019-04-12 192.8688
## 2019-04-15 193.4747
7.3.3 Bollinger band
bb <-BBands(Cl(AAPL),s.d=2)
tail(bb,n=5)
## dn mavg up pctB
## 2019-04-09 180.8742 190.7990 200.7238 0.9383450
## 2019-04-10 181.8604 191.7445 201.6286 0.9489799
## 2019-04-11 182.8662 192.5055 202.1448 0.8342832
## 2019-04-12 183.5912 193.1430 202.6948 0.7997868
## 2019-04-15 184.1045 193.7035 203.3025 0.7878678
7.3.4 Momentum
We calculate 2-day momentum based on closing price of AAPL.
M <- momentum(Cl(AAPL), n=2)
head (M,n=5)
## AAPL.Close
## 2007-01-03 NA
## 2007-01-04 NA
## 2007-01-05 0.178571
## 2007-01-08 -0.027143
## 2007-01-09 1.074286
7.3.5 ROC
ROC <- ROC(Cl(AAPL),n=2)
# 2-day ROC
head(ROC,n=5)
## AAPL.Close
## 2007-01-03 NA
## 2007-01-04 NA
## 2007-01-05 0.014806276
## 2007-01-08 -0.002220547
## 2007-01-09 0.084725818
7.3.6 MACD
macd <- MACD(Cl(AAPL), nFast=12, nSlow=26,
nSig=9, maType=SMA)
tail(macd,n=5)
## macd signal
## 2019-04-09 2.993448 3.539602
## 2019-04-10 2.991384 3.441817
## 2019-04-11 3.015484 3.350873
## 2019-04-12 2.921857 3.255855
## 2019-04-15 2.835048 3.159183
7.3.7 RSI
rsi = RSI(Cl(AAPL), n=14)
tail(rsi,n=5)
## rsi
## 2019-04-09 74.58639
## 2019-04-10 75.74467
## 2019-04-11 70.57922
## 2019-04-12 70.33177
## 2019-04-15 70.82741