-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poloniex_API.py
290 lines (239 loc) · 12.7 KB
/
Poloniex_API.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
import urllib
import urllib2
import json
import time
import hmac, hashlib
######################################################################################################
## ##
## The following code was written by postbin user oipminer and can be found at ##
## https://pastebin.com/fbkheaRb ##
## ##
## I have added many of the Trading API calls to oipminer's code ##
## ##
######################################################################################################
def createTimeStamp(datestr, format="%Y-%m-%d %H:%M:%S"):
return time.mktime(time.strptime(datestr, format))
class Poloniex_API:
def __init__(self, APIKey, Secret):
self.APIKey = APIKey
self.Secret = Secret
def post_process(self, before):
after = before
# Add timestamps if there isnt one but is a datetime
if ('return' in after):
if (isinstance(after['return'], list)):
for x in xrange(0, len(after['return'])):
if (isinstance(after['return'][x], dict)):
if ('datetime' in after['return'][x] and 'timestamp' not in after['return'][x]):
after['return'][x]['timestamp'] = float(createTimeStamp(after['return'][x]['datetime']))
return after
def api_query(self, command, req={}):
if command == "returnTicker" or command == "return24Volume":
ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/public?command=' + command))
return json.loads(ret.read())
elif (command == "returnOrderBook"):
ret = urllib2.urlopen(urllib2.Request(
'https://poloniex.com/public?command=' + command + '¤cyPair=' + str(req['currencyPair'])))
return json.loads(ret.read())
elif (command == "returnTradeHistory"):
ret = urllib2.urlopen(urllib2.Request(
'https://poloniex.com/public?command=' + "returnTradeHistory" + '¤cyPair=' + str(
req['currencyPair'])))
return json.loads(ret.read())
else:
if (command == "returnMyTradeHistory"):
command = "returnTradeHistory"
req['command'] = command
req['nonce'] = int(time.time() * 1000)
post_data = urllib.urlencode(req)
print post_data
sign = hmac.new(self.Secret, post_data, hashlib.sha512).hexdigest()
print sign
headers = {
'Sign': sign,
'Key': self.APIKey
}
urlReq = urllib2.Request('https://poloniex.com/tradingApi', post_data, headers)
ret = urllib2.urlopen(urlReq)
jsonRet = json.loads(ret.read())
return self.post_process(jsonRet)
def returnTicker(self):
return self.api_query("returnTicker")
def return24Volume(self):
return self.api_query("return24Volume")
def returnOrderBook(self, currencyPair):
return self.api_query("returnOrderBook", {'currencyPair': currencyPair})
def returnTradeHistory(self, currencyPair):
return self.api_query("returnTradeHistory", {'currencyPair': currencyPair})
# Returns candlestick chart data.
# Call https://poloniex.com/public?command=returnChartData¤cyPair=BTC_XMR&start=1405699200&end=9999999999&period=14400
# Inputs:
# currencyPair The currency pair e.g. "BTC_BTS"
# period Time interval per candle in seconds; valid values are 300, 900, 1800, 7200, 14400, and 86400
# start start date range in UNIX timestamp
# end end date range in UNIX timestamp
# Outputs:
# date UNIX timestamp
def returnChartData(self, currencyPair, period, start, end):
return self.api_query("returnChartData",
{'currencyPair': currencyPair, 'period': period, 'start': start, 'end': end})
# print "Not Implemented"
# return 0
# Returns information about currencies
def returnCurrencies(self):
return self.api_query("returnCurrencies")
# print "Not Implemented"
# return 0
######################################################################################################
## ##
## ##
## Start Trading (Account Specific) API Methods ##
## ##
## ##
######################################################################################################
# Returns all of your balances.
# Outputs:
# {"BTC":"0.59098578","LTC":"3.31117268", ... }
def returnBalances(self):
return self.api_query('returnBalances')
def returnCompleteBalances(self):
return self.api_query('returnCompleteBalances')
# print "Not Implemented"
# return 0
def returnDepositAddresses(self):
return self.api_query('returnDepositAddresses')
# print "Not Implemented"
# return 0
# Generates a new deposit address for the currency specified by the "currency" POST parameter.
def generateNewAddress(self, currencyPair):
# return self.api_query('generateNewAddress', {'currencyPair': currencyPair})
print "Not Implemented"
return 0
def returnDepositsWithdrawals(self, start, end):
return self.api_query('returnDepositsWithdrawals', {'start': start, 'end': end})
# print "Not Implemented"
# return 0
# Returns your open orders for a given market, specified by the "currencyPair" POST parameter, e.g. "BTC_XCP"
# Inputs:
# currencyPair The currency pair e.g. "BTC_XCP"
# Outputs:
# orderNumber The order number
# type sell or buy
# rate Price the order is selling or buying at
# Amount Quantity of order
# total Total value of order (price * quantity)
def returnOpenOrders(self, currencyPair):
return self.api_query('returnOpenOrders', {"currencyPair": currencyPair})
# Returns your trade history for a given market, specified by the "currencyPair" POST parameter
# Inputs:
# currencyPair The currency pair e.g. "BTC_XCP"
# Outputs:
# date Date in the form: "2014-02-19 03:44:59"
# rate Price the order is selling or buying at
# amount Quantity of order
# total Total value of order (price * quantity)
# type sell or buy
def returnMyTradeHistory(self, currencyPair):
return self.api_query('returnMyTradeHistory', {"currencyPair": currencyPair})
# Places a buy order in a given market. Required POST parameters are "currencyPair", "rate", and "amount". If successful, the method will return the order number.
# Inputs:
# currencyPair The curreny pair
# rate price the order is buying at
# amount Amount of coins to buy
# Outputs:
# orderNumber The order number
def buy(self, currencyPair, rate, amount):
return self.api_query('buy', {"currencyPair": currencyPair, "rate": rate, "amount": amount})
# Places a sell order in a given market. Required POST parameters are "currencyPair", "rate", and "amount". If successful, the method will return the order number.
# Inputs:
# currencyPair The curreny pair
# rate price the order is selling at
# amount Amount of coins to sell
# Outputs:
# orderNumber The order number
def sell(self, currencyPair, rate, amount):
return self.api_query('sell', {"currencyPair": currencyPair, "rate": rate, "amount": amount})
# Cancels an order you have placed in a given market. Required POST parameters are "currencyPair" and "orderNumber".
# Inputs:
# currencyPair The curreny pair
# orderNumber The order number to cancel
# Outputs:
# succes 1 or 0
def cancel(self, currencyPair, orderNumber):
return self.api_query('cancelOrder', {"currencyPair": currencyPair, "orderNumber": orderNumber})
# Cancels an order and places a new one of the same type in an atomic transaction
def moveOrder(self, orderNumber, rate, amount, fillOrKill=0, immediateOrCancel=0, postOnly=0):
return self.api_query('moveOrder',
{"orderNumber": orderNumber, "rate": rate, "amount": amount, "fillOrKill": fillOrKill,
"immediateOrCancel": immediateOrCancel, "postOnly": postOnly})
# print "Not Implemented"
# return 0
# Immediately places a withdrawal for a given currency, with no email confirmation. In order to use this method, the withdrawal privilege must be enabled for your API key. Required POST parameters are "currency", "amount", and "address". Sample output: {"response":"Withdrew 2398 NXT."}
# Inputs:
# currency The currency to withdraw
# amount The amount of this coin to withdraw
# address The withdrawal address
# Outputs:
# response Text containing message about the withdrawal
def withdraw(self, currency, amount, address):
return self.api_query('withdraw', {"currency": currency, "amount": amount, "address": address})
######################################################################################################
## ##
## ##
## Start Margin Trading (Account Specific) API Methods ##
## ##
## ##
######################################################################################################
# Transfers funds from one account to another.
def transferBalance(self, currency, amount, fromAccount, toAccount):
return self.api_query('transferBalance', {"currency": currency, "amount": amount, "fromAccount": fromAccount,
"toAccount": toAccount})
# print "Not Implemented"
# return 0
# Returns a summary of your entire margin account. This is the same information you will find in the Margin Account section of the Margin Trading page, under the Markets list. Sample output:
def returnMarginAccountSummary(self):
return self.api_query('returnMarginAccountSummary')
# print "Not Implemented"
# return 0
# Places a margin buy order in a given market. Required POST parameters are "currencyPair", "rate", and "amount". You may optionally specify a maximum lending rate using the "lendingRate" parameter.
# Inputs:
# currencyPair currencyPair market to palce margin buy position in
# rate price to buy at
# amount number of coins to buy
# lendingRate percent rate of maximum interest rate on borrowed funds
def marginBuy(self, currencyPair, rate, amount, lendingRate):
return self.api_query('marginBuy', {"currencyPair": currencyPair, "rate": rate, "amount": amount,
"lendingRate": lendingRate})
# print "Not Implemented"
# return 0
# Places a margin sell order in a given market. Parameters and output are the same as for the marginBuy method.
# Inputs:
# currencyPair currencyPair market to palce margin sell position in
# rate price to sell at
# amount number of coins to sell
# lendingRate percent rate of maximum interest rate on borrowed funds
# Outputs:
#
def marginSell(self, currencyPair, rate, amount, lendingRate):
return self.api_query('marginSell', {"currencyPair": currencyPair, "rate": rate, "amount": amount,
"lendingRate": lendingRate})
# print "Not Implemented"
# return 0
# Returns information about your margin position in a given market, specified by the "currencyPair" POST parameter (can be "all").
# Inputs:
# currencyPair currencyPair market to get margin position information from
# Outputs:
#
def getMarginPosition(self, currencyPair):
return self.api_query('getMarginPosition', {"currencyPair": currencyPair})
# print "Not Implemented"
# return 0
# Closes current Margin position in specified market
# Inputs:
# currencyPair currencyPair market to close position in
# Outputs:
#
def closeMarginPosition(self, currencyPair):
return self.api_query('closeMarginPosition', {"currencyPair": currencyPair})
# print "Not Implemented"
# return 0