Renew Access Token

chinmay_23
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.
  • rakeshr
    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.
  • chinmay_23
    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

    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.
  • sujith
    It is mandatory by the exchange that a user has to login manually at least once a day. You need to login and generate an access token.
  • 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.
  • sujith
    Only for reconnection you will need to use new access token.
  • chinmay_23
    Thank You. That solved a major issue for me. :)
    Also i'll need to update access token everyday for placing orders and historical data right?
  • sujith
    Yes, you will need to login and create access token everyday.
  • chinmay_23
    chinmay_23 edited November 2021
    I had started a websocket connection yesterday and i didn't closed it. But this morning it was giving me this error

    Connection closed: 1006 - connection was closed uncleanly >

    @sujith
  • chinmay_23
    Connection closed: 1006 - connection was closed uncleanly >
  • sujith
    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.
  • chinmay_23
    I'm using time.sleep() in the main thread at the close of the market till the next day. That might be the reason the connection broke?
  • rakeshr
    Connection closed: 1006 - connection was closed uncleanly >
    This is not the complete error trace-back. Can you paste here the complete error traceback? It would have logged an error message reason as well.
  • chinmay_23
    chinmay_23 edited November 2021
    Connection closed: 1006 - connection was closed uncleanly > Websocket Connection Upgrade Failed 403 - Forbidden

    It was giving this error again and again today
  • rakeshr
    Websocket Connection Upgrade Failed 403
    Go through websocket streaming FAQs here.
  • chinmay_23
    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?
  • rakeshr
    sleep(sleep_time)
    continue
    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.
  • chinmay_23
    chinmay_23 edited November 2021

    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

    elif (datetime.now().time() < time(9,45)):
    if (time(9,44,30) <datetime.now().time() < time(9,45)):
    self.login()
    continue

    elif (datetime.now().time() > time(15,0)):
    continue

    token = tick['instrument_token']
    ticker = self.token[token]
    price = tick['last_price']

    # Strategy is impemented after this

    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()
    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

  • rakeshr
    elif (datetime.now().time() < time(9,45)):
    if (time(9,44,30) <datetime.now().time() < time(9,45)):
    self.login()
    continue
    You don't need to perform login again for above condition.
    You can do something like:
    def on_ticks(ws, ticks):
    for tick in ticks:
    # Your strategy implementation
    Perform this time-based inspection on another callable function.

Sign In or Register to comment.