Build A Crypto Currency Price Alerter

randerson112358
5 min readJan 6, 2021

--

Send cryptocurrency price alerts directly to your email address using Python

In this article, I will show you how to create your very own crypto currency price alerter using the python programming language. According to Wikipedia, crypto currencies are digital assets designed to work as a medium of exchange wherein individual coin ownership records are stored in a ledger existing in a form of computerized database using strong cryptography to secure transaction records, to control the creation of additional coins, and to verify the transfer of coin ownership.

The idea of the price alerter is to send an email directly to a specific email address (your email address) after the price of a cryptocurrency like Bitcoin passes some price threshold like $30,000 USD. With this information you can then decide if you want to buy or sell the crypto.

Many people consider Bitcoin to be the first cryptocurrency and this is the crypto that I will use in the code. If you don’t already know, Bitcoin is a cryptocurrency invented in 2008 by an unknown person or group of people using the name Satoshi Nakamoto and started in 2009 when its implementation was released as open-source software according to Wikipedia. Twelve years after its creation, the price continues to grow and fluctuate.

Programming Concept

Gather the current cryptocurrency price

This is how the program is going to work, first I plan on gathering the price of a crypto currency. This can be done either by some API or I can scrape the data myself from a website. I am choosing the latter. I plan on scraping the data from google.com.

Connect to an SMTP server

The next step after gathering the price is to connect to an email server to be able to send messages to and from some email address. Luckily for us Google provides a free SMTP server we can use.

Send the cryptocurrency price as text to your email address

Last but not least is to take the price of the cryptocurrency and mail it to your email address using the SMTP server after the price passes some arbitrary threshold.

The video below, will show you how to create this program step by step, so you can use it for extra help!

Programming Code

First, I will add a description about the code, that way I can just read the description later and know what the code is supposed to do.

#Description: This program sends crypto currency price alerts

Next, I will import the libraries needed throughout the program.

#Import the libraries
from bs4 import BeautifulSoup
import requests
import time
import smtplib
import ssl
from email.mime.text import MIMEText as MT
from email.mime.multipart import MIMEMultipart as MM

Now, let’s create a function to gather the price of a cryptocurrency. With this function we can input a coin e.g. ‘bitcoin’, ‘litecoin’, etc. and get back the current price of that coin.

#Create a function to get the price of a cryptocurrency
def get_crypto_price(coin):
#Get the URL
url = "https://www.google.com/search?q="+coin+"+price"
#Make a request to the website
HTML = requests.get(url)
#Parse the HTML
soup = BeautifulSoup(HTML.text, 'html.parser')
#Find the current price
text = soup.find("div", attrs={'class':'BNeawe iBp4i AP7Wnd'}).text
#Return the text
return text

Store the email addresses and email password into variables to be able to use them later.

#Store the email addresses for the receiver, and the sender and store the senders password
receiver = '<RECEIVER_EMAIL_ADDRESS>'
sender = '<SENDER_EMAIL_ADDRESS>'
sender_password = '<SENDER_PASSWORD>'

Next, create a function to send emails, by connecting to Googles SMTP server. If this function doesn’t work for you, then you may need to configure your email client, you can learn how to do this either by watching the video above or reading my other articled “Learn How To Send Emails Using The Python Programming Language”.

#Create a function to send emails
def send_email(sender, receiver, sender_password, text_price):
#Create a MIMEMutltipart Object
msg = MM()
msg['Subject'] = "New Crypto Price Alert !"
msg['From']= sender
msg['To']= receiver
#Create the HTML for the message
HTML = """
<html>
<body>
<h1>New Crypto Price Alert !</h1>
<h2>"""+text_price+"""
</h2>
</body>
</html>
"""
#Create a html MIMEText Object
MTObj = MT(HTML, "html")
#Attach the MIMEText Object
msg.attach(MTObj)
#Create the secure socket layer (SSL) context object
SSL_context = ssl.create_default_context()
#Create the secure Simple Mail Transfer Protocol (SMTP) connection
server = smtplib.SMTP_SSL(host="smtp.gmail.com", port=465, context=SSL_context)
#Login to the email
server.login(sender, sender_password)
#Send the email
server.sendmail(sender, receiver, msg.as_string())

Let’s create a function to send price alerts to an email address according to some threshold. The threshold that I will use in this function will trigger the price to be sent via email anytime the price of the cryptocurrency changes. Be sure to set the crypto/coin to the preferred cryptocurrency e.g. ‘bitcoin’, ‘litecoin’, etc.

#Create a function to send the alert
def send_alert():
last_price = -1
#Create an infinite loop to continuously send/show the price
while True:
#Choose the cryptocurrency/coin
coin = 'bitcoin'
#Get the price of the cryptocurrency
price = get_crypto_price(coin)
#Check if the price has changed
if price != last_price:
print(coin.capitalize()+' price: ', price)
price_text = coin.capitalize()+' is '+price
send_email(sender, receiver, sender_password, price_text)
last_price = price #Update the last price
time.sleep(3)

Now let’s run it !

#Send the alert
send_alert()

If you want to start an investment portfolio, then sign up with WeBull using this link and get FREE stocks by depositing $100 or more. It’s free stocks that you can either sell, play with or create your own trading strategy with. I think it’s a great deal for a limited time and FREE money!

Thanks for reading this article I hope it’s helpful to you all! If you enjoyed this article and found it helpful please leave some claps to show your appreciation ! If you want to learn more about the python programming language, then I suggest reading “Learning Python by O’Reilly”.

Photo from Amazon

--

--

No responses yet