Technical Analysis using R Quantmod Package
Sangamesh K S
October 30, 2017
Hi I am very privileged to write this article as I am proud technical Analyst and today I am interested showing what I used to do way back 2014.
What is Technical Analysis?
Technical Analysis is a combination of quants and Time Series Analysis. Late 1980 the development of computer had let to better computation and data visualization that led to the domain of technical analysis.
Let me inform you; this is equivalent to many advance charting softwares like Amibroker, Metastocks etc.
Today in this article I will show you how to plot a chart and other indicators in using R. To plot these charts we require a package called “quantmod”.
Now we will go ahead and load the package
library(quantmod)
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.4.1
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols("^NSEI")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
##
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## Warning: ^NSEI contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "NSEI"
Now we have loaded the data into R and now we will have a look at our dataset.
head(NSEI)
## NSEI.Open NSEI.High NSEI.Low NSEI.Close NSEI.Volume
## 2007-09-17 4518.45 4549.05 4482.85 4494.65 0
## 2007-09-18 4494.10 4551.80 4481.55 4546.20 0
## 2007-09-19 4550.25 4739.00 4550.25 4732.35 0
## 2007-09-20 4734.85 4760.85 4721.15 4747.55 0
## 2007-09-21 4752.95 4855.70 4733.70 4837.55 0
## 2007-09-24 4837.15 4941.15 4837.15 4932.20 0
## NSEI.Adjusted
## 2007-09-17 4494.65
## 2007-09-18 4546.20
## 2007-09-19 4732.35
## 2007-09-20 4747.55
## 2007-09-21 4837.55
## 2007-09-24 4932.20
Now we will plot the data. Now here is a glitch and please note these data come in XTS format and all the plotting functions in quantmod support only XTS.
To plot chart we will use chartseries() a function alike plot for quantmod as these data are in OHLC ticks chartseries is a recommended package from my end.
Now let’s plot the data
chartSeries(NSEI)
Now we have only OHLC and Volume which is plotted on the chart below. Now we will go ahead and plot indicators for the same.
We will plot MACD, RSI and Bollinger Band on the same data
addMACD()
addRSI()
addBBands()