forked from yasinkuyu/binance-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinanceAPI.py
139 lines (97 loc) · 4 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
import time
import hashlib
import requests
from urllib import urlencode
#https://github.com/purboox/BinanceAPI
class BinanceAPI:
BASE_URL = "https://www.binance.com/api/v1"
def __init__(self, key, secret):
self.key = key
self.secret = secret
def get_ticker(self, market):
path = "%s/ticker/24hr" % self.BASE_URL
params = {"symbol": market}
return self._get_no_sign(path, params)
def get_orderbooks(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
return self._get(path, {})
def get_open_orders(self, market, limit = 100):
path = "%s/openOrders" % self.BASE_URL
params = {"symbol": market}
return self._get(path, params)
def buy_limit(self, market, quantity, rate):
quantity = self._chop(quantity, 2)
rate = self._chop(rate, 6)
path = "%s/order" % self.BASE_URL
params = {"symbol": market, "side": "BUY", \
"type": "LIMIT", "timeInForce": "GTC", \
"quantity": quantity, "price": rate}
return self._post(path, params)
def sell_limit(self, market, quantity, rate):
quantity = self._chop(quantity, 2)
rate = self._chop(rate, 6)
path = "%s/order" % self.BASE_URL
params = {"symbol": market, "side": "SELL", \
"type": "LIMIT", "timeInForce": "GTC", \
"quantity": quantity, "price": rate}
return self._post(path, params)
def buy_market(self, market, quantity):
quantity = self._chop(quantity, 2)
path = "%s/order" % self.BASE_URL
params = {"symbol": market, "side": "BUY", \
"type": "MARKET", "quantity": quantity}
return self._post(path, params)
def sell_market(self, market, quantity):
quantity = self._chop(quantity, 2)
path = "%s/order" % self.BASE_URL
params = {"symbol": market, "side": "SELL", \
"type": "MARKET", "quantity": quantity}
return self._post(path, params)
def query_order(self, market, orderId):
path = "%s/order" % self.BASE_URL
params = {"symbol": market, "orderId": orderId}
return self._get(path, params)
def cancel(self, market, order_id):
path = "%s/order" % self.BASE_URL
params = {"symbol": market, "orderId": order_id}
return self._delete(path, params)
def _chop(self, v, n):
s = '%.10lf' % v
return s[:s.find('.')+n+1]
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 = self.secret + "|" + urlencode(data)
signature = hashlib.sha256(h).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 _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()