#1. install quantmod if needed
# install.packages('quantmod')
#2. load quantmod
library('quantmod')
#3. get some data (you may get a warning here about the hyperparameter auto.assign)
amzn = getSymbols('AMZN', src='google', from='2016-06-15', auto.assign=FALSE)
#4. quick exploration of quantmod charting capabilities
chartSeries(amzn, theme=chartTheme('white'), TA = 'addSMA(50)')
#5. get comparison data for FB, GOOG, NFLX
fb = getSymbols('FB', src='google', from='2016-06-15', auto.assign=FALSE)
goog = getSymbols('GOOG', src='google', from='2016-06-15', auto.assign=FALSE)
nflx = getSymbols('NFLX', src='google', from='2016-06-15', auto.assign=FALSE)
#6. take a quick look at what the data looks like
head(nflx)
#7. merge data we want into new object, cast as data.frame
stocks = as.data.frame(merge(fb$FB.Close, amzn$AMZN.Close,nflx$NFLX.Close, goog$GOOG.Close))
#8. see what we have
head(stocks)
#9. change column names
names(stocks) = c('FB', 'AMZN', 'NFLX', 'GOOG')
#10. store daily returns in new dataframe
# quantmod also has a function periodReturn(stock, period='daily, monthly, etc.') and allReturns()
stocks_returns = data.frame(diff(as.matrix(log(stocks))))
#11. check transformed data
head(stocks_returns)
#12. get correlations
cor(stocks_returns)
#13. plot scatter matrix, top row second col x = fb, second row first col x = amzn, etc
plot(stocks_returns)