Hi Jonathan,
first of all, sorry for the late response, I was traveling with quite limited access to the internet / this forum.
I do not use a third party library as far as I see.
Here comes my minimal example making use of the portfolios (which I find quite handy, pleas keep support for it):
# Requires python-requests. Install with pip:
import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase
import sys #for devel exit
# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
signature_b64 = signature.digest().encode('base64').rstrip('\n')
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request
def get_eurBalance(portfolio_in,portfolio_dict_in):
#r = requests.get(api_url + 'accounts/'+EUR_ID, auth=portfolio_dict_in[portfolio_in]['auth'])
r = requests.get(api_url + 'accounts/' + portfolio_dict_in[portfolio_in]['EUR_ID'], auth=portfolio_dict_in[portfolio_in]['auth'])
a=r.json()
bal=float(a['balance'])
hold=float(a['hold'])
free=bal-hold
#print r.json()
print("EUR Balance / Hold / Free: "+str(bal) +" / " +str(hold)+" / " +str(free))
return bal,hold,free
# price is in EUR, size in coin
def place_limitedPortfolioOrder(portfolio_in,portfolio_dict_in,price, size):
# Place an order
tSucc=False
order = {
'type': 'limit',
'price': price,
'size': size,
'side': 'buy',
'product_id': portfolio_dict_in[portfolio_in]['product_id'],
}
r = requests.post(api_url + 'orders', json=order, auth=portfolio_dict_in[portfolio_in]['auth'])
#print r.json()
if r.status_code==200:
print("success")
tSucc=True
else:
print("!Error! in trade")
txt = raw_input("Continue? [y/N]: ")
if txt!='y':
raise Exception("Stopped execution of orders")
return tSucc
#============================================================================
#=== set-up keys
#============================================================================
api_url = 'https://api.pro.coinbase.com/'
#=== default
API_KEY='XXX'
API_SECRET='XXX'
API_PASS='XXX'
BTC_ID='XXX'
EUR_ID='XXX'
#auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)
#==22accBTC
API_KEY_22accBTC='XXX'
API_SECRET_22accBTC='XXX'
API_PASS_22accBTC='XXX'
#auth_22accBTC = CoinbaseExchangeAuth(API_KEY_22accBTC, API_SECRET_22accBTC, API_PASS_22accBTC)
EUR_ID_22accBTC='XXX'
#==22accATM
API_KEY_22accATM='XXX'
API_SECRET_22accATM='XXX'
API_PASS_22accATM='XXX'
#auth_22accATM = CoinbaseExchangeAuth(API_KEY_22accATM, API_SECRET_22accATM, API_PASS_22accATM)
EUR_ID_22accATM='XXX'
portfolio_dict = {
'default': {'auth': CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS),
'EUR_ID': EUR_ID
},
'22accBTC': {'auth': CoinbaseExchangeAuth(API_KEY_22accBTC, API_SECRET_22accBTC, API_PASS_22accBTC),
'product_id': 'BTC-EUR',
'EUR_ID': EUR_ID_22accBTC,
'minOrderEur': 0.1,
'amountDigits': 5,
'totalVolumeInclFee': 9614,#807,#6108,#800, #stays
'high': 17500,#32500,#<may22 #39000, #<======= SET!!!
'low': 3000 #stays
},
'22accATM': {'auth': CoinbaseExchangeAuth(API_KEY_22accATM, API_SECRET_22accATM, API_PASS_22accATM),
'product_id': 'ATOM-EUR',
'EUR_ID': EUR_ID_22accATM,
'minOrderEur': 0.1,
'amountDigits': 3,
'totalVolumeInclFee': 800, #stays
'high': 15.00, #<may22 #24.70,#26.00, #<======= SET!!!
'low': 3.50#20.00 #stays
}
}
#============================================================================
#=== do something
#============================================================================
portfolio='22accBTC'
bal,hold,freeEu=get_eurBalance(portfolio,portfolio_dict)
#determine price and size_ and place the limit order
#place_limitedPortfolioOrder(portfolio, portfolio_dict, price, size_)
print('Done')
The portfolio-dict keeps the keys and settings for the portfolios, such that one can simply call e.g. the provided get_eurBalance for a portfolie, calculate the desired limit and volume and place a limit order.
An example in this style how to fetch all open orders for a given portfolio (and how to cancel an order) would be nice!
Thanks a lot!
Alex