-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbittrex.py
More file actions
executable file
·244 lines (201 loc) · 7.93 KB
/
bittrex.py
File metadata and controls
executable file
·244 lines (201 loc) · 7.93 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
#!/usr/bin/env python
"""
bittrex.com api wrapper.
"""
import ConfigParser
import hashlib
import hmac
import json
import os
import sys
import time
import urllib
import urllib2
import urlparse
import textwrap
_KEY_FILE = os.environ.get(
'BITTREX_KEY_FILE',
os.path.join(os.path.expanduser('~'), '.bittrex.ini'))
_config = ConfigParser.SafeConfigParser()
if not _config.read(_KEY_FILE):
sys.stderr.write('Key file not read, private methods will not work.\n')
API_KEY = None
API_SECRET = None
else:
API_KEY = _config.get('bittrex', 'key')
API_SECRET = _config.get('bittrex', 'secret')
GET_MARKETS_URI = "https://bittrex.com/api/v1.1/public/getmarkets"
GET_CURRENCIES_URI = "https://bittrex.com/api/v1.1/public/getcurrencies"
GET_TICKER = "https://bittrex.com/api/v1.1/public/getticker"
GET_MARKET_SUMMARIES = "https://bittrex.com/api/v1.1/public/getmarketsummaries"
GET_MARKET_SUMMARY = "https://bittrex.com/api/v1.1/public/getmarketsummary"
GET_ORDERBOOK = "https://bittrex.com/api/v1.1/public/getorderbook"
GET_MARKET_HISTORY = "https://bittrex.com/api/v1.1/public/getmarkethistory"
BUY_LIMIT = "https://bittrex.com/api/v1.1/market/buylimit"
BUY_MARKET = "https://bittrex.com/api/v1.1/market/buymarket"
SELL_LIMIT = "https://bittrex.com/api/v1.1/market/selllimit"
SELL_MARKET = "https://bittrex.com/api/v1.1/market/sellmarket"
CANCEL = "https://bittrex.com/api/v1.1/market/cancel"
GET_OPEN_ORDERS = "https://bittrex.com/api/v1.1/market/getopenorders"
GET_BALANCE = "https://bittrex.com/api/v1.1/account/getbalance"
GET_BALANCES = "https://bittrex.com/api/v1.1/account/getbalances"
GET_ORDER = "https://bittrex.com/api/v1.1/account/getorder"
GET_ORDER_SUMMARY = "https://bittrex.com/api/v1.1/account/getorderhistory"
WITHDRAW = "https://bittrex.com/api/v1.1/account/withdraw"
def get(url, headers=None):
"""
Perform a HTTP get request returning the request body.
"""
headers = headers if headers else {}
request = urllib2.Request(url, headers=headers)
handle = urllib2.urlopen(request)
return handle.read()
def format_uri(uri, parameters):
"""
Format a uri with the given query parameters `dict`.
"""
parts = urlparse.urlsplit(uri)
query_string = urllib.urlencode(parameters)
return urlparse.urlunsplit((
parts.scheme,
parts.netloc,
parts.path,
query_string,
parts.fragment))
class BittrexAPIException(Exception):
"""
Exception when bittrex api returns a False success status.
"""
class NoAPIKeys(Exception):
"""
Exception raised when a private method called without credentials.
"""
class BittrexAPI(object):
"""
bittrex class which wraps the bittrex API.
"""
def __init__(self, api_key=None, api_secret=None, raw=False):
self._api_key = api_key
self._api_secret = api_secret
self._raw = raw
def _query(self, uri, params=None, public=True):
if public is False and not all((self._api_key, self._api_secret)):
raise NoAPIKeys
params = params if params else {}
headers = {}
if public is False:
params.update(self._auth_params)
uri = format_uri(uri, params)
if public is False:
headers = self.api_headers(uri)
if self._raw is True:
return get(uri, headers)
response = json.loads(get(uri, headers))
if not response['success']:
raise BittrexAPIException(response)
return response
@property
def _auth_params(self):
return dict(
apikey=self._api_key,
nonce=int(time.time()))
def api_sign(self, uri):
sign = hmac.new(self._api_secret, uri, hashlib.sha512)
return sign.hexdigest()
def api_headers(self, uri):
return {'apisign': self.api_sign(uri)}
def getmarkets(self):
return self._query(GET_MARKETS_URI, dict(), public=True)
def getcurrencies(self):
return self._query(GET_CURRENCIES_URI, dict(), public=True)
def getticker(self, market):
return self._query(GET_TICKER, dict(market=market), public=True)
def getmarketsummaries(self):
return self._query(GET_MARKET_SUMMARIES, dict(), public=True)
def getmarketsummary(self, market):
params = dict(market=market)
return self._query(GET_MARKET_SUMMARY, params, public=True)
def getorderbook(self, market, type_='both', depth='20'):
params = dict(market=market, type=type_, depth=depth)
return self._query(GET_ORDERBOOK, params, public=True)
def getmarkethistory(self, market, count='20'):
params = dict(market=market, count=count)
return self._query(GET_MARKET_HISTORY, params, public=True)
def buylimit(self, market, quantity, rate):
params = dict(market=market, quantity=quantity, rate=rate)
return self._query(BUY_LIMIT, params, public=False)
def buymarket(self, market, quantity):
params = dict(market=market, quantity=quantity)
return self._query(BUY_MARKET, params, public=False)
def selllimit(self, market, quantity, rate):
params = dict(market=market, quantity=quantity, rate=rate)
return self._query(SELL_LIMIT, params, public=False)
def sellmarket(self, market, quantity):
params = dict(market=market, quantity=quantity)
return self._query(SELL_MARKET, params, public=False)
def cancel(self, uuid):
return self._query(CANCEL, dict(uuid=uuid), public=False)
def getopenorders(self, market=None):
params = dict(market=market) if market else dict()
return self._query(GET_OPEN_ORDERS, params, public=False)
def getbalance(self, currency):
params = dict(currency=currency)
return self._query(GET_BALANCE, params, public=False)
def getorder(self, uuid):
params = dict(uuid=uuid)
return self._query(GET_ORDER, params, public=False)
def getbalances(self):
return self._query(GET_BALANCES, public=False)
def getorderhistory(self, market=None, count=None):
params = dict()
if market is not None:
params['market'] = market
if count is not None:
params['count'] = count
return self._query(GET_ORDER_SUMMARY, params, public=False)
def withdraw(self, currency, quantity, address, paymentid=None):
params = dict(currency=currency, quantity=quantity, address=address)
if paymentid is not None:
params['paymentid'] = paymentid
return self._query(WITHDRAW, params, public=False)
def runner(*args):
"""
Simple runner for the bittrex api methods.
"""
bittrex = BittrexAPI(API_KEY, API_SECRET, raw=True)
if len(sys.argv) > 1:
return getattr(bittrex, sys.argv[1])(*sys.argv[2:])
return getattr(bittrex, sys.argv[1])()
def usage():
return """Usage:
python bittrex.py getticker [market]
getmarkets
getcurrencies
getticker [market]
getmarketsummaries
getmarketsummary [market]
getorderbook [market] [type] [depth]
getmarkethistory [market] [count]
buylimit [market] [quantity] [price]
buymarket [market] [quantity]
selllimit [market] [quantity] [price]
sellmarket [market] [quantity]
cancel [uuid]
getopenorders [market]
getbalance [currency]
getbalances
getorder [uuid]
withdraw [currency] [quantity] [address] [paymentid]
"""
if __name__ == "__main__":
import sys
if len(sys.argv) == 1:
sys.stdout.write(textwrap.dedent(usage()))
sys.exit(1)
try:
response = runner(sys.argv[1:])
except Exception as exc:
sys.stdout.write(str(exc) + '\n')
sys.exit(1)
sys.stdout.write(str(response) + '\n')
sys.exit(0)