After reading Dr. Yves Hilpisch’s article, “Algorithmic trading using 100 lines of python code,” I was inspired to give it a shot. I wanted to apply his guide on how to use a time series momentum algorithm because I have been interested in forex trading with cryptocurrencies. I set up a free forex trial account on OANDA, jumped into a jupyter notebook, and got to work. I hit an issue. OANDA changed their API from “v1” to “v20” and all new accounts default to the new API. I ended up rewriting his sample code to work with the new OANDA v20 API using a third party python library.
If you are still wanting to use the original v1 API sample code, you could reach out to OANDA chat support and manually request a v1 legacy demo account. It takes 2 business days. Ain’t nobody got time for that! Plus, you can’t hedge in the v1 API.
Here is my github repo with both the v1 and v20 sample code in jupyter notebooks, for comparison:
https://github.com/benjaminchodroff/oandamomentum
Here is the gist of my end result using the OANDA v20 API
Thanks again, Dr. Hilpisch!
Hello Ben,
Thanks for posting the momentum code. I have been working on the backtest but when I run your code I get an error in the params. The error is as follows:
—————————————————————————
ValueError Traceback (most recent call last)
in ()
5
6 # The v20 api handles from times a little differently – be careful of the timezone
—-> 7 params={“from”: parser.parse(“2016-12-07 18:00:00 EDT”).strftime(‘%s’),
8 “to”: parser.parse(“2016-12-10 00:000:00 EDT”).strftime(‘%s’),
9 “granularity”:’M1′,
ValueError: Invalid format string
Any idea how to fix this?
Thanks, I look forward to hearing from you.
Grace
It has a typo – extra zero in the parse to time
Hi Benjamin Chodroff,
I really appreciate this article as I was too inspired by Dr. Yves Hilpisch’s article but I found it difficult to adapt the code onto the V20 API. However I am a little stuck as when I go to run the code I get the error: “AttributeError: ‘MomentumTrader’ object has no attribute ‘rates'” for the last line. How did you manage to get past that?
Thanks.
Veer.
Hi Veer – Check the code above? You can see that the rates function is defined in the MomentumTrader class above as “def rates(self, account_id, instruments, **params):”. Perhaps you have a whitespace issue? Python is very sensitive to tabs vs spaces… and it can lose a function this way. That’s my best guess 🙂
youre a genius and life saver;
what source or cheat sheet do i have to learn the new V20?
Hi Benjamin,
Thanks very much for your article and code. I have little to no experience with coding so I’m having some trouble with getting it running. I get the following error:
KeyError Traceback (most recent call last)
in
9 config.read(‘oanda.cfg’)
10
—> 11 client = API(access_token=config[‘oanda’][‘access_token’])
~\anaconda3\envs\oandaV20\lib\configparser.py in __getitem__(self, key)
956 def __getitem__(self, key):
957 if key != self.default_section and not self.has_section(key):
–> 958 raise KeyError(key)
959 return self._proxies[key]
960
KeyError: ‘oanda
any help is much appreciated.
thanks,
Rob
It appears you have not created the “oanda.cfg” file with your account_id and access_token. Check out this file as an example:
https://github.com/benjaminchodroff/oandamomentum/blob/master/example.cfg
And you can get an oanda key here:
https://www.oanda.com/demo-account/tpa/personal_token
Hi Benjamin,
I managed to get it running without creating the oanda.cfg file… I have a second question: If I want to implement the 120 minute strategy, ought I not to change the mt parameters to:
mt = MomentumTrader(momentum=1440,accountID=config[‘oanda’][‘account_id’],params={“instruments”: “EUR_USD”}? Since 2 hours = 7200 seconds/5 second intervals = 1440.
Also I get another error when I try to backtest the results with a granularity other than 1-minute intervals, any thoughts?
Rob