forked from woulgar/tradesman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_api_call_bittrex.m
404 lines (399 loc) · 12.9 KB
/
main_api_call_bittrex.m
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
function [response,status]=main_api_call_bittrex(method,params)
default_method_names={'getmarkets','getcurrencies','getticker','getmarketsummaries',...
'getmarketsummary','getorderbook','getmarkethistory','buylimit','buymarket',...
'sell','withdrawal_requests','bitcoin_withdrawal',...
'selllimit','sellmarket','cancel',...
'getopenorders','getbalances','getbalance','getdepositaddress','withdraw',...
'getorder','getorderhistory','getwithdrawalhistory','getdeposithistory'};
default_method_types={@bittrex_getmarkets,@bittrex_getcurrencies,...
@bittrex_getticker,@bittrex_getmarketsummaries,@bittrex_getmarketsummary,...
@bittrex_getorderbook,@bittrex_getmarkethistory,...
@bittrex_buylimit,@bittrex_buymarket,@bittrex_sell,...
@bittrex_withdrawal_requests,@bittrex_bitcoin_withdrawal,...
@bittrex_selllimit,@bittrex_sellmarket,...
@bittrex_cancel,@bittrex_getopenorders,@bittrex_getbalances,...
@bittrex_getbalance,@bittrex_getdepositaddress,@bittrex_withdraw,...
@bittrex_getorder,@bittrex_getorderhistory,@bittrex_getwithdrawalhistory,@bittrex_getdeposithistory};
method_select=find(strcmp(method,default_method_names), 1);
if isempty(method_select)
disp('wrong method name for bittrex, please try: getmarkets,getcurrencies,getticker,getmarketsummaries,getmarketsummary,getorderbook,getmarkethistory,buylimit,buymarket,sell,withdrawal_requests,bitcoin_withdrawal,selllimit,sellmarket,cancel,getopenorders,getbalances,getbalance,getdepositaddress,withdraw,getorder,getorderhistory,getwithdrawalhistory,getdeposithistory')
response='error';
status='error';
else
[response,status]=default_method_types{method_select}(params);
end
end
% requests w/o authentication
% getmarkets function
function [response,status]=bittrex_getmarkets(~)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
url='https://bittrex.com/api/v1.1/public/getmarkets';
[response,status] = urlread2(url,'GET',{},header_1);
end
% getcurrencies function
function [response,status]=bittrex_getcurrencies(~)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
url='https://bittrex.com/api/v1.1/public/getcurrencies';
[response,status] = urlread2(url,'GET',{},header_1);
end
% getticker function
function [response,status]=bittrex_getticker(params)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
market_loc=find(strcmp('market',params),1);
if isempty(market_loc)
url='https://bittrex.com/api/v1.1/public/getticker/?market=BTC-LTC';
disp('market missing, used default: BTC-LTC')
else
url=['https://bittrex.com/api/v1.1/public/getticker/?market=',params{market_loc+1}];
end
[response,status] = urlread2(url,'GET',{},header_1);
end
% getmarketsummaries function
function [response,status]=bittrex_getmarketsummaries(~)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
url='https://bittrex.com/api/v1.1/public/getmarketsummaries';
[response,status] = urlread2(url,'GET',{},header_1);
end
% getmarketsummary function
function [response,status]=bittrex_getmarketsummary(params)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
market_loc=find(strcmp('market',params),1);
if isempty(market_loc)
url='https://bittrex.com/api/v1.1/public/getmarketsummary/?market=BTC-LTC';
disp('market missing, used default: BTC-LTC')
else
url=['https://bittrex.com/api/v1.1/public/getmarketsummary/?market=',params{market_loc+1}];
end
[response,status] = urlread2(url,'GET',{},header_1);
end
% getorderbook function
function [response,status]=bittrex_getorderbook(params)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
market_loc=find(strcmp('market',params),1);
type_loc=find(strcmp('type',params),1);
depth_loc=find(strcmp('depth',params),1);
if isempty(market_loc)
url='https://bittrex.com/api/v1.1/public/getmarketsummary/?market=BTC-LTC';
disp('market missing, used default: BTC-LTC')
else
url=['https://bittrex.com/api/v1.1/public/getmarketsummary/?market=',params{market_loc+1}];
end
if isempty(type_loc)
url=[url,'&type=both'];
disp('type missing, used default: both')
else
url=[url,'&type=',params{type_loc+1}];
end
if isempty(depth_loc)
url=[url,'&depth=20'];
disp('depth missing, used default: 20')
else
url=[url,'&depth=',params{depth_loc+1}];
end
[response,status] = urlread2(url,'GET',{},header_1);
end
% getmarkethistory function
function [response,status]=bittrex_getmarkethistory(params)
header_1=http_createHeader('Content-Type','application/x-www-form-urlencoded');
market_loc=find(strcmp('market',params),1);
count_loc=find(strcmp('count',params),1);
if isempty(market_loc)
url='https://bittrex.com/api/v1.1/public/getmarkethistory/?market=BTC-LTC';
disp('market missing, used default: BTC-LTC')
else
url=['https://bittrex.com/api/v1.1/public/getmarkethistory/?market=',params{market_loc+1}];
end
if isempty(count_loc)
url=[url,'&count=20'];
disp('count missing, used default: 20')
else
url=[url,'&count=',params{count_loc+1}];
end
[response,status] = urlread2(url,'GET',{},header_1);
end
% requests with authentication
% header & parameters
function [response,status]=bittrex_authenticated(url_ext,parameter)
url='https://bittrex.com/api/v1.1/';
% nonce
nonce = num2str(floor((now-datenum('1970', 'yyyy'))*8640000000));
[key,secret,~]=key_secret('bittrex');
url=[url,parameter,'?apikey=',key,'&nonce=',nonce,url_ext];
Signature = crypto(url, secret, 'HmacSHA512');
header1=http_createHeader('User-Agent','Mozilla/4.0 (compatible; Node Bittrex API)');
header2=http_createHeader('Content-Type','application/x-www-form-urlencoded');
header3=http_createHeader('APISIGN',char(Signature));
header=[header1 header2 header3];
[response,status] = urlread2(url,'GET','',header);
end
% buylimit function
function [response,status]=bittrex_buylimit(params)
parameter='market/buylimit/';
market_loc=find(strcmp('market',params),1);
quantity_loc=find(strcmp('quantity',params),1);
rate_loc=find(strcmp('rate',params),1);
if isempty(market_loc)
disp('market missing')
response='error';
status='error';
else
url_ext=['&market=',params{market_loc+1}];
end
if isempty(quantity_loc)
disp('quantity missing')
response='error';
status='error';
else
url_ext=[url_ext,'&quantity=',params{quantity_loc+1}];
end
if isempty(rate_loc)
disp('rate missing')
response='error';
status='error';
else
url_ext=[url_ext,'&rate=',params{rate_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% buymarket function
function [response,status]=bittrex_buymarket(params)
parameter='market/buymarket/';
market_loc=find(strcmp('market',params),1);
quantity_loc=find(strcmp('quantity',params),1);
if isempty(market_loc)
disp('market missing')
response='error';
status='error';
else
url_ext=['&market=',params{market_loc+1}];
end
if isempty(quantity_loc)
disp('quantity missing')
response='error';
status='error';
else
url_ext=[url_ext,'&quantity=',params{quantity_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% selllimit function
function [response,status]=bittrex_selllimit(params)
parameter='market/selllimit/';
market_loc=find(strcmp('market',params),1);
quantity_loc=find(strcmp('quantity',params),1);
rate_loc=find(strcmp('rate',params),1);
if isempty(market_loc)
disp('market missing')
response='error';
status='error';
else
url_ext=['&market=',params{market_loc+1}];
end
if isempty(quantity_loc)
disp('quantity missing')
response='error';
status='error';
else
url_ext=[url_ext,'&quantity=',params{quantity_loc+1}];
end
if isempty(rate_loc)
disp('rate missing')
response='error';
status='error';
else
url_ext=[url_ext,'&rate=',params{rate_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% sellmarket function
function [response,status]=bittrex_sellmarket(params)
parameter='market/sellmarket/';
market_loc=find(strcmp('market',params),1);
quantity_loc=find(strcmp('quantity',params),1);
if isempty(market_loc)
disp('market missing')
response='error';
status='error';
else
url_ext=['&market=',params{market_loc+1}];
end
if isempty(quantity_loc)
disp('quantity missing')
response='error';
status='error';
else
url_ext=[url_ext,'&quantity=',params{quantity_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% cancel function
function [response,status]=bittrex_cancel(params)
parameter='market/cancel/';
uuid_loc=find(strcmp('uuid',params),1);
if isempty(uuid_loc)
disp('uuid missing')
response='error';
status='error';
else
url_ext=['&uuid=',params{uuid_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getopenorders function
function [response,status]=bittrex_getopenorders(params)
parameter='market/getopenorders/';
market_loc=find(strcmp('market',params),1);
if isempty(market_loc)
disp('market missing')
response='error';
status='error';
else
url_ext=['&market=',params{market_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getbalances function
function [response,status]=bittrex_getbalances(~)
parameter='account/getbalances';
url_ext='';
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getbalance function
function [response,status]=bittrex_getbalance(params)
parameter='account/getbalance/';
currency_loc=find(strcmp('currency',params),1);
if isempty(currency_loc)
disp('currency missing')
response='error';
status='error';
else
url_ext=['¤cy=',params{currency_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getdepositaddress function
function [response,status]=bittrex_getdepositaddress(params)
parameter='account/getdepositaddress/';
currency_loc=find(strcmp('currency',params),1);
if isempty(currency_loc)
disp('currency missing')
response='error';
status='error';
else
url_ext=['¤cy=',params{currency_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% withdraw function
function [response,status]=bittrex_withdraw(params)
parameter='account/withdraw/';
currency_loc=find(strcmp('currency',params),1);
quantity_loc=find(strcmp('quantity',params),1);
address_loc=find(strcmp('address',params),1);
paymentid_loc=find(strcmp('paymentid',params),1);
if isempty(currency_loc)
disp('currency missing')
response='error';
status='error';
else
url_ext=['¤cy=',params{currency_loc+1}];
end
if isempty(quantity_loc)
disp('quantity missing')
response='error';
status='error';
else
url_ext=[url_ext,'&quantity=',params{quantity_loc+1}];
end
if isempty(address_loc)
disp('address missing')
response='error';
status='error';
else
url_ext=[url_ext,'&address=',params{address_loc+1}];
end
if isempty(paymentid_loc)
disp('paymentid missing')
response='error';
status='error';
else
url_ext=[url_ext,'&paymentid=',params{paymentid_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getorder function
function [response,status]=bittrex_getorder(params)
parameter='account/getorder/';
uuid_loc=find(strcmp('uuid',params),1);
if isempty(uuid_loc)
disp('uuid missing')
response='error';
status='error';
else
url_ext=['&uuid=',params{uuid_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getorderhistory function
function [response,status]=bittrex_getorderhistory(params)
parameter='account/getorderhistory/';
market_loc=find(strcmp('market',params),1);
count_loc=find(strcmp('count',params),1);
if isempty(market_loc)
disp('market missing')
response='error';
status='error';
else
url_ext=['&market=',params{market_loc+1}];
end
if isempty(count_loc)
disp('count missing')
response='error';
status='error';
else
url_ext=[url_ext,'&count=',params{count_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getwithdrawalhistory function
function [response,status]=bittrex_getwithdrawalhistory(params)
parameter='account/getwithdrawalhistory/';
currency_loc=find(strcmp('currency',params),1);
count_loc=find(strcmp('count',params),1);
if isempty(currency_loc)
disp('currency missing')
response='error';
status='error';
else
url_ext=['¤cy=',params{currency_loc+1}];
end
if isempty(count_loc)
disp('count missing')
response='error';
status='error';
else
url_ext=[url_ext,'&count=',params{count_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end
% getdeposithistory function
function [response,status]=bittrex_getdeposithistory(params)
parameter='account/getdeposithistory/';
currency_loc=find(strcmp('currency',params),1);
count_loc=find(strcmp('count',params),1);
if isempty(currency_loc)
disp('currency missing')
response='error';
status='error';
else
url_ext=['¤cy=',params{currency_loc+1}];
end
if isempty(count_loc)
disp('count missing')
response='error';
status='error';
else
url_ext=[url_ext,'&count=',params{count_loc+1}];
end
[response,status]=bittrex_authenticated(url_ext,parameter);
end