How To Import Forex Data in R
Tags: r, importforexdata, forexdata, streamingforexrates
Published: 2022-12-05 11:55:00

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)](https://www.youtube.com/watch?v=jWyt-ctD0mY)

### Let’s get started!
To start with, please sign up for our API by clicking [Join API for Free](https://tradermade.com/signup). 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. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0z5n6a2w2keuhbsq8x0n.png)

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)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2cnjy382lmd0fsayd52.png)

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)

```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62unnhbtgo00bed0xuq7.png)

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](https://tradermade.com/forex). 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](https://tradermade.com/tutorials/how-to-import-forex-data-in-r/)

> **Also, read our other tutorials:**
> [Your First Golang REST API Client](https://dev.to/shridhargv/your-first-golang-rest-api-client-69i)

> [Your First PHP WebSocket Client](https://dev.to/shridhargv/your-first-php-websocket-client-1aic)

> [Fetch Forex API With Python and Pandas](https://dev.to/shridhargv/fetch-forex-api-with-python-and-pandas-dgn)

> [Python Development Kit for Forex and CFDs](https://dev.to/shridhargv/python-development-kit-for-forex-and-cfds-n76)

> [Data Visualization Python](https://dev.to/shridhargv/data-visualization-python-2ief)