LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
678 stars 166 forks source link

How to start websocket from python #214

Closed yoobi closed 2 years ago

yoobi commented 2 years ago

Hello, I'm trying out Telethon and your application while learning Python. Is it possible to start the websocket from the same file as my telethon code ?

telethon.py

async def main():
    initValues()

with client:
    client.loop.run_until_complete(main())

client.start()
client.run_until_disconnected()

Websocket.py

binance_websocket = BinanceWebSocketApiManager(exchange="binance.com")
binance_stream_id = binance_websocket.create_stream(channels=['!userData'],
                                                    markets=['arr'],
                                                    stream_buffer_name=True,
                                                    api_key='API_KEY',
                                                    api_secret='API_SECRET')

worker_thread = threading.Thread(target=print_stream_buffer_data, args=(binance_websocket, binance_stream_id))
worker_thread.start()

while True:
    time.sleep(1)

So far i'm doing

python3 telethon.py
python3 websocket.py

But I need both file to be able to communicate between them and I'm kind of stuck Thanks for your help

oliver-zehentleitner commented 2 years ago

Sure, you can run everything in just one file.

Play around with the commands you want to use and remove the "while True:" block and add the defention of print_stream_buffer_data().

yoobi commented 2 years ago

Okay I've managed to make it work thank you !