How to Code a Bitcoin Price Tracker Using Python

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

  1. 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.
  2. Libraries: You will require `requests` to make API calls and `json` to parse the response. Install the requests library using pip:
    pip install requests
    
  3. 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:

  1. import requests
  2. import json
  3. import time

Step 2: Define a Function to Get Bitcoin Price

Now, let’s create a function that will fetch the current Bitcoin price:

  1. def get_btc_price():
  2.     url = 'https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT'
  3.     response = requests.get(url)
  4.     if response.status_code == 200:
  5.         data = json.loads(response.text)
  6.         return float(data['price'])
  7.     else:
  8.         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:

def display_price():     print("Real-time Bitcoin Price Tracker (BTC/USDT)n")     while True:         price = get_btc_price()           if  price is not None:             print(f"Current Bitcoin Price: ${price:,.2f}")         else:             print("Could not retrieve Bitcoin price.")            time.sleep(5)  # Refresh every 5 seconds

Step 4: Main Function

Finally, let’s create a main function that will run our display_price function:

  1. if __name__ == "__main__":
  2.     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