33 – Real-World Python Projects – Crypto Portfolio Tracker

Purpose

A real-time Python system that tracks cryptocurrency investments, calculates profit/loss, fetches live prices, draws charts, and notifies the user when prices hit targets.


🧠 What the Project Will Do

βœ” Track your crypto holdings (BTC, ETH, Solana, etc.)
βœ” Fetch live market prices using an API
βœ” Calculate:

  • Total portfolio value
  • Individual crypto value
  • Profit/loss per coin
  • Total profit/loss
    βœ” Visualize portfolio using a pie chart
    βœ” Auto-refresh every X seconds
    βœ” Export daily report to CSV

🧰 Tech Stack

  • requests (API calls)
  • pandas
  • matplotlib
  • json
  • (Optional) schedule for automation

πŸ“ Folder Structure

Crypto_Portfolio_Tracker/
│── portfolio.json
│── tracker.py
│── output/

πŸ“„ portfolio.json (Your holdings)

{
    "BTC": 0.05,
    "ETH": 1.2,
    "SOL": 15,
    "XRP": 200,
    "DOGE": 3000
}

You can add any coins.


πŸ”— Live Price API

We use CoinGecko free API (no key required):

https://api.coingecko.com/api/v3/simple/price

🧠 Full Python Code: tracker.py

import json
import requests
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime

# Load portfolio
with open("portfolio.json") as f:
    portfolio = json.load(f)

coins = ",".join(portfolio.keys()).lower()

# Fetch live prices
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coins}&vs_currencies=usd"
prices = requests.get(url).json()

data = []
total_value = 0

for coin, qty in portfolio.items():
    coin_id = coin.lower()
    price = prices[coin_id]["usd"]
    value = price * qty
    total_value += value

    data.append({
        "Coin": coin,
        "Quantity": qty,
        "Live Price (USD)": price,
        "Total Value (USD)": value
    })

df = pd.DataFrame(data)
df.loc["TOTAL"] = ["-", "-", "-", total_value]

print(df)

# Save CSV
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
csv_path = f"output/Portfolio_{timestamp}.csv"
df.to_csv(csv_path, index=False)

# Chart
plt.figure(figsize=(6, 6))
plt.pie([row["Total Value (USD)"] for row in data],
        labels=[row["Coin"] for row in data],
        autopct="%1.1f%%")
plt.title("Crypto Portfolio Distribution")
plt.show()

print("Saved:", csv_path)

🟦 Example Output

CoinQuantityLive PriceTotal Value
BTC0.0596,2004,810
ETH1.22,4802,976
SOL152103,150
XRP2000.64128
DOGE30000.12360
TOTALβ€”β€”11,424 USD

πŸ“Š Portfolio Pie Chart

Shows the percentage of each crypto’s share in your portfolio.


πŸš€ Advanced Features You Can Add

1. Telegram/WhatsApp Alerts

Notify when:

  • BTC < $90,000
  • ETH > $3,000
  • SOL drops 10%

2. Auto-Refresh Mode

Refresh every 5 seconds:

import time
while True:
    run_tracker()
    time.sleep(5)

3. Historical Tracking

Store values daily β†’ generate profit/loss graphs.

4. Dashboard With Streamlit

Live charts
Search coins
Dark mode UI
Realtime updates

5. Multiply on Rebalancing

Tell you which coins to buy/sell to maintain your ratio.


Comments

Leave a Reply

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