Get live crypto prices with python

csdj92

csdj92

Posted on April 17, 2021

Get live crypto prices with python

Getting started

With all the craze around crypto coins I thought it would be a fun project to pull live prices from Binance.us. To first get started head over to https://www.binance.us/ to get signed up, from there hover over your user account and select API management. Give a name for your api project then copy and paste your api key and api secret to a text file, you only can view your secret once so make sure you save it in a good place.

The Code

First you need to pip install python-binance, from there

from binance.websockets import BinanceSocketManager
from binance.client import Client
client = Client(api_key, api_secret)
Enter fullscreen mode Exit fullscreen mode

Pass in your key and secret however you would like, then create the manager like so

bm = BinanceSocketManager(client)
# start any sockets here, i.e a trade socket
conn_key = bm.start_trade_socket('DOGEUSDT', process_message)
# then start the socket manager
bm.start()
Enter fullscreen mode Exit fullscreen mode

Almost there now all we need to do is create a callback to process the data

def process_message(msg):
    print("Current Price: {}".format(msg['p']))
Enter fullscreen mode Exit fullscreen mode

Now we can see the live price for Doge
Doge price

Read more here docs

💖 💪 🙅 🚩
csdj92
csdj92

Posted on April 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related