-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
306 lines (261 loc) · 8.66 KB
/
client.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
from functools import partial
from pprint import pprint
from config import APIKEY, SECRET, DATA_URL, PRIVATE_URL, REAL
from utils import post, get, async_get, async_post
post = partial(post, api_key=APIKEY, secret=SECRET)
async_post = partial(async_post, api_key=APIKEY, secret=SECRET)
def funding_balances():
""" 理财账号资金余额 """
rst = post(PRIVATE_URL, "/fundingbalances", {})
return rst
def balances():
"""
账号资金余额
:return:
{
"result": "true",
"available": {
"BTC": "1000",
"ETH": "968.8",
"ETC": "0",
},
"locked": {
"ETH": "1"
}
}
"""
min_value = 0.001
rst = post(PRIVATE_URL, "/balances", {})
available, locked = rst['available'], rst['locked']
for d in (available, locked):
for k in d.keys():
d[k] = round(float(d[k]), 6)
available = {k: v for k, v in available.items() if v > min_value}
locked = {k: v for k, v in locked.items() if v > min_value}
return available, locked
async def async_balances():
min_value = 0.001
rst = await async_post(PRIVATE_URL, "/balances", {})
available, locked = rst['available'], rst['locked']
for d in (available, locked):
for k in d.keys():
d[k] = round(float(d[k]), 6)
available = {k: v for k, v in available.items() if v > min_value}
locked = {k: v for k, v in locked.items() if v > min_value}
return available, locked
def trade_pairs():
""" 所有交易对 """
rst = get(DATA_URL, "/pairs")
return rst
def order_book(currency_pair: str):
"""
市场深度(委托挂单/买单)
:param currency_pair: 交易对
:return:
{
"result": "true",
"asks": [ // 卖方深度
[29500, 4.07172355],
[29499, 0.00203397],
[29495, 1],
[29488, 0.0672],
[29475, 0.001]
],
"bids": [ // 买方深度
[28001, 0.0477],
[28000, 0.35714018],
[28000, 2.56222976],
[27800, 0.0015],
[27777, 0.1]
]
}
"""
rst = get(DATA_URL, "/orderBook/" + currency_pair)
asks = rst['asks']
bids = rst['bids']
return asks, bids
def c2c_order_book(currency_pair: str):
"""
返回系统支持的所有C2C交易对的市场深度(委托挂单)
:param currency_pair:
:return:
[价格,数量,单笔最小交易量,单笔最大交易量]
"""
rst = get(DATA_URL, "/orderBook_c2c/" + currency_pair)
asks, bids = rst['asks'], rst['bids']
asks = [{"price": item[0], "amount": item[1], "min_amount": item[2], "max_amount": item[3]} for item in asks]
bids = [{"price": item[0], "amount": item[1], "min_amount": item[2], "max_amount": item[3]} for item in bids]
return asks, bids
async def async_c2c_order_book(currency_pair: str):
rst = await async_get(DATA_URL, "/orderBook_c2c/" + currency_pair)
asks, bids = rst['asks'], rst['bids']
asks = [{"price": item[0], "amount": item[1], "min_amount": item[2], "max_amount": item[3]} for item in asks]
bids = [{"price": item[0], "amount": item[1], "min_amount": item[2], "max_amount": item[3]} for item in bids]
return asks, bids
def open_orders(currency_pair=""):
""" 当前挂单列表 """
rst = post(PRIVATE_URL, "/openOrders", {"currencyPair": currency_pair})
return rst['orders']
async def async_open_orders(currency_pair=""):
rst = await async_post(PRIVATE_URL, "/openOrders", {"currencyPair": currency_pair})
return rst['orders']
def trade_history(currency_pair=""):
""" 24小时内成交记录 """
rst = post(PRIVATE_URL, "/tradeHistory", {"currencyPair": currency_pair})
return rst['trades']
def buy(currency_pair: str, rate: float, amount: int, order_type=""):
"""
下单交易买入
:param currency_pair: 交易对 ltc_btc
:param rate: 价格
:param amount: 交易量
:param order_type: 订单类型,默认是普通; ioc,立即执行否则取消订单
:return:
{
"result":"true",
"orderNumber":"123456", // 订单号,可用于查询/取消订单
"rate":"1000", // 下单价格
"leftAmount":"0", // 剩余数量
"filledAmount":"0.1", // 成交数量
"filledRate":"800.00", // 成交价格
"message":"Success"
}
"""
params = {
"currencyPair": currency_pair,
"rate": str(rate),
"amount": str(amount),
"orderType": order_type
}
if REAL:
rst = post(PRIVATE_URL, "/buy", params)
return rst
else:
pprint(params)
return {"result": "true", "orderNumber": None}
async def async_buy(currency_pair: str, rate: float, amount: int, order_type=""):
params = {
"currencyPair": currency_pair,
"rate": str(rate),
"amount": str(amount),
"orderType": order_type
}
if REAL:
rst = await async_post(PRIVATE_URL, "/buy", params)
rst['_type'] = "buy"
return rst
else:
pprint(params)
return {"result": "true", "orderNumber": None}
def sell(currency_pair: str, rate: float, amount: int, order_type=""):
"""
下单交易卖出
:param currency_pair: 交易对 ltc_btc
:param rate: 价格
:param amount: 交易量
:param order_type: 订单类型,默认是普通; ioc,立即执行否则取消订单
:return:
{
"result":"true",
"orderNumber":"123456", // 订单号,可用于查询/取消订单
"rate":"1000", // 下单价格
"leftAmount":"0", // 剩余数量
"filledAmount":"0.1", // 成交数量
"filledRate":"800.00", // 成交价格
"message":"Success"
}
"""
params = {
"currencyPair": currency_pair,
"rate": str(rate),
"amount": str(amount),
"orderType": order_type
}
if REAL:
rst = post(PRIVATE_URL, "/sell", params)
return rst
else:
pprint(params)
return {"result": "true", "orderNumber": None}
async def async_sell(currency_pair: str, rate: float, amount: int, order_type=""):
params = {
"currencyPair": currency_pair,
"rate": str(rate),
"amount": str(amount),
"orderType": order_type
}
if REAL:
rst = await async_post(PRIVATE_URL, "/sell", params)
rst['_type'] = "sell"
return rst
else:
pprint(params)
return {"result": "true", "orderNumber": None}
def cancel_order(order_number: str, currency_pair: str):
"""
取消订单
:param order_number:
:param currency_pair:
:return: {"result":"true","message":"Success"}
"""
params = {"orderNumber": order_number, "currencyPair": currency_pair}
rst = post(PRIVATE_URL, "/cancelOrder", params)
return rst
def cancel_orders(order_pairs: [dict]):
"""
批量取消订单(此方法目前有问题,无法使用)
:param order_pairs:
[{
"orderNumber":"7942422",
"currencyPair":"ltc_btc"
},{
"orderNumber":"7942423",
"currencyPair":"ltc_btc"
}
]
:return:
"""
rst = post(PRIVATE_URL, "/cancelOrders", {"orders_json": order_pairs})
return rst
def cancel_all_orders(_type: int, currency_pair: str):
"""
取消所有订单
:param _type: 下单类型(0:卖出,1:买入,-1:不限制)
:param currency_pair:
:return:
"""
params = {
"type": _type,
"currencyPair": currency_pair
}
rst = post(PRIVATE_URL, "/cancelAllOrders", params)
return rst
def get_order(order_number: str, currency_pair: str):
"""
获取订单信息
:param order_number:
:param currency_pair:
:return:
{
"result":"true",
"order":{
"id":"15088",
"status":"cancelled", // 订单状态 open已挂单 cancelled已取消 closed已完成
"currencyPair":"eth_btc",
"type":"sell", // 买卖类型 sell卖出, buy买入
"rate":811, // 价格
"amount":"0.39901357", // 买卖数量
"initialRate":811, // 下单价格
"initialAmount":"1" // 下单量
},
"message":"Success"
}
"""
params = {
"orderNumber": order_number,
"currencyPair": currency_pair
}
rst = post(PRIVATE_URL, "/getOrder", params)
return rst['order']
if __name__ == '__main__':
pprint(trade_history("vidy_usdt"))