-
Notifications
You must be signed in to change notification settings - Fork 15
/
oanda.js
262 lines (227 loc) · 7.86 KB
/
oanda.js
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
var OANDA = OANDA || {};
OANDA.baseURL = OANDA.baseURL || "https://api-fxpractice.oanda.com";
OANDA.auth = OANDA.auth || {};
OANDA.auth.enabled = OANDA.auth.enabled || false;
OANDA.auth.token = OANDA.auth.token || "";
var setAuthHeader = function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + OANDA.auth.token);
};
var sendAjaxRequest = function(endpoint, method, parameters, requiresAuth, onComplete) {
var contentType = "";
if(method === 'POST' || method === 'PUT' || method === 'PATCH') {
contentType = "application/x-www-form-urlencoded";
}
var beforeSend = function() {};
if(OANDA.auth.enabled && requiresAuth) {
beforeSend = setAuthHeader;
}
var req = $.ajax({
url: OANDA.baseURL + endpoint,
type: method,
dataType: 'json',
data : parameters,
contentType: contentType,
beforeSend: beforeSend,
});
req.always(onComplete);
};
/* Send a raw api request on an endpoint.
*/
OANDA.api = function(endpoint, method, parameters, callback) {
sendAjaxRequest(endpoint, method, parameters, true, function(jqXHR, textResponse) {
var response = {};
if(textResponse != 'success') {
response.error = { 'HTTPCode' : jqXHR.status };
try {
var errorJSON = JSON.parse(jqXHR.responseText);
$.extend(response.error, errorJSON);
} catch(e) {
}
} else {
response = jqXHR;
}
if(callback) {
callback(response);
}
});
};
OANDA.transaction = OANDA.transaction || {};
/*
* Lists all transactions for a specified account.
* Accepts optional parameters:
* maxId => Number
* mindId => Number
* count => Number
* Instrument => String
*
*/
OANDA.transaction.list = function(accountId, optParameters, callback) {
//Disallow passing ids parameter (use listSpecific instead).
if(optParameters.ids) { delete optParameters.ids; }
OANDA.api("/v1/accounts/" + accountId + "/transactions", 'GET', optParameters, callback);
};
/* List specific transactions by transaction id.
* Accepts no optional parameters
*/
OANDA.transaction.listSpecific = function(accountId, transId, callback) {
OANDA.api("/v1/accounts/" + accountId + "/transactions/" + transId, 'GET', {}, callback);
};
OANDA.trade = OANDA.trade || {};
/* List all trade in account.
* Accepts optional parameters:
* maxId => Number
* count => Number
* instrument => Number
*/
OANDA.trade.list = function(accountId, optParameters, callback) {
if(optParameters.ids) { delete optParameters.ids; }
OANDA.api("/v1/accounts/" + accountId + "/trades", 'GET', optParameters, callback);
};
/* List specific trades by id.
* Accepts no optional parameters
*/
OANDA.trade.listSpecific = function(accountId, tradeIds, callback) {
var tradesStr = tradeIds.join(',');
OANDA.api("/v1/accounts/" + accountId + "/trades", 'GET', {ids : tradesStr}, callback);
};
/* Close an existing trade
* Accepts no optional parameters.
*/
OANDA.trade.close = function(accountId, tradeId, callback) {
OANDA.api("/v1/accounts/" + accountId + "/trades/" + tradeId, 'DELETE', {}, callback);
};
/* Modfify and existing trade
* Accepts optional parameters:
* stopLoss => number
* takeProfit => number
* trailingStop => number
*/
OANDA.trade.change = function(accountId, tradeId, optParameters, callback) {
OANDA.api("/v1/accounts/" + accountId + "/trades/" + tradeId, 'PATCH', optParameters, callback);
};
OANDA.order = OANDA.order || {};
/* List all orders in account.
* Accepts optional parameters:
* maxId => Number
* count => Number
* Instrument => String
*/
OANDA.order.list = function(accountId, optParameters, callback) {
if(optParameters.ids) { delete optParameters.ids; }
OANDA.api("/v1/accounts/" + accountId + "/orders", 'GET', optParameters, callback);
};
/* List specific orders by id.
* Accepts no optional parameters
*/
OANDA.order.listSpecific = function(accountId, orderIds, callback) {
var ordersStr = orderIds.join(',');
OANDA.api("/v1/accounts/" + accountId + "/orders", 'GET', {ids: ordersStr}, callback);
};
/* Create a new order.
* expiry and price are only required if order type is 'marketIfTouched', 'stop' or 'limit'
* Accepts optional parameters
* expiry => string RFC 3339 format
* price => number
* stopLoss => number
* takeProfit => number
* trailingStop => number
* upperBound => number
* lowerBound => number
*/
OANDA.order.open = function(accountId, instrument, units, side, type, optParameters, callback) {
OANDA.api("/v1/accounts/" + accountId + "/orders", 'POST',
$.extend({instrument: instrument, units: units, side:side, type:type}, optParameters),
callback);
};
/* Close an existing order.
* Accepts no optional parameters
*/
OANDA.order.close = function(accountId, orderId, callback) {
OANDA.api("/v1/accounts/" + accountId + "/orders/" + orderId, 'DELETE', {}, callback);
};
/* Modify an existing order
* Accepts optional parameters:
* units => number
* price => number
* expiry => string
* lowerBound => number
* upperBound => number
* stopLoss => number
* takeProfit => number
* trailingStop => number
*/
OANDA.order.change = function(accountId, orderId, optParameters, callback) {
OANDA.api("/v1/accounts/" + accountId + "/orders/" + orderId, 'PATCH', optParameters, callback);
};
OANDA.position = OANDA.position || {};
/* List all positions
* Accepts no optional parameters
*/
OANDA.position.list = function(accountId, callback) {
OANDA.api("/v1/accounts/" + accountId + "/positions", 'GET', {}, callback);
};
/* List only position for specific instrument
* Accepts no optional parameters
*/
OANDA.position.listSpecific = function(accountId, instrument, callback) {
OANDA.api("/v1/accounts/" + accountId + "/positions/" + instrument, 'GET' ,{}, callback);
};
/* Close all trades in a positions
* Accepts no optional parameters
*/
OANDA.position.close = function(accountId, instrument, callback) {
OANDA.api("/v1/accounts/" + accountId + "/positions/" + instrument, 'DELETE', {}, callback);
};
OANDA.account = OANDA.account || {};
/* Register an account
* Accepts no optional parameters
*/
OANDA.account.register = function(currency, callback) {
OANDA.api("/v1/accounts", 'POST', {currency:currency}, callback);
};
/* Get accounts
* Accepts no optional parameters
*/
OANDA.account.get = function(callback) {
OANDA.api("/v1/accounts", 'GET', {}, callback);
};
/* List all accounts associated with user
* Accepts no optional parameters
*/
OANDA.account.list = function(callback) {
OANDA.api("/v1/accounts", 'GET', {}, callback);
}
/* List specific account details
* Accepts no optional parameters
*/
OANDA.account.listSpecific = function(accountId, callback) {
OANDA.api("/v1/accounts/" + accountId, 'GET', {}, callback);
}
OANDA.rate = OANDA.rate || {};
/* List all instruments available.
* Accepts optional parameters:
* fields => array of strings
*/
OANDA.rate.instruments = function(accountId, fields, callback) {
var fieldStr = fields.join(',');
var data = fieldStr ? { "fields" : fieldStr , "accountId" : accountId} : {};
OANDA.api("/v1/instruments", 'GET', data, callback);
};
/* Return candlesticks for a specific instrument
* Accepts optional parameters:
* granularity => string
* count => number
* start => string
* end => string
* candleFormat => string
* includeFirst => boolean
*/
OANDA.rate.history = function(symbol, optParameters, callback) {
OANDA.api("/v1/candles", 'GET', $.extend({instrument:symbol}, optParameters), callback);
};
/* Lists the current price for a list of instruments
* Accepts no optional parameters
*/
OANDA.rate.quote = function(symbols, callback) {
OANDA.api("/v1/prices", 'GET', {instruments: symbols.join(',')}, callback);
};