Navigation

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

    Python Sdk websocket not working

    Python SDK
    0
    1
    17
    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.
    • T
      Thiyaneswaran last edited by

      Hi Team,
      I am using python sdk WebSocket streaming code to get live ticks but we are not getting ticks. Sharing the sample code let us know is there any mistake from our side if not check from your end. With the same code login is working fine and in streaming is not working properly.

      # package import statement
      from smartapi import SmartConnect
      from smartapi import SmartWebSocket
       #or from smartapi.smartConnect import SmartConnect
      import pyotp
      #import smartapi.smartExceptions(for smartExceptions)
      
      #create object of call
      obj=SmartConnect(api_key="r8psjpm9")
                      #optional
                      #access_token = "your access token",
                      #refresh_token = "your refresh_token")
      
      #login api call
      
      data = obj.generateSession("T41918","****",pyotp.TOTP("****").now())
      print(data)
      refreshToken= data['data']['refreshToken']
      
      #fetch the feedtoken
      feedToken=obj.getfeedToken()
      
      #fetch User Profile
      userProfile= obj.getProfile(refreshToken)
      print(userProfile)
      #place order
      
      
      # feed_token=092017047
      print(feedToken)
      FEED_TOKEN=feedToken
      CLIENT_CODE="T41918"
      # token="mcx_fo|224395"
      token="nse_fo|41665"    #SAMPLE: nse_cm|2885&nse_cm|1594&nse_cm|11536&nse_cm|3045
      # token="mcx_fo|226745&mcx_fo|220822&mcx_fo|227182&mcx_fo|221599"
      task="mw"   # mw|sfi|dp
      
      ss = SmartWebSocket(FEED_TOKEN, CLIENT_CODE)
      
      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()
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post