Ethereum Price Email Alerts With Python

Ethereum Price Email Alerts With Python

Isn't it cool getting notified by mail when there are price changes? Yes, It is! Today, we shall use python and the SMTP protocol to notify us if a certain price of crypto for example Bitcoin or Ethereum is reached.

If you have not read my article about sending emails by Python, please read it here.

ethereum1.jpg Otherwise, let's not waste time today with introductions.

Let's import our modules;

import requests
import time
import smtplib
from email.message import EmailMessage

Before we fetch our current price of Ether in USD. Let's define a function that will help us send an email;

def send_email():

      msg = EmailMessage()

      msg['From'] = "your_gmail"
      msg['To'] = send_email_to
      msg['Subject'] = "💥 Ether Price Alert 💥, ACT FAST!"


      message = msg.set_content("Dear " + your_name + "," + "\nEther prices are now "
                                + str(eth_rate) + ". Better transact quickly🙂.\nRegards,\n")

      server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
      server.login("your_gmail", "your_password")
      server.send_message(msg)
      server.quit()

      # prints to your console
      print("successfully sent email to %s:" % (msg['To']))
      print("Price of ether was at " + str(eth_rate))

If you have any trouble understanding this code, please read the full explanation in my Sending Emails With Python article.

I just created an email object and started a server, you will have to use your own Gmail while connecting. So you just need to replace those strings.

Before we continue, let's get some user input and where we shall be sending our alerts.

your_name = input('Enter your name: ')

send_email_to = input('Enter email address to send to: ')

alert_amount = input('Alert if Ether price is above: ')

Please note that you can also hard code the above into variables if you want.

We shall use Crypto Compare APIthis time;

url = "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD"

If you want other currency, you can use this and specify while getting the JSON data;

https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD,EUR,CNY,JPY,GBP

I will stick to the first one.

Now let's create an infinity loop that will continuously get the current ether price but well-timed.

while True: 

      response = requests.get(
      url,
      headers={"Accept": "application/json"},
      )
      data = response.json()
      USD = data['USD']
      eth_rate = int(USD)
      # print(eth_rate)


      if eth_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(eth_rate) + '. Will check again in 5 minutes. Ctrl + C to quit.')

The first portion of the code was to get our current USD equivalent. Then parse it as an integer in eth_rate.

The final part is our main logic, if ether price is higher than the alert amount, then we shall call the send_email() function and we obviously take trades.

We allow it to rest and check again after 3 minutes, you can increase this timing if you want.

The else part is met when the eth_rate is below the alert_amount and hence no mail is sent here and it will continuously get the data after every 5 minutes.

If you hard-coded all the variables and everything above and you did not want to run the script every time. You can run it in the background by executing this in your terminal after cding into the project root.

pythonw.exe .\ether_alert.py

GitHub Repo 🚀

That's It! Other Free APIs you could try;
📌 Coin Gecko Eth API
📌 Coin Desk Bitcoin API

You can also read about Bitcoin Price Desktop Notifier here if you prefer notifications.

🔹 Conclusion

Once again, hope you learned something today from my little closet.

Please consider subscribing or following me for related content, especially about Tech, Python & General Programming.

You can show extra love by buying me a coffee to support this free content and I am also open to partnerships, technical writing roles, collaborations and Python-related training or roles.

Buy Ronnie A Coffee 📢 You can also follow me on Twitter : ♥ ♥ Waiting for you! 🙂