{"id":130619,"date":"2022-12-17T21:44:25","date_gmt":"2022-12-17T21:44:25","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=985731"},"modified":"2022-12-17T21:44:25","modified_gmt":"2022-12-17T21:44:25","slug":"i-made-a-python-script-that-beeps-when-btc-or-eth-prices-drop","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/12\/17\/i-made-a-python-script-that-beeps-when-btc-or-eth-prices-drop\/","title":{"rendered":"I Made a Python Script That Beeps When BTC or ETH Prices Drop"},"content":{"rendered":"\n<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;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 128px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 4.5\/5 &#8211; (2 votes) <\/div>\n<\/div>\n<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>\n<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&#8217;t really trade, this script may be useful to time some buy or sell orders in a volatile market environment.<\/p>\n<p>Besides &#8212; it&#8217;s fun and easy and quick, 5 minutes tops, so let&#8217;s just do it!<\/p>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Challenge<\/strong>: I want to create a small Python script &#8212; that also works for Jupyter notebooks &#8212; to play a tone or warning signal as soon as Bitcoin or ETH price cross a certain threshold!<\/p>\n<div class=\"wp-block-image\">\n<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\/uploads\/2022\/12\/image-246-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-246-768x512.png 768w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><figcaption class=\"wp-element-caption\"><em><strong>My Python Script if Crypto Prices Drop<\/strong><\/em><\/figcaption><\/figure>\n<\/div>\n<p>This short clip shows you the tone it generates when BTC falls under a certain price &#8212; wait for the beep:<\/p>\n<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 decoding=\"async\" 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>\n<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>\n<p>Okay, let&#8217;s build the code in three easy steps.<\/p>\n<h2>Step 1: Get Bitcoin, Ethereum, or Crypto Prices in Python<\/h2>\n<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>\n<pre class=\"wp-block-preformatted\"><strong>Jupyter Notebook:<\/strong> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>\n<code>!pip install Historic-Crypto<\/code> <strong>Shell or Terminal:<\/strong> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f447.png\" alt=\"\ud83d\udc47\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/>\n<code>pip install Historic-Crypto<\/code><\/pre>\n<p>Second, create a new object of the <code>LiveCryptoData<\/code> class, passing in the currency pair<\/p>\n<ul>\n<li><code>'BTC-USD'<\/code> for Bitcoin and USD<\/li>\n<li><code>'ETH-USD'<\/code> for Ethereum and USD<\/li>\n<li><code>'BTC-ETH'<\/code> for Bitcoin and Ethereum <\/li>\n<\/ul>\n<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>\n<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\ndata = LiveCryptoData('BTC-USD').return_data()\nprint(data)<\/pre>\n<p>Output:<\/p>\n<pre class=\"wp-block-preformatted\"><code>Collecting data for 'BTC-USD'\nChecking if user supplied is available on the CoinBase Pro API...\nConnected to the CoinBase Pro API.\nTicker 'BTC-USD' found at the CoinBase Pro API, continuing to extraction.\nStatus Code: 200, successful API call. ask bid volume \\\ntime 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>\n<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>\n<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])\n# 16722.21<\/pre>\n<p>I&#8217;m sure the price is completely outdated when you read this. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f680.png\" alt=\"\ud83d\ude80\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Okay, now that I have the price data, I&#8217;ll create some code to create a warning tone.<\/p>\n<h2>Step 2: Play Sinus Tone in Jupyter Notebook<\/h2>\n<div class=\"wp-block-image\">\n<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\/uploads\/2022\/12\/image-247-300x200.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-247-768x512.png 768w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/figure>\n<\/div>\n<p>My goal is to play a tone &#8212; any audio signal, really &#8212; even when the Python script or Jupyter notebook is not in the foreground.<\/p>\n<p>I decided on an audio signal rather than a popup because popups are more intrusive to my workflow, and they may &#8220;pop up&#8221; in the background without me even seeing it.<\/p>\n<p>Also, I may want to walk around and get some coffee <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2615.png\" alt=\"\u2615\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> &#8212; and still be warned when BTC crosses my threshold! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/26a0.png\" alt=\"\u26a0\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>How to create a sinus wave in a Jupyter Notebook in Python?<\/strong><\/p>\n<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>\n<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\nfrom IPython.display import Audio wave = np.sin(2*np.pi*500*np.arange(15000*2)\/15000)\nAudio(wave, rate=10000, autoplay=True)<\/pre>\n<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" 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>\n<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 decoding=\"async\" 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>\n<h2>Step 3: Putting It All Together in a Jupyter Notebook<\/h2>\n<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>\n<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\nfrom Historic_Crypto import LiveCryptoData\nimport numpy as np\nfrom IPython.display import Audio\nimport time wave = np.sin(2*np.pi*500*np.arange(15000*2)\/15000)\nthreshold = 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')\nprint('Starting price', get_price(), 'USD per BTC') while get_price() > threshold: time.sleep(4) Audio(wave, rate=10000, autoplay=True)<\/pre>\n<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>\n<p>Try it yourself in my interactive Jupyter notebook here (Colab):<\/p>\n<div class=\"wp-block-image\">\n<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\/uploads\/2022\/12\/image-244-300x181.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/12\/image-244-768x464.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/a><\/figure>\n<\/div>\n<p>You can change the price data to Ethereum by using this function instead:<\/p>\n<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>\n<p>In a similar manner, this will also work for other crypto tickers or trading pairs.<\/p>\n<h2>Thanks! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2665.png\" alt=\"\u2665\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/h2>\n<p>I loved having you here. Feel free to stay updated with all our programming projects and download your coding cheat sheets here:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>4.5\/5 &#8211; (2 votes) 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. Why would I need this? Well, the script can be useful for trading signals if I want to react quickly. While I don&#8217;t really trade, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-130619","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=130619"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/130619\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=130619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=130619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=130619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}