Skip to content

Instantly share code, notes, and snippets.

@mike-weiner
Last active May 13, 2022 19:26
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 mike-weiner/4d368f4cd601a792c80488011516707d to your computer and use it in GitHub Desktop.
Save mike-weiner/4d368f4cd601a792c80488011516707d to your computer and use it in GitHub Desktop.
A Python script that uses the Coinbase API to get the current spot total of specified portfolio of cryptocurrencies.
# import required tools
import requests
from datetime import datetime
# print current date and time
print(datetime.now())
print()
# define array of cryptos & quantities owned
# TO DO: Enter Your Portfolio Holdings
COINS = {
"BTC-USD" : 0,
"ETH-USD" : 0,
}
# initialize tracking variables
counter = 0
total = 0
# for each cryptocurrency defined in COINS
for coin in COINS:
URL = "https://api.coinbase.com/v2/prices/" + coin + "/spot" # generate the URL for the current crypto to make an API call
requestResponse = requests.get(URL) # make the GET API call to Coinbase and store the response
parsedJSONResponse = requestResponse.json()
# print out the crypto name, QTY, Spot Price, and Total
print(str(COINS[coin]) + " " + parsedJSONResponse['data']['base'] + " at $" + parsedJSONResponse['data']['amount'] + " /" + parsedJSONResponse['data']['base'] + " = " + str(float(parsedJSONResponse['data']['amount']) * COINS[coin]))
total += float(parsedJSONResponse['data']['amount']) * COINS[coin]
counter += 1
print("- - -")
print("Total: $" + str(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment