How to Code a Bitcoin Price Tracker Using Python
![](/sites/default/files/styles/large/public/images/oretnom23/how-to-code-bitcoin-price-tracker-using-python_0.jpg?itok=OYRDYg8J)
In the frenetic field of cryptocurrencies, information is the key. Also, knowing how live prices move is something that most hobbyists need to do, such as trading and investing. Hold on! Python is even cooler! Today, we’ll just learn how to track Bitcoin price and other cryptocurrencies on our own and how to integrate them with finance and technology!
Why Choose Python?
So, what is Python? Well, it’s a high-level, interpreted general-purpose programming language, and for the past few years, it’s been gaining traction worldwide. Why? You might ask simply because it’s readable and has clean syntax. Cool… but why the heck you should choose Python among numerous languages? Well, are you even able to write beautiful, practical, and extremely fast-running code you can without that big developing community, a couple of packages, and a framework?
What You Will Need
- Python In Your Machine: The latest version you can have is version 3.6, and anything that came after installed on your machine. You can get it from the Python website.
- Libraries: You will require `requests` to make API calls and `json` to parse the response. Install the requests library using pip:
pip install requests
- An API Key: We'll be requesting the Bitcoin price data from Binance API. Registration on Binance is quite easy, and you will be able to generate the API key.
Understanding the Binance API
Binance is the top crypto exchange in the world by volume, and it offers a robust API to get market data. The endpoint to get the Bitcoin price that we'll be using looks like this:
https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT
The code will result in giving you the current price of Bitcoin in USDT which is Tether, a stablecoin linked to USD.
Step-by-Step Code
Step 1: Import Libraries
Start by importing the necessary libraries:
- import requests
- import json
- import time
Step 2: Define a Function to Get Bitcoin Price
Now, let’s create a function that will fetch the current Bitcoin price:
- def get_btc_price():
- url = 'https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT'
- response = requests.get(url)
- if response.status_code == 200:
- data = json.loads(response.text)
- return float(data['price'])
- else:
- return None
Step 3: Display the Price
Ok, so now that you have written this code, you should be able to keep an eye on the price any time you want to! That is what all this was about! Here’s how to do that:
Step 4: Main Function
Finally, let’s create a main function that will run our display_price function:
- if __name__ == "__main__":
- display_price()
Step 5: Run the Tracker
Go ahead and run the code in your terminal. You should see the updated Bitcoin price every 5 seconds! Isn't this a cool way to keep track of crypto prices?
Your Very Own Python Bitcoin Price Tracker
Bitcoin price today is very unstable, so you should always keep an eye on it — do it with your Python price tracker not only cause you can, but it’s really necessary. And now you can, too — code it by yourself and watch your investment grow.
Add new comment
- 94 views