DEV Community

Shridhar G Vatharkar
Shridhar G Vatharkar

Posted on • Edited on

Python Development Kit for Forex and CFDs

Python enthusiasts looking for an SDK (Software Development Kit) to access forex data are in the right place. I take this opportunity to introduce TraderMade Python-SDK, which will bring a great deal of ease in getting live and historical forex rates.

So, here we go!

Initially, you should register and log in to the platform to obtain your API Key from the dashboard. It just takes a few seconds to set up, and you get up to 1000 monthly requests for free. As you get the API Key, please note it in a secure place.

I would also recommend referring to the Video Tutorial of Python SDK.

After this, you would need to install the SDK. It is also an easy step. You would need Python 3 and run the below command from the terminal.

pip install tradermade
Enter fullscreen mode Exit fullscreen mode

The other way is to visit PyPI to gather additional information. Once you install TraderMade, open an Integrated Development Environment (IDE) and run the below command.

import tradermade as tm

# set api key
tm.set_rest_api_key("api_key")
Enter fullscreen mode Exit fullscreen mode

Now, you can set the API Key to retrieve the data.

Live Forex Data

#get live data
tm.live(currency='EURUSD,GBPUSD',fields=["bid", "mid", "ask"]) # returns live data - fields is optional
Enter fullscreen mode Exit fullscreen mode

Image description

As you run the above command, you start getting the data. You can select the desired options from the fields. For instance, to get ‘bid’ prices provide fields=[“bid”]. Likewise, you can choose the options you require. If you wish to get the currency codes, run the below:

#get currency codes

tm.currency_list() 
# gets list of all currency codes available add two codes to get code for currencypair ex EUR + USD gets EURUSD
Enter fullscreen mode Exit fullscreen mode

Image description

Historical Data
If you require historical forex data, you would need to provide the date and the currency pairs for which you require the OHLC prices. The SDK will return the data for your desired currency pairs for the particular date.

#get historical data

tm.historical(currency='EURUSD,GBPUSD', date="2011-01-20",interval="daily", fields=["open", "high", "low","close"]) 
# returns historical data for the currency requested interval is daily, hourly, minute - fields is optional
Enter fullscreen mode Exit fullscreen mode

Image description

If you need granular data, change the date parameter to “YYYY-MM-DD-HH-MM” format. You will receive the granular data as follows:

Image description

Timeseries Data
Do you want to plot forex data on a chart, or are you interested in timeseries analysis? For such a format, the timeseries function is appropriate. You should provide the start and end date and the desired interval, as shown here. For a daily endpoint, daily data for a maximum of one year can be available. If you wish to obtain data for more than a year, you can loop over the request as shown here.

# get timeseries data
import pandas as pd
df = pd.DataFrame()
for i in range(2011, 2021):  
    x = tm.timeseries(currency='EURUSD', start=str(i)+"-06-17",fields=["open", "high", "low","close"], end=str(i+1)+"-06-16")
    df = df.append(x)
df = df.drop_duplicates()
df
# returns timeseries data for the currency requested interval is daily, hourly, minute - fields parameter is optional (you can select ["close"] if you just want close prices)
Enter fullscreen mode Exit fullscreen mode

You can obtain a data set of 10 years through the above example. If you are looking for hourly and minute details, you can get all the required data in a single call. In the hourly timeseries, you get data for up to the last 2 months. Similarly, for a minute timeseries, you get data for two days. You can pass the period parameter if the default minute interval is set to 15.

# get timeseries data

tm.timeseries(currency='EURUSD', start="2021-04-20",end="2021-04-22",interval="hourly",fields=["open", "high", "low","close"]) 
# returns timeseries data for the currency requested interval is daily, hourly, minute - fields parameter is optional (you can select ["close"] if you just want close prices)
Enter fullscreen mode Exit fullscreen mode

Image description

To request data for multiple currency pairs, you need to set the fields to [“close”] as below.

tm.timeseries(currency='EURUSD,GBPUSD', start="2021-04-26",end="2021-04-27",interval="minute",fields=["close"],period=15)

# returns 15 min bar for two currencies - you may need to adjust date to two days back or function will return an error that only two days of data is allowed for minute interval.
Enter fullscreen mode Exit fullscreen mode

Image description

You must have observed that obtaining forex data becomes easier than directly using an API. For additional information, please visit the TraderMade Documentation Page. I hope the details are helpful. For any further queries, please feel free to get in touch with TraderMade Support through email or live chat.

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 go through the originally published tutorial on the TraderMade website: Python SDK

Here are a few more Python-based tutorials that will help you enhance your coding and data retrieval skills:

  1. Stream Data Using Python in 8 Lines of Code
  2. Data Visualization Python

Also, go through our tutorials on this platform:

  1. Fetch Forex API with Python and Pandas
  2. Your First PHP WebSocket Client and
  3. Golang REST API Client.

Top comments (0)