Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    Returning user - Would need help understanding the issue

    Python SDK
    0
    2
    35
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      Vignesha last edited by Vignesha

      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

      V 1 Reply Last reply Reply Quote 1
      • V
        Vignesha @Vignesha last edited by

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

        1 Reply Last reply Reply Quote 0
        • First post
          Last post