![]() |
|
[Tut] I Made a Python Script That Beeps When BTC or ETH Prices Drop - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] I Made a Python Script That Beeps When BTC or ETH Prices Drop (/thread-100426.html) |
[Tut] I Made a Python Script That Beeps When BTC or ETH Prices Drop - xSicKxBot - 12-18-2022 I Made a Python Script That Beeps When BTC or ETH Prices Drop 4.5/5 – (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’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! [i]My Python Script if Crypto Prices Drop[/i]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. Code: Jupyter Notebook:
!pip install Historic-Crypto Shell or Terminal:
pip install Historic-CryptoSecond, create a new object of the Code: LiveCryptoData
Third, use the Code: LiveCryptoData(...).return_data()Code: dataCode: from Historic_Crypto import LiveCryptoData
data = LiveCryptoData('BTC-USD').return_data()
print(data)Output: 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.0028569Fourth, print the first element in the Code: priceCode: print(data['price'][0])
# 16722.21I’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 Code: IPython.displayCode: Audio()Code: IPython.displayCode: 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 Code: thresholdCode: !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 Code: thresholdTry it yourself in my interactive Jupyter notebook here (Colab): ![]() You can change the price data to Ethereum by using this function instead: Code: 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: |