DEV Community

Shridhar G Vatharkar
Shridhar G Vatharkar

Posted on • Edited on

How To Import Forex Data in 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 Dataframe. 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.

You can also watch a video tutorial for importing Forex data in R on our YouTube channel:

How To Import Live and Historical Forex Data in R (Programming)

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")
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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)
Enter fullscreen mode Exit fullscreen mode

Image description

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)

Enter fullscreen mode Exit fullscreen mode

Image description

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.

TraderMade provides reliable and accurate Forex data via Forex API. You can sign up for a free API key and start exploring real-time and historical data at your fingertips.

Please refer to the originally published tutorial on the TraderMade website: How to Import Forex Data in R

Also, read our other tutorials:
Your First Golang REST API Client

Your First PHP WebSocket Client

Fetch Forex API With Python and Pandas

Python Development Kit for Forex and CFDs

Data Visualization Python

Top comments (1)

Collapse
 
anahale23 profile image
anahale23

Traders often employ various strategies, such as scalping, day finex penipuan trading, swing trading, and position trading, depending on their goals, risk tolerance, time commitment, and market conditions.Traders often have preferences based on liquidity, volatility, spreads, and familiarity with specific currency pairs. Major pairs like EUR/USD, GBP/USD, and USD/JPY are popular choices due to their liquidity and trading volume.Traders utilize a range of resources for staying updated on market news and events, including financial news websites, economic calendars, central bank announcements, geopolitical developments, and analysis from reputable sources like Bloomberg, Reuters, CNBC, and Forex Factory.