\

The price of bitcoin continued to rise on April 30, breaking the $9,400 mark, up 17.42% in 24 hours. Bitcoin is up 26.33% in 2020, outpacing gold year-to-date gains. In fact, bitcoin’s price is up more than 130% since the 312 crash, a recovery that far outpaces the Dow.

When the price of bitcoin falls, you need to receive a price alert to buy long. Let’s create a simple Python application to do this. The application will send you a bitcoin price alert email when the price of bitcoin falls below a specified amount.

In this tutorial, you will learn:

  • Send email in Python
  • Extract data from the Coinbase exchange API
  • Use Python to hide passwords in terminals
  • inTimeUse the timeout function in the module

Application Overview

1. Ask you to enter the following: name, email address (Gmail only), password, email address to send alerts to, and the price of bitcoin to be alerted.

2. Next, it checks the current price of the Coinbase API (updated every minute).

3. If it is not lower than the price you specified, it will be checked again in 5 minutes.

4. If it falls below the price you specify, it will send you an email alert and check again within 3 minutes.

Code implementation Principle

You must allow less secure applications in your Google Settings. Simply set this feature to “on”. You cannot do this if you are using two-factor authentication.

To get started, first insert some necessary Python modules:

  • Requests – Get API data
  • Time – Sets the 5-minute timeout function
  • Email.mime & smtplib – Sends messages
  • Getpass – Hide the password when entering it
import requests
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import getpass
Copy the code

Next, we create a function called send_email () to send an email, with comments for each part:

Def send_email(): def send_email(): MSG = MIMEMultipart()'From'] = your_email
  msg['To'] = send_email_to
  msg['Subject'] = "Bitcoin price alert!"# message = your_name +"\n Bitcoin price is now" + str(bitcoin_rate) + ! "" Please add warehouse operation! \n"MSG. Attach (MIMEText(message,'plain'Server = smtplib.smtp ()'smtp.gmail.com: 587'Server.starttls () # server.login(MSG [)'From'], password) # send message server.sendmail(MSG ['From'], msg['To'], message) server.quit() # print to consoleprint("Successfully sent mail to %s:" % (msg['To']))
  print("The price of Bitcoin is + str(bitcoin_rate))
Copy the code

Next, we create user input to get the expected data and save it to the appropriate variables.

# user input your_name = input('Enter your name:')
your_email = input('Enter your server email address (Gmail only):')
your_password = getpass.getpass()
send_email_to = input('Enter the email address you are sending:')
alert_amount = input('Execute warning when bitcoin price is below how much:')
Copy the code

We can then create a while loop that will do the following:

1. Check the current bitcoin price.

2. If it exceeds your specified price, it will be checked again in 5 minutes.

3. If it falls below the price you specify, it will run the program to send an 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, press Ctrl + C to exit! ')

    time.sleep(180)
  else:
    time.sleep(300)
    print('The current price of Bitcoin is' + str(bitcoin_rate) + '. Will check again in 5 minutes, press Ctrl + C to exit! ')
Copy the code

The complete code

import requests
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
importGetpass def send_email(): MSG = MIMEMultipart()'From'] = your_email
  msg['To'] = send_email_to
  msg['Subject'] = "Bitcoin price alert!"# message = your_name +"\n Bitcoin price is now" + str(bitcoin_rate) + ! "" Please add warehouse operation! \n"MSG. Attach (MIMEText(message,'plain'Server = smtplib.smtp ()'smtp.gmail.com: 587'Server.starttls () # server.login(MSG [)'From'], password) # send message server.sendmail(MSG ['From'], msg['To'], message) server.quit() # print to consoleprint("Successfully sent mail to %s:" % (msg['To']))
  print("The price of Bitcoin is+ STR (bitcoin_rate) # enter your_name = input('Enter your name:')
your_email = input('Enter your server email address (Gmail only):')
your_password = getpass.getpass()
send_email_to = input('Enter the email address you are sending:')
alert_amount = input('Execute warning when bitcoin price is below how much:')

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, press Ctrl + C to exit! ')

    time.sleep(180)
  else:
    time.sleep(300)
    print('The current price of Bitcoin is' + str(bitcoin_rate) + '. Will check again in 5 minutes, press Ctrl + C to exit! ')
Copy the code

conclusion

In this article, we created a program to send bitcoin price warning emails in Python. We can make some improvements to the program:

  • Decorate email with HTML and CSS
  • Change time interval
  • Add other digital currencies and set alerts
  • Make an alarm sound when sending a message text

Recommended reading:

2020Python Recruitment promotion channel now open! \

Old driver teaches you to read Python decorator in 5 minutes \

Using Python to implement particle swarm optimization \

Bargain-hunting us stocks? Use Python to analyze the real return on U.S. stocks