-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoloniexSDK.py
281 lines (263 loc) · 9.56 KB
/
poloniexSDK.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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import requests
import hmac
import hashlib
import urllib.parse
import json
import time
import accountConfig
try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
# poloniex 网址
ENDPOINT = "https://poloniex.com"
def poloniex_service(key_index='USD_1'):
access_key = accountConfig.POLONIEX[key_index]['ACCESS_KEY']
secret_key = accountConfig.POLONIEX[key_index]['SECRET_KEY']
return Client_Poloniex(access_key, secret_key)
def formatNumber(x):
if isinstance(x, float):
return "{:.8f}".format(x)
else:
return str(x)
class Client_Poloniex():
def __init__(self, access_key, secret_key,):
self._public_key = access_key
self._private_key = secret_key
self.ssion = requests.Session()
self.adapter = requests.adapters.HTTPAdapter(pool_connections=5, pool_maxsize=5, max_retries=5)
self.ssion.mount('http://', self.adapter)
self.ssion.mount('https://', self.adapter)
def compatible(self,symbol):
"""
调整数字货币名字
:param symbol: 传入的数字货币交易pair
:return: 输出符合poloniex交易平台命名规则的pair
"""
if symbol == 'all':
return symbol
else:
symbol = symbol.upper()
if 'USD' in symbol:
symbol = symbol.replace('USD', 'USDT')
return symbol
#http请求
def http_request(self, method, path, params=None):
"""
适用于public的api接口请求
:param method: 请求方式:POST/GET
:param path: 请求路径
:param params: 请求参数dict
:return: 返回json格式的响应
"""
#print("params: \n {}".format(params))
response = self.ssion.request(method, ENDPOINT + path, params=params)
return response.json()
#apikey验证登录
def signedRequest(self, method="POST", path='/tradingApi', params={}):
"""
All calls to the trading API are sent via HTTP POST to https://poloniex.com/
tradingApi and must contain the following headers:
Key - Your API key.
Sign - The query's POST data signed by your key's "secret" according to the
HMAC-SHA512 method.
Additionally, all queries must include a "nonce" POST parameter. The nonce
parameter is an integer which must always be greater than the previous nonce
used.
All responses from the trading API are in JSON format. In the event of an
error, the response will always be of the following format:
适用于private的api接口请求
:param method: 请求方式:POST/GET,默认POST
:param path: 请求路径
:param params: 请求参数dict
:return: 返回json格式的响应
"""
payload = {
'nonce': int(time.time() * 1000), #要求的随机数,必须大于上一个请求参数nonce
}
payload.update(params)
#print(payload)
paybytes = urllib.parse.urlencode(payload).encode('utf8')
# print(paybytes)
url = ENDPOINT + path
Key = self._public_key
secret = bytes(self._private_key.encode("utf-8"))
#使用hashlibsha512加密secret
sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest()
#print(sign)
headers = {
'Key': Key,
'Sign': sign,
}
response = self.ssion.request(method, url, headers=headers, data=payload)
data = json.loads(response.text)
return data
def get_depth(self, currencyPair, **kwargs):
"""
get order book public
:param currencyPair: symbol
:param kwargs:
:return:
"""
"""
Returns the order book for a given market, as well as a sequence number for
use with the Push API and an indicator specifying whether the market is frozen.
You may set currencyPair to "all" to get the order books of all markets.
command=returnOrderBook¤cyPair=BTC_NXT&depth=10
"""
currencyPair = self.compatible(currencyPair)
try:
params = {
"command":"returnOrderBook",
"currencyPair": currencyPair,
"depth": "25"}
params.update(kwargs)
data = self.http_request("GET", "/public", params)
#print(data)
bids = []
asks = []
#isFrozen = []
for i in data['bids']:
bids.append([float(i[0]), float(i[1])])
for i in data['asks']:
asks.append([float(i[0]), float(i[1])])
depth = {'bids': bids, 'asks': asks}
return depth
except Exception as e:
time.sleep(0.1)
print(e)
def balance(self):
"""
Used to retrieve all balances from your account
"""
try:
params = {
"command": "returnCompleteBalances",
"account": "all"
}
path = "/tradingApi"
data = self.signedRequest("POST", path, params=params)
#print(data)
balance = {'asset': {'total': 0, 'net': 0},
'trade': {'btc': 0, 'usd': 0, 'cny': 0, 'eth': 0, 'ltc': 0, 'etc': 0},
'frozen': {'btc': 0, 'usd': 0, 'cny': 0, 'eth': 0, 'ltc': 0, 'etc': 0}}
#获取键值
balance_trade_keys= balance['trade'].keys()
balance_frozen_keys = balance['frozen'].keys()
data['USD'] = data.pop('USDT')
data['CNY'] = data.pop('BITCNY')
for i in balance_trade_keys:
balance['trade'][i] = data[i.upper()]['available']
for i in balance_frozen_keys:
balance['frozen'][i] = data[i.upper()]['onOrders']
#print(balance)
return balance
except Exception as e:
return e
def trade(self, trade_type, amount, price, symbol, test=False):
"""
Send in a new order.
:param trade_type: 交易市场类型
:param amount: 交易数量
:param price: 交易价格
:param symbol: 数字货币类型
:param test:
:return:
"""
symbol = self.compatible(symbol)
side, orderType = trade_type.split('_')
orderType = orderType.upper()
params = {
'command': side,
'currencyPair': symbol,
'rate': formatNumber(price),
'Amount': str(amount),
}
#print(params)
path = "/tradingApi"
data = self.signedRequest("POST", path, params)
return data
def cancel(self, orderNumber, currencyPair, **kwargs):
"""
Cancel an active order.
:param orderNumber: 交易ID
:param currencyPair:
:param kwargs:
:return:
"""
""" symbol (str)
orderId (int, optional)
origClientOrderId (str, optional)
newClientOrderId (str, optional): Used to uniquely identify this
cancel. Automatically generated by default.
recvWindow (int, optional)
"""
currencyPair = self.compatible(currencyPair)
params = {
"command": "cancelOrder",
'currencyPair': currencyPair,
'orderNumber': orderNumber,
}
params.update(kwargs)
#print(params)
path = "/tradingApi"
data = self.signedRequest("POST", path, params)
return data
def openOrders(self, symbol='all', **kwargs):
"""
Returns your open orders for a given market, specified by the "currencyPair"
POST parameter, e.g. "BTC_XCP". Set "currencyPair" to "all" to return open
orders for all markets
:param symbol:
:param kwargs:
:return:
"""
currencyPair = self.compatible(symbol)
params = {
"command": "returnOpenOrders",
"currencyPair": currencyPair
}
params.update(kwargs)
#print(params)
path = "/tradingApi"
data = self.signedRequest("POST", path, params)
return data
def cancel_all(self, order_id_list=None, currencyPair ='ETH_BTC'):
"""
:param order_id_list:
:param currencyPair:
:return:
"""
currencyPair = self.compatible(currencyPair)
if order_id_list:
for i in order_id_list:
try:
result = self.cancel(orderNumber=i, currencyPair=currencyPair)
print(result)
except:
continue
else:
order_id_list=[]
openorders = self.openOrders(currencyPair)
for i in openorders:
if type(i) == type({}):
order_id_list.append(i['orderId'])
#print(order_id_list)
for i in order_id_list:
try:
result = self.cancel(orderNumber=i, currencyPair=currencyPair)
print(result)
except Exception as e:
continue
def main():
#print(poloniex_service().get_depth("usd_btc"))
print(poloniex_service().balance())
#print(poloniex_service().trade("sell_LIMIT",1.00077106,0.01,"BTC_XVC"))
#print(poloniex_service().cancel("31226040",'BTC_XCP'))
#print(poloniex_service().openOrders("usd_bch"))
#print(poloniex_service().cancel_all())
pass
if __name__ == '__main__':
main()