sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.96k stars 2.19k forks source link

Leaving user_data_stream listening to get back to main and loop on main #1226

Open Meomeoowww opened 2 years ago

Meomeoowww commented 2 years ago

Describe the bug I am using the following code to execute some data preparations first and create an order first, listen to user_data_stream to update my trades and once a limit hit, close my trades and get out of listening and start again the main. I use the following code :

To Reproduce

async def main():

client = await AsyncClient.create(binance_api_k, binance_api_sk)

client = Client(binance_api_k, binance_api_sk)
asynclient = await AsyncClient.create(binance_api_k, binance_api_sk)
_dict_orders, symbol = rtrade(client, leverage)

bm = BinanceSocketManager(asynclient)
# start any sockets here, i.e a trade socket
ts = bm.futures_user_socket()
# then start receiving messages
async with ts as tscm:
    while True:
        message = await tscm.recv()
        await rtrailmeo(message, symbol, client, _dict_orders, asynclient)
        #print(res)

await asynclient.close_connection()

if name == "main": loop = asyncio.get_event_loop() loop.run_until_complete(main())

Expected behavior

I was expecting that the first part will execute and start listening to user_data until my take profit is hit. From there, all other opened orders are deleted manually. It does delete everything once a particular message is received, but I am still stuck in the user_data_stream instead of closing the user stream and restart the main to track other signals.

Environment (please complete the following information):

Logs or Additional context p.s. :