Websocket sleep() problem

Hi, I’d like to ask for your help. I am using ws and I would also like to use sleep() in my strategy. Unfortunately, after each use of sleep() I get a delayed message from ws. Any idea what to do about it? Thank you.

from time import sleep
import websocket, datetime, pprint, json, config
import numpy as np
from binance.enums import *
from binance.client import Client

client = Client(config.api_key, config.api_secret)

SymoblTrading = ‘BTCUSDT’
symbol = ‘btcusdt’

ask = None
high = None
highs =

def on_message(ws, message):
print(str(datetime.datetime.now()) + ": ")
json_message = json.loads(message)
print(json_message)

if 'asks' in json_message:
    global ask
    ask_string = json_message['asks'][0][0]
    ask = float(ask_string)
    print("Ask")
    print(ask)



if 'h' in json_message:
    global high
    high_string = json_message['h']
    high = float(high_string)
    print("High")
    print(high)


if ask is not None and high is not None:
    sleep(30)

def on_error(ws, error):
print(error)

def on_close(ws, close_msg):
print("### closed ###" + close_msg)

def streamKline(symbol):
websocket.enableTrace(False)
socket = f’wss://stream.binance.com:9443/ws/{symbol}@ticker_1h/{symbol}@depth5

ws = websocket.WebSocketApp(socket,
                            on_message=on_message,
                            on_error=on_error,
                            on_close=on_close)
ws.run_forever()

streamKline(symbol)

It’s not sure about what you are trying to do? How about not sleep?

I use Sleep as a trigger to sell at the exact time after buy. Any idea how else to sell at the exact time after the buy?

        order = client.order_market_buy(symbol='BTCUSDT', quantity=quant)

        print(order)
        print()

        sleep(Exit_in_time)    

        order = client.order_market_sell(symbol='BTCUSDT', quantity=quant)