How to Use Python to Get Live Forex Data

How to Use Python to Get Live Forex Data

As an Amazon Associate I earn from qualifying purchases.

How to Use Python to Get Live Forex Data

CurrencyAPI.io is one of the APIs that allow traders and programmers to get live Forex data using Python programming language. Besides Python, CurrencyAPI.io also supports a majority of other programming languages including Ruby, PHP, JavaScript, PowerShell, C#, cURL, Go (Native), NodeJs, Java, and Swift.

In this guide, we shall concentrate on how to use the Python programming language to get live Forex data.

The first step is to sign up with CurrencyAPI.io

To query forex data using CurrencyAPI.io one must sign up to get an API access key to enable them to access API calls.

There are two plans that one can choose from; the Starter Plan (which has a 7-day free trial) that costs $19 per month and the Premium Plan that costs $199 per month. The Starter Plan allows one to access 10,000 API calls in a month while the Premium Plan allows one to access 100,000 API calls in a month. The Starter Plan cannot be used to query Forex Historical data, instead one would have to sign up for the Premium Plan.

Using Python to get live forex data using CurrencyAPI.io

Once the signing up process is complete, one is provided with an ACCESS KEY, which acts as the API’s authentication key or token that must be used to notify CurrencyAPI.io that you are subscribed with it so that it can allow you to query the Forex market using its API calls.

It is also important to note that contrary to other APIs that have several endpoints, CurrencyAPI.io only has one endpoint called markets.

Now let’s get to how to get Forex live data with CurrencyAPI.io using Python.

When using the Python programming language to get live forex data using CurrencyAPI.io, there are two options.

One is by importing the ‘http.client’ module’ using ‘import http.client’ and the other is by importing the ‘requests’ module using ‘import requests’. We shall show an example of how to use the two.

Python code example using ‘import http.client’


import requests
gettinginfo = "https://api.currencyapi.io/markets?token=ACCESS_TOKEN&symbol=EURUSD"
payload={}
headers = {}
infoout = requests.request("GET", gettinginfo, headers=headers, data=payload)
print(infoout.text)

The above example is for querying the live exchange rate of EURUSD and the result are:
[
{
"t":"2021-07-16T12:37:00.523Z",
"tms":"5316558790086",
"s":"EURUSD",
"b":1.18135,
"bd":0,
"a":1.18733,
"ad":1,
"p":5
}
]

“t” shows the date and time of the query result, “tms” shows the update timestamp, “s” shows the currency pair, “b” shows the bid price, “bd” shows the bid price direction, “a” shows the ask price, “ad” shows the ask price direction and “p” shows the currency precision (decimal points).

Python code example using ‘import http.client’


import http.client
gettinginfo = http.client.HTTPSConnection("api.currencyapi.io")
gettinginfo.request(
"GET",
"/markets?token=ACCESS_TOKEN&symbol=EURUSD",
)
response = gettinginfo.getresponse()
data = response.read()
print(data.decode("utf-8"))

The results of the above example are similar to those of when using the ‘import http.client’. Therefore, it doesn’t matter which module import one uses.

Conclusion

Using Python programming language to get live Forex data using CurrencyAPI.io, is relatively simple as demonstrated in the above examples. It can be used in simple programs like the one a trader would use to just get the exchange rates to complex programs like the one a trader would use in trading EAs or scripts to generate signals.

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

Share