Bob Smith
Bob Smith

Reputation: 262

OANDA Deprecated REST API

As a means of learning the very basics of algorithmic trading and OANDA, I found a tutorial on how to make a very basic trading algorithm to "practice" algorithmic trading. The only issue is that the tutorial uses OANDA's v1 REST API, whereas it now uses v20 REST API.

The Python module oandapyV20 seems to have replaced oandapy, and it seems like there are methods that have become deprecated in the newest module. For example, on line #7 of the tutorial, it uses a method called get_history, and that seems to be totally deprecated now from what I can tell.

My question is, what could I do to replace the get_history method in particular, and are there any other sections of the code in the tutorial that someone who is familiar with the OANDA v20 REST API might see that are also going to be problematic/need to be totally reworked?

Upvotes: 3

Views: 1113

Answers (2)

Xinthral
Xinthral

Reputation: 437

Edit: I found some example code here that may be of use:

import oandapyV20
>>> import oandapyV20.endpoints.instruments as instruments
>>> client = oandapyV20.API(access_token=...)
>>> params = ...
>>> r = instruments.InstrumentsCandles(instrument="DE30_EUR",
>>>                                    params=params)
>>> client.request(r)
>>> print r.response

Therefore I would edit the tutorial as follows:

import oandapyV20
import oandapyV20.endpoints.instruments as instruments
oanda = oandapyV20.API(access_token=...)
params = {'start': '2016-12-08', 
          'end': '2016-12-10', 
          'granularity': 'M1'}
data = instruments.InstrumentsCandles(instrument='EUR_USD', params=params)
oanda.request(data)
print(data.response)

As I don't have a token to test, I am not sure what parameters are required for the new api, but hopefully this helps!

Edit #2: So I got about this far, but this documentation uses iPython and %matplotlib inline which I'm unfamiliar with. I couldn't quite get it all to work but this is where I'm at.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np                                                              

import oandapyV20
import oandapyV20.endpoints.pricing as pricing
import seaborn as sns; sns.set()                                                

TOKEN = #<oauth_token>
IDENT = #<accountID>
oanda = oandapyV20.API(access_token=TOKEN)
params = {
    'instruments': 'EUR_USD,EUR_JPY',
    'since': '2016-12-10',
    'granularity': 'M1'
}
data = pricing.PricingInfo(accountID=IDENT, params=params)
oanda.request(data)
df = pd.DataFrame(data.response['prices']).set_index('time')

df['closeoutAsk'].astype(float)

df['returns'] = np.log(float(df['closeoutAsk'][1]) / float(df['closeoutAsk'].shift(1)[1]))         

cols = []                                                                       
for momentum in [15, 30, 60, 120]:                                              
    col = f'position_{momentum}'                                                
    df[col] = np.sign(df['returns'].rolling(momentum).mean())                   
    cols.append(col)                                                            

strats = ['returns']                                                            
for col in cols:
    strat = f'strategy_{col.split("_")[1]}'                                    
    df[strat] = df[col].shift(1) * df['returns']                               
    strats.append(strat)                                                       

ts = df[strats]
ts = ts.cumsum()
plt.figure(); ts.plot(); plt.legend(loc='best')

Feel free to take a run at it.

Upvotes: 1

Ron
Ron

Reputation: 6591

I believe you are looking for the History instrument via the candles

Upvotes: 0

Related Questions