Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] I Made a Python Script That Beeps When BTC or ETH Prices Drop

#1
I Made a Python Script That Beeps When BTC or ETH Prices Drop

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;985731&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;2&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4.5\/5 - (2 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;128&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 128px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 4.5/5 – (2 votes) </div>
</div>
<p>This tutorials shares my experience of creating a simple Python script that warns me if crypto price data (e.g., BTC or ETH) crosses a certain threshold. </p>
<p><strong>Why would I need this?</strong> Well, the script can be useful for trading signals if I want to react quickly. While I don’t really trade, this script may be useful to time some buy or sell orders in a volatile market environment.</p>
<p>Besides — it’s fun and easy and quick, 5 minutes tops, so let’s just do it!</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Challenge</strong>: I want to create a small Python script — that also works for Jupyter notebooks — to play a tone or warning signal as soon as Bitcoin or ETH price cross a certain threshold!</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="1002" height="668" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-246.png" alt="" class="wp-image-985881" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-246.png 1002w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w" sizes="(max-width: 1002px) 100vw, 1002px" /><figcaption class="wp-element-caption"><em><strong>My Python Script if Crypto Prices Drop</strong></em></figcaption></figure>
</div>
<p>This short clip shows you the tone it generates when BTC falls under a certain price — wait for the beep:</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-i-created-a-python-script-that-warns-me-if-bitcoin-or-eth-price-is-above-a-certain-value/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F98lLfuKq7hA%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<p>You can run the <strong>Bitcoin price warning script</strong> in your background in a separate browser tab in a Colab Jupyter Notebook (code below).</p>
<p>Okay, let’s build the code in three easy steps.</p>
<h2>Step 1: Get Bitcoin, Ethereum, or Crypto Prices in Python</h2>
<p>First, install the <a href="https://pypi.org/project/Historic-Crypto/" data-type="URL" data-id="https://pypi.org/project/Historic-Crypto/" target="_blank" rel="noreferrer noopener">Historic Crypto</a> library to access live cryptocurrency data. </p>
<pre class="wp-block-preformatted"><strong>Jupyter Notebook:</strong> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />
<code>!pip install Historic-Crypto</code> <strong>Shell or Terminal:</strong> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />
<code>pip install Historic-Crypto</code></pre>
<p>Second, create a new object of the <code>LiveCryptoData</code> class, passing in the currency pair</p>
<ul>
<li><code>'BTC-USD'</code> for Bitcoin and USD</li>
<li><code>'ETH-USD'</code> for Ethereum and USD</li>
<li><code>'BTC-ETH'</code> for Bitcoin and Ethereum </li>
</ul>
<p>Third, use the <code>LiveCryptoData(...).return_data()</code> method to return a DataFrame and store it in a variable called <code>data</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from Historic_Crypto import LiveCryptoData
data = LiveCryptoData('BTC-USD').return_data()
print(data)</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>Collecting data for 'BTC-USD'
Checking if user supplied is available on the CoinBase Pro API...
Connected to the CoinBase Pro API.
Ticker 'BTC-USD' found at the CoinBase Pro API, continuing to extraction.
Status Code: 200, successful API call. ask bid volume \
time 2022-12-17 18:36:26.149769+00:00 16720.58 16720.56 28130.05026215 trade_id price size time 2022-12-17 18:36:26.149769+00:00 472092300 16720.58 0.0028569</code></pre>
<p>Fourth, print the first element in the <code>price</code> Series, which represents the current price of Bitcoin in US Dollars. So to get the current price, I simply call:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(data['price'][0])
# 16722.21</pre>
<p>I’m sure the price is completely outdated when you read this. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>Okay, now that I have the price data, I’ll create some code to create a warning tone.</p>
<h2>Step 2: Play Sinus Tone in Jupyter Notebook</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="1002" height="668" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-247.png" alt="" class="wp-image-985886" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-247.png 1002w, https://blog.finxter.com/wp-content/uplo...00x200.png 300w, https://blog.finxter.com/wp-content/uplo...68x512.png 768w" sizes="(max-width: 1002px) 100vw, 1002px" /></figure>
</div>
<p>My goal is to play a tone — any audio signal, really — even when the Python script or Jupyter notebook is not in the foreground.</p>
<p>I decided on an audio signal rather than a popup because popups are more intrusive to my workflow, and they may “pop up” in the background without me even seeing it.</p>
<p>Also, I may want to walk around and get some coffee <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2615.png" alt="☕" class="wp-smiley" style="height: 1em; max-height: 1em;" /> — and still be warned when BTC crosses my threshold! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/26a0.png" alt="⚠" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>How to create a sinus wave in a Jupyter Notebook in Python?</strong></p>
<p class="has-global-color-8-background-color has-background">This code imports the <a href="https://blog.finxter.com/numpy-tutorial/" data-type="post" data-id="1356" target="_blank" rel="noreferrer noopener">NumPy library</a> and the <code>IPython.display</code> library. It then creates a waveform with a frequency of 500 Hz and a duration of 2 seconds. The code then plays the waveform using the <code>Audio()</code> function from the <code>IPython.display</code> library. The rate is set to 10,000 Hz and autoplay is set to True so the sound will automatically play when the code is run.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np
from IPython.display import Audio wave = np.sin(2*np.pi*500*np.arange(15000*2)/15000)
Audio(wave, rate=10000, autoplay=True)</pre>
<p>Note that this code will only work in a Jupyter Notebook. To make a tone in any Python script, you can read the following tutorial on the Finxter blog:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/how-to-make-a-beep-sound-in-python-linux-macos-win/" data-type="URL" data-id="https://blog.finxter.com/how-to-make-a-beep-sound-in-python-linux-macos-win/" target="_blank" rel="noreferrer noopener">How to Create a Beep in Python?</a></p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-i-created-a-python-script-that-warns-me-if-bitcoin-or-eth-price-is-above-a-certain-value/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F7j5gu7YYgMQ%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<h2>Step 3: Putting It All Together in a Jupyter Notebook</h2>
<p>The following script for Jupyter Notebooks runs forever until the current Bitcoin price drops below a user-defined <code>threshold</code>. If it does, it issues an audio wave sound that makes you aware of the event. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="8" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">!pip install Historic-Crypto
from Historic_Crypto import LiveCryptoData
import numpy as np
from IPython.display import Audio
import time wave = np.sin(2*np.pi*500*np.arange(15000*2)/15000)
threshold = 16710 # usd per btc def get_price(): data = LiveCryptoData('BTC-USD', verbose=False).return_data() return float(data['price'][0]) print('Price warning below', threshold, 'USD per BTC')
print('Starting price', get_price(), 'USD per BTC') while get_price() > threshold: time.sleep(4) Audio(wave, rate=10000, autoplay=True)</pre>
<p>You can change the <code>threshold</code> variable that is highlighted in the code above to control the <strong>price threshold </strong>that will cause the <strong>beep sound to play</strong>.</p>
<p>Try it yourself in my interactive Jupyter notebook here (Colab):</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://colab.research.google.com/drive/1Kd4G_9sn6EFy7lZLLUYBsUQh7DXz-SSG?usp=sharing" target="_blank" rel="noreferrer noopener"><img decoding="async" loading="lazy" width="940" height="568" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-244.png" alt="" class="wp-image-985845" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-244.png 940w, https://blog.finxter.com/wp-content/uplo...00x181.png 300w, https://blog.finxter.com/wp-content/uplo...68x464.png 768w" sizes="(max-width: 940px) 100vw, 940px" /></a></figure>
</div>
<p>You can change the price data to Ethereum by using this function instead:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def get_price(): data = LiveCryptoData('ETH-USD', verbose=False).return_data() return float(data['price'][0])</pre>
<p>In a similar manner, this will also work for other crypto tickers or trading pairs.</p>
<h2>Thanks! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2665.png" alt="♥" class="wp-smiley" style="height: 1em; max-height: 1em;" /> </h2>
<p>I loved having you here. Feel free to stay updated with all our programming projects and download your coding cheat sheets here:</p>
</div>


https://www.sickgaming.net/blog/2022/12/...ices-drop/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016