Navigation

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

    How to Unsubscribe Symbol from WebSocket?

    General Discussion
    3
    8
    248
    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.
    • D
      dineshpokharka last edited by

      Documentation mentions the Subscription for any Symbol / ticker by mentioning "task":"mw"

      How can i unsubscribe for any symbol? what should be the task value?

      K 1 Reply Last reply Reply Quote 0
      • K
        krunal96 @dineshpokharka last edited by

        Hi @raaxus ,

        Could you please explain more? What are you trying to achieve?

        punit 1 Reply Last reply Reply Quote 0
        • punit
          punit @krunal96 last edited by

          @vishant
          There should be a way to dynamically subscribe/un-subscribe ws feed for a token. Use case is: If I am watching a future derivative of a a index/EQ and want to enter a option position. After entering a option position, I need to watch the tick data of that option to dynamically set my SL/Target or exit.
          I can subscribe to all option data of that derivative, but that will be overkill and unnecessarily process unwanted data and wastes CPU cycles at both the ends.

          BR
          Dheepika

          K 1 Reply Last reply Reply Quote 0
          • K
            krunal96 @punit last edited by

            Hi @dheepi ,

            As per their Documentation there seems no functionality available to subscribe & unsubscribe on the fly.
            One possible solution I have applied to overcome this issue is to disconnect the existing ws connection & re-establish a new connection. (Though it's not the perfect one, but on temporary basis it's doable )
            The exact solution is to have a subscribe & unscubscribe methods to be available for any script.

            Thanks
            vishant

            punit 1 Reply Last reply Reply Quote 0
            • punit
              punit @krunal96 last edited by

              @vishant Thanks. Even before that I am struck with Python SDK and WS streaming. I did exactly like the sample code, but only getting heartbeats. Could you help me with the format of the token to pass?

              I am getting only below output;

              Ticks: [{'ak': 'nk', 'msg': 'cn', 'task': 'cn'}]
              Ticks: [{'ak': 'nk', 'msg': 'mw', 'task': 'mw'}]
              Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
              Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
              Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
              Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]
              Ticks: [{'ak': 'nk', 'msg': 'hb', 'task': 'hb'}]

              K 1 Reply Last reply Reply Quote 0
              • K
                krunal96 @punit last edited by

                Hi @dheepi ,

                Have you tried passing the correct value for the token which is mentioned in Doc?
                FYI: multiple token subscriptions will be as mentioned below:

                token="nse_cm|2885&nse_cm|1594&nse_cm|11536"
                

                Also, are you trying to get tick data during market hours / post-market hours? (Post-market hours you won't get any data)

                punit 1 Reply Last reply Reply Quote 0
                • punit
                  punit @krunal96 last edited by

                  @vishant I did exactly the same... and Yes I tried during market hours.

                  Below is my snippet

                  ## WebSocket
                  from smartapi import WebSocket
                  import greenstalk
                  import json
                  from datetime import datetime
                  
                  atFile  = open('/opt/kite/abToken.fi','r')
                  atData  = json.loads(atFile.read())
                  
                  tQ          = greenstalk.Client(('127.0.0.1', 11300))
                  FEED_TOKEN  = atData['feedToken']
                  CLIENT_CODE = 'xxxxxx'
                  token       = 'nse_cm|2885&nse_cm|1594&nse_cm|11536'
                  
                  ss = WebSocket(FEED_TOKEN, CLIENT_CODE)
                  
                  def on_tick(ws, tick):
                      print("Ticks: {}".format(tick))
                  
                      for i in tick:
                          ts          = datetime.now() 
                          i['sysTS']  = ts.strftime('%Y-%m-%d %H:%M:%S.%f')
                          i['scr']    = 'NFO'
                          tickJson    = json.dumps(i)
                          tQ.put(tickJson)
                  
                      for i in tick:
                          if 'hb' in i['task']:
                              print(i['ak'])
                          
                  def on_connect(ws, response):
                      ws.send_request(token)
                      print(ws)
                  
                  def on_close(ws, code, reason):
                      ws.stop()
                  
                  # Assign the callbacks.
                  ss.on_ticks = on_tick
                  ss.on_connect = on_connect
                  ss.on_close = on_close
                  
                  ss.connect( )
                  
                  K 1 Reply Last reply Reply Quote 0
                  • K
                    krunal96 @punit last edited by

                    @dheepi Checkout this thread that might help.

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