Python Binance – Get price percentage change

In this post we will be creating a Python script that will retrieve the 24 hour price percentage change for all trading pairs using the Binance API. To do this we will be using the requests library which allows Python code to send HTTP requests and receive HTTP responses via the Binance API.

Interested in creating your own Binance account? Click here to sign up.

See the snippet of Python code below where we retrieve the price percentage change for all trading pairs Binance API. We start by defining our URL for the Binance API endpoint that returns price change data. Next we use the requests.get() function to send a HTTP GET request to the endpoint and retrieve its response. Now we call the json() method on the response object in order to convert it in to a Python dictionary. To access the price change for each pair, we iterate over the dictionary and print out the symbol on the Binance exchange and the percentage change.

import requests

url = "https://api.binance.com/api/v3/ticker/24hr"

response = requests.get(url)

if response.status_code == 200:
    data = response.json()
    for symbol in data:
        print(f"Symbol: {symbol['symbol']}, Price change: {symbol['priceChangePercent']}%")
else:
    print("Failed to retrieve data")

A sample of output can be seen below.

Symbol: LQTYBTC, Price change: -0.499%
Symbol: AMBUSDT, Price change: 12.000%
Symbol: BETHUSDT, Price change: 8.531%
Symbol: CFXTRY, Price change: 30.619%
Symbol: STXTRY, Price change: 24.226%
Symbol: USTCUSDT, Price change: 2.909%
>>> 

Interested in creating your own Binance account? Click here to sign up. Also be sure to visit our Python posts page by clicking here.

Leave a Reply