i'm using a bnh strategy and my code need to be running all time long, which means i need to update the access token from the on_ticks function. Any idea on how i can do that.
You need to generate access_token once at the start of the trading day and re-use it throughout the day. Go through this thread to know more about access_token flush timings.
Hi @rakeshr i know that we can use the same access token for that day. But for the next day we are in the on_ticks function of the websocket where ticks are streamed. And i have to update access token how can i do that.
def run(self):
def on_ticks(ws, ticks):
for tick in ticks:
# Checking For Holidays or Weekends if (datetime.today().weekday() == 5) or (datetime.today().weekday() == 6) or (datetime.now().date() in self.holidays): dt_end = datetime.now().replace(hour = 9,minute = 15) dt_now = datetime.now() sleep_time = (dt_end - dt_now).seconds sleep(sleep_time) continue
If it is not disconnected then next day, Websocket won't close existing connection. You will continue to receive ticks even after access token is expired.
You need to make sure the main thread is not blocked. If the connection disconnects then you will need a new access token to reconnect and for that you will need to go through login process.
Hi @rakeshr i went through the FAQ, there was a thread regarding the error i faced, you mentioned that the access token i used was invalid but as mentioned above by @sujith ' If it is not disconnected then next day, Websocket won't close existing connection. You will continue to receive ticks even after access token is expired. ' Can you please help, Why that might be happening?
Just remove sleep from on_tick main thread. Python WebSocket will keep the connection active. You can check the client implementation here. Also, make sure, there is no other reason for Websocket disconnection like local network, the killing of WebSocket program, etc.
def login(self): # Logging in kite API kite_login = api_login() self.kite = kite_login.get_kite() self.kws = kite_login.get_kws() self.access_token = kite_login.get_access_token() print('Kite API Login Successful')
def on_ticks(ws, ticks): for tick in ticks: # Checking For Holidays or Weekends if (datetime.today().weekday() == 5) or (datetime.today().weekday() == 6) or (datetime.now().date() in self.holidays): continue
I want my algo to start after 9:45 AM I'm not using any sleep function here. Still it gave me this error. I need help urgently.
Error i faced the next day : Connection closed: 1006 - connection was closed uncleanly <Websocket Connection upgrade failed<403 : Forbidden>> @sujith@rakeshr
def run(self):
def on_ticks(ws, ticks):
for tick in ticks:
# Checking For Holidays or Weekends
if (datetime.today().weekday() == 5) or (datetime.today().weekday() == 6) or (datetime.now().date() in self.holidays):
dt_end = datetime.now().replace(hour = 9,minute = 15)
dt_now = datetime.now()
sleep_time = (dt_end - dt_now).seconds
sleep(sleep_time)
continue
elif (datetime.now().time() < time(9,45)):
dt_now = datetime.now()
dt_end = datetime.now().replace(hour=9,minute=45,second=0,microsecond=0)
sleep_time = (dt_end - dt_now).seconds
sleep(sleep_time)
self.hour = datetime.now().replace(hour=9,minute=15,second=0,microsecond=0)
# def renew_access_token(self, refresh_token, self.api_secret)
self.login()
continue
token = tick['instrument_token']
ticker = self.token[token]
price = tick['last_price']
def on_connect(ws, response):
ws.subscribe(self.tokens)
ws.set_mode(ws.MODE_FULL,self.tokens)
self.kws.on_ticks = on_ticks
self.kws.on_connect = on_connect
self.kws.connect()
Take the above given example as reference.
You will continue to receive ticks even after access token is expired.
Also i'll need to update access token everyday for placing orders and historical data right?
Connection closed: 1006 - connection was closed uncleanly >
@sujith
It was giving this error again and again today
@sujith ' If it is not disconnected then next day, Websocket won't close existing connection. You will continue to receive ticks even after access token is expired. '
Can you please help, Why that might be happening?
I'm not using any sleep function here. Still it gave me this error. I need help urgently.
Error i faced the next day :
Connection closed: 1006 - connection was closed uncleanly <Websocket Connection upgrade failed<403 : Forbidden>>
@sujith @rakeshr
You can do something like: Perform this time-based inspection on another callable function.