-
Notifications
You must be signed in to change notification settings - Fork 4
/
mxc_v2.py
276 lines (235 loc) · 7.55 KB
/
mxc_v2.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
import requests
import hmac
import hashlib
from urllib import parse
import os
API_KEY = os.environ['MXC_ACCESS_KEY']
SECRET_KEY = os.environ['MXC_SECRET_KEY']
ROOT_URL = 'https://www.mxc.ceo'
def _get_server_time():
return int(time.time())
def _sign(method, path, original_params=None):
params = {
'api_key': API_KEY,
'req_time': _get_server_time(),
}
if original_params is not None:
params.update(original_params)
params_str = '&'.join('{}={}'.format(k, params[k]) for k in sorted(params))
to_sign = '\n'.join([method, path, params_str])
params.update({'sign': hmac.new(SECRET_KEY.encode(), to_sign.encode(), hashlib.sha256).hexdigest()})
return params
def get_symbols():
"""marget data"""
method = 'GET'
path = '/open/api/v2/market/symbols'
url = '{}{}'.format(ROOT_URL, path)
params = {'api_key': API_KEY}
response = requests.request(method, url, params=params)
return response.json()
def get_rate_limit():
"""rate limit"""
method = 'GET'
path = '/open/api/v2/common/rate_limit'
url = '{}{}'.format(ROOT_URL, path)
params = {'api_key': API_KEY}
response = requests.request(method, url, params=params)
return response.json()
def get_timestamp():
"""get current time"""
method = 'GET'
path = '/open/api/v2/common/timestamp'
url = '{}{}'.format(ROOT_URL, path)
params = {'api_key': API_KEY}
response = requests.request(method, url, params=params)
return response.json()
def get_ticker(symbol):
"""get ticker information"""
method = 'GET'
path = '/open/api/v2/market/ticker'
url = '{}{}'.format(ROOT_URL, path)
params = {
'api_key': API_KEY,
'symbol': symbol,
}
response = requests.request(method, url, params=params)
return response.json()
def get_depth(symbol, depth):
"""get market depth"""
method = 'GET'
path = '/open/api/v2/market/depth'
url = '{}{}'.format(ROOT_URL, path)
params = {
'api_key': API_KEY,
'symbol': symbol,
'depth': depth,
}
response = requests.request(method, url, params=params)
return response.json()
def get_deals(symbol, limit):
"""get deals records"""
method = 'GET'
path = '/open/api/v2/market/deals'
url = '{}{}'.format(ROOT_URL, path)
params = {
'api_key': API_KEY,
'symbol': symbol,
'limit': limit,
}
response = requests.request(method, url, params=params)
return response.json()
def get_kline(symbol, interval):
"""k-line data"""
method = 'GET'
path = '/open/api/v2/market/kline'
url = '{}{}'.format(ROOT_URL, path)
params = {
'api_key': API_KEY,
'symbol': symbol,
'interval': interval,
}
response = requests.request(method, url, params=params)
return response.json()
def get_account_info():
"""account information"""
method = 'GET'
path = '/open/api/v2/account/info'
url = '{}{}'.format(ROOT_URL, path)
params = _sign(method, path)
response = requests.request(method, url, params=params)
return response.json()
def place_order(symbol, price, quantity, trade_type, order_type):
"""place order"""
method = 'POST'
path = '/open/api/v2/order/place'
url = '{}{}'.format(ROOT_URL, path)
params = _sign(method, path)
data = {
'symbol': symbol,
'price': price,
'quantity': quantity,
'trade_type': trade_type,
'order_type': order_type,
}
response = requests.request(method, url, params=params, json=data)
return response.json()
def batch_orders(orders):
"""batch order"""
method = 'POST'
path = '/open/api/v2/order/place_batch'
url = '{}{}'.format(ROOT_URL, path)
params = _sign(method, path)
response = requests.request(method, url, params=params, json=orders)
return response.json()
def cancel_order(order_id):
"""cancel in batch"""
origin_trade_no = order_id
if isinstance(order_id, list):
origin_trade_no = parse.quote(','.join(order_id))
method = 'DELETE'
path = '/open/api/v2/order/cancel'
url = '{}{}'.format(ROOT_URL, path)
params = _sign(method, path, original_params={'order_ids': origin_trade_no})
if isinstance(order_id, list):
params['order_ids'] = ','.join(order_id)
response = requests.request(method, url, params=params)
return response.json()
def get_open_orders(symbol):
"""current orders"""
method = 'GET'
path = '/open/api/v2/order/open_orders'
original_params = {
'symbol': symbol,
}
url = '{}{}'.format(ROOT_URL, path)
params = _sign(method, path, original_params=original_params)
response = requests.request(method, url, params=params)
return response.json()
def get_all_orders(symbol, trade_type):
"""order list"""
method = 'GET'
path = '/open/api/v2/order/list'
original_params = {
'symbol': symbol,
'trade_type': trade_type,
}
url = '{}{}'.format(ROOT_URL, path)
params = _sign(method, path, original_params=original_params)
response = requests.request(method, url, params=params)
return response.json()
def query_order(order_id):
"""query order"""
origin_trade_no = order_id
if isinstance(order_id, list):
origin_trade_no = parse.quote(','.join(order_id))
method = 'GET'
path = '/open/api/v2/order/query'
url = '{}{}'.format(ROOT_URL, path)
original_params = {
'order_ids': origin_trade_no,
}
params = _sign(method, path, original_params=original_params)
if isinstance(order_id, list):
params['order_ids'] = ','.join(order_id)
response = requests.request(method, url, params=params)
return response.json()
def get_deal_orders(symbol):
"""account deal records"""
method = 'GET'
path = '/open/api/v2/order/deals'
url = '{}{}'.format(ROOT_URL, path)
original_params = {
'symbol': symbol,
}
params = _sign(method, path, original_params=original_params)
response = requests.request(method, url, params=params)
return response.json()
def get_deal_detail(order_id):
"""deal detail"""
method = 'GET'
path = '/open/api/v2/order/deal_detail'
url = '{}{}'.format(ROOT_URL, path)
original_params = {
'order_id': order_id,
}
params = _sign(method, path, original_params=original_params)
response = requests.request(method, url, params=params)
return response.json()
if __name__ == '__main__':
get_symbols()
get_rate_limit()
get_timestamp()
get_ticker('BTC_USDT')
get_depth('BTC_USDT', 5)
get_deals('BTC_USDT', 5)
get_kline('BTC_USDT', '1m')
get_account_info()
place_order('BTC_USDT', 7900, 0.1, 'BID', 'LIMIT_ORDER')
place_order('BTC_USDT', 7900, 0.1, 'BID', 'LIMIT_ORDER')
cancel_order('cfc5a95618f****6d751dd04b2')
cancel_order(['cfc5a95618f****d751dd04b2', 'b956dfc923d***31b383c9d'])
batch_orders([
{
'symbol': 'BTC_USDT',
'price': '7900',
'quantity': '1',
'trade_type': 'BID',
'order_type': 'LIMIT_ORDER',
},
{
'symbol': 'BTC_USDT',
'price': '7901',
'quantity': '1',
'trade_type': 'ASK',
'order_type': 'LIMIT_ORDER',
},
])
get_open_orders('BTC_USDT')
get_all_orders('BTC_USDT', 'BID')
query_order('ccbd62471d***dd109903e')
query_order(['ec72970d2****8264d7e86e', 'fd4d614ee4cf46***c7c82c0'])
get_deal_orders('BTC_USDT')
get_deal_detail('ccbd62471d*****ddd109903e')