BOK ECOS Open API using R code
[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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
BOK (Bank of Korea) administrates ECOS database for economic ststistics/market data and provides Open API for this database. ECOS API also supports several popular programming language such as R, Python. In this post, we use R programming to download data through ECOS API. This work can really help you avoid a time-consuming and tedious data collection job.
ECOS (http://ecos.bok.or.kr/) economic statistics database provide the open API to download economic time series using ticker. (http://ecos.bok.or.kr/jsp/openapi/OpenApiController.jsp) It is easy to use.
At first, you need to get your own license key from ECOS Open API homepage. This key is used as a input variable. Next, find the statistics and item codes at
The above figures show how to find statistics code and its item code for Quarterly Real GDP (seasonal-adjusted).
Now we can code R script for which we input key for authorization, codes(statistics, items), search period(begin, end), etc.
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 | #=========================================================================# # Financial Econometrics & Derivatives, ML/DL using R, Python, Tensorflow # by Sang-Heon Lee # # https://kiandlee.blogspot.com #————————————————————————-# # BOK ECOS database Open API #=========================================================================# library(jsonlite) #—————————————————————–# # Input #—————————————————————–# api_key <– “XXXXXXXXXXXX” # ECOS API key start_no <– “1” # query start number end_no <– “100” # quary end number # state_code – item_no stat_code <– “111Y017” # statistics code (Quarterly Real GDP) item_no <– “10601” # item code (Domestic Demand) cycle_type <– “QQ” # frequency start_date <– “200001” # end_date <– “202001” # #—————————————————————–# # Calling ECOS Open API #—————————————————————–# url = paste0(“http://ecos.bok.or.kr/api/StatisticSearch/”, api_key,“/”,“json/kr/”,start_no,“/”,end_no,“/”, stat_code,“/”,cycle_type,“/”,start_date,“/”, end_date,“/”,item_no) raw.data <– readLines(url, warn = “F”,encoding=“UTF-8”) lt.data.str <– fromJSON(raw.data) str(lt.data.str) lt.data.str$StatisticSearch$row$DATA_VALUE #—————————————————————–# # Data Manipulation Part #—————————————————————–# # string to numeric rgdp <– as.numeric(lt.data.str$StatisticSearch$row$DATA_VALUE) # time axis to numeric yyyyqq <– lt.data.str$StatisticSearch$row$TIME yyyyqq <– as.numeric(substr(yyyyqq,1,4))+ (as.numeric(substr(yyyyqq,5,6))–1)*0.25+0.25 # YOY growth rate rgdp.lnd4 <– diff(log(rgdp),4) x11(width=6, height=5); plot(yyyyqq[–c(1:4)], rgdp.lnd4*100,type=“l”,xlab=“YYYYQQ”, ylab=“GDP growth rate(yoy,%)”,main=“GDP growth rate”) | cs |
This R code download Korean quarterly real GDP from 2000 to 2019, calculate year-on-year growth rate, and draw it.
Since most empirical analysis use multiple time series, we can modify the above R code to handle it.
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 | #=========================================================================# # Financial Econometrics & Derivatives, ML/DL using R, Python, Tensorflow # by Sang-Heon Lee # # https://kiandlee.blogspot.com #————————————————————————-# # BOK ECOS database Open API # multiple time series download #=========================================================================# library(jsonlite) graphics.off(); rm(list=ls()) #—————————————————————–# # Input #—————————————————————–# api_key <– “XXXXXXXXXXXX” # ECOS API key start_no <– “1” end_no <– “100” cycle_type <– “QQ” start_date <– “200001” end_date <– “201904” #—————————————————————–# # Calling ECOS Open API #—————————————————————–# # Real GDP, CPI, Call rate stat_code <– c(“111Y017”, “021Y125”,“028Y001”) # expenditures on final goods, total index, uncollateral(1d) item_no <– c(“10601”,“0”,“BEEA11”) # time series data matrix m.data <– NULL for(i in 1:length(stat_code)) { url = paste0(“http://ecos.bok.or.kr/api/StatisticSearch/”, api_key,“/”,“json/kr/”,start_no,“/”,end_no,“/”, stat_code[i],“/”,cycle_type,“/”,start_date,“/”, end_date,“/”,item_no[i]) lt.data.str <– fromJSON(readLines(url, warn = “F”,encoding=“UTF-8”)) # concatenate m.data <– cbind(m.data, as.numeric(lt.data.str$StatisticSearch$row$DATA_VALUE)) } colnames(m.data) <– c(“RGDP”,“CPI”,“Call”) # column names setting print(m.data) | cs |
The contents of R code are as follows.
1) make an array for ststistics and items code
2) use for loop to download each time series successively
3) m.data라는 matrix 객체에 저장한다는 것이다. save multiple time series data as m.data which is matrix object
It is better to use cbind() to concatenate each time series into one matrix than direct substitution.
Multiple time series (Real GDP, CPI, Call rate) is printed out as follows.
1 2 3 4 5 6 7 8 9 10 11 12 | RGDP CPI Call [1,] 220786.3 66.084 4.95 [2,] 223728.3 65.995 5.10 [3,] 229891.1 66.882 5.12 [4,] 229145.2 67.326 5.33 [5,] 231953.5 68.524 5.14 … [72,] 443599.8 103.000 1.34 [73,] 447909.3 103.910 1.49 [74,] 450495.8 104.250 1.49 [75,] 452561.1 104.810 1.51 [76,] 456769.7 104.840 1.60 | cs |
From this post, we can use ECOS API for download useful data for empirical analysis. \(\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.