October 30, 2017

Objective

  • These slides will show how to use R package quantmod to download stock prices.
  • Then I will try to show how these can be plotted using Plotly
  • First load the required package
library(quantmod)
library(plotly)

Function to Download and Plot

  • Now create function which takes as input a symbol, downloads data using getSymbols and then does some data manipulations and displays using plotly
plot_sym<-function(sym){
  fin_data<-getSymbols(sym,src="google",env=NULL)
  fin_data1<-as.data.frame(fin_data)
  fin_data1[,6]<-time(fin_data)
  x = list(title = 'Year',
           zeroline = TRUE)
  y = list(title = 'Opening Stock Price (in $)',
           zeroline = TRUE)
  plot_ly(x=fin_data1[,6],y=fin_data1[,1],type="scatter",mode="lines") %>% 
    layout(title="Stock Price",xaxis=x,yaxis=y)
}

Stock Price of Google

  • Create a plot for Google
plot_sym("GOOG")

Stock Price of Amazon

  • Create a plot for Amazon
plot_sym("AMZN")

Stock Price of Apple

  • Create a plot for Apple
plot_sym("AAPL")