DEV Community

csdj92
csdj92

Posted on

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

Top comments (1)

Collapse
 
syedirfanhussaini profile image
Syed Irfan Hussaini

Cannot import this in new version

from binance.websockets import BinanceSocketManager
Enter fullscreen mode Exit fullscreen mode