Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

How to Build a Cryptocurrency Price Tracker in Python

A continuous Python script that fetches real-time cryptocurrency prices from the CoinGecko API and displays them on a loop.

Medium Python 3.9+ Jun 27, 2026 Automation & scripting 1 views 0 copies

Requires third-party packages — install first
pip install requests

Python code

30 lines
Python 3.9+
import requests
import time

def get_crypto_prices(coin_ids=["bitcoin", "ethereum", "solana"]):
    url = "https://api.coingecko.com/api/v3/simple/price"
    params = {
        "ids": ",".join(coin_ids),
        "vs_currencies": "usd"
    }
    try:
        response = requests.get(url, params=params, timeout=10)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error fetching prices: {e}")
        return {}

def main():
    print("Cryptocurrency Price Tracker")
    print("-" * 30)
    while True:
        prices = get_crypto_prices()
        if prices:
            for coin, data in prices.items():
                print(f"{coin.title()}: ${data['usd']:.2f}")
            print("-" * 30)
        time.sleep(60)

if __name__ == "__main__":
    main()

Output

stdout
Cryptocurrency Price Tracker
------------------------------
Bitcoin: $67543.21
Ethereum: $3452.89
Solana: $143.56
------------------------------
Bitcoin: $67540.15
Ethereum: $3453.12
Solana: $143.44
------------------------------

How it works

The script uses the requests library to call CoinGecko's free API endpoint /simple/price. The get_crypto_prices function builds query parameters with comma-separated coin IDs and the target currency (USD). It includes error handling with raise_for_status() and a try/except block to gracefully handle network issues or API failures. The main loop runs indefinitely, printing formatted prices every 60 seconds using time.sleep(). The .title() method beautifies coin names, and f-string formatting ensures prices display with two decimal places.

Common mistakes

  • Forgetting to install the `requests` library with `pip install requests` before running the script.
  • Exceeding CoinGecko's free API rate limits by calling the API too frequently (more than 10-30 calls per minute).
  • Not handling API errors or network failures, causing the script to crash on connectivity issues.

Variations

  1. Use `asyncio` and `aiohttp` for asynchronous API calls to track multiple cryptocurrencies without blocking.
  2. Add Telegram or email notifications using `python-telegram-bot` or `smtplib` to alert on price thresholds.

Real-world use cases

  • Running on a Raspberry Pi to display live crypto prices on an LED screen or dashboard.
  • Powering a notification bot that texts you when Bitcoin drops below a certain price.
  • Collecting price data into a CSV or database for historical analysis and trend detection.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.