Last active
May 13, 2022 19:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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