Quantmod, or known as Quantitative Financial Modelling & Trading Framework, is a useful library to gather and analyse quantitative financial trading strategies through stock prices. In this analysis, we will be using quantmod library to mine and chart stock prices data. Additionally, we will also be using other libraries to do time series and forecasting using the data mined.
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(forecast) #plot autocorrelation
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
Define start and end date. Let’s look at the data since beginning of the last year.
There are many data sources that are available, except of course for our favorite search engine i.e. Google Finance who stopped providing data since March 2018. Will be using JPM, simply because I own this shares.
## '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.
## [1] "JPM"
View top 6 rows of the datasets
head(JPM)
## JPM.Open JPM.High JPM.Low JPM.Close JPM.Volume JPM.Adjusted
## 2018-01-02 107.63 108.02 106.81 107.95 13578800 103.1568
## 2018-01-03 107.86 108.49 107.48 108.06 11901000 103.2620
## 2018-01-04 108.36 110.03 108.20 109.04 12953700 104.7412
## 2018-01-05 109.26 109.55 107.78 108.34 14155000 104.0688
## 2018-01-08 108.15 108.68 107.70 108.50 12466500 104.2225
## 2018-01-09 108.72 109.63 108.49 109.05 13292300 104.7509
Stock market is volatile, so if we were looking at let’s say 2019 data, it would show JPM stock prices could change dramatically, either up or down, over a short period of time. However it should be adjusted and smooth out over time (of course provided it is a good company and stable economy).
plot(JPM[, "JPM.Close"], main="JPM Stock Prices at closing")
Let’s take a look at the last 3 months candlestick charts, which is a wonderful visual aid that help us to see the price action e.g. whether higher or lower closing price prior to prior candle’s close, etc.
candleChart(last(JPM, "3 months"), theme="white", color.vol=TRUE, multi.col=TRUE)
Adding moving average convergence divergence and bollinger bands to the chart series is a nice option. The MACD shows the two moving averages of JPM’s price, and it is usually used to gauge the strength of stock price movement. Bollinger bands serve as a way to measure the width between the upper and lower bands.
This plot shows that the autocorrelation at lag 1 is close to perfect correlation, which makes sense because the stock price is usually very close to the prior day and this applies to JPM as well.Autocorrelations up to lag seven (if you want, change lag.max to 25) are greater 0.7 which indicates high association up to a week. The 2 parallel blue lines are the confidence interval at 0.95.
ggAcf(JPM[, "JPM.Close"], lag.max = 200)
The partial autocorrelation is simply a conditional correlation, that shows the correlation between observations of a time series that separated by k time units.
ggPacf(JPM[, "JPM.Close"], lag.max=20)
These plots will help to determine the next step, in deciding ARIMA model, whether AR or MA terms are needed to correct any autocorrelation.