March 2019
by @mw

The R way to explore APIs

Data access is often a nightmare. Especially with irregular data shapes or multiple data types. APIs, or application programming interfaces, offer a simple access gates to the information resources in their native structures, and therefore they offer a powerful tool to quickly boost many research projects.

In a nutshell, an API is a gate through which a user may access the resources or data located on a server in a quick and friendly way. APIs have a generic address, typically in the form of http address, and endpoints. Endpoints direct the user to specific parts of of the database (like tables), the user may need to access. APIs require an authentication key, called a token, which offers the server the access control mechanism. Sometimes you need to pay for a token, but oftentimes some limited functionality is offered for free.

To demonstrate the performance of the API, I will access the trading database of OANDA, an online web trading platform. The platform offers a REST API, which in short offers a standardised and easily accessible web architecture. The documentation of the API together with the description of the endpoints and some working examples can be found here.

To access the OANDA resources, one needs to register on the website and to generate the unique token, which will serve as an online authentication when calling the API. It is all for free and can be done in 1 minute. As a second step, I will make use of three very handy R packages:
. The first one offers a great functionality for calling the web requests, the second one is an efficient tool to handle JSON data (typically the API data come in JSON format), and the last one is a fantastic plotting tool for financial data.

Calling the API requires building the http address which may be called to return a specific output. In my example, I will download the last 100 observations of hourly candlestick data for EUR/USD exchange rate. I will then convert the output into the data frame and plot a the candlestick chart.

#load necessary packages

Token       <- "your token here"
Instrument  <- "EUR_USD"
Count       <- 100
Granularity <- "H1"

#generic API address (for practice)
httpAccount <- ""

#build the URL
auth        <- c(Authorization = paste("Bearer",Token,sep=" "))
content     <- c("Content-Type" = "application/json")
queryHttp   <- paste0(httpAccount,paste0("/v3/instruments/",Instrument,"/candles"))
queryHttp   <- paste0(queryHttp,paste0("?count=",Count))
queryHttp   <- paste0(queryHttp,paste0("&granularity=",Granularity))

#get the data from API and clean up
res         <- httr::GET(url = queryHttp, add_headers(c(content,auth))) 
queryData   <- content(res,as="text",encoding="UTF-8") 

#clean JSON format to a data frame
df          <- fromJSON(queryData, simplifyDataFrame = TRUE)

#plot the candlestick chart
p <- plot_ly(x = lubridate::as_datetime(df\$candles\$time), type="candlestick",
             open = df\$candles\$mid\$o, close = df\$candles\$mid\$c,
             high = df\$candles\$mid\$h, low = df\$candles\$mid\$l) %>%
  layout(title = "EUR/USD H1 candles",
         xaxis = list(rangeslider = list(visible = F)))

The output chart looks as below. Note that I took the 100 most recent candles, which in this case crosses the weekend, hence the blank space in the middle of the graph. Your charts could look differently. If you look R Studio or any other R platform, your chart will be more interactive to play with.

Candlestick chart

For more advanced functionality of OANDA API, or any other API, including modification of the time frames or data types, I refer you to the API documentation.

Leave your comment

M. Wolski
Marcin Wolski, PhD
Advisor to Vice-President
European Investment Bank
E-mail: M.Wolski (at)
Phone: +352 43 79 88708

View my LinkedIn profile View my profile
View my IDEAS/RePEc profile  IDEAS/RePEc