Track DogeCoin Real Time Price with Python

Source Node: 862579

Dogecoin was plummeting this morning and surging tonight, I was thinking, what if there is an alert that can send out mail saying, “Hey, the Dogecoin price is dropped 20%, it is time to buy in”.

Em, why not create one with Python by myself. Here are my overall steps.

  1. Find a crypto price API or scrape the web.
  2. Send out mail if the pice is meet the rule, say, drop 20%.

After searching and googling, I found Robinhood is the best place to grab real-time price info. No need to sign in, no call limitation, and for free.

Using python requests to request HTML text data, then use BeautifulSoup to extract the price info from the web page. Here is the code:

from bs4 import BeautifulSoup 
import requests
import time
def get_crypto_price_robin(coin):
url = "https://robinhood.com/crypto/"+coin
HTML = requests.get(url)
soup = BeautifulSoup(HTML.text,'html.parser')
crypto_name = soup.find("span",{"class":["css-10dsbjj"]}).text
price_text = soup.find("div",{"class":["_1Nw7xfQTjIvcCkNYkwQMzL"]}).text
price = float(price_text.replace("$","").replace(",",""))
price_chg_daily = soup.find("div",{"class":["_27rSsse3BjeLj7Y1bhIE_9"]}).text
crypto_obj = {
"crypto" : coin
,"crypto_name" : crypto_name
,"price" : price
,"price_chg_d" : price_chg_daily
}
return crypto_obj

This function works also for other crypto coins like Bitcoin and ETH. You can use it simply by calling a function with a different input parameter.

print(get_crypto_price_robin("ETH"))
print(get_crypto_price_robin("doge"))
print(get_crypto_price_robin("btc"))

You will see the result like this:

{'crypto': 'ETH', 'crypto_name': 'Ethereum', 'price': 3813.75, 'price_chg_d': '+$92.52 (+2.49%) Today'}
{'crypto': 'doge', 'crypto_name': 'Dogecoin', 'price': 0.525279, 'price_chg_d': '+$0.034555 (+7.04%) Today'}
{'crypto': 'btc', 'crypto_name': 'Bitcoin', 'price': 49314.69, 'price_chg_d': '-$384.86 (-0.77%) Today'}

Note that the price change percentage is based on UTC time, so it may look a bit different compare with your web Robinhood.

Create a send mail function using smtplib. I am using Hotmail, you can also shift the mail service to Gmail or any other kinds of mail service support SMTP.

import smtplib
from email.message import EmailMessage
def send_mail(to_email, subject, message, server='smtp.live.com',
from_email='abc@hotmail.com',pwd="password"):
msg = EmailMessage()
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ', '.join(to_email)
msg.set_content(message)
server = smtplib.SMTP(server,587)
server.ehlo()
server.starttls() #Puts connection to SMTP server in TLS mode
server.ehlo()
server.set_debuglevel(1)
server.login(from_email, pwd) # user & password
server.send_message(msg)
server.quit()
print('successfully sent the mail.')

Now use a while loop to check the price info and send out mail based on your own rule.

while True:
price_obj = get_crypto_price_robin("doge")
if price_obj['price']<0.6:
send_mail(to_email=['abc@hotmail.com'],
server = "smtp.live.com",from_email = "xhinker@hotmail.com",pwd="***",
subject='this is mail subject from Andrew by python', message='Time to buy Doge coin')
print("sent alert mail")
time.sleep(5)

Done, you have your own crypto coin price alert, fully under your control. Again, Python is really an amazing language can get work done with minimum lines of code. Enjoy.

Source: https://medium.datadriveninvestor.com/track-dogecoin-real-time-price-with-python-e09a1cae6b71?source=rss——-8—————–cryptocurrency

Time Stamp:

More from Medium