DEV Community

Market Masters
Market Masters

Posted on

Build a Real-Time Crypto Portfolio Tracker in Python

Build a Real-Time Crypto Portfolio Tracker in Python: Market Masters API Tutorial

Retail traders track holdings across exchanges manually. Spreadsheets fail on live prices. Build a Python script that pulls real-time data for 2,500+ cryptos from the Market Masters API, computes PnL, and updates every minute. Covers BTC, ETH, alts on Binance/Bybit. Free tier works.

Tested on Python 3.12. Runnable in 10 minutes.

Why This API

Market Masters indexes Binance, Bybit spots/futures real-time. No rate limits on free tier for basics. Docs at marketmasters.ai/docs. Get key: sign up, free tier no CC.

Setup

pip install requests pandas matplotlib schedule
Enter fullscreen mode Exit fullscreen mode

API base: https://api.marketmasters.ai/v1

Step 1: Define Your Holdings

Hardcode or load from CSV. Example positions:

holdings = {
    'BTCUSDT': 0.5,
    'ETHUSDT': 2.3,
    'SOLUSDT': 45.0,
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Fetch Live Prices

Hit /prices/multi endpoint. Pass symbols.

import requests
import pandas as pd
from datetime import datetime

API_KEY = 'your_api_key_here'
BASE_URL = 'https://api.marketmasters.ai/v1'

def get_prices(symbols):
    url = f"{BASE_URL}/prices/multi"
    params = {'symbols': ','.join(symbols), 'apikey': API_KEY}
    resp = requests.get(url, params=params)
    data = resp.json()['data']
    return {item['symbol']: float(item['price']) for item in data}

symbols = list(holdings.keys())
prices = get_prices(symbols)
print(prices)
Enter fullscreen mode Exit fullscreen mode

Output: {'BTCUSDT': 69234.5, 'ETHUSDT': 3421.8, 'SOLUSDT': 178.2} (live values).

Step 3: Compute Portfolio Value and PnL

Assume avg buy prices. Track basis.

basis = {
    'BTCUSDT': 65000.0,
    'ETHUSDT': 3200.0,
    'SOLUSDT': 150.0,
}

portfolio_value = sum(holdings[s] * prices.get(s, 0) for s in holdings)
basis_value = sum(holdings[s] * basis[s] for s in holdings)
pnl_pct = ((portfolio_value - basis_value) / basis_value) * 100

df = pd.DataFrame({
    'Symbol': symbols,
    'Qty': [holdings[s] for s in symbols],
    'Price': [prices.get(s, 0) for s in symbols],
    'Basis': [basis.get(s, 0) for s in symbols],
    'Value': [holdings[s] * prices.get(s, 0) for s in symbols],
    'PnL%': [((prices.get(s,0) - basis.get(s,0)) / basis.get(s,0) * 100) for s in symbols]
})
print(df)
print(f"Total Value: ${portfolio_value:,.0f}")
print(f"PnL: {pnl_pct:.1f}%")
Enter fullscreen mode Exit fullscreen mode

Sample:

Total Value: $52,340
PnL: +4.2%
Enter fullscreen mode Exit fullscreen mode

Step 4: Real-Time Loop + Chart

Update every 60s. Plot with matplotlib.

import schedule
import time
import matplotlib.pyplot as plt

def update_portfolio():
    prices = get_prices(symbols)
    # recompute df, pnl_pct here...
    plt.clf()
    df['PnL%'].plot(kind='bar', title=f"Portfolio PnL {datetime.now().strftime('%H:%M')}")
    plt.tight_layout()
    plt.savefig('portfolio.png')
    print(f"Updated: PnL {pnl_pct:.1f}%")

schedule.every(1).minutes.do(update_portfolio)
update_portfolio()

while True:
    schedule.run_pending()
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Bonus: React Dashboard

Serve JSON from Python FastAPI, fetch in React.

Python endpoint:

from fastapi import FastAPI
app = FastAPI()

@app.get("/portfolio")
def portfolio():
    return {"prices": prices, "pnl": pnl_pct}
Enter fullscreen mode Exit fullscreen mode

React:

import { useEffect, useState } from 'react';

function Portfolio() {
  const [data, setData] = useState({});
  useEffect(() => {
    const fetchData = async () => {
      const res = await fetch('http://localhost:8000/portfolio');
      setData(await res.json());
    };
    fetchData();
    const interval = setInterval(fetchData, 60000);
    return () => clearInterval(interval);
  }, []);
  return <div>Portfolio PnL: {data.pnl?.toFixed(1)}%</div>;
}
Enter fullscreen mode Exit fullscreen mode

Gotchas

  • API free tier: 100 calls/min. Batch symbols.
  • Weekend liquidity thin: prices stable but volumes low.
  • Error handling: add resp.raise_for_status().
  • CORS for React: proxy or prod deploy.

Start tracking live: marketmasters.ai free tier API key. Build smarter.

Top comments (0)