Friday, March 31, 2017

Algo Trading Oanda v20 REST API accounts with Python


Oanda is switching most of the new accounts to make use of their new v20 REST API. Unfortunately, the v20 REST API is not compatible with the v1 REST API.

This article demonstrates how to extract market rates using the v20 REST API. I assumed that you already have some python packages installed on your system. I am using the Jupyter Notebook via Continuum Analytics. I further assumed that you already have an Oanda v20 accounts set up. v20 account id is in the form of xxx-xxx-xxxxxxx-xxx whereas v1 account id is in the form of xxxxxxx. If you do not already have an account, you can set up a free demo trading account and follow along.

Step-by-step Guide

In step 1, we import the third party Oanda v20 REST API wrapper.

We then import the built-in configparser in step 2 to segregate our codes from our access codes.

 import oandapyV20                                           # step 1
 from oandapyV20 import API  
 import oandapyV20.endpoints.pricing as pricing  
 import configparser                                         # step 2

The next step then is to instantiate our configparser and import our accountID and access_token.

 config = configparser.ConfigParser()'../config/config_v20.ini')  
 accountID = config['oanda']['account_id']  
 access_token = config['oanda']['api_key']  
 api = API(access_token=access_token)  

I am interested in the EURUSD and USDJPY market rates so I stored this in the params variable by using a python dictionary as shown below. We instantiated pricing.PricingInfo and provide accountID and params and followed by making an API call asking Oanda to store the returned data to  the variable rv. There's nothing special to that name. You can call it whatever you like.

 params ={  
      "instruments": "EUR_USD,EUR_JPY"  
 r = pricing.PricingInfo(accountID=accountID, params=params)  
 rv = api.request(r)  

When we print the returned data using the following code

Although the returned data is not in an easy to read format, we can always clean that up. However, I will leave that for another day. For now, we can see that Oanda v20 REST API provides substantially richer information that the v1 REST API.

 {'prices': [{'asks': [{'liquidity': 10000000, 'price': '1.06598'},  
   {'liquidity': 10000000, 'price': '1.06600'}],  
   'bids': [{'liquidity': 10000000, 'price': '1.06498'},  
   {'liquidity': 10000000, 'price': '1.06496'}],  
   'closeoutAsk': '1.06602',  
   'closeoutBid': '1.06494',  
   'instrument': 'EUR_USD',  
   'quoteHomeConversionFactors': {'negativeUnits': '1.39803000',  
   'positiveUnits': '1.39620000'},  
   'status': 'non-tradeable',  
   'time': '2017-03-31T21:08:47.465756855Z',  
   'tradeable': False,  
   'type': 'PRICE',  
   'unitsAvailable': {'default': {'long': '3355020', 'short': '3362572'},  
   'openOnly': {'long': '3355020', 'short': '3362572'},  
   'reduceFirst': {'long': '3355020', 'short': '3362572'},  
   'reduceOnly': {'long': '0', 'short': '0'}}},  
  {'asks': [{'liquidity': 1000000, 'price': '118.746'},  
   {'liquidity': 2000000, 'price': '118.747'},  
   {'liquidity': 5000000, 'price': '118.748'},  
   {'liquidity': 10000000, 'price': '118.750'}],  
   'bids': [{'liquidity': 1000000, 'price': '118.646'},  
   {'liquidity': 2000000, 'price': '118.645'},  
   {'liquidity': 5000000, 'price': '118.644'},  
   {'liquidity': 10000000, 'price': '118.642'}],  
   'closeoutAsk': '118.750',  
   'closeoutBid': '118.642',  
   'instrument': 'EUR_JPY',  
   'quoteHomeConversionFactors': {'negativeUnits': '0.01255304',  
   'positiveUnits': '0.01253243'},  
   'status': 'non-tradeable',  
   'time': '2017-03-31T21:08:47.463728785Z',  
   'tradeable': False,  
   'type': 'PRICE',  
   'unitsAvailable': {'default': {'long': '3354208', 'short': '3362601'},  
   'openOnly': {'long': '3354208', 'short': '3362601'},  
   'reduceFirst': {'long': '3354208', 'short': '3362601'},  
   'reduceOnly': {'long': '0', 'short': '0'}}}]}  

Thank you for reading. If you would like to learn more, I have a course on this at Udemy and I am offering free coupons to first 20 sign ups. Once the first 20 free coupons are gone, you can still make sign up for the course at the ridiculous price of USD10 via this link.

For professionals, you can also check out my github where I posted all my codes for both v1 and v20 REST API. If you find the codes quite a challenging to understand, the Udemy course is for you. It took me some 10 - 20 hours to work through all the documentations for both v1 and v20 REST API and you are only paying USD10 via this link. Still not convinced, check out the preview, especially the lesson on rates. Why waste your precious time figuring out all the ins-and-outs of the API when I have already done it all for you.

1 comment:

  1. Thx for the post. I've set up the same code but I get an "Insufficient authorization to perform request" error. How is that fixed?


About Me

My photo
Data Science, algorithmic trading and Python enthusiast and researcher. Regularly conduct workshops for Quantopian, a Boston-based crowd-source hedge fund. Look out for my courses on Udemy as well. My motto? Being right is over-rated. This is especially true in trading. I rather keep exploring and learn continuously. All strategies suffer decay. Keep learning and growing. On that note, if you notice whatever I shared is wrong. Let me know. Rather than rejecting correction, I welcome them.