· Travis Rodgers · Programming  · 6 min read

Let's Create a Bitcoin Price Alert App in Python: Tutorial

Need an alert when Bitcoin prices drop so you can buy? Here's a simple Python app I created that sends you a Bitcoin price alert email when it drops below a specified amount.

Two days ago, for about 12 hours, Bitcoin dropped below $6000, the lowest price it's seen in 2018. While bloggers and news outlets took the opportunity to criticize cryptocurrency again, many of us who believe in the future of digital assets found it a wonderful time to buy.

Well, except for me as I didn't happen to check it in that time period. By the time I did it was back up over $6,100.

So, to avoid this again I created a simple Bitcoin price alert app that will shoot me an email when it drops again below $6,000.

And given that I love coding and helping others learn to code, I created this brief tutorial on how I created it. In this tutorial you will learn:

  • How to send an email with Python
  • How to pull data from an API
  • How to mask your password in the Terminal with Python
  • How to use timeouts with the Time module

bitcoin price alert app python

A word about the Bitcoin price alert app

I've become completely enamored with Python and am learning as much as I can about it these days.

So logically this app would present a wonderful opportunity for more practice with the language and its modules. That's the main drive behind creating this little Bitcoin price alert app.

So remember, this is just a fun experiment. It runs in the terminal. It's very, very basic and intended for hobbyists and of course, fellow CodeNewbies out there learning to code.

That's all it is. But…..enjoy!

An outline of the Bitcoin price alert app

  1. You are asked for a few inputs: Enter your name, Enter your email address (gmail only), Enter your password, Enter an email address to send alerts to, and the amount of Bitcoin by which you want to be alerted.
  2. Next, it checks the Coinbase API for the current price (which is updated every minute).
  3. If it is NOT below the amount you indicated, it will check again in 5 minutes.
  4. If it IS below the amount you indicated, it will send you an email alert, and it will check again in 3 minutes.

The Code Explained

PREREQUISITE: You have to allow for Less Secure Apps in your Google settings. Just set this feature on 'On' and everything will work fine. If you have double verification, you cannot do this.

So here we go:

———————–

First, we have to insert a number of Python modules:

requests – to pull the API data
time – to set the 5-minute timeouts
email.mime & smtplib – to send the emails
getpass – to mask your password when you enter it.

```pythonimport requests import time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import smtplib import getpass

 

Next, we create a function to send the email called send_email():

Each section is explained with comments

def send_email():
  # create message object instance
  msg = MIMEMultipart()
  
  # the parameters of the message
  password = your_password
  msg['From'] = your_email
  msg['To'] = send_email_to
  msg['Subject'] = "Bitcoin price, ACT FAST"

  # your message
  message = "Dear " + your_name + "\nBitcoin prices are now " + str(bitcoin_rate) + ". Better buy quick.\nRegards,\n" + your_name
  
  # adds in the message from the above variable
  msg.attach(MIMEText(message, 'plain'))
  
  # create the gmail server
  server = smtplib.SMTP('smtp.gmail.com: 587')
  
  server.starttls()
  
  # Login Creds for sending the email
  server.login(msg['From'], password)
  
  # sends the message
  server.sendmail(msg['From'], msg['To'], message)
  
  server.quit()
  
  # prints to your console
  print("successfully sent email to %s:" % (msg['To']))
  print("Price of bitcoin was at " + str(bitcoin_rate))

 

Next, we create our user inputs to get the intended data and save to appropriate variables.
FYI: Your password is masked with the getpass module.

# user inputs
your_name = input('Enter your name: ')
your_email = input('Enter your email address (gmail only): ')
your_password = getpass.getpass()
send_email_to = input('Enter email address to send to: ')
alert_amount = input('Alert if Bitcoin drops below: ')

 

Then we create an infinite while loop that will:
1. Check the current Bitcoin price.
2. If it is above your specified amount it will check again in 5 minutes.
3. If it is below your specified amount, it will run our function to send the email and check again in 3 minutes.

while True:
  url = "https://api.coindesk.com/v1/bpi/currentprice.json"
  response = requests.get(
    url, 
    headers={"Accept": "application/json"},
  )
  data = response.json()
  bpi = data['bpi']
  USD = bpi['USD']
  bitcoin_rate = int(USD['rate_float'])
  if bitcoin_rate < int(alert_amount):
    send_email()
    print('Will check again in 3 minutes. Ctrl + C to quit.')
    
    time.sleep(180)
  else:
    time.sleep(300)
    print('Price is ' + str(bitcoin_rate) + '. Will check again in 5 minutes. Ctrl + C to quit.')

The Final Code

So with all that being said, here is the final code:

import requests
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import getpass

def send_email():
  # create message object instance
  msg = MIMEMultipart()
  
  # the parameters of the message
  password = your_password
  msg['From'] = your_email
  msg['To'] = send_email_to
  msg['Subject'] = "Bitcoin price, ACT FAST"

  # your message
  message = "Dear " + your_name + "\nBitcoin prices are now " + str(bitcoin_rate) + ". Better buy quick.\nRegards,\n" + your_name
  
  # adds in the message from the above variable
  msg.attach(MIMEText(message, 'plain'))
  
  # create the gmail server
  server = smtplib.SMTP('smtp.gmail.com: 587')
  
  server.starttls()
  
  # Login Creds for sending the email
  server.login(msg['From'], password)
  
  # sends the message
  server.sendmail(msg['From'], msg['To'], message)
  
  server.quit()
  
  # prints to your console
  print("successfully sent email to %s:" % (msg['To']))
  print("Price of bitcoin was at " + str(bitcoin_rate))

# user inputs
your_name = input('Enter your name: ')
your_email = input('Enter your email address (gmail only): ')
your_password = getpass.getpass()
send_email_to = input('Enter email address to send to: ')
alert_amount = input('Alert if Bitcoin drops below: ')

while True:
  url = "https://api.coindesk.com/v1/bpi/currentprice.json"
  response = requests.get(
    url, 
    headers={"Accept": "application/json"},
  )
  data = response.json()
  bpi = data['bpi']
  USD = bpi['USD']
  bitcoin_rate = int(USD['rate_float'])
  if bitcoin_rate < int(alert_amount):
    send_email()
    print('Will check again in 3 minutes. Ctrl + C to quit.')
    
    time.sleep(180)
  else:
    time.sleep(300)
    print('Price is ' + str(bitcoin_rate) + '. Will check again in 5 minutes. Ctrl + C to quit.')

Further Improvements You Could Make To the Bitcoin Price Alert App

  • Dress up the email with some HTML and CSS. Make it look great!
  • Change the time intervals
  • Get the Subject to work as it currently doesn't.
  • Get other Altcoin requests and set alerts.
  • Have it play an iTunes song for an alert, or have it send you a text.

Have at it!

And let me know if you have any questions or comments!

    Share:

    Related Posts

    View All Posts »