Overview
This Python-based Bitcoin Price Tracker API continuously monitors BTC/USD exchange rates using AlphaVantage data and delivers real-time email notifications every 60 seconds. Ideal for crypto traders and enthusiasts, this solution combines financial data integration with automated alert systems.
Key Features
- Minute-by-minute tracking of Bitcoin prices via AlphaVantage API
- SMTP email integration for instant price alerts
- Customizable parameters for different cryptocurrencies
- Error handling for reliable continuous operation
Technical Implementation
Required Libraries
import requests # API data fetching
import time # Interval timing
import smtplib # Email delivery
import ssl # Secure connectionsCore Components
Email Notification System
def send_email(crypto_symbol, current_price): # Configure SMTP server (Gmail example) email_from = "[email protected]" pswd = "your_password" email_to = "[email protected]" # Create secure SSL context context = ssl.create_default_context() try: with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(email_from, pswd) message = f"Subject: {crypto_symbol} Price Alert\n\nCurrent {crypto_symbol} price: ${current_price}" server.sendmail(email_from, email_to, message) print("Email notification sent successfully") except Exception as e: print(f"Email sending failed: {str(e)}")Price Tracking Loop
api_key = "YOUR_ALPHAVANTAGE_API_KEY" symbol = "BTC" while True: url = f"https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency={symbol}&to_currency=USD&apikey={api_key}" response = requests.get(url) data = response.json() if "Realtime Currency Exchange Rate" in data: current_price = data["Realtime Currency Exchange Rate"]["5. Exchange Rate"] print(f"Current BTC Price: ${current_price}") send_email(symbol, current_price) else: print("Error retrieving price data") time.sleep(60) # 60-second interval
Security Recommendations
๐ Best practices for API key management
Always store sensitive credentials like API keys and email passwords securely:
- Use environment variables instead of hardcoding
- Implement configuration files with restricted permissions
- Consider using dedicated secret management services
Customization Options
Track Multiple Cryptocurrencies
crypto_list = ["BTC", "ETH", "SOL"] for symbol in crypto_list: # Modify API call for each cryptocurrencyAdjust Notification Frequency
# Change sleep duration (in seconds) time.sleep(300) # 5-minute intervals
FAQ Section
Q: How do I get an AlphaVantage API key?
A: Visit AlphaVantage's website and sign up for a free account. Free tier includes 25 API requests per day.
Q: Can I use this with non-Gmail email providers?
A: Yes, but you'll need to modify SMTP server settings (e.g., "smtp.office365.com" for Outlook).
๐ Compare crypto exchange APIs
Q: Why is my script stopping after several hours?
A: This could be due to API rate limits. Consider adding error recovery logic or upgrading your API plan.
Advanced Implementation Tips
- Add price change percentage calculations
- Implement historical price tracking
- Create threshold-based alerts (e.g., notify when BTC drops below $X)
- Integrate with messaging platforms like Slack or Telegram
Remember to test your implementation thoroughly before deployment, and monitor script performance regularly for optimal results.