DEV Community

qing
qing

Posted on

Build a Crypto Portfolio Tracker with Python and Binance API

Build a Crypto Portfolio Tracker with Python and Binance API

Are you looking to integrate crypto data into your Python projects? This guide shows you exactly how to do it using free public APIs โ€” no paid subscription needed.

Why Python for Crypto?

Python is the go-to language for crypto developers because:

  • ๐Ÿ Simple, readable syntax for rapid prototyping
  • ๐Ÿ“Š Excellent data analysis libraries (pandas, numpy)
  • ๐Ÿ”Œ Easy HTTP requests with httpx/aiohttp
  • ๐Ÿค– Perfect for automation and bots

Getting Started

First, install the required library:

pip install httpx
Enter fullscreen mode Exit fullscreen mode

Implementation

Here's the working code:

import httpx
import asyncio

COINS = ["BTC", "ETH", "SOL", "BNB"]

async def get_prices(symbols):
    prices = {}
    async with httpx.AsyncClient() as client:
        for sym in symbols:
            r = await client.get(
                "https://api.binance.com/api/v3/ticker/price",
                params={"symbol": f"{sym}USDT"}
            )
            if r.status_code == 200:
                prices[sym] = float(r.json()["price"])
        return prices

prices = asyncio.run(get_prices(COINS))
for coin, price in prices.items():
    print(f"{coin}: ${price:,.2f}")
Enter fullscreen mode Exit fullscreen mode

How It Works

  1. Async HTTP: We use httpx.AsyncClient for non-blocking requests
  2. Free API: Binance/CoinGecko public APIs require no authentication
  3. Error handling: Always wrap API calls in try/except

Real-World Applications

You can extend this to:

  • ๐Ÿ“ฑ Build a price alert bot
  • ๐Ÿ“Š Track your portfolio automatically
  • ๐Ÿค– Create a trading signal generator
  • ๐Ÿ“ˆ Monitor DeFi yield opportunities

๐Ÿ”ฅ Trending This Week

  • Bitcoin (BTC) โ€” Rank #1
  • Ethereum (ETH) โ€” Rank #2

Resources


If you found this useful, follow me for more Python + Crypto tutorials! ๐Ÿš€

Follow for more Python content!


ๅ–œๆฌข่ฟ™็ฏ‡ๆ–‡็ซ ๏ผŸๅ…ณๆณจ่Žทๅ–ๆ›ดๅคšPython่‡ชๅŠจๅŒ–ๅ†…ๅฎน๏ผ


๐Ÿ’ก Related: **Content Creator Ultimate Bundle (Save 33%)* โ€” $29.99*

Top comments (0)