-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitmart_api.py
More file actions
153 lines (119 loc) · 6.54 KB
/
bitmart_api.py
File metadata and controls
153 lines (119 loc) · 6.54 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
import hashlib
import hmac
import json
import time
import urllib.parse
import requests
class Bitmart:
def __init__(self, api_key, secret_key, memo):
"""Constructor"""
self.__api_key = api_key
self.__secret_key = secret_key
self.__memo = memo
self.__access_token = ''
self.precision = {}
self.base_min_size = {}
self.__load_precision()
def __create_sha256_signature(self, message):
return hmac.new(self.__secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()
def __get_access_token(self):
url = "https://openapi.bitmart.com/v2/authentication"
message = self.__api_key + ':' + self.__secret_key + ':' + self.__memo
data = {"grant_type": "client_credentials", "client_id": self.__api_key,
"client_secret": self.__create_sha256_signature(message)}
response = requests.post(url, data=data)
# print(response.content)
accessToken = response.json()['access_token']
self.__access_token = accessToken
return accessToken
def __get(self, request_path):
if self.__access_token == '':
self.__get_access_token()
headers = {"X-BM-TIMESTAMP": str(int(time.time() * 1000)),
"X-BM-AUTHORIZATION": "Bearer {}".format(self.__access_token)}
response = requests.get(request_path, headers=headers)
if 'Unauthorized' in response.text:
print('Unauthorized')
self.__get_access_token()
headers = {"X-BM-TIMESTAMP": str(int(time.time() * 1000)),
"X-BM-AUTHORIZATION": "Bearer {}".format(self.__access_token)}
response = requests.get(request_path, headers=headers)
return response.json()
def __post(self, request_path, params):
if self.__access_token == '':
self.__get_access_token()
data = {}
for key in sorted(params.keys()):
data.update({key: params[key]})
url = urllib.parse.urlencode(data)
sign = self.__create_sha256_signature(url)
headers = {"X-BM-TIMESTAMP": str(int(time.time() * 1000)),
"X-BM-AUTHORIZATION": "Bearer {}".format(self.__access_token),
"X-BM-SIGNATURE": sign,
"Content-Type": "application/json"}
response = requests.post(request_path, data=json.dumps(params), headers=headers)
return response.json()
def __delete(self, request_path, entrust_id):
if self.__access_token == '':
self.__get_access_token()
sign = self.__create_sha256_signature('entrust_id={}'.format(entrust_id))
headers = {"X-BM-TIMESTAMP": str(int(time.time() * 1000)),
"X-BM-AUTHORIZATION": "Bearer {}".format(self.__access_token),
"X-BM-SIGNATURE": sign}
response = requests.delete(request_path, headers=headers)
return response.json()
def __delete2(self, request_path):
if self.__access_token == '':
self.__get_access_token()
headers = {"X-BM-TIMESTAMP": str(int(time.time() * 1000)),
"X-BM-AUTHORIZATION": "Bearer {}".format(self.__access_token)}
response = requests.delete(request_path, headers=headers)
return response.json()
# Public Endpoints
def ping(self):
return requests.request("GET", 'https://openapi.bitmart.com/v2/ping').json()
def time(self):
return requests.request("GET", 'https://openapi.bitmart.com/v2/time').json()
def steps(self):
return requests.request("GET", 'https://openapi.bitmart.com/v2/steps').json()
def currencies(self):
return requests.request("GET", 'https://openapi.bitmart.com/v2/currencies').json()
def symbols(self):
return requests.request("GET", 'https://openapi.bitmart.com/v2/symbols').json()
def symbols_details(self):
return requests.request("GET", 'https://openapi.bitmart.com/v2/symbols_details').json()
def ticker(self, symbol):
return requests.request("GET", 'https://openapi.bitmart.com/v2/ticker?symbol={}'.format(symbol)).json()
def kline(self, symbol, step, time_from, time_to):
return requests.request("GET",
'https://openapi.bitmart.com/v2/symbols/{}/kline?step={}&from={}&to={}'.format(symbol,
step,
time_from,
time_to)).json()
def orderbook(self, symbol, precision):
return requests.request("GET", 'https://openapi.bitmart.com/v2/symbols/{}/orders?precision={}'.format(symbol,
precision)).json()
def trades(self, symbol):
return requests.request("GET", 'https://openapi.bitmart.com/v2/symbols/BMX_ETH/trades'.format(symbol)).json()
# Authenticated Endpoints
def wallet(self):
return self.__get('https://openapi.bitmart.com/v2/wallet')
def place_order(self, symbol, amount, price, side):
params = {"symbol": symbol, "amount": amount, "price": price, "side": side}
return self.__post('https://openapi.bitmart.com/v2/orders', params)
def cancel_order(self, order_id):
return self.__delete('https://openapi.bitmart.com/v2/orders/{}'.format(order_id), order_id)
def cancel_all_order(self, symbol, side):
return self.__delete2('https://openapi.bitmart.com/v2/orders?symbol={}&side={}'.format(symbol, side))
def list_orders(self, symbol, status, offset, limit):
return self.__get('https://openapi.bitmart.com/v2/orders?symbol={}&status={}&offset={}&limit={}'.format(symbol,
status,
offset,
limit))
def order_details(self, order_id):
return self.__get('https://openapi.bitmart.com/v2/orders/{}'.format(order_id))
# Service
def __load_precision(self):
r = self.symbols_details()
for x in r:
self.precision.update({x['id']: x['price_max_precision']})