Here’s a step by step that helps you manage multiply webshckets to get to the crypto prise on Binance:
Prerequisites
- You have account and you’ll be the app using
app_id
and asecret_id
.
- You are installed the “Rerectory Instant Resorts of the “WebSocket-Clients” in Python project. If not, you can instal not using the “PIP Install WebSocket-Client”.
Code
Here’s an example code that will show you to connect to three differentiate webSocket streams at Bitcoin (BTC), ETHUUM (LTC) (LTC):
`Python
Import WebSocket
import time
Replace API Creditials
Api_id = “your_apu_id”
Api_Secret = “Your_api_Secret”
Repace the creek URLs
stream_urls =
“WSS: //binnance.com/ws? Symbol = Btcusdt & Type = Limit & Stream = Order_Book & Limit = 100”,
Bitcoin
“WSS: //binnance.us/api/v3 WebSocket API (WebSocket+Limit+24h)? Sumby = Type = Com_Book & Stream = Order_Book & Limit = 10”,
Ethereum
“WSS: //binnance.com/ws? Symbol = ltcusdt & type = limit & stream = len
]
Initiial WebSocket Connections
Ws_btc = websocket.create_connection (stream_urls [0])
Ws_eth = websocket.create_connection (stream_urls [1])
Ws_ltc = websocket.create_connection (stream_urls [2])
Features to Exchange Rates
Def update_price_stream (symbol, webSocket):
Although True:
try:
Get the symbols current order book
Answer = WebSocket.rev ()
Data = json.loads (reply)
price = float (data [“price”])
Print (F “{symbol}: {price: .2f}”)
Except.
Print (F “Error: {E}”)
Start your exchange rate uploads
Ws_btc.update_price_stream (“btcusdt”)
ws_eth.update_price_stream (“Etusdt”)
Ws_ltc.update_price_stream (“ltcusdt”)
Rn indefinitely until stops
Although True:
Time.sleep (60)
`
Explanation
This code is related to three webSocket stream of Binance at Bitcoin (BTC), ETHEREUM (LTC) and Litecoin (LTC). It’s a “Update_price_stream” feature, a which will request the current Price off the data stream every minute. The feature uses are WebSocket.rekk () meat to receive the WebSocket Contacts the Data to the currt order book in JSON format.
The the ones in which thews_create_connnection () met and start-ups”
Note
: Bee sure to replace “your_api_id` and “ Remeber’s own this is simplfied example and it will give you the addi ional handling and logging treatment.
Hope this helps! Tell me if you have any questions or need addion help.
Leave a Reply