How to Import Forex Data in R

02/09/2022 at 12:18PM

Learn how to import live and historical forex data in R using a REST API. You will also learn how to parse JSON responses into a Dataframe. To follow this tutorial you need next to no knowledge of programming R. That said, the tutorial will still be helpful to people with experience in other programming languages and people who would like to use our REST API.

Let's Begin

You will need to signup for our API click Join API for Free. Once you have the key, keep it safe. We offer 1000 requests a month free forever! You can then visit the official website to download R.

Get Live Forex rates

Once you have R downloaded, install libraries as shown below

# Installing the packages
install.packages("httr")
install.packages("jsonlite")

We can now import libraries that we just installed and set the req variable to the URL string we need the data. The currencies we request is EURUSD and GBPUSD. You will need to replace api_key with the REST API key you received by signing up.

library (httr)
library (jsonlite)

req <- "https://marketdata.tradermade.com/api/v1/live?currency=EURUSD,GBPUSD&api_key=api_key"

We will now request data by making a Get request and setting this to the data_raw variable. Once we have the data in the raw format we will convert it to text format using the content function.

data_raw <- GET(url = req)

data_text <- content(data_raw, "text", encoding = "UTF-8")

It will be fairly easy for python users to understand the code but any user with limited programming skills will be able to understand the following code. We will convert text to JSON then create a dataframe that will nicely format the data into a table that we can use.

data_json <- fromJSON(data_text, flatten=TRUE)

dataframe <- as.data.frame(data_json)

dataframe

Once you run the above code you will see our API provide bid, ask prices with a timestamp in seconds. This is very useful to conduct volatility analysis. Most of the free resources and many paid ones do not provide bid-ask spreads.

live-forex-dataframe

We also provide intraday forex data for R users via our API. This includes tick, minute and hourly rates that can be useful for quantitative analysis.

Get Historical Forex rates

We will first get tick historical rates (API gives the previous 4 days historical tick data for free, not including today). Maximum we can request in one call is 30 min and each call will use 10 of 1000 free requests a month. As shown below we request data for GBPUSD from 08 Februray 2022 08:30 to 09:00. Make sure to change the date to last four days or an error will be returned.

tick_req <- "https://marketdata.tradermade.com/api/v1/tick_historical_sample/GBPUSD/2022-02-08 08:30/2022-02-08 09:00?api_key=api_key&format=json"

data_tick_raw <- GET(url = tick_req)

data_tick_text <- content(data_tick_raw, "text", encoding = "UTF-8")

data_tick_json <- fromJSON(data_tick_text, flatten=TRUE)

dataframe_tick <- as.data.frame(data_tick_json)

head(dataframe_tick)

tick-forex-dataframe

The data is very dense with roughly 2800+ quotes every 30min, this is useful if you are a serious data analyst. Now you can see it's fairly easy to get data from our Forex API. Let us do one more but this time request OHLC data for hourly data (max history is only 2 months from the current date). Please see our docs page for more information on how much history is provided for different endpoints.

hour_req <- "https://marketdata.tradermade.com/api/v1/timeseries?currency=EURUSD&api_key=api_key&start_date=2022-02-08-00:00&end_date=2022-02-09-12:11&format=records&interval=hourly"

data_hour_raw <- GET(url = hour_req)

data_hour_text <- content(data_hour_raw, "text", encoding = "UTF-8")

data_hour_json <- fromJSON(data_hour_text, flatten=TRUE)

dataframe_hour <- as.data.frame(data_hour_json["quotes"])

head(dataframe_hour)

hour-forex-dataframe

As you can see it's fairly easy to pull unbiased forex rates from our Forex REST API in R. We provide free data for up to 1000 requests a month for free as it's our mission to provide forex data for all. You can also get CFD data, please refer to our docs page for detailed info.

Please let us know if you have any questions or would like to make a suggestion. We are always keen to hear from you.