Crypto Portfolio with CoinGecko & Python

   If you hold multiple cryptocurrencies, it can be useful to quickly calculate their total value in a specific currency.

In this example we use the CoinGecko API to retrieve the latest prices and calculate the value of a small crypto portfolio using Python.

 

 

Install the required library

pip install requests

 

 

Python Example

The script below retrieves the latest prices from the API and calculates the value of each cryptocurrency in the portfolio.

import requests

quantities = {
    "bitcoin": 0.001,
    "ripple": 300.1,
}

currency = "eur"


crypto_ids = ",".join(quantities.keys())

url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto_ids}&vs_currencies={currency}"

prices = requests.get(url).json()


total = 0

for crypto, amount in quantities.items():
    price = prices[crypto][currency]
    value = amount * price
    total += value

    print(f"{amount} {crypto.capitalize()} = {value:.2f} {currency.upper()}")

print(f"Total: {total:.2f} {currency.upper()}")

 

 

Cryptocurrency IDs

The API requires the CoinGecko coin IDs, not the trading symbols. Some popular examples:

bitcoin
ethereum
ripple

A full list of supported coins can be found here: https://www.coingecko.com/en/all-cryptocurrencies


Python