-
Notifications
You must be signed in to change notification settings - Fork 2
/
binanceapi.py
158 lines (126 loc) · 5.25 KB
/
binanceapi.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
import time
import hashlib
import requests
import hmac
try:
from urllib import urlencode
# python3
except ImportError:
from urllib.parse import urlencode
class BinanceRestful:
BASE_URL = "https://www.binance.com/api/v1"
BASE_URL_V3 = "https://api.binance.com/api/v3"
PUBLIC_URL = "https://www.binance.com/exchange/public/product"
def __init__(self, key, secret):
self.key = key
self.secret = secret
def get_history(self, market, limit=50):
path = "%s/historicalTrades" % self.BASE_URL
params = {"symbol": market, "limit": limit}
return self._get_no_sign(path, params)
def get_trades(self, market, limit=50):
path = "%s/trades" % self.BASE_URL
params = {"symbol": market, "limit": limit}
return self._get_no_sign(path, params)
def get_kline(self, market, start, end):
path = "%s/klines" % self.BASE_URL
params = {"symbol": market, "interval": "1m", "startTime": start, "endTime": end}
# params = {"symbol": market, "interval": "1m"}
return self._get_no_sign(path, params)
def get_ticker(self, market):
path = "%s/ticker/24hr" % self.BASE_URL
params = {"symbol": market}
return self._get_no_sign(path, params)
def get_order_books(self, market, limit=50):
path = "%s/depth" % self.BASE_URL
params = {"symbol": market, "limit": limit}
return self._get_no_sign(path, params)
def get_account(self):
path = "%s/account" % self.BASE_URL_V3
return self._get(path, {})
def get_products(self):
return requests.get(self.PUBLIC_URL, timeout=30, verify=True).json()
def get_exchange_info(self):
path = "%s/exchangeInfo" % self.BASE_URL
return requests.get(path, timeout=30, verify=True).json()
def get_open_orders(self, market, limit=100):
path = "%s/openOrders" % self.BASE_URL_V3
params = {"symbol": market}
return self._get(path, params)
def get_my_trades(self, market, limit=50):
path = "%s/myTrades" % self.BASE_URL_V3
params = {"symbol": market, "limit": limit}
return self._get(path, params)
def buy_limit(self, market, quantity, rate):
path = "%s/order" % self.BASE_URL_V3
params = self._order(market, quantity, "BUY", rate)
return self._post(path, params)
def sell_limit(self, market, quantity, rate):
path = "%s/order" % self.BASE_URL_V3
params = self._order(market, quantity, "SELL", rate)
return self._post(path, params)
def buy_market(self, market, quantity):
path = "%s/order" % self.BASE_URL_V3
params = self._order(market, quantity, "BUY")
return self._post(path, params)
def sell_market(self, market, quantity):
path = "%s/order" % self.BASE_URL_V3
params = self._order(market, quantity, "SELL")
return self._post(path, params)
def query_order(self, market, orderId):
path = "%s/order" % self.BASE_URL_V3
params = {"symbol": market, "orderId": orderId}
return self._get(path, params)
def cancel(self, market, order_id):
path = "%s/order" % self.BASE_URL_V3
params = {"symbol": market, "orderId": order_id}
return self._delete(path, params)
def _get_no_sign(self, path, params={}):
query = urlencode(params)
url = "%s?%s" % (path, query)
return requests.get(url, timeout=30, verify=True).json()
def _sign(self, params={}):
data = params.copy()
ts = str(int(1000 * time.time()))
data.update({"timestamp": ts})
h = urlencode(data)
b = bytearray()
b.extend(self.secret.encode())
signature = hmac.new(b, msg=h.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
data.update({"signature": signature})
return data
def _get(self, path, params={}):
params.update({"recvWindow": 120000})
query = urlencode(self._sign(params))
url = "%s?%s" % (path, query)
header = {"X-MBX-APIKEY": self.key}
return requests.get(url, headers=header, \
timeout=30, verify=True).json()
def _post(self, path, params={}):
params.update({"recvWindow": 120000})
query = urlencode(self._sign(params))
url = "%s?%s" % (path, query)
header = {"X-MBX-APIKEY": self.key}
return requests.post(url, headers=header, \
timeout=30, verify=True).json()
def _order(self, market, quantity, side, rate=None):
params = {}
if rate is not None:
params["type"] = "LIMIT"
params["price"] = self._format(rate)
params["timeInForce"] = "GTC"
else:
params["type"] = "MARKET"
params["symbol"] = market
params["side"] = side
params["quantity"] = '%.8f' % quantity
return params
def _format(self, price):
return "{:.8f}".format(price)
def _delete(self, path, params={}):
params.update({"recvWindow": 120000})
query = urlencode(self._sign(params))
url = "%s?%s" % (path, query)
header = {"X-MBX-APIKEY": self.key}
return requests.delete(url, headers=header, \
timeout=30, verify=True).json()