How To Import Forex Data in R

How To Import Forex Data in R

This Tutorial Shows An Easy Way to Get Forex Data from TraderMade Using 'R'

This tutorial lets you know about importing live and historical forex data in R using a REST API. Similarly, you can understand resolving JSON responses into a Data frame. You require least to no knowledge of the R programming language to follow this tutorial. Yet, this tutorial will be helpful to programmers experienced in other languages and willing to use TraderMade’s REST API.

Let’s get started!

To start with, please sign up for our API by clicking Join API for Free. As you obtain the API key, please note it down securely. TraderMade offers 1000 monthly requests for free forever!

Next, you need to download R from the official website.

Obtain Streaming Forex Rates

After downloading the R programming language from the official website, start the process by installing the required libraries as shown here:

# Installing the packages

install.packages("httr")

install.packages("jsonlite")

Then, we can import these installed libraries in the previous step. We will set the ‘req’ variable to the URL string to retrieve the data. We request EURUSD and GBPUSD currency pairs. You should precisely substitute the API Key you received after signing up.

library (httr)

library (jsonlite)

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

We need to make a ‘Get’ request and set it to the ‘data_raw’ variable to obtain data. We receive the data in raw format and can convert it into text format using the ‘content’ function.

data_raw <- GET(url = req)

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

Python users can easily understand this code. At the same time, users with limited programming skills will also understand the following code with ease. We need to convert the data in text format into JSON and then create a data frame to get data in a tabular form to make it easy to understand and use.

data_json <- fromJSON(data_text, flatten=TRUE)

dataframe <- as.data.frame(data_json)

dataframe

As you run the above code, you can get the bid and ask prices for the selected currency pairs along with a timestamp in seconds. This information helps you conduct a thorough market volatility analysis. Many free and paid data vendors do not provide bid-ask spreads.

live-forex-dataframe

The R users can also get intraday forex data via TraderMade’s API. You can obtain tick, minute, and hourly rates, which help in thorough quantitative analysis.

Obtain Historical Forex Rates

Firstly, we will get historical tick rates. It is important to note that the API provides the historical tick data for the previous 4 days, excluding today. We can request 30 minutes of data in each call, and every call uses 10 requests (out of 1000 free monthly requests). We request data for the GBPUSD currency pair for 08 February 2022, from 08:30 to 09:00. Please ensure you change the dates to the last four days. Otherwise, you will receive an error message.

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

We receive highly dense data with around 2800+ quotes for every 30 minutes duration. If you are a pro data analyst, this data is helpful. You realise that you can get data from TraderMade’s Forex API with ease. Let us obtain OHLC values for hourly data as another exercise. You can get a history of 2 months from the current date. Please refer to our documentation page to check how much historical data is provided for various 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

Thus, you would see that it is easy to gather unbiased forex rates from TraderMade’s Forex REST API in R. We try to make forex data accessible to everyone, and that is why we offer 1000 monthly requests for free. You can also obtain CFD data. For additional information, please refer to our documentation page.

Your technical queries or suggestions are most welcome. We would love to hear from you.