-
Notifications
You must be signed in to change notification settings - Fork 0
/
APIpoloniex.py
288 lines (263 loc) · 13.1 KB
/
APIpoloniex.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
#In English
# The APIpoloniex class works with the API https://poloniex.com/
#
# This class has an error handler for poloniex requests, thanks to this
# Your trading terminal will not hang for a long time.
#
# If the query data, poloniex.com does not respond more timeOutSec seconds,
# then the function returns -1 and an error message is displayed (APIerror)
#
# The script is written for yourself, for trading on the stock exchange. Distributed without a license
#Для Русских
# Класс APIpoloniex работает с API https://poloniex.com/
#
# В данный клас встроен обработчик ошибок на запросы к poloniex, благодаря этому
# Ваш торговый терминал не будет зависать на длительное время.
#
# Если на запрос данных, poloniex.com не отвечает больше timeOutSec секунд,
# то функция возвращает -1 и выводится сообщение об ошибке (APIerror)
#
# Скрипт написан для себя, для торговли на бирже. Распростроняется без лицензии
#For Example
#
# from APIpoloniex import *
# polo = APIpoloniex('api_key', 'api_secret',1.0)
# print (polo.returnBalances())
#Donate
#
# BTC fb0a34933ca0781f5e9917a52ea86d72cbb1c05b4ccfff56f9c78bdce5f8a573
# LTC LRsm54XYJxG7NJCuAntK98odJoXhwp1GBK
# ETH 0x8750793385349e2edd63e87d5c523b3b2c972b82
import requests
from urllib.parse import urlencode
import json
from hmac import new
from hashlib import sha512
import sys
import time
import logging
logging.basicConfig(
level = logging.WARNING,
format = '[LINE:%(lineno)d] %(asctime)s %(levelname)s : %(message)s',
filename = 'AliceLog.log',
filemode = 'w'
)
class APIpoloniex(object):
def __init__(self, APIKey, secret, timeOutSec = 3.0):
"""
key = str api key supplied by Poloniex
secret = str secret hash supplied by Poloniex
timeout = int time in sec to wait for an api response
"""
self.APIKey=APIKey
self.secret=secret
self.timeOutSec= timeOutSec
# set time labels
self.MINUTE, self.HOUR, self.DAY = 60, 60 * 60, 60 * 60 * 24
self.WEEK, self.MONTH = self.DAY * 7, self.DAY * 30
self.YEAR = self.DAY * 365
return
def __APIchekError(self,command = 0, info = 0, html = 0):
""" Exception for handling poloniex api errors """
if html == 0 :
logging.error('\"' + str(command) + '\" : ' + str(info) )
return
if 'error' in html.text:
logging.error('\"' + str(command) + '\" : ' + html.text )
return -1
if command in ['buy','sell'] :
logging.error('\"' + str(command) + '\" : ' + html.text )
return html.json()
def CommandPublic (self, command, args={}, timeOutSec=False):
""" Handler for public commands
Example:
command = 'returnOrderBook'
arg = { 'currencyPair' = 'BTC_SC'}
timeOutSec = 3.0"""
if timeOutSec == False : timeOutSec= self.timeOutSec
url = 'https://poloniex.com/public?command=' + command
if args.get("currencyPair"): url= url +"¤cyPair=" + args['currencyPair'].upper()
if args.get("period"): url= url +"&period=" + args['period']
if args.get("start"): url= url +"&start=" + args['start']
if args.get("end"): url= url +"&end=" + args['end']
try:
html = requests.get(url,timeout=(timeOutSec,timeOutSec))
except:
self.__APIchekError(command = command, info = sys.exc_info()[1])
return -1
else :
return self.__APIchekError(command = command, html =html)
def CommandPrivate(self,command ,args={},timeOutSec=False):
""" Handler for private commands
Example:
command = 'returnOrderBook'
arg = { 'returnCompleteBalances' = 'all'}
timeOutSec = 3.0"""
if timeOutSec == False : timeOutSec= self.timeOutSec
args['command'] = command
args['nonce'] = int(time.time()*1000000)
url = "https://poloniex.com/tradingApi"
sign = new(
self.secret.encode('utf-8'),
urlencode(args).encode('utf-8'),
sha512).hexdigest()
headers = {
'Key' : self.APIKey,
'Sign' : sign,
}
try:
html = requests.post(url, data=args, headers=headers,timeout=(timeOutSec, timeOutSec))
except:
self.__APIchekError(command = command, info = sys.exc_info()[1])
return -1
else:
return self.__APIchekError(command = command, html = html)
# --PUBLIC COMMANDS-------------------------------------------------------
def returnTicker(self, timeOutSec = False):
""" Returns the ticker for all markets.
Sample output:
{"BTC_LTC":{"last":"0.0251","lowestAsk":"0.02589999","highestBid":"0.0251","percentChange":"0.02390438",
"baseVolume":"6.16485315","quoteVolume":"245.82513926"},"BTC_NXT":{"last":"0.00005730","lowestAsk":"0.00005710",
"highestBid":"0.00004903","percentChange":"0.16701570","baseVolume":"0.45347489","quoteVolume":"9094"}, ... } """
return self.CommandPublic('returnTicker', timeOutSec = timeOutSec)
def return24hVolume(self, timeOutSec = False):
""" Returns the 24-hour volume for all markets, plus totals for primary currencies.
Sample output:
{"BTC_LTC":{"BTC":"2.23248854","LTC":"87.10381314"},"BTC_NXT":{"BTC":"0.981616","NXT":"14145"}, ...
"totalBTC":"81.89657704","totalLTC":"78.52083806"} """
return self.CommandPublic('return24hVolume', timeOutSec = timeOutSec)
def returnOrderBook(self, currencyPair='all', timeOutSec=False):
""" 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.
Sample output:
{"asks":[[0.00007600,1164],[0.00007620,1300], ... ], "bids":[[0.00006901,200],[0.00006900,408], ... ], "isFrozen": 0, "seq": 18849} """
args = {
'currencyPair' : currencyPair
}
return self.CommandPublic('returnOrderBook', args = args , timeOutSec = timeOutSec)
def returnTradersHistory(self, currencyPair, start=False, end=False, timeOutSec=False):
""" Returns the past 200 trades for a given market, or up to 50,000 trades between a range specified
in UNIX timestamps by the "start" and "end" GET parameters.
Sample output:
[{"date":"2014-02-10 04:23:23","type":"buy","rate":"0.00007600","amount":"140","total":"0.01064"},
{"date":"2014-02-10 01:19:37","type":"buy","rate":"0.00007600","amount":"655","total":"0.04978"}, ... ] """
if start == False : start = time.time() - self.DAY
if end == False : end = time.time()
args = {
'currencyPair' : str(currencyPair),
'start' : str(start),
'end' : str(end)
}
return self.CommandPublic('returnTradeHistory', args = args , timeOutSec = timeOutSec)
def returnChartData(self, currencyPair, period=False, start=False, end=False, timeOutSec=False):
""" Returns candlestick chart data. Required GET parameters are "currencyPair", "period" (candlestick period in seconds;
valid values are 300, 900, 1800, 7200, 14400, and 86400), "start", and "end". "Start" and "end" are given in UNIX timestamp
format and used to specify the date range for the data returned.
Sample output:
[{"date":1405699200,"high":0.0045388,"low":0.00403001,"open":0.00404545,"close":0.00427592,"volume":44.11655644,
"quoteVolume":10259.29079097,"weightedAverage":0.00430015}, ...] """
if period not in [300, 900, 1800, 7200, 14400, 86400]:
print ("%s invalid candle period" % str(period))
if not start:
start = time.time() - self.DAY
if not end:
end = time.time()
agrs = {
'currencyPair': str(currencyPair).upper(),
'period': str(period),
'start' : str(start),
'end' : str(end)
}
return self.CommandPublic('returnChartData', args=agrs, timeOutSec=timeOutSec )
# --PRIVATE COMMANDS------------------------------------------------------
def returnBalances(self, timeOutSec = False):
""" Returns all of your available balances.
Sample output:
{"BTC":"0.59098578","LTC":"3.31117268", ... } """
return self.CommandPrivate('returnBalances', timeOutSec = timeOutSec)
def returnCompleteBalances(self, account='all', timeOutSec = False):
""" Returns all of your balances, including available balance, balance
on orders, and the estimated BTC value of your balance. By default,
this call is limited to your exchange account; set the "account"
parameter to "all" to include your margin and lending accounts.
Sample output:
{"LTC":{"available":"5.015","onOrders":"1.0025","btcValue":"0.078"},"NXT:{...} ... }"""
args = {
'account' : account
}
return self.CommandPrivate('returnCompleteBalances', args = args, timeOutSec = timeOutSec)
def returnDepositAddresses(self, timeOutSec = False):
""" Returns all of your deposit addresses. Sample output: {"BTC":"19YqztHmspv2egyD6jQM3yn81x5t5krVdJ","LTC """
return self.CommandPrivate('returnDepositAddresses', timeOutSec = timeOutSec)
def returnTradeHistory(self, currencyPair='all', start=False, end=False, limit=10000, timeOutSec = False):
""" Returns your trade history for a given market, specified by the
"currencyPair" parameter. You may specify "all" as the currencyPair to
receive your trade history for all markets. You may optionally specify
a range via "start" and/or "end" POST parameters, given in UNIX
timestamp format; if you do not specify a range, it will be limited to one day.
You may optionally limit the number of entries returned using the "limit" parameter,
up to a maximum of 10,000. If the "limit" parameter is not specified, no more than 500 entries will be returned.
Sample output:
{"BTC_MAID": [ { "globalTradeID": 29251512, "tradeID": "1385888", "date": "2016-05-03 01:29:55", "rate": "0.00014243",
"amount": "353.74692925", "total": "0.05038417", "fee": "0.00200000", "orderNumber": "12603322113", "type": "buy",
"category": "settlement" }, { "globalTradeID": 29251511, "tradeID": "1385887", "date": "2016-05-03 01:29:55",
"rate": "0.00014111", "amount": "311.24262497", "total": "0.04391944", "fee": "0.00200000", "orderNumber": "12603319116",
"type": "sell", "category": "marginTrade" }, ... ],"BTC_LTC":[ ... ] ... }"""
if start == False : start = time.time() - self.DAY
if end == False : end = time.time()
args = {
'currencyPair': str(currencyPair),
'start' : str(start),
'end' : str(end),
'limit' : str(limit)
}
return self.CommandPrivate('returnTradeHistory', args=args, timeOutSec = timeOutSec)
def buy(self, currencyPair, rate, amount, timeOutSec = False):
""" Places a limit buy order in a given market. Required parameters are
"currencyPair", "rate", and "amount". You may optionally set "orderType"
to "fillOrKill", "immediateOrCancel" or "postOnly". A fill-or-kill order
will either fill in its entirety or be completely aborted. An
immediate-or-cancel order can be partially or completely filled, but
any portion of the order that cannot be filled immediately will be
canceled rather than left on the order book. A post-only order will
only be placed if no portion of it fills immediately; this guarantees
you will never pay the taker fee on any part of the order that fills.
If successful, the method will return the order number.
Sample output:
{"orderNumber":31226040,"resultingTrades":[{"amount":"338.8732","date":"2014-10-18 23:03:21","rate":"0.00000173",
"total":"0.00058625","tradeID":"16164","type":"buy"}]}"""
args = {
'currencyPair': str(currencyPair),
'rate' : str(rate),
'amount': str(amount),
}
return self.CommandPrivate('buy', args=args, timeOutSec = timeOutSec)
def sell(self, currencyPair, rate, amount, timeOutSec = False):
""" Places a sell order in a given market. Parameters and output are
the same as for the buy method. """
args = {
'currencyPair': str(currencyPair),
'rate' : str(rate),
'amount': str(amount),
}
return self.CommandPrivate('sell',args=args, timeOutSec = timeOutSec)
def cancelOrder(self, orderNumber, timeOutSec = False ):
""" Cancels an order you have placed in a given market. Required
parameter is "orderNumber". If successful, the method will return:
{"success":1} """
args= {
'orderNumber': orderNumber
}
return self.CommandPrivate('cancelOrder', args=args, timeOutSec = timeOutSec)
def returnOpenOrders(self, currencyPair='all', timeOutSec = False):
""" Returns your open orders for a given market, specified by the
"currencyPair" parameter, e.g. "BTC_XCP". Set "currencyPair" to
"all" to return open orders for all markets.
Sample output for single market:
[{"orderNumber":"120466","type":"sell","rate":"0.025","amount":"100","total":"2.5"},
{"orderNumber":"120467","type":"sell","rate":"0.04","amount":"100","total":"4"}, ... ] """
args = {
'currencyPair': currencyPair
}
return self.CommandPrivate('returnOpenOrders', args=args, timeOutSec = timeOutSec)