A script to retrieve account results from Oanda using the RESTv20 API

Last year I wrote a post about how to retrieve account results using the Oanda RESTv1 API. Just by introducing your account ID and token within the script you could obtain a summary with your account’s current statistics. However with the advent of the new RESTv20 API from Oanda this script is now more or less obsolete – as it only works with the older API – and it therefore becomes  important to introduce a new script to deal with this new account type. Today I want to talk about a new python script I designed for this purpose as well as some new features I have introduced to make the script more useful for those of you who have several accounts and want to be able to easily analyze them.

The script above can help you retrieve account information from your RESTv20 API account. However the script does not work by directly introducing your Oanda credentials and account ID within the code but instead it allows for command line parameters that you can use to define what account you want to get, from when you want to get data and whether the account you want to get is from the “real” or “demo” server. In order to use the script you’ll need to install Python 2.7 plus the qqpat and requests libraries which you can easily install if you’re using Python 64 bit on Windows by first installing the Python C++ compiler for python and then simply issuing the command “python -m pip install qqpat requests”.  On linux you can simply do “sudo pip install qqpat requests”.

Once you have all python libraries installed you can then proceed to use the script by saving it to a file (for example “oanda_script.py”) and then using the command “python oanda_script.py -a {account_id} -t {token} -e {real or demo} -st {starting date in YYYY-MM-DD}”. It is also worth noting that you do not need to define a starting date if you want to analyze the account from inception, in this case you simply should avoid defining the “-st” command line parameter and all the account data will be analyzed. Also note that the RESTv20 processing can be a bit slow if you’re requesting a lot of transactions – for example if you’re requesting several thousand transactions – so be patient when the script executes.

In the end you will be greeted with results like the ones above. You will be able to see some general account statistics including profit, maximum DD, CAGR, Sharpe and average daily, weekly and monthly returns plus a matplot lib generated graph showing you your account balance evolution as a function of time. It is also worth noting that the Oanda API won’t include transactions that have been closed in the current day, so at most the above will allow you to update your account trading results once every day. If you want you can also modify the script to get other statistics, the script uses the qqpat library to obtain system statistics so you can introduce additional qqpat function calls to get things like the Omega ratio, maximum drawdown length, average recovery, etc.

Since this script can be called from the command line with the account information as arguments it lends itself really well to the automation of your account analysis. You can simply create a script that calls the python analysis script once for each account you have with the appropriate arguments and in this manner you will be able to easily analyze all your account results in a matter of minutes. By using the starting date option you will also be able to avoid analyzing results that might be irrelevant to your trading, for example when you decide to test a different setup and you don’t care about the account’s results before that point in time.

The above script provides a needed update for the RESTv1 script I had posted before. Additionally not only does it implement support for the new RESTv20 API but it also implements command line arguments to get account results which makes the entire processing much easier when there are multiple trading accounts to analyze. If you would like to learn more about coding and how you too can create your own automated trading systems please consider joining Asirikuy.com, a website filled with educational videos, trading systems, development and a sound, honest and transparent approach towards automated trading.strategies.

Print Friendly, PDF & Email
You can leave a response, or trackback from your own site.

Leave a Reply

WordPress › Error

The site is experiencing technical difficulties.