If you are a trader or developer you can always use Python to get historical forex data using CurrencyAPI.io.
The Python software development kit (SDK) is simple to use and thus provides one of the best platforms for developers who want to use forex historical data. So, let’s get into how you should do this.
Sign up for the Premium account with CurrencyAPI.io
The first step is to sign up for an account with CurrencyAPI.io to get your API key. CurrencyAPI.io offers two types of accounts; the starter account, which comes with a 7 day trial period, and the premium account. If you want to access historical data using the CurrencyAPI, you should ensure you register for the premium plan which costs $199 per month, since it is the one that allows you to access historical data.
The signup process only takes a matter of seconds. Once you complete signing up, ensure to keep your API key safe because it is the one that you will use when writing your programs to show that you have registered for an account with CurrencyAPI.io.
Install SDK
Once you have your CurrencyAPI.io account, ensure you have installed Python 3 or a later version installed to your developer environment. Contrary to other APIs that require you to install a python package SDK to enable you to integrate the API with Python, CurrencyAPI.io does not require you to install anything else other than the Python SDK.
Testing the historical data API in CurrencyAPI.io
The API URL for CurrencyAPI.io is https://api.currencyapi.io/
The currencyAPI.io has only one API endpoint, the “markets” that allows you to query live data or historical data depending on the account you have subscribed for. In this case, we shall be using the endpoint to query historical data through the premium account.
To get the historical data of any symbol (single currency or a currency pair) simply type the following:
GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&symbol=SYMBOL
&historical=true
&offset=OFFSET // Optional (candles offset)
&scale=SCALE // Optional (e.g., 1, 60, 1D, 1W, 1M), default to 1D
Note the question mark (?) used at the beginning of the term token; it shows where the query begins. Everything that comes after that is part of the query and you use the ‘&’ sign to add all the other things you want to query. In our case, we are querying a symbol (currency or currency pair), historical data, candle offset, and scale.
You should replace ‘ACCESS_TOKEN’ with your CurrencyAPI API key, and ‘SYMBOL’ with the currency or currency pair that you want to get data for.
For example, you could use EURUSD for the symbol and the code would look like this:
GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&symbol=EURUSD
&historical=true
&offset=OFFSET // Optional (candles offset)
&scale=SCALE // Optional (e.g., 1, 60, 1D, 1W, 1M), default to 1D
Notice the &historical is set to true to enable the code to query the historical data of the currency pair. You could choose not to use the offset and scale functions although we have included them in the above examples.
The scale function is however very important since it specifies the period within which you want to get the historical data. When the scale is not set, the API by default queries data from the past day. But if you want data for the past 1 Week, for instance, you should use the &scale=SCALE and replace ‘SCALE’ with 1W.
If you run the above code in CurrencyAPI.io, the response should be something similar to this:
[
[
1920605400,
1.21363,
1.25400
1.21361,
1.25374
],
...
]
The first result is the timestamp showing when the response was given. The second is the opening price of the market. The third is the highest market price the market attained during the duration of time. The fourth is the lowest market price that the market attained during the specified period. The fifth is the closing price.
Using CurrencyAPI.io with Python to get historical data
Now that we have tested the historical data query with the CurrencyAPI.io, let us look at how you can use the API to write a python code that can query forex historical data.
Python is the simplest programing language to use when using CurrencyAPI.io. Python’s code is generally short, precise, and not hard to interpret.
There are two routes that you can use to write python code with CurrencyAPI.io. These are using the import request and using import http. client.
Using the import request
Your python code should look something like this:
import requests
url = "https://api.currencyapi.io/markets?token=ACCESS_TOKEN&symbol=SYMBOL&historical=true
&offset=OFFSET&scale=SCALE”
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
If you look closely, you will notice that we have used what we were using when using ‘GET’ in the currencyAPI.io platform and assigned it to the integer ‘url’.
When trying out this code, you should replace ‘ACCESS_TOKEN’ with your API key, ‘SYMBOL’ with the currency pair or currency you are querying about, and ‘SCALE’ with the period you want to consider in your query.It is important to note that when using the ‘import request’, we use the CurrencyAPI.io API URL (https://api.currencyapi.io), which precedes the API endpoint (/markets).
Using the import http.client
Your python code should look something similar to this:
import http.client
conn = http.client.HTTPSConnection("api.currencyapi.io")
conn.request(
"GET",
"/markets?token=ACCESS_TOKEN&symbol=SYMBOL&historical=true
&offset=OFFSET&scale=SCALE”,
)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
When trying out this code, you should replace ‘ACCESS_TOKEN’ with your API key, ‘SYMBOL’ with the currency pair or currency you are querying about, and ‘SCALE’ with the period you want to consider in your query.
When using the import http.client, we do not specify the CurrencyAPI.io API URL. After ‘GET’, we go directly to the API endpoint (/market) and start listing the query stating with the API key presided by the ‘&’ sign.
It is important to note the difference in where ‘GET’ is used in the two codes. Though in both, it is used after request.
If you run either of the two python codes, you should get a response similar to the one below. The response is similar to that obtained when querying using the CurrencyAPI.io platform.
[
[
2320603700,
1.23363,
1.24400
1.21361,
1.22374
],
...
]
And that is it! Today we learned how to create and make historical currency pairs API calls using Python and CurrencyAPI.io. You are now good to go to start using Python with CurrencyAPI.io to get forex historical data.