How to Use Python for Forex Historical Data

How to Use Python for Forex Historical Data

As an Amazon Associate I earn from qualifying purchases.

How to Use Python for Forex Historical Data

Forex historical data is an important aspect when it comes to market analysis, especially when creating an indicator, script, or expert that relies on the historical data analysis for operations.

Currency API – Free Trial

Several APIs are currently available online and developers and traders can sign up with them for historical data queries. But, one will require to first check whether the API allows querying of historical data because some do not. Also, with some APIs, one will have to sign up for a higher plan to be able to query historical data using the API calls.

One of the best APIs that allows you to get forex historical data is CurrencyAPI.io, a fast and friendly currency API for getting data on FX currency pairs, commodities, and cryptocurrencies.

Signing up with CurrencyAPI.io

If you intend to use the CurrencyAPI.io for getting historical forex data, then you should go for the Premium plan, which costs $199 per month.

Using the historical parameter to get forex historical data

Before delving on how to use the historical data parameter to get forex historical data, we have to first look at how the rest of the program should look like.

Some of the things that one should take note of are the CurrencyAPI.io URL and your Authentication Token.

CurrencyAPI.io URL: https://api.currencyapi.io/

Your authentication token is a unique access key that you will be passing to the CurrencyAPI.io URL to verify that you have subscribed for the API calls.

If you are just using the CurrencyAPI.io platform to get Forex data, an appended access token should look something like this:

https://api.currencyapi.io/markets
?token=ACCESS_TOKEN

ACCESS_TOKEN in the above example is a placeholder and you should replace it with your access token, which is usually a combination of letters and digits.

When querying current data for a certain market (includes results for all assets covered by the CurrencyAPI.io), you should use a code similar to the one below:

https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&market=MARKET_NAME

If we replaced the “MARKET_NAME” with forex, for example, we would get the current prices for all the 1200+ FX currency pairs. You could as well replace “MARKET_NAME” with crypto or commodity.

But of course, you want to narrow down your query to only one asset. So your code should look something similar to the one below:

GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&search=SYMBOL

You should replace “SYMBOL” with either a base currency if you want results for all the currency pairs with a specific base currency or you could use a complete currency pair like EURUSD.

Just to expound, it should look like this:

GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&search=EUR

The above will return results with currency pairs whose base currency is EUR.

Or

GET https://api.currencyapi.io/markets
?token=ACCESS_TOKEN
&search=EURUSD

The above should return results for only one currency pair, the EURUSD. The result should be something like the one below:

{
"t":"2021-08-06T15:17:23.554Z",
"tms":"7518448680023",
"s":"EURUSD",
"b": 1.17924,
"bd":0,
"a": 1.17874,
"ad":1,
"p":5
}

b is the bid price, bd is the direction of the bid price, a is the ask price, ad is the ask price direction, and p is the precision of the ask and bid prices. In the example above the precision is set to 5 meaning 5 decimal points.

Now we can go to applying the historical parameter to query forex historical data. So, the code should look something similar to the one below:

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

As you can see from the above example, we have introduced “&historical=true” below “&symbol=SYMBOL”

By setting the “&historical” to true, it means the code will query historical data of the used SYMBOL. Just as mentioned in the above sections, you can decide to query the historical data of several pairs sharing a certain base currency by replacing “SYMBOL” with a base currency or of one single currency pair by replacing “SYMBOL” with a simple currency pair like GBPUSD.

And of course, you can also decide to query the historical data of all the currency pairs that CurrencyAPI.io can handle by using &market=MARKET_NAME” instead of “&symbol=SYMBOL” below the “?token=ACCESS_TOKEN”.

The most important aspect when querying the forex historical data is the “&scale=SCALE”, which determines the historical data you are querying. It is an optional statement and if not included the CurrencyAPI.io queries the historical data on the past day.

Let us look at an example using EURUSD and default setting of “&scale=SCALE”.

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

The results of this should be something similar to this:

[
[
9720103205,
1.18376,
1.18583,
1.18278,
1.18322
],
...
]

The first line of data represents the timestamp, the second represents the daily open price, the third represents the high price, the fourth represents the low price, and the fifth (which is the last) represents the close price.

So now that you are good to go with writing the code on the CurrencyAPI.io platform, let us now look at how you can use this knowledge when using Python for Forex historical data.

Using Python to get Forex historical data using CurrencyAPI.io

You can choose to use the “import requests” or “import http.client”.

We shall look at examples when using both.

Using “import requests”


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 shall realize that we have just placed all the statements of getting historical data in the previous example into one statement and assigned it to the variable url as:

url = https://api.currencyapi.io/markets?token=ACCESS_TOKEN&symbol=SYMBOL&historical=true&offset=OFFSET&scale=SCALE

The rest of the code is for passing the results of the API call to the different functions till printing. If you use EURSD as the “SYMBOL” the results should be similar to the one in the example in the previous section.

Using “import http.client”


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

The same parameters just that this time round are put in a function request. If you used EURUSD as the “SYMBOL”, you should get similar results as the example in the previous section.

Conclusion

If you are a Python programmer, then CurrencyAPI.io is one of the best APIs you can use in getting forex history data for your trading and trading software.

You only require a very short code to query any data including the historical data using CurrencyAPI.io when coding using Python.
Free Trial

Amazon and the Amazon logo are trademarks of Amazon.com, Inc, or its affiliates.

Share