Site icon R-bloggers

Retrieving Stock Price using R

[This article was first published on K & L Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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.

< !--반응형 last AD....-->< !--horizontal001-->
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.

< !-- HTML generated using hilite.me -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#=========================================================================#
# 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 Range
sdate < 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 NA
no.na < which(is.na(ss_stock[,6]))      # no for NA
ss_stock[no.na,6< ss_stock[no.na1,6]
 
no.na < which(is.na(nv_stock[,6]))
nv_stock[no.na,6< nv_stock[no.na1,6
 
# Only stock price
ss_price < ss_stock[,6]
nv_price < nv_stock[,6]
 
# log return using adjusted stock price
ss_rtn < diff(log(ss_price),1)
nv_rtn < diff(log(nv_price),1)
 
# draw graph
x11(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)
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\)

To leave a comment for the author, please follow the link and comment on their blog: K & L Fintech Modeling.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.