Learning & Reasoning/R

Financial Time Series Import

이현봉 2015. 12. 21. 01:41

Financial 시계열 구하기 

1.
Reading the financial time series data from the local CSV file
‘quantmod’ package loads xts & zoo package internally
이 경우 local file은 zoo 시계열로 읽기에 적절한 형태로 되어 있어야 함
.

library(quantmod)
## Warning: package 'quantmod' was built under R version 3.2.2
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.2.2
## Version 0.4-0 included new data defaults. See ?getSymbols.
# GSPC는 Yahoo Finance에서 sp500 ticker. 
# First read csv file as zoo type, then transform it to xts. We can also 
# read it as dataframe then transform it to xts. 
GSPC <- as.xts(read.zoo('sp500.csv', header=T))  
plot(GSPC)  # "plot.xts" in called 
## Warning in plot.xts(GSPC): only the univariate series will be plotted

2
Get the financial data from the Web. R은 여러 가지로 웹에서 financial 자료를
갖고 올 수 있게 한다.

library(tseries)
GSPC <- as.xts(get.hist.quote("^GSPC",start="1970-01-02",   # 1970-01-02 ~ 오늘까지
          quote=c("Open", "High", "Low", "Close","Volume","AdjClose")))
## time series ends   2015-12-18
str(GSPC)
## An 'xts' object on 1970-01-02/2015-12-18 containing:
##   Data: num [1:11599, 1:6] 92.1 93 93.5 92.8 92.6 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "Open" "High" "Low" "Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
GSPC <- as.xts(get.hist.quote("^GSPC",   # get data from "yahoo" by default
          start="2000-01-02", end='2010-12-31',
          quote=c("Open", "High", "Low", "Close","Volume","AdjClose")))
## time series starts 2000-01-03
str(GSPC)
## An 'xts' object on 2000-01-03/2010-12-31 containing:
##   Data: num [1:2767, 1:6] 1469 1455 1399 1402 1403 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "Open" "High" "Low" "Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
head(GSPC, 2)
##               Open    High     Low   Close    Volume AdjClose
## 2000-01-03 1469.25 1478.00 1438.36 1455.22 9.318e+08  1455.22
## 2000-01-04 1455.22 1455.22 1397.43 1399.42 1.009e+09  1399.42
last(GSPC, 4)
##               Open    High     Low   Close     Volume AdjClose
## 2010-12-28 1259.10 1259.90 1256.22 1258.51 2478450000  1258.51
## 2010-12-29 1258.78 1262.60 1258.78 1259.78 2214380000  1259.78
## 2010-12-30 1259.44 1261.09 1256.32 1257.88 1970720000  1257.88
## 2010-12-31 1256.76 1259.34 1254.19 1257.64 1799770000  1257.64

“quantmod” 패키지의 “getSymbols” 함수를 사용해 Yahoo에서 데이터 수집

getSymbols('^GSPC')   # get S&P 500 from "yahoo" by default 
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now 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 more details.
## [1] "GSPC"
getSymbols('^KS11', from='2010-01-01')   # 코스피 종합주가지수 (KOSPI Composite Index) 
## [1] "KS11"
str(GSPC)
## An 'xts' object on 2007-01-03/2015-12-18 containing:
##   Data: num [1:2258, 1:6] 1418 1417 1418 1409 1413 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "GSPC.Open" "GSPC.High" "GSPC.Low" "GSPC.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2015-12-21 01:31:22"
head(GSPC)
##            GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume
## 2007-01-03   1418.03   1429.42  1407.86    1416.60  3429160000
## 2007-01-04   1416.60   1421.84  1408.43    1418.34  3004460000
## 2007-01-05   1418.34   1418.34  1405.75    1409.71  2919400000
## 2007-01-08   1409.26   1414.98  1403.97    1412.84  2763340000
## 2007-01-09   1412.84   1415.61  1405.42    1412.11  3038380000
## 2007-01-10   1408.70   1415.99  1405.32    1414.85  2764660000
##            GSPC.Adjusted
## 2007-01-03       1416.60
## 2007-01-04       1418.34
## 2007-01-05       1409.71
## 2007-01-08       1412.84
## 2007-01-09       1412.11
## 2007-01-10       1414.85
plot(GSPC) 
## Warning in plot.xts(GSPC): only the univariate series will be plotted

str(KS11)
## An 'xts' object on 2010-01-04/2015-12-18 containing:
##   Data: num [1:1481, 1:6] 1682 1702 1698 1703 1694 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "KS11.Open" "KS11.High" "KS11.Low" "KS11.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2015-12-21 01:31:22"
plot(KS11)
## Warning in plot.xts(KS11): only the univariate series will be plotted

# quantmod의 다른 함수들과 잘 쓸 수 있도록 OHLC filed 명칭을 변경 
colnames(KS11) <- c("Open", "High", "Low", "Close","Volume","Adjusted")
chartSeries(KS11)

# 월봉으로 바꾸고, 상승을 붉은색, 하락을 파란색으로 
chartSeries(to.monthly(KS11), theme=chartTheme('white',up.col='red',dn.col='blue'))


삼성전자 주가 추세를 Yahoo에서 수집

Samsung <- getSymbols("005930.KS", src="yahoo", from="2010-01-01", auto.assign = F)
colnames(Samsung) <- c("Open", "High", "Low", "Close","Volume","Adjusted")
head(Samsung)
##              Open   High    Low  Close Volume Adjusted
## 2010-01-04 803000 809000 800000 809000 233300 775652.2
## 2010-01-05 826000 829000 815000 822000 531600 788116.4
## 2010-01-06 829000 841000 826000 841000 452400 806333.2
## 2010-01-07 841000 841000 813000 813000 428000 779487.4
## 2010-01-08 820000 821000 806000 821000 291100 787157.6
## 2010-01-11 821000 823000 797000 797000 386300 764146.9
return_1 = Delt(Samsung$Close, k=1, type="arithmetic") # return of today compared to last day 
str(return_1)  # xts object 
## An 'xts' object on 2010-01-04/2015-12-18 containing:
##   Data: num [1:1541, 1] NA 0.01607 0.02311 -0.03329 0.00984 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr "Delt.1.arithmetic"
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2015-12-21 01:31:24"
summary(return_1)
##      Index            Delt.1.arithmetic   
##  Min.   :2010-01-04   Min.   :-0.0745098  
##  1st Qu.:2011-07-07   1st Qu.:-0.0099798  
##  Median :2013-01-02   Median : 0.0000000  
##  Mean   :2012-12-30   Mean   : 0.0004458  
##  3rd Qu.:2014-06-27   3rd Qu.: 0.0096440  
##  Max.   :2015-12-18   Max.   : 0.0868810  
##                       NA's   :1
plot(return_1)

Google Finance에서 financial data 갖고오기
http://stackoverflow.com/questions/20472376/quantmod-empty-dates-in-getsymbols-from-google

invisible(Sys.setlocale("LC_TIME", "C"))  # For some; google에서 getSymbol 사용시 필요 
KOSPI200 = getSymbols('KRX:KOSPI200', src="google", auto.assign = F, 
                      from="2005-01-01")  # 코스피200
str(KOSPI200)
## An 'xts' object on 2005-01-03/2015-12-18 containing:
##   Data: num [1:2720, 1:5] 116 115 113 114 114 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:5] "KRX:KOSPI200.Open" "KRX:KOSPI200.High" "KRX:KOSPI200.Low" "KRX:KOSPI200.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "google"
##  $ updated: POSIXct[1:1], format: "2015-12-21 01:31:25"
chartSeries(KOSPI200,theme="white")

chartSeries(to.weekly(KOSPI200), subset='2012-03::', 
          theme=chartTheme('white',up.col='red',dn.col='blue'))
## Warning in to.period(x, "weeks", name = name, ...): missing values removed
## from data

Get financial data from Federal Reserve Economic Data (FRED) of Federal Reserve Bank of St.Louis.

# Unemploy_Rate = getSymbols("UNRATE",src="FRED", auto.assign = F) # unemployment rates from FRED.
# str(Unemploy_Rate)
# head(Unemploy_Rate) # 실업률은 월별로 되어있음 
# chartSeries(Unemploy_Rate, theme="white")  # Plot monthly unemployment rates

Reading the data from a MySQL database

# library(RODBC)
# ch <- odbcConnect("QuotesDSN",uid="myusername",pwd="mypassword")
# allQuotes <- sqlFetch(ch,"gspc")
# GSPC <- xts(allQuotes[,-1],order.by=as.Date(allQuotes[,1]))
# head(GSPC)
# odbcClose(ch)
# 
# 
# library(DBI)
# library(RMySQL)
# drv <- dbDriver("MySQL")
# ch <- dbConnect(drv, dbname="Quotes","root","pass_wd")
# allQuotes <- dbGetQuery(ch,"select * from gspc")
# GSPC <- xts(allQuotes[,-1],order.by=as.Date(allQuotes[,1]))
# head(GSPC)
# dbDisconnect(ch)
# dbUnloadDriver(drv)