-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbittrex.py
430 lines (318 loc) · 11.3 KB
/
bittrex.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
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
"""
See https://bittrex.com/Home/Api
"""
import urllib
import time
import requests
import hmac
import hashlib
BUY_ORDERBOOK = 'buy'
SELL_ORDERBOOK = 'sell'
BOTH_ORDERBOOK = 'both'
PUBLIC_SET = ['getmarkets', 'getcurrencies', 'getticker', 'getmarketsummaries', 'getorderbook',
'getmarkethistory', 'getticks']
MARKET_SET = ['getopenorders', 'cancel', 'sellmarket', 'selllimit', 'buymarket', 'buylimit']
ACCOUNT_SET = ['getbalances', 'getbalance', 'getdepositaddress', 'withdraw', 'getorder', 'getorderhistory', 'getwithdrawalhistory', 'getdeposithistory']
class Bittrex(object):
"""
Used for requesting Bittrex with API key and API secret
"""
def __init__(self, api_key, api_secret):
self.api_key = str(api_key) if api_key is not None else ''
self.api_secret = str(api_secret) if api_secret is not None else ''
self.public_set = set(PUBLIC_SET)
self.market_set = set(MARKET_SET)
self.account_set = set(ACCOUNT_SET)
def api_query(self, method, options=None):
"""
Queries Bittrex with given method and options
:param method: Query method for getting info
:type method: str
:param options: Extra options for query
:type options: dict
:return: JSON response from Bittrex
:rtype : dict
"""
if not options:
options = {}
nonce = str(int(time.time() * 1000))
base_url = 'https://bittrex.com/api/v1.1/%s/'
request_url = ''
if method in self.public_set:
request_url = (base_url % 'public') + method + '?'
elif method in self.market_set:
request_url = (base_url % 'market') + method + '?apikey=' + self.api_key + "&nonce=" + nonce + '&'
elif method in self.account_set:
request_url = (base_url % 'account') + method + '?apikey=' + self.api_key + "&nonce=" + nonce + '&'
request_url += urllib.parse.urlencode(options)
signature = hmac.new(self.api_secret.encode('utf-8'), request_url.encode('utf-8'), hashlib.sha512).hexdigest()
headers = {"apisign": signature}
ret = requests.get(request_url, headers=headers)
return ret.json()
def api_query2(self, method, market, options=None):
"""
DEZE METHOD TOEGEVOEGD DOOR KARUN
----
Met de standaard wrapper heb je niet toegang tot alle info, zoals:
https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-WAVES&tickInterval=thirtyMin&_=1499127220008
Deze functie en get_ticks heb ik toegevoegd om dit beschikbaar te maken.
Roep aan met: api.get_ticks('BTC-DOGE')
Of als je 1 veld wil bijv.: api.get_ticks('BTC-DOGE')['success']
----
Queries Bittrex with given method and options
:param method: Query method for getting info
:type method: str
:param options: Extra options for query
:type options: dict
:return: JSON response from Bittrex
:rtype : dict
"""
if not options:
options = {}
nonce = str(int(time.time() * 1000))
base_url = 'https://bittrex.com/api/v2.0/%s/'
request_url = ''
if method in self.public_set:
request_url = (base_url % 'pub/market') + method + '?' + 'marketName=' + "{0[market]}".format(market) + '&tickInterval=fiveMin'
elif method in self.market_set:
request_url = (base_url % 'market') + method + '?apikey=' + self.api_key + "&nonce=" + nonce + '&'
elif method in self.account_set:
request_url = (base_url % 'account') + method + '?apikey=' + self.api_key + "&nonce=" + nonce + '&'
# uncomment voor debuggen:
#print(request_url)
#request_url += urllib.parse.urlencode(options)
signature = hmac.new(self.api_secret.encode('utf-8'), request_url.encode('utf-8'), hashlib.sha512).hexdigest()
headers = {"apisign": signature}
ret = requests.get(request_url, headers=headers)
return ret.json()
def get_markets(self):
"""
Used to get the open and available trading markets
at Bittrex along with other meta data.
:return: Available market info in JSON
:rtype : dict
"""
return self.api_query('getmarkets')
def get_currencies(self):
"""
Used to get all supported currencies at Bittrex
along with other meta data.
:return: Supported currencies info in JSON
:rtype : dict
"""
return self.api_query('getcurrencies')
def get_ticker(self, market):
"""
Used to get the current tick values for a market.
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:return: Current values for given market in JSON
:rtype : dict
"""
return self.api_query('getticker', {'market': market})
def get_ticks(self, market):
return self.api_query2('getticks', {'market': market})
def get_market_summaries(self):
"""
Used to get the last 24 hour summary of all active exchanges
:return: Summaries of active exchanges in JSON
:rtype : dict
"""
return self.api_query('getmarketsummaries')
def get_orderbook(self, market, depth_type, depth=20):
"""
Used to get retrieve the orderbook for a given market
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param depth_type: buy, sell or both to identify the type of orderbook to return.
Use constants BUY_ORDERBOOK, SELL_ORDERBOOK, BOTH_ORDERBOOK
:type depth_type: str
:param depth: how deep of an order book to retrieve. Max is 100, default is 20
:type depth: int
:return: Orderbook of market in JSON
:rtype : dict
"""
return self.api_query('getorderbook', {'market': market, 'type': depth_type, 'depth': depth})
def get_market_history(self, market, count):
"""
Used to retrieve the latest trades that have occured for a
specific market.
/market/getmarkethistory
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param count: Number between 1-100 for the number of entries to return (default = 20)
:type count: int
:return: Market history in JSON
:rtype : dict
"""
return self.api_query('getmarkethistory', {'market': market, 'count': count})
def buy_market(self, market, quantity, rate):
"""
Used to place a buy order in a specific market. Use buymarket to
place market orders. Make sure you have the proper permissions
set on your API keys for this call to work
/market/buymarket
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
return self.api_query('buymarket', {'market': market, 'quantity': quantity, 'rate': rate})
def buy_limit(self, market, quantity, rate):
"""
Used to place a buy order in a specific market. Use buylimit to place
limit orders Make sure you have the proper permissions set on your
API keys for this call to work
/market/buylimit
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
return self.api_query('buylimit', {'market': market, 'quantity': quantity, 'rate': rate})
def sell_market(self, market, quantity, rate):
"""
Used to place a sell order in a specific market. Use sellmarket to place
market orders. Make sure you have the proper permissions set on your
API keys for this call to work
/market/sellmarket
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
return self.api_query('sellmarket', {'market': market, 'quantity': quantity, 'rate': rate})
def sell_limit(self, market, quantity, rate):
"""
Used to place a sell order in a specific market. Use selllimit to place
limit orders Make sure you have the proper permissions set on your
API keys for this call to work
/market/selllimit
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param quantity: The amount to purchase
:type quantity: float
:param rate: The rate at which to place the order.
This is not needed for market orders
:type rate: float
:return:
:rtype : dict
"""
return self.api_query('selllimit', {'market': market, 'quantity': quantity, 'rate': rate})
def cancel(self, uuid):
"""
Used to cancel a buy or sell order
/market/cancel
:param uuid: uuid of buy or sell order
:type uuid: str
:return:
:rtype : dict
"""
return self.api_query('cancel', {'uuid': uuid})
def get_open_orders(self, market):
"""
Get all orders that you currently have opened. A specific market can be requested
/market/getopenorders
:param market: String literal for the market (ie. BTC-LTC)
:type market: str
:return: Open orders info in JSON
:rtype : dict
"""
return self.api_query('getopenorders', {'market': market})
def get_balances(self):
"""
Used to retrieve all balances from your account
/account/getbalances
:return: Balances info in JSON
:rtype : dict
"""
return self.api_query('getbalances', {})
def get_balance(self, currency):
"""
Used to retrieve the balance from your account for a specific currency
/account/getbalance
:param currency: String literal for the currency (ex: LTC)
:type currency: str
:return: Balance info in JSON
:rtype : dict
"""
return self.api_query('getbalance', {'currency': currency})
def get_deposit_address(self, currency):
"""
Used to generate or retrieve an address for a specific currency
/account/getdepositaddress
:param currency: String literal for the currency (ie. BTC)
:type currency: str
:return: Address info in JSON
:rtype : dict
"""
return self.api_query('getdepositaddress', {'currency': currency})
def withdraw(self, currency, quantity, address):
"""
Used to withdraw funds from your account
/account/withdraw
:param currency: String literal for the currency (ie. BTC)
:type currency: str
:param quantity: The quantity of coins to withdraw
:type quantity: float
:param address: The address where to send the funds.
:type address: str
:return:
:rtype : dict
"""
return self.api_query('withdraw', {'currency': currency, 'quantity': quantity, 'address': address})
def get_order(self, uuid):
"""
Used to get an order from your account
/account/getorder
:param uuid: The order UUID to look for
:type uuid: str
:return:
:rtype : dict
"""
return self.api_query('getorder', {'uuid': uuid})
def get_order_history(self, market = ""):
"""
Used to retrieve your order history
/account/getorderhistory
:param market: Bittrex market identifier (i.e BTC-DOGE)
:type market: str
:return:
:rtype : dict
"""
return self.api_query('getorderhistory', {"market": market})
def get_withdrawal_history(self, currency = ""):
"""
Used to retrieve your withdrawal history
/account/getwithdrawalhistory
:param currency: String literal for the currency (ie. BTC) (defaults to all)
:type currency: str
:return:
:rtype : dict
"""
return self.api_query('getwithdrawalhistory', {"currency": currency})
def get_deposit_history(self, currency = ""):
"""
Used to retrieve your deposit history
/account/getdeposithistory
:param currency: String literal for the currency (ie. BTC) (defaults to all)
:type currency: str
:return:
:rtype : dict
"""
return self.api_query('getdeposithistory', {"currency": currency})