Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Let’s download stock prices using getSymbols() function in quantmod R library. I hope he will come back soon.

It is easy to download. The first thing is to find the ticker for the security. The second thing is to run the following R code with ticker. Tickers are easily found in search engines such as Google.

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 #=========================================================================## Financial Econometrics & Derivatives, ML/DL using R, Python, Tensorflow  # by Sang-Heon Lee #————————————————————————-## Retreiving Stock Price using quantmod#=========================================================================#library(quantmod)library(xts)library(ggplot2)library(gridExtra) # grid.arrange graphics.off()rm(list=ls()) # Data Rangesdate <– as.Date(“2018-07-01”)edate <– as.Date(“2019-12-31”) # Samsung Electronics (005930), Naver (035420)ss_stock=getSymbols(‘005930.KS’,from=sdate,to=edate,auto.assign = F)nv_stock=getSymbols(‘035420.KS’,from=sdate,to=edate,auto.assign = F) # Typically use previous value for NAno.na <– which(is.na(ss_stock[,6]))      # no for NAss_stock[no.na,6] <– ss_stock[no.na–1,6] no.na <– which(is.na(nv_stock[,6]))nv_stock[no.na,6] <– nv_stock[no.na–1,6]  # Only stock pricess_price <– ss_stock[,6]nv_price <– nv_stock[,6] # log return using adjusted stock pricess_rtn <– diff(log(ss_price),1)nv_rtn <– diff(log(nv_price),1) # draw graphx11(width=5.5, height=6)plot1<–ggplot(ss_price, aes(x = index(ss_price), y = ss_price)) +    geom_line(color = “blue”, size=1.2) +     ggtitle(“SEC stock price”) + xlab(“Date”) + ylab(“Price(￦)”) +     theme(plot.title = element_text(hjust = 0.5)) +     scale_x_date(date_labels = “%y-%m”, date_breaks = “3 months”) plot2 <– ggplot(ss_rtn, aes(x = index(ss_rtn), y = ss_rtn)) +    geom_line(color = “red”, size=1.2) +     ggtitle(“SEC stock return”) + xlab(“Date”) + ylab(“Return(%)”) +     theme(plot.title = element_text(hjust = 0.5)) +     scale_x_date(date_labels = “%y-%m”, date_breaks = “3 months”) grid.arrange(plot1, plot2, ncol=1, nrow = 2) x11(width=5.5, height=6)plot1<–ggplot(nv_price, aes(x = index(nv_price), y = nv_price)) +    geom_line(color = “blue”, size=1.2) +     ggtitle(“Naver stock price”) + xlab(“Date”) + ylab(“Price(￦)”) +     theme(plot.title = element_text(hjust = 0.5)) +     scale_x_date(date_labels = “%y-%m”, date_breaks = “3 months”) plot2 <– ggplot(nv_rtn, aes(x = index(nv_rtn), y = nv_rtn)) +    geom_line(color = “red”, size=1.2) +     ggtitle(“Naver stock return”) + xlab(“Date”) + ylab(“Return(%)”) +     theme(plot.title = element_text(hjust = 0.5)) +     scale_x_date(date_labels = “%y-%m”, date_breaks = “3 months”) grid.arrange(plot1, plot2, ncol=1, nrow = 2)Colored by Color Scripter cs

Running the above R code downloads the daily stock prices of Samsung Electronic Company(SEC) and Naver from 2018.07 to 2019.12 and calculates daily returns and draws a chart.

From this work, we can prepare historical stock prices for the further analysis. You can change the period to include recent data. $$\blacksquare$$