forked from Reidmcc/Binance-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
420 lines (283 loc) · 11.7 KB
/
api.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
##### This wrapper is https://github.com/CryptoVader/pyBinance
##### Modified to support client/server clock drift offsetting.
##### Binance rejects any API call that arrives with a timestamp 1000ms ahead of server time.
##### My clock runs fast, so re-offsetting is necessary.
##### If you don't have this problem I suggest using the original (cited above)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from requests.exceptions import ConnectionError
from http.client import RemoteDisconnected
from http.client import HTTPException
from urllib3.exceptions import ProtocolError
import hmac
import hashlib
import requests
import json
import time
from excepts import MalformedRequest, StatusUnknown, InternalError
class BinanceAPI(object):
WEBSITE = 'https://www.binance.com'
API_URL = 'https://www.binance.com/api/'
def __init__(self, api_key=None, api_secret=None):
self.key = api_key
self.secret = api_secret
self.session = requests.session()
self.time_offset = 2000
self.session.headers.update({'Accept': 'application/json',
'X-MBX-APIKEY': self.key})
def refresh_session(self):
self.session = 'kill and refresh'
self.session = requests.session()
def set_offset(self):
got_time = False
while not got_time:
try:
cur_time = int(time.time() * 1000)
bintime = int(self.time()['serverTime'])
time_diff = cur_time - bintime
if time_diff > 0:
self.time_offset = time_diff
else:
self.time_offset = 500
except (InternalError, StatusUnknown,
ConnectionError, RemoteDisconnected, ProtocolError, HTTPException) as e:
print(str(e) + ' ' + str(e.__traceback__) + 'Time check failed, retry')
time.sleep(.5)
else:
got_time = True
def _sign(self, data):
assert(self.key and self.secret)
url_data = "&".join(['%s=%s' % (k,v) for k,v in data.items()])
return hmac.new(bytes(self.secret, 'utf-8'), msg=url_data.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
def _request(self, endpoint, params, http_method):
r = getattr(self.session, http_method)(BinanceAPI.API_URL + endpoint, params=params)
if r.status_code == 200:
return json.loads(r.text)
# HTTP 504 return code is used when the API successfully sent the message but not get a response within the timeout period.
# It is important to NOT treat this as a failure; the execution status is UNKNOWN and could have been a success."""
elif r.status_code == 504:
raise StatusUnknown(r.text)
# HTTP 5XX return codes are used for internal errors; the issue is on Binance's side.
elif r.status_code >= 500:
raise InternalError(r.text)
# HTTP 4XX return codes are used for for malformed requests; the issue is on the sender's side.
else:
raise MalformedRequest(r.text)
return json.loads(r.text)
def _get(self, endpoint, param=None):
return self._request(endpoint, param, "get")
def _post(self, endpoint, param=None):
return self._request(endpoint, param, "post")
def _delete(self, endpoint, param=None):
return self._request(endpoint, param, "delete")
def _put(self, endpoint, param=None):
return self._request(endpoint, param, "put")
def set_api(self, api_key, api_secret):
self.key = api_key
self.secret = api_secret
self.session.headers.update({'Accept': 'application/json',
'X-MBX-APIKEY': self.key})
# Public endpoints, unsigned
def ping(self):
"""
Test connectivity to the Rest API.
GET /api/v1/ping
"""
return self._get('v1/ping')
def time(self):
"""
Test connectivity to the Rest API and get the current server time.
GET /api/v1/time
"""
return self._get('v1/time')
def depth(self, symbol, limit=100):
"""
Get order book.
GET /api/v1/depth
"""
p = {'symbol': symbol, 'limit': limit}
return self._get('v1/depth', p)
def aggTrades(self, symbol, fromId=None, startTime=None, endTime=None, limit=500):
"""
Get compressed, aggregate trades. Trades that fill at the time, from the same order, with the same price will have the quantity aggregated.
GET /api/v1/aggTrades
"""
p = {'symbol': symbol}
if fromId:
p['fromId'] = fromId
if startTime:
p['startTime'] = startTime
if endTime:
p['endTime'] = endTime
# If both startTime and endTime are sent, limit should not be sent.
if not startTime or not endTime:
p['limit'] = limit
return self._get('v1/aggTrades', p)
def klines(self, symbol, interval, startTime=None, endTime=None, limit=500):
"""
Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.
GET /api/v1/klines
"""
p = {'symbol': symbol, 'interval': interval}
if startTime:
p['startTime'] = startTime
if endTime:
p['endTime'] = endTime
if limit:
p['limit'] = limit
return self._get('v1/klines?', p)
def stats24hr(self, symbol):
"""
24 hour price change statistics.
GET /api/v1/ticker/24hr
"""
p = {'symbol': symbol}
return self._get('v1/ticker/24hr?', p)
def allPrices(self):
"""
Latest price for all symbols.
GET /api/v1/ticker/allPrices
"""
return self._get('v1/ticker/allPrices')
def allBookTickers(self):
"""
Best price/qty on the order book for all symbols.
GET /api/v1/ticker/allBookTickers
"""
return self._get('v1/ticker/allBookTickers')
# Public methods that require signature
def newLimitBuyOrder(self, symbol, quantity, price):
return self.newOrder(symbol, "BUY", "LIMIT", "GTC", quantity, price)
def newLimitSellOrder(self, symbol, quantity, price):
return self.newOrder(symbol, "SELL", "LIMIT", "GTC", quantity, price)
def newMarketBuyOrder(self, symbol, quantity, price):
return self.newOrder(symbol, "BUY", "MARKET", "GTC", quantity, price)
def newMarketSellOrder(self, symbol, quantity, price):
return self.newOrder(symbol, "SELL", "MARKET", "GTC", quantity, price)
def newOrder(self, symbol, side, type, timeInForce, quantity, price, newClientOrderId=None, stopPrice=None, icebergQty=None, recvWindow=None):
"""
Send in a new order.
POST /api/v3/order
"""
p = {'symbol': symbol,
'side': side,
'type': type,
'timeInForce': timeInForce,
'quantity': quantity,
'price': price,
'timestamp': int((time.time() * 1000) - self.time_offset)}
if newClientOrderId:
p['newClientOrderId'] = newClientOrderId
if stopPrice:
p['stopPrice'] = stopPrice
if icebergQty:
p['icebergQty'] = icebergQty
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._post('v3/order?', p)
def queryOrder(self, symbol, orderId=None, origClientOrderId=None, recvWindow=None):
"""
Check an order's status.
GET /api/v3/order
"""
p = {'symbol': symbol, 'timestamp': int((time.time() * 1000) - self.time_offset)}
if orderId:
p['orderId'] = orderId
elif origClientOrderId:
p['origClientOrderId'] = origClientOrderId
else:
raise MalformedRequest("Either orderId or origClientOrderId must be sent.")
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._get('v3/order?', p)
def deleteOrder(self, symbol, orderId=None, origClientOrderId=None, newClientOrderId=None, recvWindow=None):
"""
Cancel an active order.
DELETE /api/v3/order
"""
p = {'symbol': symbol, 'timestamp': int((time.time() * 1000) - self.time_offset)}
if orderId:
p['orderId'] = orderId
if origClientOrderId:
p['origClientOrderId'] = origClientOrderId
if newClientOrderId:
p['newClientOrderId'] = newClientOrderId
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._delete('v3/order?', p)
def openOrders(self, symbol, recvWindow=None):
"""
Get all open orders on a symbol.
GET /api/v3/openOrders
"""
p = {'symbol': symbol, 'timestamp': int((time.time() * 1000) - self.time_offset)}
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._get('v3/openOrders?', p)
def allOrders(self, symbol, orderId=None, limit=500, recvWindow=None):
"""
Get all account orders; active, canceled, or filled.
GET /api/v3/allOrders
"""
p = {'symbol': symbol, 'limit': limit, 'timestamp': int((time.time() * 1000) - self.time_offset)}
if orderId:
p['orderId'] = orderId
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._get('v3/allOrders?', p)
def account(self, recvWindow=None):
"""
Get current account information.
GET /api/v3/account
"""
p = {'timestamp': int((time.time() * 1000) - self.time_offset)}
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._get('v3/account?', p)
def myTrades(self, symbol, limit=500, fromId=None, recvWindow=None):
"""
Get trades for a specific account and symbol.
GET /api/v3/myTrades
https://www.binance.com/restapipub.html#account-trade-list-signed
"""
p = {'symbol': symbol, 'limit': limit, 'timestamp': int((time.time() * 1000) - self.time_offset)}
if fromId:
p['fromId'] = fromId
if recvWindow:
p['recvWindow'] = recvWindow
p['signature'] = self._sign(p)
return self._get('v3/myTrades?', p)
# User stream endpoints
def new_stream(self):
"""
Start a new user data stream.
POST /api/v1/userDataStream
https://www.binance.com/restapipub.html#start-user-data-stream-api-key
"""
assert(self.key)
res = self._post('v1/userDataStream')
return res['listenKey']
def keepalive_stream(self, listenKey):
"""
PING a user data stream to prevent a time out.
PUT /api/v1/userDataStream
https://www.binance.com/restapipub.html#keepalive-user-data-stream-api-key
"""
assert(self.key)
param = "listenKey=%s" % (listenKey)
return self._put('v1/userDataStream', param)
def keepalive_stream(self, listenKey):
"""
PING a user data stream to prevent a time out.
DELETE /api/v1/userDataStream
https://www.binance.com/restapipub.html#close-user-data-stream-api-key
"""
assert(self.key)
param = "listenKey=%s" % (listenKey)
return self._delete('v1/userDataStream', param)