Python Development Kit for Forex and CFDs
04/27/2021 at 02:02PM
If you are a fan of python, SDK (Software Development Kit) and are looking for forex data then look no further. I would like to introduce TraderMade Python-SDK that will make your life much simpler with respect to getting live and historical forex rates.
So let’s begin.
You can also follow the Python SDK tutorial as a video.
The second step is to install SDK which is also fairly simple just run the following command from the terminal (python 3 is required).
pip install tradermade
You can alternatively visit PyPI for more info. Once you have tradermade installed open an IDE and run the following.
import tradermade as tm # set api key tm.set_rest_api_key("api_key")
You can now set the API key that will enable you to get data.
Live Forex Data
#get live data tm.live(currency='EURUSD,GBPUSD',fields=["bid", "mid", "ask"]) # returns live data - fields is optional
Running the above command gets you data you can select what you want from the fields if you just want bid simply provide fields =[“bid”] and so on.
In order to obtain currency codes simply run the following:
#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
If you need historical forex data the simply provide the date and the currency pairs you need the OHLC data for and the SDK will return data for all those pairs for that 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
In order to request granular data simply change the date parameter to “YYYY-MM-DD-HH-MM” format and you will get granular data as shown below.
if you looking for timeseries analysis or plot data on a chart then the timeseries function is the best. Simply provide a start and end date and interval you looking for as shown below. The daily endpoint only provides one year max of daily data per call, so to get more than a year's data we can loop over the request as shown below.
# 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)
The above example will provide a ten-year data set. However, for hourly and minute granularity, a single call can provide all data needed. The hourly timeseries is available up to 2 months and minutes for up to 2 days. You can pass the period parameter if the minute interval is selected default is 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)
If you want to request data for multiple currencies then fields must be set to [“close”] as shown 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.
As you can see this makes it a lot easier to get forex data so compared to using an API directly. For more info visit TraderMade docs page. Hope this helps, if you have any request or question please let us know.