Skip to content

Instantly share code, notes, and snippets.

@arnavs
Created April 13, 2021 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnavs/e854531342f4dd51c6d6522c3bf6d5e5 to your computer and use it in GitHub Desktop.
Save arnavs/e854531342f4dd51c6d6522c3bf6d5e5 to your computer and use it in GitHub Desktop.
update coinbase pro DCA for Python 3
# secrets
API_KEY = "something"
API_PASS = "something else"
API_SECRET = "also something else"
# boilerplate
import requests
import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase
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())
# add ascii fix
message = bytes(timestamp + request.method + request.path_url, "ascii") + (
request.body or ""
)
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
# add b"\n" and update encoding of digest
signature_b64 = base64.b64encode(signature.digest()).rstrip(b"\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
api_url = "https://api.pro.coinbase.com/"
auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)
# logic
order = {
"funds": 10,
"type": "market",
"side": "buy",
"product_id": "BTC-USD",
}
r = requests.post(api_url + "orders", json=order, auth=auth)
print(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment