-
Notifications
You must be signed in to change notification settings - Fork 8
/
api_stamp.py
378 lines (328 loc) · 12.1 KB
/
api_stamp.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import hashlib
import hmac
import json
import time
import httplib
import urllib
import urllib2
import helper
import logging
import sys
import requests
class tradeapi:
key = ''
secret = ''
nonce = 1
wait_for_nonce = False
def __init__(self, key, secret, id, wait_for_nonce = False):
self.key = key
self.secret = secret
self.id = id
self.wait_for_nonce = wait_for_nonce
self.tradeData = {}
def _nonce(self):
if self.wait_for_nonce: time.sleep(1)
self.nonce = str(int(time.time() * 5) % 4294967296)
#self.nonce = str(int(time.time()))
def _default_data(self, *args, **kwargs):
"""
Generate a one-time signature and other data required to send a secure
POST request to the Bitstamp API.
"""
data = {}
data['key'] = self.key
self._nonce()
nonce = self.nonce
msg = str(nonce) + self.id + self.key
signature = hmac.new(
self.secret.encode('utf-8'), msg=msg.encode('utf-8'),
digestmod=hashlib.sha256).hexdigest().upper()
data['signature'] = signature
data['nonce'] = nonce
return data
def _post(self, *args, **kwargs):
"""
Make a POST request.
"""
data = self._default_data()
data.update(kwargs.get('data') or {})
kwargs['data'] = data
return self._request(requests.post, *args, **kwargs)
def _request(self, func, url, *args, **kwargs):
"""
Make a generic request, adding in any proxy defined by the instance.
Raises a ``requests.HTTPError`` if the response status isn't 200, and
raises a :class:`BitstampError` if the response contains a json encoded
error message.
"""
return_json = kwargs.pop('return_json', False)
url = "https://www.bitstamp.net/api/" + url
response = func(url, *args, **kwargs)
# Check for error, raising an exception if appropriate.
response.raise_for_status()
try:
json_response = response.json()
except ValueError:
json_response = None
if isinstance(json_response, dict):
error = json_response.get('error')
if error:
print "error json responce is not dict"
if return_json:
if json_response is None:
print "Error json response is none"
return json_response
return response
# Trading api
def account_balance(self):
"""
Returns dictionary::
{u'btc_reserved': u'0',
u'fee': u'0.5000',
u'btc_available': u'2.30856098',
u'usd_reserved': u'0',
u'btc_balance': u'2.30856098',
u'usd_balance': u'114.64',
u'usd_available': u'114.64'}
"""
#return self.__api_call("balance")
return self._post("balance/", return_json=True)
def user_transactions(self, offset=0, limit=100, descending=True):
"""
Returns descending list of transactions. Every transaction (dictionary)
contains::
{u'usd': u'-39.25',
u'datetime': u'2013-03-26 18:49:13',
u'fee': u'0.20', u'btc': u'0.50000000',
u'type': 2,
u'id': 213642}
"""
data = {
'offset': offset,
'limit': limit,
'sort': 'desc' if descending else 'asc',
}
return self._post("user_transactions/", data=data, return_json=True)
def open_orders(self):
"""
Returns JSON list of open orders. Each order is represented as a
dictionary.
"""
return self._post("open_orders/", return_json=True)
def cancel_order(self, order_id):
"""
Cancel the order specified by order_id.
Returns True if order was successfully canceled,otherwise raise a
BitstampError.
"""
data = {'id': order_id}
return self._post("cancel_order/", data=data, return_json=True)
def buy_limit_order(self, amount, price):
"""
Order to buy amount of bitcoins for specified price.
"""
data = {'amount': amount, 'price': price}
return self._post("buy/", data=data, return_json=True)
def sell_limit_order(self, amount, price):
"""
Order to buy amount of bitcoins for specified price.
"""
data = {'amount': amount, 'price': price}
return self._post("sell/", data=data, return_json=True)
def check_bitstamp_code(self, code):
"""
Returns JSON dictionary containing USD and BTC amount included in given
bitstamp code.
"""
data = {'code': code}
return self._post("check_code/", data=data, return_json=True)
def redeem_bitstamp_code(self, code):
"""
Returns JSON dictionary containing USD and BTC amount added to user's
account.
"""
data = {'code': code}
return self._post("redeem_code/", data=data, return_json=True)
def withdrawal_requests(self):
"""
Returns list of withdrawal requests.
Each request is represented as a dictionary.
"""
return self._post("withdrawal_requests/", return_json=True)
def bitcoin_withdrawal(self, amount, address):
"""
Send bitcoins to another bitcoin wallet specified by address.
"""
data = {'amount': amount, 'address': address}
return self._post("bitcoin_withdrawal/", data=data, return_json=True)
def bitcoin_deposit_address(self):
"""
Returns bitcoin deposit address as unicode string
"""
return self._post("bitcoin_deposit_address/", return_json=True)
def unconfirmed_bitcoin_deposits(self):
"""
Returns JSON list of unconfirmed bitcoin transactions.
Each transaction is represented as dictionary:
amount
bitcoin amount
address
deposit address used
confirmations
number of confirmations
"""
return self._post("unconfirmed_btc/", return_json=True)
# def get_param(self, pair, param):
# conn = httplib.HTTPSConnection("btc-e.com")
# conn.request("GET", "/api/"+param)
# response = conn.getresponse()
# data = json.load(response)
# conn.close()
# return data
# def getInfo(self):
# return self.__api_call('getInfo', {})
# def update(self):
# '''Wrapper for get trade information'''
# # sys.stdout.write('-')
# raw = self.getInfo()
# # sys.stdout.write('K')
# if raw == None:
# print ('Error in getInfo')
# time.sleep(5)
# return None
# if raw['success'] == 0:
# print('API response returned status "fail".')
# return None
# output = raw.get('return')
# if output == None:
# print ('output is None')
# return None
# self.tradeData['funds'] = output['funds']
# self.tradeData['openOrders'] = output['open_orders']
# self.tradeData['transCount'] = output['transaction_count']
# self.tradeData['apiRights'] = output['rights']
# self.tradeData['serverTime'] = output['server_time']
# if self.tradeData['openOrders'] > 0:
# self.tradeData['orders'] = self.activeOrders()
# return self.tradeData
# def transHistory(self, tfrom, tcount, tfrom_id, tend_id, torder, tsince, tend):
# params = {
# "from" : tfrom,
# "count" : tcount,
# "from_id" : tfrom_id,
# "end_id" : tend_id,
# "order" : torder,
# "since" : tsince,
# "end" : tend}
# return self.__api_call('TransHistory', params)
# def tradeHistory(self, tfrom, tcount, tfrom_id, tend_id, torder, tsince, tend, tpair):
# params = {
# "from" : tfrom,
# "count" : tcount,
# "from_id" : tfrom_id,
# "end_id" : tend_id,
# "order" : torder,
# "since" : tsince,
# "end" : tend,
# "pair" : tpair}
# return self.__api_call('TradeHistory', params)
# def activeOrders(self, tpair = 'btc_usd'):
# params = { "pair" : tpair }
# return self.__api_call('ActiveOrders', params)
# def trade(self, tpair, ttype, trate, tamount):
# params = {
# "pair" : tpair,
# "type" : ttype,
# "rate" : trate,
# "amount": tamount}
# return self.__api_call('Trade', params)
# def cancelOrder(self, torder_id):
# params = { "order_id" : torder_id }
# return self.__api_call('CancelOrder', params)
class publicapi(object):
'''Parse BTC-e Public API'''
def __init__(self):
self.url = 'https://www.bitstamp.net/api/' #append pair, method
#self.log = logging.getLogger('TickerAPI')
self.tickerDict = {}
def update(self,pairs):
'''Updates pairs set to True,
where pairs is dict of booleans currencies.'''
for pair in pairs:
if pairs[pair]:
self.updatePair(pair)
return self.tickerDict
def poll(self,url):
'''Generic public API parsing method, returns parsed dict'''
for i in range(5):
try:
request = urllib2.Request(url)
response = json.loads(urllib2.urlopen(request, timeout = 20).read())
# sys.stdout.write("V")
return response
except urllib2.URLError as e:
print "Caught URL Error, sleeping..."
for second in range(5):
time.sleep(1)
print "Retrying connection now."
continue
except urllib2.HTTPError as e:
print "Caught HTTP Error, sleeping..."
for second in range(5):
time.sleep(1)
print "Retrying connection now."
continue
except Exception as e:
print 'publicapi.poll caught other Exception:'
print e
print 'Sleeping...'
for second in range(5):
time.sleep(1)
print "Retrying now."
continue
print "Unknown Error in publicapi poll"
return None
# def ticker(self,pair):
# '''Returns ticker dict for a single pair'''
# url = self.url + pair + '/ticker'
# raw = self.poll(url)
# if raw == None: return None
# ticker = raw['ticker']
# return ticker
def depth(self):
'''Returns depth dict for a single pair'''
url = self.url + 'order_book/'
depth = self.poll(url)
return depth
# def trades(self,pair):
# url = self.url + pair + '/trades'
# trades = self.poll(url)
# return trades
# def getLast(self,pair):
# '''Returns most recent traded price of pair'''
# trades = self.trades(pair)
# price = trades[0].get('price')
# return price
# def getLastID(self,pair):
# '''Returns ID of last trade for pair'''
# trades = self.trades(pair)
# tradeID = trades[0].get('tid')
# return tradeID
# def updatePair(self,pair):
# '''Update stored ticker info for a single pair, reassigns to variables'''
# tick = self.ticker(pair)
# if tick == None: return None
# data = {}
# data['high'] = tick.get('high',0)
# data['low'] = tick.get('low',0)
# data['last'] = tick.get('last',0)
# data['buy'] = tick.get('buy',0)
# data['sell'] = tick.get('sell',0)
# data['vol'] = tick.get('vol',0)
# data['volCur'] = tick.get('vol_cur',0)
# data['avg'] = tick.get('avg',0)
# # uncomment depth/trades for gigantic dict
# #data['depth'] = self.depth(pair)
# #data['trades'] = self.trades(pair)
# self.tickerDict[pair] = data
# return self.tickerDict[pair]