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

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’t really trade, this script may be useful to time some buy or sell orders in a volatile market environment.

Besides — it’s fun and easy and quick, 5 minutes tops, so let’s just do it!

πŸ’¬ Challenge: 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!

My Python Script if Crypto Prices Drop

This short clip shows you the tone it generates when BTC falls under a certain price — wait for the beep:

You can run the Bitcoin price warning script in your background in a separate browser tab in a Colab Jupyter Notebook (code below).

Okay, let’s build the code in three easy steps.

Step 1: Get Bitcoin, Ethereum, or Crypto Prices in Python

First, install the Historic Crypto library to access live cryptocurrency data.

Jupyter Notebook: πŸ‘‡
!pip install Historic-Crypto

Shell or Terminal: πŸ‘‡
pip install Historic-Crypto

Second, create a new object of the LiveCryptoData class, passing in the currency pair

  • 'BTC-USD' for Bitcoin and USD
  • 'ETH-USD' for Ethereum and USD
  • 'BTC-ETH' for Bitcoin and Ethereum

Third, use the LiveCryptoData(...).return_data() method to return a DataFrame and store it in a variable called data.

from Historic_Crypto import LiveCryptoData
data = LiveCryptoData('BTC-USD').return_data()
print(data)

Output:

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

Fourth, print the first element in the price Series, which represents the current price of Bitcoin in US Dollars. So to get the current price, I simply call:

print(data['price'][0])
# 16722.21

I’m sure the price is completely outdated when you read this. πŸš€

Okay, now that I have the price data, I’ll create some code to create a warning tone.

Step 2: Play Sinus Tone in Jupyter Notebook

My goal is to play a tone — any audio signal, really — even when the Python script or Jupyter notebook is not in the foreground.

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.

Also, I may want to walk around and get some coffee β˜• — and still be warned when BTC crosses my threshold! ⚠️

πŸ‘‰ How to create a sinus wave in a Jupyter Notebook in Python?

This code imports the NumPy library and the IPython.display 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 Audio() function from the IPython.display 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.

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)

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:

πŸ‘‰ Recommended: How to Create a Beep in Python?

Step 3: Putting It All Together in a Jupyter Notebook

The following script for Jupyter Notebooks runs forever until the current Bitcoin price drops below a user-defined threshold. If it does, it issues an audio wave sound that makes you aware of the event.

!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)

You can change the threshold variable that is highlighted in the code above to control the price threshold that will cause the beep sound to play.

Try it yourself in my interactive Jupyter notebook here (Colab):

You can change the price data to Ethereum by using this function instead:

def get_price():
  data = LiveCryptoData('ETH-USD', verbose=False).return_data()
  return float(data['price'][0])

In a similar manner, this will also work for other crypto tickers or trading pairs.

Thanks! β™₯️

I loved having you here. Feel free to stay updated with all our programming projects and download your coding cheat sheets here: