Track Your Crypto Portfolio with Python: A Tutorial Using CoinMarketCap API
By: Date: April 16, 2023 Categories: Crypto Tags: , ,

Python programming language is an open-source high-level language that is increasingly becoming popular among developers. Python has numerous libraries that make it a very versatile tool for various applications, including tracking crypto portfolios. The script below is an example of a Python script that tracks a crypto portfolio. The script uses the CoinMarketCap API to retrieve the price of various cryptocurrencies and calculates the portfolio’s total value. To run the script, you need to have an API key from CoinMarketCap, which you can get by creating a free account on their website. The script starts by importing the required libraries, including requests, json, and datetime. It then defines the API key and the list of cryptocurrencies to track. The script also defines the API endpoint and the parameters required to retrieve the cryptocurrency prices. Once the API parameters are set, the script sends an HTTP request to the CoinMarketCap API, and the response is stored in a variable called ‘response.’ The script then parses the response to retrieve the cryptocurrency prices and calculates the total value of the portfolio. Finally, the script prints the total value of the portfolio and the breakdown of each cryptocurrency’s value. The script can be scheduled to run at intervals to ensure that the portfolio’s value is always up-to-date. There are several ways to customize this script to fit your needs. For instance, you can add more cryptocurrencies to track or modify the API parameters to retrieve different data. You can also integrate the script with a web application or a mobile app to make it more accessible. In conclusion, Python is a versatile language that can be used for various applications, including tracking crypto portfolios. The script above is just an example of what you can achieve with Python. With some programming knowledge, you can create more complex applications that automate various tasks and make life easier.

import requests
import json

# replace the below with your own API keys
COINMARKETCAP_API_KEY = "YOUR_COINMARKETCAP_API_KEY"
CRYPTOCOMPARE_API_KEY = "YOUR_CRYPTOCOMPARE_API_KEY"

# list of cryptocurrencies in your portfolio
portfolio = ['BTC', 'ETH', 'ADA', 'DOGE']

def get_crypto_prices():
    # get prices from CoinMarketCap API
    url = f"https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol={','.join(portfolio)}"
    headers = {'X-CMC_PRO_API_KEY': COINMARKETCAP_API_KEY}
    response = requests.get(url, headers=headers)
    data = json.loads(response.text)['data']
    
    # create dictionary of prices
    prices = {}
    for symbol in portfolio:
        prices[symbol] = data[symbol]['quote']['USD']['price']
        
    return prices

def get_crypto_holdings():
    # replace the below with your own holdings
    holdings = {'BTC': 0.5, 'ETH': 1.25, 'ADA': 1000, 'DOGE': 5000}
    return holdings

def calculate_portfolio_value(prices, holdings):
    # calculate value of each holding
    holding_values = {}
    for symbol in portfolio:
        holding_values[symbol] = prices[symbol] * holdings[symbol]
    
    # calculate total portfolio value
    total_value = sum(holding_values.values())
    return total_value

def main():
    # get prices and holdings
    prices = get_crypto_prices()
    holdings = get_crypto_holdings()
    
    # calculate portfolio value
    portfolio_value = calculate_portfolio_value(prices, holdings)
    
    # print portfolio value
    print(f"Portfolio Value: ${portfolio_value:.2f}")

if __name__ == "__main__":
    main()

This script uses the CoinMarketCap API to get the latest prices of cryptocurrencies in your portfolio and the holdings of each cryptocurrency. It then calculates the value of each holding and the total portfolio value.

Note: In order to use this script, you will need to replace the placeholders for YOUR_COINMARKETCAP_API_KEY and YOUR_CRYPTOCOMPARE_API_KEY with your own API keys.

Leave a Reply

Your email address will not be published. Required fields are marked *