DEV Community

loading...

Get live crypto prices with python

csdj92 profile image csdj92 ・1 min read

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

Discussion (0)

pic
Editor guide