Here is my problem : I'm trying to get the price of three pairs of crypto on Binance using websocket
My problem is that I have difficulties in managing the fact that there are multiple streams (from the three pairs) whereas with just one it works perfectly well.
Here is my code:
import websocket, json, pprint, talib, numpy
cc = ["btcusdt","adausdt","solusdt"]
interval = "1m"
for i in cc:
socket = f'wss://stream.binance.com:9443/ws/{i}@kline_{interval}'
def on_message(ws, message):
json_message = json.loads(message)
candle = json_message['k']
close = candle["c"]
print(i,': ',close)
def on_close(ws):
print("Connection Closed")
wsapp = websocket.WebSocketApp(socket, on_message=on_message, on_close=on_close)
wsapp.run_forever()
This is the results that I'm getting each second :
btcusdt : 47009.01000000
btcusdt : 47009.02000000
btcusdt : 47004.00000000
...
However this is not what I want, it takes into account the first pair only. I am looking for this kind of result :
btcusdt : #price
adausdt : #price
solusdt : #price
btcusdt : #price
adausdt : #price
solusdt : #price
...
I know that my problem comes from the wsapp.run_forever() because by doing that it takes the first pair only without looping to the others. But I don't know how to manage it and I would be very glad if somebody has a solution on managing this issue.
Thank you very much