Python
Gmail Crypto Price Alerts
Due to the extreme volatility of the cryptocurrency market, you’re required to always have an eye on your favourite token’s prices, lest you miss the optimal buy or sell opportunity. You need a way of being notified at the crucial prices in the cycle. There are plenty of crypto price notification services already out there but I set out to create my own in Python.
I wanted to learn 3 things with this code that I hadn’t used before:
- How to check the price of a cryptocurrency in code. This checks the price of the Shiba Inu token
- How to call a function defined in a separate file (called a module)
- How to send an email through code
You’ll need a Gmail account to use this. I used my personal email: nasanbotmail@gmail.com. You’ll also need to turn on ‘Access for less secure apps’. This option is found in your Google Account -> Security tab. Keep in mind that Google will automatically turn this setting off if you don’t use it for a while, breaking the code until you turn it back on.
This is the code containing the function used to send the email:
import smtplib, ssl port = 465 # For SSL smtp_server = "smtp.gmail.com" sender_email = "nasanbotmail@gmail.com" # Enter your address password = "********" #Enter your password def SendGmail(receiver_email, message): context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message)
This file is named GmailEmail.py which is important as I'll import the SendGmail function into the main code as a module. This is similar to adding in libraries.
Next we write the main code. Ensure the main file is in the same directory as the module you just created. I use the CoinGecko API to get the Shiba Inu price in AUD.
import requests from time import sleep from GmailEmail import SendGmail sellthreshold = 0.00004 buythreshold = 0.000015 targetemail = "nasanbotmail@gmail.com" #enter email target here while(1): response = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=shiba-inu&vs_currencies=aud') json = response.json() shibprice = format(json['shiba-inu']['aud'], '.9f') shibpricefloat= float(format(json['shiba-inu']['aud'], '.9f')) print(shibprice) if shibpricefloat > sellthreshold: SendGmail(targetemail,"Subject:Shib Sell Alert\nSell Shib!! Price is ${}".format(shibprice)) exit() if shibpricefloat < buythreshold: SendGmail(targetemail,"Subject:Shib Buy Alert\nBuy Shib!! Price is ${}".format(shibprice)) exit() sleep(20)
Set your sell and buy price thresholds and wait for an email. The GET request only pulls the price every 20 seconds as the CoinGecko free API only allows for a limited number of requests per hour. Also the code exits after sending one email as not to spam your inbox. Another way around this could be to add a wait after the email is sent, say for an hour before repeating the loop. But I still prefer to only receive one price alert email, which gives me a chance to re-set the thresholds.