Repository navigation
Expand file tree
/
Copy pathOandaAPI.m
More file actions
740 lines (547 loc) · 29.7 KB
/
Copy pathOandaAPI.m
File metadata and controls
740 lines (547 loc) · 29.7 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
classdef OandaAPI
% Class to interact with OANDA API - TradEA SYSTEMS
% (www.tradeasystems.com) 2014 Javier Falces Marin
%
% REQUIREMENTS
% JSONLAB : http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave
% URLREAD2: http://www.mathworks.com/matlabcentral/fileexchange/35693-urlread2
% thanks to Qianqian Fang(JSONLAB) and Jim Hokanson
properties
% define the properties of the class here, (like fields of a struct)
account = '';
token =''%'12345678900987654321-abc34135acde13f13530';
s_apiServer = 'http://api-sandbox.oanda.com/';%url to OANDA api sandbox
%username;
%password;
connected = false;
streaming ;
headers='';
environment = 'sandbox';
end
%% METHODS
methods
%% CONSTRUCTOR------------------------------------------------
function api = OandaAPI(token,environment,account)
%streaming = oandafunctions.OandaFunctions();
addpath 'libs'; % Json toolbox and urlread2
javaaddpath('libs/OandaFunctions.jar');%methodsview oandafunctions.OandaFunctions
javaaddpath('libs/lib/httpclient-4.3.3.jar');
javaaddpath('libs/lib/httpclient-cache-4.3.3.jar');
javaaddpath('libs/lib/commons-cli-1.2.jar');
javaaddpath('libs/lib/commons-codec-1.6.jar');%methodsview oandafunctions.OandaFunctions
javaaddpath('libs/lib/commons-logging-1.1.3.jar');
javaaddpath('libs/lib/fluent-hc-4.3.3.jar');
javaaddpath('libs/lib/httpcore-4.3.2.jar');
javaaddpath('libs/lib/httpcore-ab-4.3.2.jar');
javaaddpath('libs/lib/httpcore-nio-4.3.2.jar');
javaaddpath('libs/lib/httpmime-4.3.3.jar');
javaaddpath('libs/lib/json-simple-1.1.1.jar');
javaaddpath('libs/lib/commons-io-2.4.jar');
api.streaming = oandafunctions.OandaFunctions();
if strcmp(class(account),'double')==1
account = num2str(account);
end
if(nargin ==3)
api.environment = environment;
api.token = token;
api.account = account;
api.connected = true;
elseif( nargin ==2)
api.environment = environment;
api.token = token;
api.account = '0';
elseif(nargin ==0)
api.token = '';%'12345678900987654321-abc34135acde13f13530';%demo token
account='0';
print('DEMO');
api.environment = 'sandbox';
elseif(nargin ==1)
api.token = token;
api.environment = 'sandbox';
else
disp('Max 3 args:token,environment,account');
end
if strcmp(api.environment ,'sandbox' )
disp('Set enviroment to Sandbox');
api.s_apiServer = 'http://api-sandbox.oanda.com/';
elseif strcmp(api.environment ,'practice' )
disp('Set enviroment to practice');
api.s_apiServer = 'https://api-fxpractice.oanda.com/';
header0 = http_createHeader('X-HostCommonName','api-fxpractice.oanda.com');
header1 = http_createHeader('Host','api-fxpractice.oanda.com');
header2 = http_createHeader('X-Target-URI',api.s_apiServer);
header_auth = http_createHeader('Authorization',sprintf('Bearer %s',api.token));
api.headers = [header0;header1;header2;header_auth];
% header1 = struct('name','Content-Type','value','application/x-www-form-urlencoded; charset=UTF-8');
% header2 = struct('name','Connection','value','Keep-Alive');
% header3 = struct('name','Host','value','api-fxpractice.oanda.com');
% %api.headers = [header1;api.headers;header2;header3];
% api.headers = [header1 api.headers header2 header3];
elseif strcmp(api.environment ,'live' )
disp('Set enviroment to live');
api.s_apiServer = 'https://api-fxtrade.oanda.com/';
header0 = http_createHeader('X-HostCommonName','api-fxtrade.oanda.com');
header1 = http_createHeader('Host','api-fxtrade.oanda.com');
header2 = http_createHeader('X-Target-URI',api.s_apiServer);
header_auth = http_createHeader('Authorization',sprintf('Bearer %s',api.token));
api.headers = [header0;header1;header2;header_auth];
% header1 = struct('name','Content-Type','value','application/x-www-form-urlencoded; charset=UTF-8');
% header2 = struct('name','Connection','value','Keep-Alive');
% header3 = struct('name','Host','value','api-fxtrade.oanda.com');
% api.headers = [header1;api.headers;header2;header3];
else
disp('Uknown Environment(sandbox,practice,live) => Sandbox');
end
end
%% ACCOUNTS ---------------------------------------------------
%% Create Account, Only valid in SANDBOX
function accountid = create_account (api)
if strcmp(api.environment ,'sandbox' )
requestString = strcat(api.s_apiServer,'v1/accounts');
accountid=0;
header = struct('name','Bearer','value',api.token);
% header.name1 = 'Content-Type';
% header.value1 = 'application/x-www-form-urlencoded; charset=UTF-8';
% header.name2 = 'Connection';
% header.value2 = 'Keep-Alive';
% header.name3 = 'Host';
% header.value3 = 'api-sandbox.oanda.com';
response = urlread2(requestString,'POST','',header);
response = loadjson(response);
if response.accountId~=0
api.connected = true;
api.account = response.accountId;
api.username = response.username;
api.password = response.password;
text = sprintf('Connected to account %i , NAME: %s , PASSWORD: %s \n',api.account, api.username, api.password);
disp(text);
accountid = api.account;
else
disp('Error Connecting');
end
else
disp('ONLY avaible in sandbox Environment');
end
end
%% Get accounts of a token access
function ListAccounts = GetAccount(api)
requestString = strcat(api.s_apiServer,'v1/accounts/');
accountid=0;
response = api.MakeRequest(requestString);
if exist('response.accountId','var') && response.accountId~=0
accountid = response.accountId;
api.connected = true;
end
ListAccounts = response.accounts;
end
%% Instruments -----------------------------------------------
%% List of instruments
function ListInstruments = GetInstruments(api)
if(~api.connected)
disp('must be connected to an account') ;
return ;
end
requestString = strcat(api.s_apiServer , 'v1/instruments?accountId=' , api.account )%http://api-sandbox.oanda.com/v1/accounts/12345/trades
ListInstruments = api.MakeRequest(requestString).instruments;
end
%% List of current prices for a cell of symbols
function Prices = GetPrices(api,Symbols)
if(~api.connected)
disp('must be connected to an account') ;
return ;
end
symbolscell = '';
for i=1:length(Symbols)
symbolscell = strcat(symbolscell,Symbols(i),'%2C');
end
symbolsString = symbolscell{1}(1:length(symbolscell{1})-3) ;%Delete last separator
requestString = strcat(api.s_apiServer , 'v1/prices?instruments=' , symbolsString )%http://api-sandbox.oanda.com/v1/accounts/12345/trades
Prices = api.MakeRequest(requestString);
Prices = Prices(1).prices;
end
%% Retrieve instrument history
function History = GetHistory(api, symbol ,granularity, count,startdate,enddate, candleFormat,includeFirst )
if nargin ==2
granularity='';
count=0;
startdate=0;
enddate= 0;
candleFormat='bidask';
end
optdate = '';
if strcmp(class(startdate),'char')==1 || strcmp(class(enddate),'char')==1
disp 'startdate and endate must be a double'
History = '';
return;
end
optincludeFirst = '';
if startdate ~= 0 || enddate ~= 0
startdate = datestr(startdate,'yyyy-mm-dd');%'yyyy-mm-ddTHH:MM:SS.FFFZ');
optdate = strcat(optdate,'&start=',startdate);
%if enddate ~= 0
enddate = datestr(enddate,'yyyy-mm-dd');%'yyyy-mm-ddTHH:MM:SS.FFFZ');
optdate = strcat(optdate,'&end=',enddate);
count = 0;
if includeFirst == 1
optincludeFirst = strcat('&includeFirst= ','true');
else
optincludeFirst = '';
end
end
if count ~=0
count = num2str(count);
optcount = strcat('&count=',count);
else
optcount = '';
end
if strcmp(granularity,'')==0
optgranularity = strcat('&granularity=',granularity);
else
optgranularity = '';
end
if strcmp(candleFormat,'')==0
optcandleFormat = strcat('&candleFormat=',candleFormat);
else
optcandleFormat = '';
end
% if strcmp(api.environment,'sandbox')
requestString = strcat(api.s_apiServer , 'v1/candles?instrument=' , symbol,optgranularity,optcount,optdate,optcandleFormat,optincludeFirst )%http://api-sandbox.oanda.com/v1/accounts/12345/trades
%else
% requestString = strcat(api.s_apiServer , 'v1/candles?instrument=' , symbol,optgranularity,granularity,optcount,optdate,optcandleFormat,optincludeFirst )%http://api-sandbox.oanda.com/v1/accounts/12345/trades
%end
History = api.MakeRequest(requestString)
end
%% Order ( not executed) -----------------------------------------------
%% Create new limit,stop,marketIfTouched or market.
function [orderId,ret] = CreateOrder(api, symbol , volume, side, type,expiry,price, lowerBound,upperBound,stopLoss,takeProfit, trailingStop )
requestString = '';
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/' , api.account,'/orders');
%stringsconversion
if strcmp(class(volume),'double')==1
volume = num2str(volume);
end
if nargin==4 %market order
%Market Order
%https://api-fxpractice.oanda.com/v1/accounts/{account_id}/orders
%http://api-sandbox.oanda.com/v1/accounts/12345/trades
body = strcat('Content-Type=application%2Fx-www-form-urlencoded&instrument=',symbol,'&units=',volume,'&side=',side,'&type=market');
else %limit order
opt = '';
if strcmp(class(price),'double')==1
price = num2str(price);
end
if lowerBound ~=0
lowerBoundstr = num2str(lowerBound);
opt = strcat(opt,'&lowerBound=',lowerBoundstr);
end
if upperBound ~=0
upperBoundstr = num2str(upperBound);
opt = strcat(opt,'&upperBound=',upperBoundstr);
end
if stopLoss ~=0
stopLossstr = num2str(stopLoss);
opt = strcat(opt,'&stopLoss=',stopLossstr);
end
if takeProfit ~=0
takeProfitstr = num2str(takeProfit);
opt = strcat(opt,'&takeProfit=',takeProfitstr);
end
if trailingStop ~=0
trailingStopstr = num2str(trailingStop);
opt = strcat(opt,'&trailingStop=',trailingStopstr);
end
body = strcat('Content-Type=application%2Fx-www-form-urlencoded&instrument=',symbol,'&units=',volume,'&side=',side,'&type=',type,...
'&price=',pricestr,opt);
end
end
ret = api.MakePost(requestString,body)
orderId = ret.tradeOpened.id(1);
end
%% Get Order Info
function [order] = GetOrder(api, orderId)
requestString = '';
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/orders/',orderId );
order = api.MakeRequest(requestString);
else
disp('Must be connected - api.account not chosen');
order='0';
end
end
%% Cancel Order
function [order] = CloseOrder(api, orderId)
requestString = '';
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/orders/',orderId );
order = api.MakeDelete(requestString);
else
disp('Must be connected - api.account not chosen');
order='0';
end
end
%% Modify Order
function [orderId,ret] = ModifyOrder(api,orderId, symbol , volume, side, type,expiry,price, lowerBound,upperBound,stopLoss,takeProfit, trailingStop )
requestString = '';
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/' , api.account,'/orders/',orderId);
%stringsconversion
volumestr = num2str(volume);
%limit order
opt = '';
pricestr = num2str(price);
if lowerBound ~=0
lowerBoundstr = num2str(lowerBound);
opt = strcat(opt,'&lowerBound=',lowerBoundstr);
end
if upperBound ~=0
upperBoundstr = num2str(upperBound);
opt = strcat(opt,'&upperBound=',upperBoundstr);
end
if stopLoss ~=0
stopLossstr = num2str(stopLoss);
opt = strcat(opt,'&stopLoss=',stopLossstr);
end
if takeProfit ~=0
takeProfitstr = num2str(takeProfit);
opt = strcat(opt,'&takeProfit=',takeProfitstr);
end
if trailingStop ~=0
trailingStopstr = num2str(trailingStop);
opt = strcat(opt,'&trailingStop=',trailingStopstr);
end
body = strcat('Content-Type=application%2Fx-www-form-urlencoded&instrument=',symbol,'&units=',volumestr,'&side=',side,'&type=',type,...
'&price=',pricestr,opt);
end
ret = api.MakePatch(requestString,body)
orderId = ret.tradeOpened.id(1);
end
%% Trades (Executed)-----------------------------------------------
%% List of open trades
function [ListTrades] = GetListTrades(api,symbolorid,count)
requestString = '';
opt = '';
if nargin ==1
opt = '';
else
if strcmp(class(symbolorid),'double')==1
symbolorid = num2str(symbolorid);
opt = strcat('ids=',symbolorid);
else
opt = strcat('instrument=',symbolorid);
end
if strcmp(class(count),'double')==1
count = num2str(count);
opt = strcat(opt,'&count=',count);
end
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/trades?',opt );
ListTrades = api.MakeRequest(requestString);
ListTrades = ListTrades.trades;
else
disp('Must be connected - api.account not chosen');
ListTrades='0';
end
end
%% Get trade- id
function [trade] = GetTrade(api, orderId)
requestString = '';
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/trades/',orderId );
trade = api.MakeRequest(requestString);
else
disp('Must be connected - api.account not chosen');
trade='0';
end
end
%% Modify trade
function [ret] = ModifyTrade(api,orderId,stopLoss,takeProfit, trailingStop )
requestString = '';
if nargin < 3
disp 'Must have orderrID and variable min'
ret = 0;
return ;
end
if nargin == 3
disp 'TP and trail not modify'
takeProfit = 0;
trailingStop = 0;
end
if nargin == 4
disp 'trail not modify'
trailingStop = 0;
end
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/' , api.account,'/trades/',orderId);
%limit order
opt = '';
if stopLoss ~=0
stopLossstr = num2str(stopLoss);
opt = strcat(opt,'stopLoss=',stopLossstr,'&');
end
if nargin>3
if takeProfit ~=0
takeProfitstr = num2str(takeProfit);
opt = strcat(opt,'takeProfit=',takeProfitstr,'&');
end
if nargin>4
if trailingStop ~=0
trailingStopstr = num2str(trailingStop);
opt = strcat(opt,'trailingStop=',trailingStopstr,'&');
end
end
end
opt = opt(1:length(opt)-1) ;%Delete last separator
end
ret = api.MakePatch(requestString,opt)
end
%% Close Trade
function [order] = CloseTrade(api, orderId)
requestString = '';
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/trades/',orderId );
order = api.MakeDelete(requestString);
else
disp('Must be connected - api.account not chosen');
order='0';
end
end
%% Positions -----------------------------------------------
%% List of Positions
function [ListPositions] = GetListPositions(api,symbol)
requestString = '';
if nargin ==1
opt = '';
else
opt = symbol;
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/positions/',opt );
ListPositions = api.MakeRequest(requestString);
ListPositions = Listpositions.positions;
else
disp('Must be connected - api.account not chosen');
ListPositions='0';
end
end
%% Close Positions
function [order] = ClosePosition(api, symbol)
requestString = '';
if api.connected
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/positions/',symbol );
order = api.MakeDelete(requestString);
else
disp('Must be connected - api.account not chosen');
order='0';
end
end
%% Transaction History ---------------------------------------
%% Get Transaction history
function history = GetTransactionHistory(api, maxId , minId , count , symbol,orderId )
if api.connected
requestString = '';
opt = '';
if nargin >1
opt = '?';
if maxId ~=0
if strcmp(class(maxId),'double')==1
maxId = num2str(maxId);
end
option = strcat(opt,'maxid=',maxId,'&');
end
if minId ~=0
if strcmp(class(minId),'double')==1
minId = num2str(minId);
end
opt = strcat(opt,'minid=',minId,'&');
end
if count ~=0
if strcmp(class(count),'double')==1
count = num2str(count);
end
opt = strcat(opt,'count=',count,'&');
end
if strcmp(symbol, '')==0
opt = strcat(opt,'instrument=',symbol,'&');
end
if orderId ~=0
if strcmp(class(orderId),'double')==1
orderId = num2str(orderId);
end
opt = strcat(opt,'ids=',orderId,'&');
end
end
requestString = strcat(api.s_apiServer , 'v1/accounts/',api.account,'/transactions',opt );
history = api.MakeRequest(requestString).transactions;
else
disp('Must be connected - api.account not chosen');
history='0';
end
end
%% Streaming Prices ---------------------------------------
function prices = GetPricesSuscribe(api, Symbols)
requestString = '';
symbolscell = '';
if iscell(Symbols)
for i=1:length(Symbols)
symbolscell = strcat(symbolscell,Symbols(i),'%2C');
end
symbolsString = symbolscell{1}(1:length(symbolscell{1})-3) ;%Delete last separator
else
symbolsString = Symbols;
end
if api.connected
requestString = strcat(api.s_apiServer , 'v1/prices?accountId=',api.account,'&instruments=',symbolsString );
prices = '';
api.streaming.streamingSuscribe(requestString, api.token) ;
%%prices = api.MakeRequest(requestString);
%%prices = prices.prices;
%prices = loadjson(prices.prices);
%prices = prices.prices;
else
disp('Must be connected - api.account not chosen');
prices='0';
end
end
%% REQUEST , POST , DELETE
function [ response ] = MakeRequest(api,requestString )
resp = urlread2(requestString,'GET','',api.headers);
response = loadjson(resp);
end
function [ response ] = MakePost(api,requestString, body )
resp = urlread2(requestString,'POST',body,api.headers);
response = loadjson(resp);
end
function [ response ] = MakeDelete(api,requestString )
resp = urlread2(requestString,'DELETE','',api.headers);
response = loadjson(resp);
end
function [ response ] = MakePatch(api,requestString ,body)
disp('PATH ethod not implemnted') ;
% headerPatch = [http_createHeader('X-HTTP-Method-Override',''); api.headers ];
resp= char(oandafunctions.OandaFunctions.PatchRequest(requestString,api.token,body) );
%resp = urlread2(strcat(requestString,'?_HttpMethod=PATCH'),'POST',body,api.headers);
response = loadjson(resp);
end
end
end
% methods, including the constructor are defined in this block