async def main(): client = await AsyncClient.create() socket_manager = BinanceSocketManager(client) # start any sockets here, i.e a trade socket kline = socket_manager.kline_socket( order_symbol) # .trade_socket('BNBBTC') # then start receiving messages async with kline as tscm: while True: response = await tscm.recv() price = float(response["k"]["c"]) pourcentage_change = percent_change( float(order_price), price) profit_in_buy = buy_order and pourcentage_change >= TAKE_PROFIT profit_in_sell = not buy_order and -pourcentage_change >= TAKE_PROFIT profit = profit_in_buy or profit_in_sell loss_in_buy = buy_order and -pourcentage_change >= STOP_LOSS loss_in_sell = not buy_order and pourcentage_change >= STOP_LOSS loss = loss_in_buy or loss_in_sell if profit or loss: cout("tracking ended") self.profit_change(pourcentage_change) break else: cout( f"price:{price} - profit:{round(pourcentage_change,2)} - all time profit : {round(self.profit,2)}" + " - still waiting...") time.sleep(2.5) await client.close_connection()
async def kline_listener(client): bm = BinanceSocketManager(client) symbol = symbolo res_count = 0 async with bm.kline_socket(symbol=symbol) as stream: while True: res = await stream.recv() res_count += 1 print(res) if res_count == 5: res_count = 0 loop.call_soon(asyncio.create_task, order_book(client, symbol))