Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs
    1. Home
    2. Ramesh
    3. Best
    R
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 13
    • Best 1
    • Groups 0

    Best posts made by Ramesh

    • Unable to get to tick data

      @admin , I used to get the tick data by using the below code, in the recent past it was NOT providing the tick data, can you pl help me know if anything needs to be updated?

      from smartapi import SmartConnect # or from smartapi.smartConnect import SmartConnect
      #from smartapiwebsocket import SmartWebSocket
      from smartapi import SmartWebSocket

      ANGEL_API_KEY = "XXXXXX"
      #ANGEL_SEC_KEY = "XXXXX"
      TOTP="XXXXX"

      ANGEL_CLIENT_ID = "XXXXXX"
      ANGEL_PASSSWORD = "XXXXX"

      refreshToken = None
      feedToken = None
      smartObj = None

      tokens = None
      ss = None
      task = None

      def smartApiInitialization():
      global refreshToken
      global feedToken
      global smartObj

      try:
      

      smartObj = SmartConnect(api_key=ANGEL_API_KEY)
      totp = pyotp.TOTP(TOTP) #'replace with Text that appears below QR code'
      totp = totp.now()
      print("pasrse: ",totp)
      attempts = 5
      while attempts > 0:
      attempts = attempts - 1

      data = self.smartObj.generateSession(ANGEL_CLIENT_ID,ANGEL_PASSSWORD,totp)
      print(data)
      if data['status']:
      break
      time.sleep(2)

      refreshToken = data['data']['refreshToken']
      print(refreshToken)

      fetch the feedtoken

      feedToken = self.smartObj.getfeedToken()
      print(feedToken)
      time.sleep(1)

      except Exception as ex:

      print("Exception in smartApiInitialization method : {}".format(str(ex)))

      def on_message(ws, message):

      print("Ticks: {}".format(message))
      

      def on_open(ws):
      global tokens
      global task
      global ss

      print("on open")
      ss.subscribe(task, tokens)
      

      def on_error(ws, error):
      print(error)

      def on_close(ws):
      print("Close")

      def initialize_websocket():
      global tokens
      global ss
      global task
      global feedToken

      try:
      
          tokens = "nse_cm|26000&nse_cm|26009"
          task = "mw"
          ss = SmartWebSocket(feedToken, ANGEL_CLIENT_ID)
      
          # 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()
      
      except Exception as ex:
          print('exception in initialize_websocket : {}'.format(ex))
      

      if name == 'main':

      smartApiInitialization()
      initialize_websocket()
      
      posted in Python SDK
      R
      Ramesh