Returning user - Would need help understanding the issue


  • Hi Can anyone help connect to the WebSocket and sub-unsub and disconnect?

    I used to use Smartapi then focused more on other things now I came back to using APIs. Can anyone help me with a code that is useful to do the above tasks mentioned right now.

    This is my old code. Right now I can see my holdings but i cant fetch any real date. Tried connecting to IRCTC but I'm getting no output

    
    from smartapi import SmartConnect
    
    import pyotp
    import time
    
    username = ***
    apikey= ***
    pwd = ***
    token = ***
    
    try:
      obj = SmartConnect(apikey)
      totp = pyotp.TOTP(s='token')
    
      attempts = 5
      while attempts > 0:
        data = obj.generateSession(username, pwd, totp.now())
        attempts = attempts-1
    
        if data['status']:
          break
        time.sleep(2) 
      
      refreshToken= data['data']['refreshToken']
      feedToken=obj.getfeedToken()
      userProfile= obj.getProfile(refreshToken)
      print("Connecting to SMART API: {}".format(userProfile['message']))
    
    except:
      print("Connection failed")
    
    #%%
    print(obj.holding())
    
    Above code are working
    ---------------------
    
    #%%
    from smartapi import SmartWebSocket
    token = "nse_cm|13611"
    
    task = "mw"   # mw|sfi|dp
    
    ss = SmartWebSocket(feedToken, username)
    
    
    def on_message(ws, message):
        print("Ticks: {}".format(message))
    
    
    def on_open(ws):
        print("on open")
        ss.subscribe(task, token)
    
    
    def on_error(ws, error):
        print(error)
    
    
    def on_close(ws):
        print("Close")
    
    
    # Assign the callbacks.
    ss._on_open = on_open
    ss._on_message = on_message
    ss._on_error = on_error
    ss._on_close = on_close
    
    ss.connect()
    #%%
    

    Output getting right now

    __on_open################
    {'task': 'cn', 'channel': 'NONLM', 'token': '0895067909', 'user': 'my_username', 'acctid': 'my_account'}
    2023-02-07 11:39:13.684251 : Start task in the background
    on open
    {'task': 'hb', 'channel': '', 'token': '0895067909', 'user': 'my_username', 'acctid': 'my_account'}
    Ticks: [{'ak': 'nk', 'msg': 'cn', 'task': 'cn'}]Ticks: [{'ak': 'nk', 'msg': 'mw', 'task': 'mw'}]
    Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
    2023-02-07 11:39:43.688366 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': '0895067909', 'user': 'my_username', 'acctid': 'my_account'}
    

    Replaced my user name and accountid but this the error


  • @admin can you help me on this its been 20 days since I posted this and still getting the same output.