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.
pip install requests
Python code
30 linesimport 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
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
- Use `asyncio` and `aiohttp` for asynchronous API calls to track multiple cryptocurrencies without blocking.
- 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
More from Automation & scripting
- Batch Rename Hundreds of Files in Python easy
- Build a Command-Line Password Generator in Python easy
- Build a Complete Web Scraper with Requests and BeautifulSoup in Python medium
- Build a Network Ping Monitor in Python medium
- Create a Local Search Engine to Instantly Find Files on Your Computer in Python medium
- Create a Simple HTTP File Server in Python easy
Keep learning
Related tutorials and quizzes for this topic.