travis.media

Let's Create a Bitcoin Price Alert App in Python: Tutorial

Need an alert when Bitcoin prices drop so you can buy? Here's a simple Python app I created that sends you a Bitcoin price alert email when it drops below a specified amount.

Two days ago, for about 12 hours, Bitcoin dropped below $6000, the lowest price it's seen in 2018. While bloggers and news outlets took the opportunity to criticize cryptocurrency again, many of us who believe in the future of digital assets found it a wonderful time to buy.

Well, except for me as I didn't happen to check it in that time period. By the time I did it was back up over $6,100.

So, to avoid this again I created a simple Bitcoin price alert app that will shoot me an email when it drops again below $6,000.

And given that I love coding and helping others learn to code, I created this brief tutorial on how I created it. In this tutorial you will learn:

  • How to send an email with Python
  • How to pull data from an API
  • How to mask your password in the Terminal with Python
  • How to use timeouts with the Time module

bitcoin price alert app python

A word about the Bitcoin price alert app

I've become completely enamored with Python and am learning as much as I can about it these days.

So logically this app would present a wonderful opportunity for more practice with the language and its modules. That's the main drive behind creating this little Bitcoin price alert app.

So remember, this is just a fun experiment. It runs in the terminal. It's very, very basic and intended for hobbyists and of course, fellow CodeNewbies out there learning to code.

That's all it is. But…..enjoy!

An outline of the Bitcoin price alert app

  1. You are asked for a few inputs: Enter your name, Enter your email address (gmail only), Enter your password, Enter an email address to send alerts to, and the amount of Bitcoin by which you want to be alerted.
  2. Next, it checks the Coinbase API for the current price (which is updated every minute).
  3. If it is NOT below the amount you indicated, it will check again in 5 minutes.
  4. If it IS below the amount you indicated, it will send you an email alert, and it will check again in 3 minutes.

The Code Explained

PREREQUISITE: You have to allow for Less Secure Apps in your Google settings. Just set this feature on 'On' and everything will work fine. If you have double verification, you cannot do this.

So here we go:

———————–

First, we have to insert a number of Python modules:

requests – to pull the API data
time – to set the 5-minute timeouts
email.mime & smtplib – to send the emails
getpass – to mask your password when you enter it.

import requests
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import getpass</pre>
<p>&nbsp;</p>
<p><strong>Next, we create a function to send the email called send_email():</strong></p>
<p>Each section is explained with comments</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">send_email</span><span class="p">():</span>
  <span class="c1"># create message object instance</span>
  <span class="n">msg</span> <span class="o">=</span> <span class="n">MIMEMultipart</span><span class="p">()</span>
  
  <span class="c1"># the parameters of the message</span>
  <span class="n">password</span> <span class="o">=</span> <span class="n">your_password</span>
  <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">your_email</span>
  <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">send_email_to</span>
  <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&#34;Bitcoin price, ACT FAST&#34;</span>

  <span class="c1"># your message</span>
  <span class="n">message</span> <span class="o">=</span> <span class="s2">&#34;Dear &#34;</span> <span class="o">+</span> <span class="n">your_name</span> <span class="o">+</span> <span class="s2">&#34;</span><span class="se">\n</span><span class="s2">Bitcoin prices are now &#34;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bitcoin_rate</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&#34;. Better buy quick.</span><span class="se">\n</span><span class="s2">Regards,</span><span class="se">\n</span><span class="s2">&#34;</span> <span class="o">+</span> <span class="n">your_name</span>
  
  <span class="c1"># adds in the message from the above variable</span>
  <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">MIMEText</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="s1">&#39;plain&#39;</span><span class="p">))</span>
  
  <span class="c1"># create the gmail server</span>
  <span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">&#39;smtp.gmail.com: 587&#39;</span><span class="p">)</span>
  
  <span class="n">server</span><span class="o">.</span><span class="n">starttls</span><span class="p">()</span>
  
  <span class="c1"># Login Creds for sending the email</span>
  <span class="n">server</span><span class="o">.</span><span class="n">login</span><span class="p">(</span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">],</span> <span class="n">password</span><span class="p">)</span>
  
  <span class="c1"># sends the message</span>
  <span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">],</span> <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">],</span> <span class="n">message</span><span class="p">)</span>
  
  <span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
  
  <span class="c1"># prints to your console</span>
  <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;successfully sent email to </span><span class="si">%s</span><span class="s2">:&#34;</span> <span class="o">%</span> <span class="p">(</span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]))</span>
  <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;Price of bitcoin was at &#34;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bitcoin_rate</span><span class="p">))</span></code></pre></div>
<p>&nbsp;</p>
<p><strong>Next, we create our user inputs to get the intended data and save to appropriate variables.</strong><br />
FYI: Your password is masked with the getpass module.</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="c1"># user inputs</span>
<span class="n">your_name</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter your name: &#39;</span><span class="p">)</span>
<span class="n">your_email</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter your email address (gmail only): &#39;</span><span class="p">)</span>
<span class="n">your_password</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">()</span>
<span class="n">send_email_to</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter email address to send to: &#39;</span><span class="p">)</span>
<span class="n">alert_amount</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Alert if Bitcoin drops below: &#39;</span><span class="p">)</span></code></pre></div>
<p>&nbsp;</p>
<p><strong>Then we create an infinite while loop that will:</strong><br />
1. Check the current Bitcoin price.<br />
2. If it is above your specified amount it will check again in 5 minutes.<br />
3. If it is below your specified amount, it will run our function to send the email and check again in 3 minutes.</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  <span class="n">url</span> <span class="o">=</span> <span class="s2">&#34;https://api.coindesk.com/v1/bpi/currentprice.json&#34;</span>
  <span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
    <span class="n">url</span><span class="p">,</span> 
    <span class="n">headers</span><span class="o">=</span><span class="p">{</span><span class="s2">&#34;Accept&#34;</span><span class="p">:</span> <span class="s2">&#34;application/json&#34;</span><span class="p">},</span>
  <span class="p">)</span>
  <span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
  <span class="n">bpi</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;bpi&#39;</span><span class="p">]</span>
  <span class="n">USD</span> <span class="o">=</span> <span class="n">bpi</span><span class="p">[</span><span class="s1">&#39;USD&#39;</span><span class="p">]</span>
  <span class="n">bitcoin_rate</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">USD</span><span class="p">[</span><span class="s1">&#39;rate_float&#39;</span><span class="p">])</span>
  <span class="k">if</span> <span class="n">bitcoin_rate</span> <span class="o">&lt;</span> <span class="nb">int</span><span class="p">(</span><span class="n">alert_amount</span><span class="p">):</span>
    <span class="n">send_email</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Will check again in 3 minutes. Ctrl + C to quit.&#39;</span><span class="p">)</span>
    
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">180</span><span class="p">)</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">300</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Price is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bitcoin_rate</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;. Will check again in 5 minutes. Ctrl + C to quit.&#39;</span><span class="p">)</span></code></pre></div>
<h3>The Final Code</h3>
<p>So with all that being said, here is the final code:</p>
<div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">email.mime.multipart</span> <span class="kn">import</span> <span class="n">MIMEMultipart</span>
<span class="kn">from</span> <span class="nn">email.mime.text</span> <span class="kn">import</span> <span class="n">MIMEText</span>
<span class="kn">import</span> <span class="nn">smtplib</span>
<span class="kn">import</span> <span class="nn">getpass</span>

<span class="k">def</span> <span class="nf">send_email</span><span class="p">():</span>
  <span class="c1"># create message object instance</span>
  <span class="n">msg</span> <span class="o">=</span> <span class="n">MIMEMultipart</span><span class="p">()</span>
  
  <span class="c1"># the parameters of the message</span>
  <span class="n">password</span> <span class="o">=</span> <span class="n">your_password</span>
  <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">your_email</span>
  <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">send_email_to</span>
  <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&#34;Bitcoin price, ACT FAST&#34;</span>

  <span class="c1"># your message</span>
  <span class="n">message</span> <span class="o">=</span> <span class="s2">&#34;Dear &#34;</span> <span class="o">+</span> <span class="n">your_name</span> <span class="o">+</span> <span class="s2">&#34;</span><span class="se">\n</span><span class="s2">Bitcoin prices are now &#34;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bitcoin_rate</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&#34;. Better buy quick.</span><span class="se">\n</span><span class="s2">Regards,</span><span class="se">\n</span><span class="s2">&#34;</span> <span class="o">+</span> <span class="n">your_name</span>
  
  <span class="c1"># adds in the message from the above variable</span>
  <span class="n">msg</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">MIMEText</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="s1">&#39;plain&#39;</span><span class="p">))</span>
  
  <span class="c1"># create the gmail server</span>
  <span class="n">server</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s1">&#39;smtp.gmail.com: 587&#39;</span><span class="p">)</span>
  
  <span class="n">server</span><span class="o">.</span><span class="n">starttls</span><span class="p">()</span>
  
  <span class="c1"># Login Creds for sending the email</span>
  <span class="n">server</span><span class="o">.</span><span class="n">login</span><span class="p">(</span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">],</span> <span class="n">password</span><span class="p">)</span>
  
  <span class="c1"># sends the message</span>
  <span class="n">server</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">],</span> <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">],</span> <span class="n">message</span><span class="p">)</span>
  
  <span class="n">server</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
  
  <span class="c1"># prints to your console</span>
  <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;successfully sent email to </span><span class="si">%s</span><span class="s2">:&#34;</span> <span class="o">%</span> <span class="p">(</span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]))</span>
  <span class="nb">print</span><span class="p">(</span><span class="s2">&#34;Price of bitcoin was at &#34;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bitcoin_rate</span><span class="p">))</span>

<span class="c1"># user inputs</span>
<span class="n">your_name</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter your name: &#39;</span><span class="p">)</span>
<span class="n">your_email</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter your email address (gmail only): &#39;</span><span class="p">)</span>
<span class="n">your_password</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">()</span>
<span class="n">send_email_to</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Enter email address to send to: &#39;</span><span class="p">)</span>
<span class="n">alert_amount</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Alert if Bitcoin drops below: &#39;</span><span class="p">)</span>

<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
  <span class="n">url</span> <span class="o">=</span> <span class="s2">&#34;https://api.coindesk.com/v1/bpi/currentprice.json&#34;</span>
  <span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span>
    <span class="n">url</span><span class="p">,</span> 
    <span class="n">headers</span><span class="o">=</span><span class="p">{</span><span class="s2">&#34;Accept&#34;</span><span class="p">:</span> <span class="s2">&#34;application/json&#34;</span><span class="p">},</span>
  <span class="p">)</span>
  <span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
  <span class="n">bpi</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;bpi&#39;</span><span class="p">]</span>
  <span class="n">USD</span> <span class="o">=</span> <span class="n">bpi</span><span class="p">[</span><span class="s1">&#39;USD&#39;</span><span class="p">]</span>
  <span class="n">bitcoin_rate</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">USD</span><span class="p">[</span><span class="s1">&#39;rate_float&#39;</span><span class="p">])</span>
  <span class="k">if</span> <span class="n">bitcoin_rate</span> <span class="o">&lt;</span> <span class="nb">int</span><span class="p">(</span><span class="n">alert_amount</span><span class="p">):</span>
    <span class="n">send_email</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Will check again in 3 minutes. Ctrl + C to quit.&#39;</span><span class="p">)</span>
    
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">180</span><span class="p">)</span>
  <span class="k">else</span><span class="p">:</span>
    <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">300</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Price is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">bitcoin_rate</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;. Will check again in 5 minutes. Ctrl + C to quit.&#39;</span><span class="p">)</span></code></pre></div>
<p><strong>Further Improvements You Could Make To the Bitcoin Price Alert App</strong></p>
<ul>
<li>Dress up the email with some HTML and CSS. Make it look great!</li>
<li>Change the time intervals</li>
<li>Get the Subject to work as it currently doesn't.</li>
<li>Get other Altcoin requests and set alerts.</li>
<li>Have it <a href="https://gist.github.com/Cam1337/380121" target="_blank" rel="noopener">play an iTunes song</a> for an alert, or have it send you a text.</li>
</ul>
<p>Have at it!</p>
<p>And let me know if you have any questions or comments!</p>

----------

** This article may contain affiliate links. Please read the affiliate disclaimer for more details.

About Me Author

Travis of

Travis Media

From agency work with WordPress and PHP, to freelancing full-time, to jumping into the corporate space and working on large, mission-critical projects as a DevOps engineer, I create content to share what I’m learning along the way and to help you, the developer, succeed in the industry. Read More
Explore

You May Also Like