Permalink
Browse files

Update to support new Bitstamp API

Updates the private API to support the latest Bitstamp key based API (the old one will no longer function after 31 October 2013).
Adds a few function that previously weren't implemented, so that the full private API is supported.
  • Loading branch information...
vorwrath committed Sep 27, 2013
1 parent 7257cbe commit 55c82cee4a4016be050e6c09977c9f2952f27d0e
Showing with 136 additions and 24 deletions.
  1. +136 −24 bitstamp/client.py
View
@@ -1,7 +1,9 @@
__author__ = 'kmadac'
import requests
import time
import hmac
import hashlib
class public():
def __init__(self, proxydict=None):
@@ -65,11 +67,24 @@ def conversion_rate_usd_eur(self):
class trading():
def __init__(self, user, password, proxydict=None):
def __init__(self, username, key, secret, proxydict=None):
self.proxydict = proxydict
self.params = {'user': user, 'password': password}
self.username = username
self.key = key
self.secret = secret
self.nonce = int(time.time())
def get_params(self):
params = {}
params['key'] = self.key
msg = str(self.nonce) + self.username + self.key
signature = hmac.new(self.secret, msg=msg, digestmod=hashlib.sha256).hexdigest().upper()
params['signature'] = signature
params['nonce'] = self.nonce
self.nonce += 1
return params
def account_ballance(self):
def account_balance(self):
"""
Returns dictionary:
{u'btc_reserved': u'0',
@@ -80,7 +95,8 @@ def account_ballance(self):
u'usd_balance': u'114.64',
u'usd_available': u'114.64'}
"""
r = requests.post("https://www.bitstamp.net/api/balance/", data=self.params, proxies=self.proxydict)
params = self.get_params()
r = requests.post("https://www.bitstamp.net/api/balance/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
@@ -89,7 +105,7 @@ def account_ballance(self):
else:
r.raise_for_status()
def user_transactions(self, timedelta_secs=86400):
def user_transactions(self, offset=0, limit=100, descending=True):
"""
Returns descending list of transactions. Every transaction (dictionary) contains
{u'usd': u'-39.25',
@@ -98,9 +114,15 @@ def user_transactions(self, timedelta_secs=86400):
u'type': 2,
u'id': 213642}
"""
self.params['timedelta'] = timedelta_secs
params = self.get_params()
params['offset'] = offset
params['limit'] = limit
if descending:
params['sort'] = "desc"
else:
params['sort'] = "asc"
r = requests.post("https://www.bitstamp.net/api/user_transactions/", data=self.params, proxies=self.proxydict)
r = requests.post("https://www.bitstamp.net/api/user_transactions/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
@@ -113,7 +135,8 @@ def open_orders(self):
"""
Returns JSON list of open orders. Each order is represented as dictionary:
"""
r = requests.post("https://www.bitstamp.net/api/open_orders/", data=self.params, proxies=self.proxydict)
params = self.get_params()
r = requests.post("https://www.bitstamp.net/api/open_orders/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
@@ -128,8 +151,9 @@ def cancel_order(self, order_id):
Returns True if order was successfully canceled,
otherwise tuple (False, msg) like (False, u'Order not found')
"""
self.params['id'] = order_id
r = requests.post("https://www.bitstamp.net/api/cancel_order/", data=self.params, proxies=self.proxydict)
params = self.get_params()
params['id'] = order_id
r = requests.post("https://www.bitstamp.net/api/cancel_order/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if r.text == u'true':
return True
@@ -142,10 +166,11 @@ def buy_limit_order(self, amount, price):
"""
Order to buy amount of bitcoins for specified price
"""
self.params['amount'] = amount
self.params['price'] = price
params = self.get_params()
params['amount'] = amount
params['price'] = price
r = requests.post("https://www.bitstamp.net/api/buy/", data=self.params, proxies=self.proxydict)
r = requests.post("https://www.bitstamp.net/api/buy/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
@@ -158,26 +183,75 @@ def sell_limit_order(self, amount, price):
"""
Order to buy amount of bitcoins for specified price
"""
self.params['amount'] = amount
self.params['price'] = price
params = self.get_params()
params['amount'] = amount
params['price'] = price
r = requests.post("https://www.bitstamp.net/api/sell/", data=self.params, proxies=self.proxydict)
r = requests.post("https://www.bitstamp.net/api/sell/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
else:
return r.json()
else:
r.raise_for_status()
def check_bitstamp_code(self, code):
"""
Returns JSON dictionary containing USD and BTC amount included in given bitstamp code.
"""
params = self.get_params()
params['code'] = code
r = requests.post("https://www.bitstamp.net/api/check_code/", data=params,
proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
else:
return r.json()
else:
r.raise_for_status()
def redeem_bitstamp_code(self, code):
"""
Returns JSON dictionary containing USD and BTC amount added to user's account.
"""
params = self.get_params()
params['code'] = code
r = requests.post("https://www.bitstamp.net/api/redeem_code/", data=params,
proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
else:
return r.json()
else:
r.raise_for_status()
def withdrawal_requests(self):
"""
Returns list of withdrawal requests. Each request is represented as dictionary
"""
params = self.get_params()
r = requests.post("https://www.bitstamp.net/api/withdrawal_requests/", data=params,
proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
else:
return r.json()
else:
r.raise_for_status()
def bitcoin_withdrawal(self, amount, address):
"""
Send bitcoins to another bitcoin wallet specified by address
"""
self.params['amount'] = amount
self.params['address'] = address
params = self.get_params()
params['amount'] = amount
params['address'] = address
r = requests.post("https://www.bitstamp.net/api/bitcoin_withdrawal/", data=self.params, proxies=self.proxydict)
r = requests.post("https://www.bitstamp.net/api/bitcoin_withdrawal/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if r.text == u'true':
return True
@@ -190,7 +264,8 @@ def bitcoin_deposit_address(self):
"""
Returns bitcoin deposit address as unicode string
"""
r = requests.post("https://www.bitstamp.net/api/bitcoin_deposit_address/", data=self.params,
params = self.get_params()
r = requests.post("https://www.bitstamp.net/api/bitcoin_deposit_address/", data=params,
proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
@@ -200,11 +275,15 @@ def bitcoin_deposit_address(self):
else:
r.raise_for_status()
def withdrawal_requests(self):
def unconfirmed_bitcoin_deposits(self):
"""
Returns list of withdrawal requests. Each request is represented as dictionary
Returns JSON list of unconfirmed bitcoin transactions. Each transaction is represented as dictionary:
amount - bitcoin amount
address - deposit address used
confirmations - number of confirmations
"""
r = requests.post("https://www.bitstamp.net/api/withdrawal_requests/", data=self.params,
params = self.get_params()
r = requests.post("https://www.bitstamp.net/api/unconfirmed_btc/", data=params,
proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
@@ -213,3 +292,36 @@ def withdrawal_requests(self):
return r.json()
else:
r.raise_for_status()
def ripple_withdrawal(self, amount, address, currency):
"""
Returns true if successful
"""
params = self.get_params()
params['amount'] = amount
params['address'] = address
params['currency'] = currency
r = requests.post("https://www.bitstamp.net/api/ripple_withdrawal/", data=params, proxies=self.proxydict)
if r.status_code == 200:
if r.text == u'true':
return True
else:
return False, r.json()['error']
else:
r.raise_for_status()
def ripple_deposit_address(self):
"""
Returns ripple deposit address as unicode string
"""
params = self.get_params()
r = requests.post("https://www.bitstamp.net/api/ripple_address/", data=params,
proxies=self.proxydict)
if r.status_code == 200:
if 'error' in r.json():
return False, r.json()['error']
else:
return r.text
else:
r.raise_for_status()

0 comments on commit 55c82ce

Please sign in to comment.