forked from oanda/oandapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoandapy.py
277 lines (224 loc) · 9.78 KB
/
oandapy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import json
import requests
from .exceptions import BadEnvironment, OandaError
""" OANDA API wrapper for OANDA's REST API """
""" EndpointsMixin provides a mixin for the API instance
Parameters that need to be embedded in the API url just need to be passed as a
keyword argument.
E.g. oandapy_instance.get_instruments(instruments="EUR_USD")
"""
class EndpointsMixin(object):
"""Rates"""
def get_instruments(self, account_id, **params):
""" Get an instrument list
Docs: http://developer.oanda.com/rest-live/rates
"""
params['accountId'] = account_id
endpoint = 'v1/instruments'
return self.request(endpoint, params=params)
def get_prices(self, **params):
""" Get current prices
Docs: http://developer.oanda.com/rest-live/rates
"""
endpoint = 'v1/prices'
return self.request(endpoint, params=params)
def get_history(self, **params):
""" Retrieve instrument history
Docs: http://developer.oanda.com/rest-live/rates
"""
endpoint = 'v1/candles'
return self.request(endpoint, params=params)
"""Accounts"""
def create_account(self, **params):
""" Create an account. Valid only in sandbox.
Docs: http://developer.oanda.com/rest-live/accounts
"""
endpoint = 'v1/accounts'
return self.request(endpoint, "POST", params=params)
def get_accounts(self, **params):
""" Get accounts for a user.
Docs: http://developer.oanda.com/rest-live/accounts
"""
endpoint = 'v1/accounts'
return self.request(endpoint, params=params)
def get_account(self, account_id, **params):
""" Get account information
Docs: http://developer.oanda.com/rest-live/accounts
"""
endpoint = 'v1/accounts/%s' % (account_id)
return self.request(endpoint, params=params)
"""Orders"""
def get_orders(self, account_id, **params):
""" Get orders for an account
Docs: http://developer.oanda.com/rest-live/orders
"""
endpoint = 'v1/accounts/%s/orders' % (account_id)
return self.request(endpoint, params=params)
def create_order(self, account_id, **params):
""" Create a new order
Docs: http://developer.oanda.com/rest-live/orders
"""
endpoint = 'v1/accounts/%s/orders' % (account_id)
return self.request(endpoint, "POST", params=params)
def get_order(self, account_id, order_id, **params):
""" Get information for an order
Docs: http://developer.oanda.com/rest-live/orders
"""
endpoint = 'v1/accounts/%s/orders/%s' % (account_id, order_id)
return self.request(endpoint, params=params)
def modify_order(self, account_id, order_id, **params):
""" Modify an existing order
Docs: http://developer.oanda.com/rest-live/orders
"""
endpoint = 'v1/accounts/%s/orders/%s' % (account_id, order_id)
return self.request(endpoint, "PATCH", params=params)
def close_order(self, account_id, order_id, **params):
""" Close an order
Docs: http://developer.oanda.com/rest-live/orders
"""
endpoint = 'v1/accounts/%s/orders/%s' % (account_id, order_id)
return self.request(endpoint, "DELETE", params=params)
"""Trades"""
def get_trades(self, account_id, **params):
""" Get a list of open trades
Docs: http://developer.oanda.com/rest-live/trades
"""
endpoint = 'v1/accounts/%s/trades' % (account_id)
return self.request(endpoint, params=params)
def get_trade(self, account_id, trade_id, **params):
""" Get information on a specific trade
Docs: http://developer.oanda.com/rest-live/trades
"""
endpoint = 'v1/accounts/%s/trades/%s' % (account_id, trade_id)
return self.request(endpoint, params=params)
def modify_trade(self, account_id, trade_id, **params):
""" Modify an existing trade
Docs: http://developer.oanda.com/rest-live/trades
"""
endpoint = 'v1/accounts/%s/trades/%s' % (account_id, trade_id)
return self.request(endpoint, "PATCH", params=params)
def close_trade(self, account_id, trade_id, **params):
""" Close an open trade
Docs: http://developer.oanda.com/rest-live/trades
"""
endpoint = 'v1/accounts/%s/trades/%s' % (account_id, trade_id)
return self.request(endpoint, "DELETE", params=params)
"""Positions"""
def get_positions(self, account_id, **params):
""" Get a list of all open positions
Docs: http://developer.oanda.com/rest-live/positions
"""
endpoint = 'v1/accounts/%s/positions' % (account_id)
return self.request(endpoint, params=params)
def get_position(self, account_id, instrument, **params):
""" Get the position for an instrument
Docs: http://developer.oanda.com/rest-live/positions
"""
endpoint = 'v1/accounts/%s/positions/%s' % (account_id, instrument)
return self.request(endpoint, params=params)
def close_position(self, account_id, instrument, **params):
""" Close an existing position
Docs: http://developer.oanda.com/rest-live/positions
"""
endpoint = 'v1/accounts/%s/positions/%s' % (account_id, instrument)
return self.request(endpoint, "DELETE", params=params)
"""Transaction History"""
def get_transaction_history(self, account_id, **params):
""" Get transaction history
Docs: http://developer.oanda.com/rest-live/transaction-history
"""
endpoint = 'v1/accounts/%s/transactions' % (account_id)
return self.request(endpoint, params=params)
def get_transaction(self, account_id, transaction_id):
""" Get information for a transaction
Docs: http://developer.oanda.com/rest-live/transaction-history
"""
endpoint = 'v1/accounts/%s/transactions/%s' % \
(account_id, transaction_id)
return self.request(endpoint)
"""Forex Labs"""
def get_eco_calendar(self, **params):
"""Returns up to 1 year of economic calendar info
Docs: http://developer.oanda.com/rest-live/forex-labs/
"""
endpoint = 'labs/v1/calendar'
return self.request(endpoint, params=params)
def get_historical_position_ratios(self, **params):
"""Returns up to 1 year of historical position ratios
Docs: http://developer.oanda.com/rest-live/forex-labs/
"""
endpoint = 'labs/v1/historical_position_ratios'
return self.request(endpoint, params=params)
def get_historical_spreads(self, **params):
"""Returns up to 1 year of spread information
Docs: http://developer.oanda.com/rest-live/forex-labs/
"""
endpoint = 'labs/v1/spreads'
return self.request(endpoint, params=params)
def get_commitments_of_traders(self, **params):
"""Returns up to 4 years of Commitments of Traders data from the CFTC
Docs: http://developer.oanda.com/rest-live/forex-labs/
"""
endpoint = 'labs/v1/commitments_of_traders'
return self.request(endpoint, params=params)
def get_orderbook(self, **params):
"""Returns up to 1 year of OANDA Order book data
Docs: http://developer.oanda.com/rest-live/forex-labs/
"""
endpoint = 'labs/v1/orderbook_data'
return self.request(endpoint, params=params)
""" Provides functionality for access to core OANDA API calls """
class API(EndpointsMixin, object):
def __init__(self,
environment="practice", access_token=None, headers=None):
"""Instantiates an instance of OandaPy's API wrapper
:param environment: (optional) Provide the environment for oanda's
REST api, either 'sandbox', 'practice', or 'live'. Default: practice
:param access_token: (optional) Provide a valid access token if you
have one. This is required if the environment is not sandbox.
"""
if environment == 'sandbox':
self.api_url = 'http://api-sandbox.oanda.com'
elif environment == 'practice':
self.api_url = 'https://api-fxpractice.oanda.com'
elif environment == 'live':
self.api_url = 'https://api-fxtrade.oanda.com'
else:
raise BadEnvironment(environment)
self.access_token = access_token
self.client = requests.Session()
# personal token authentication
if self.access_token:
self.client.headers['Authorization'] = 'Bearer '+self.access_token
if headers:
self.client.headers.update(headers)
def request(self, endpoint, method='GET', params=None):
"""Returns dict of response from OANDA's open API
:param endpoint: (required) OANDA API (e.g. v1/instruments)
:type endpoint: string
:param method: (optional) Method of accessing data, either GET or POST.
(default GET)
:type method: string
:param params: (optional) Dict of parameters (if any) accepted the by
OANDA API endpoint you are trying to access (default None)
:type params: dict or None
"""
url = '%s/%s' % (self.api_url, endpoint)
method = method.lower()
params = params or {}
func = getattr(self.client, method)
request_args = {}
if method == 'get':
request_args['params'] = params
else:
request_args['data'] = params
try:
response = func(url, **request_args)
except requests.RequestException as e:
print (str(e))
content = response.content.decode('utf-8')
content = json.loads(content)
# error message
if response.status_code >= 400:
raise OandaError(content)
return content