How to Import Forex Data in R
09 February 2022
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.
You can also follow this tutorial via a Video :
You need to sign up for our API by clicking Join API for Free. Once you have the key, note it in a secure place. 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 currency pair we request is EURUSD and GBPUSD. You will need to replace the 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 easy for python users to understand the code, but any user with limited programming skills can understand the following code. We will convert text to JSON and then create a data frame that will nicely format the data into a table.
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 and ask prices with a timestamp in seconds. This is very useful in volatility analysis. Most free resources and many paid ones do not offer bid-ask spreads.
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-level historical rates (API gives the previous 4 days' historical tick data for free, not including today). The maximum we can request in one call is 30 minutes. Each call will use 5 of 1000 free requests a month. As shown below, we seek data for GBPUSD from 08 February 2022, 08:30 to 09:00. Make sure to change the date to the last four days, or an error will be returned.
tick_req <- "https://marketdata.tradermade.com/api/v1/tick_historical_sample/GBPUSD/2022-02-08%2008:30/2022-02-08%2009: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)
The data is very dense and has roughly 2800+ quotes every 30min. This is useful if you are a pro data analyst. Now you can see it's easy to get data from our Forex API. Let us do one more but this time, seek 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)
As you can see, it's 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 avail 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.