This script sets up a cryptocurrency price tracker and alert system using Python. It begins by importing necessary libraries, including pandas
for data manipulation, plotly
for visualization, dash
for creating the interactive dashboard, smtplib
for sending email alerts, twilio
for SMS alerts, and pycoingecko
for fetching cryptocurrency data. The data collection step involves defining a function, fetch_historical_data
, which retrieves historical price data for a specified cryptocurrency (in this case, Bitcoin) over a specified number of days using the CoinGecko API. This data is converted into a pandas DataFrame, with the timestamps formatted as datetime objects.
The data analysis step calculates daily returns and computes a 20-day rolling mean and standard deviation of the price. For visualization, a Dash application is created, and a Plotly figure is configured to display the price and the 20-day moving average as line charts. The Dash app layout includes a heading and the price chart, and the app is run to display this interactive dashboard.
In the alerts step, two functions are defined to send email and SMS alerts. The send_email_alert
function sets up an email with a subject and body, using SMTP to send the email via a Gmail account. The send_sms_alert
function uses the Twilio API to send an SMS message. An example usage of these functions is provided, where an alert is sent if the daily return exceeds 5%. This setup allows users to visualize cryptocurrency prices and receive real-time alerts based on specified conditions.