-
Notifications
You must be signed in to change notification settings - Fork 5
/
bittrex.py
496 lines (406 loc) · 15 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
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
"""
See https://bittrex.com/Home/Api
"""
import time
import hmac
import hashlib
try:
from urllib import urlencode
from urlparse import urljoin
except ImportError:
from urllib.parse import urlencode
from urllib.parse import urljoin
try:
from Crypto.Cipher import AES
except ImportError:
encrypted = False
else:
import getpass
import ast
import json
encrypted = True
import requests
BUY_ORDERBOOK = 'buy'
SELL_ORDERBOOK = 'sell'
BOTH_ORDERBOOK = 'both'
BASE_URL = 'https://bittrex.com/api/v1.1/{method_set}/{method}?'
MARKET_SET = {
'getopenorders',
'cancel',
'sellmarket',
'selllimit',
'buymarket',
'buylimit'
}
ACCOUNT_SET = {
'getbalances',
'getbalance',
'getdepositaddress',
'withdraw',
'getorderhistory',
'getorder',
'getdeposithistory',
'getwithdrawalhistory'
}
def encrypt(api_key, api_secret, export=True, export_fn='secrets.json'):
cipher = AES.new(getpass.getpass(
'Input encryption password (string will not show)'))
api_key_n = cipher.encrypt(api_key)
api_secret_n = cipher.encrypt(api_secret)
api = {'key': str(api_key_n), 'secret': str(api_secret_n)}
if export:
with open(export_fn, 'w') as outfile:
json.dump(api, outfile)
return api
def using_requests(request_url, apisign):
return requests.get(
request_url,
headers={"apisign": apisign}
).json()
class Bittrex(object):
"""
Used for requesting Bittrex with API key and API secret
"""
def __init__(self, api_key, api_secret, dispatch=using_requests):
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.dispatch = dispatch
def decrypt(self):
if encrypted:
cipher = AES.new(getpass.getpass(
'Input decryption password (string will not show)'))
try:
if isinstance(self.api_key, str):
self.api_key = ast.literal_eval(self.api_key)
if isinstance(self.api_secret, str):
self.api_secret = ast.literal_eval(self.api_secret)
except Exception:
pass
self.api_key = cipher.decrypt(self.api_key).decode()
self.api_secret = cipher.decrypt(self.api_secret).decode()
else:
raise ImportError('"pycrypto" module has to be installed')
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))
method_set = 'public'
if method in MARKET_SET:
method_set = 'market'
elif method in ACCOUNT_SET:
method_set = 'account'
request_url = BASE_URL.format(method_set=method_set, method=method)
if method_set != 'public':
request_url = "{0}apikey={1}&nonce={2}&".format(
request_url, self.api_key, nonce)
request_url += urlencode(options)
apisign = hmac.new(self.api_secret.encode(),
request_url.encode(),
hashlib.sha512).hexdigest()
return self.dispatch(request_url, apisign)
def get_markets(self):
"""
Used to get the open and available trading markets
at Bittrex along with other meta data.
Endpoint: /public/getmarkets
Example ::
{'success': True,
'message': '',
'result': [ {'MarketCurrency': 'LTC',
'BaseCurrency': 'BTC',
'MarketCurrencyLong': 'Litecoin',
'BaseCurrencyLong': 'Bitcoin',
'MinTradeSize': 1e-08,
'MarketName': 'BTC-LTC',
'IsActive': True,
'Created': '2014-02-13T00:00:00',
'Notice': None,
'IsSponsored': None,
'LogoUrl': 'https://i.imgur.com/R29q3dD.png'},
...
]
}
: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.
Endpoint: /public/getcurrencies
: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.
Endpoint: /public/getticker
: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_market_summaries(self):
"""
Used to get the last 24 hour summary of all active exchanges
Endpoint: /public/getmarketsummary
:return: Summaries of active exchanges in JSON
:rtype : dict
"""
return self.api_query('getmarketsummaries')
def get_marketsummary(self, market):
"""
Used to get the last 24 hour summary of all active
exchanges in specific coin
Endpoint: /public/getmarketsummary
:param market: String literal for the market(ex: BTC-XRP)
:type market: str
:return: Summaries of active exchanges of a coin in JSON
:rtype : dict
"""
return self.api_query('getmarketsummary', {'market': market})
def get_orderbook(self, market, depth_type, depth=20):
"""
Used to get retrieve the orderbook for a given market
Endpoint: /public/getorderbook
: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=20):
"""
Used to retrieve the latest trades that have occurred for a
specific market.
Endpoint: /market/getmarkethistory
Example ::
{'success': True,
'message': '',
'result': [ {'Id': 5625015,
'TimeStamp': '2017-08-31T01:29:50.427',
'Quantity': 7.31008193,
'Price': 0.00177639,
'Total': 0.01298555,
'FillType': 'FILL',
'OrderType': 'BUY'},
...
]
}
: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_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
Endpoint: /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_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
Endpoint: /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
Endpoint: /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=None):
"""
Get all orders that you currently have opened.
A specific market can be requested.
Endpoint: /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} if market else None)
def get_balances(self):
"""
Used to retrieve all balances from your account.
Endpoint: /account/getbalances
Example ::
{'success': True,
'message': '',
'result': [ {'Currency': '1ST',
'Balance': 10.0,
'Available': 10.0,
'Pending': 0.0,
'CryptoAddress': None},
...
]
}
: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
Endpoint: /account/getbalance
Example ::
{'success': True,
'message': '',
'result': {'Currency': '1ST',
'Balance': 10.0,
'Available': 10.0,
'Pending': 0.0,
'CryptoAddress': None}
}
: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
Endpoint: /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
Endpoint: /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_history(self, market=None):
"""
Used to retrieve order trade history of account
Endpoint: /account/getorderhistory
:param market: optional a string literal for the market (ie. BTC-LTC).
If omitted, will return for all markets
:type market: str
:return: order history in JSON
:rtype : dict
"""
return self.api_query('getorderhistory',
{'market': market} if market else None)
def get_order(self, uuid):
"""
Used to get details of buy or sell order
Endpoint: /account/getorder
:param uuid: uuid of buy or sell order
:type uuid: str
:return:
:rtype : dict
"""
return self.api_query('getorder', {'uuid': uuid})
def get_withdrawal_history(self, currency=None):
"""
Used to view your history of withdrawals
Endpoint: /account/getwithdrawalhistory
:param currency: String literal for the currency (ie. BTC)
:type currency: str
:return: withdrawal history in JSON
:rtype : dict
"""
return self.api_query('getwithdrawalhistory',
{'currency': currency} if currency else None)
def get_deposit_history(self, currency=None):
"""
Used to view your history of deposits
Endpoint: /account/getdeposithistory
:param currency: String literal for the currency (ie. BTC)
:type currency: str
:return: deposit history in JSON
:rtype : dict
"""
return self.api_query('getdeposithistory',
{'currency': currency} if currency else None)
def list_markets_by_currency(self, currency):
"""
Helper function to see which markets exist for a currency.
Endpoint: /public/getmarkets
Example ::
>>> Bittrex(None, None).list_markets_by_currency('LTC')
['BTC-LTC', 'ETH-LTC', 'USDT-LTC']
:param currency: String literal for the currency (ex: LTC)
:type currency: str
:return: List of markets that the currency appears in
:rtype: list
"""
return [market['MarketName'] for market in self.get_markets()['result']
if market['MarketName'].lower().endswith(currency.lower())]