Navigation

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

    Working WebSocket Sample code to retrieve LTP - Python

    Python SDK
    7
    20
    752
    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.
    • James Bond
      James Bond last edited by James Bond

      Here is the working WebSocket sample code to retrieve LTP:

      from smartapi import SmartConnect 
      from smartapi import WebSocket 
      
      obj=SmartConnect(api_key="your_secret_key")
      data = obj.generateSession('your_client_id', 'your_password')
      feedToken=obj.getfeedToken()
      
      FEED_TOKEN= feedToken
      CLIENT_CODE= 'your_client_id'
      token="nse_cm|26009"
      
      WS = WebSocket(FEED_TOKEN, CLIENT_CODE)
      
      def on_tick(ws, tick):
          for i in tick:
              for j, k in i.items():
                  if j == 'ltp':
                      print(j, k)
                  else:
                      pass
      
      def on_connect(ws, response):
          ws.send_request(token)
      
      def on_close(ws, code, reason):
          ws.stop()
      
      # Assign the callbacks.
      WS.on_ticks = on_tick
      WS.on_connect = on_connect
      WS.on_close = on_close
      WS.connect()
      
      D James Bond L 4 Replies Last reply Reply Quote 0
      • D
        djdenish @James Bond last edited by

        @James-Bond
        help me with this : https://smartapi.angelbroking.com/topic/376/invalid-refresh-token-error-code-ab8050

        James Bond 1 Reply Last reply Reply Quote 0
        • James Bond
          James Bond @djdenish last edited by James Bond

          @djdenish
          Hello @djdenish, I found some basic flaws in official documentation about complete login flow.

          Both Login and Token generation python scripts returning the same response at my end.

          Still I'll check everything one more time and get back to you.

          Bye

          1 Reply Last reply Reply Quote 0
          • James Bond
            James Bond @James Bond last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • D
              djdenish @James Bond last edited by djdenish

              @James-Bond

              def on_tick(ws, tick):
              for i in tick:
              for j, k in i.items():
              if j == 'ltp':
              print(j, k)
              else:
              pass

              If j is ltp and k is the value of ltp then Instead of just LTP, I want to print Open,Close,High,Low,Bid Price, Ask Price. Any idea how?

              S 1 Reply Last reply Reply Quote 0
              • L
                lavly @James Bond last edited by

                @James-Bond this code is giving following errorrs


                ReactorNotRestartable Traceback (most recent call last)
                <ipython-input-71-f3a22d8e176e> in <module>()
                21 ss.on_close = on_close
                22
                ---> 23 ss.connect( )

                3 frames
                /usr/local/lib/python3.7/dist-packages/twisted/internet/base.py in startRunning(self)
                841 raise error.ReactorAlreadyRunning()
                842 if self._startedBefore:
                --> 843 raise error.ReactorNotRestartable()
                844 self._started = True
                845 self._stopped = False

                ReactorNotRestartable:

                James Bond 1 Reply Last reply Reply Quote 0
                • S
                  Surya 1 @djdenish last edited by

                  @djdenish

                  you can look at the key value and its corresponding explanation here

                  https://smartapi.angelbroking.com/docs/WebSocket

                  1 Reply Last reply Reply Quote 0
                  • James Bond
                    James Bond @lavly last edited by

                    Hey @djdenish, did above code give you same error @lavly facing while you executes the code. ?

                    L D 2 Replies Last reply Reply Quote 0
                    • L
                      lavly @James Bond last edited by

                      @James-Bond yes

                      S 1 Reply Last reply Reply Quote 0
                      • S
                        Surya 1 @lavly last edited by

                        @lavly what's your sdk version ??

                        1 Reply Last reply Reply Quote 0
                        • S
                          sudhakar last edited by

                          is there any way to get low, high , ltp and volumes, & market watch (best 5 bids & ask)?

                          S 1 Reply Last reply Reply Quote 0
                          • S
                            Surya 1 @sudhakar last edited by

                            @sudhakar

                            @sudhakar said in Working WebSocket Sample code to retrieve LTP - Python:

                            is there any way to get low, high , ltp and volumes, &

                            this you get it in WebSocket..

                            @sudhakar said in Working WebSocket Sample code to retrieve LTP - Python:

                            market watch (best 5 bids & ask)

                            yes via api call.. rate limit is 1 call per seconds if I remember right..

                            S 1 Reply Last reply Reply Quote 0
                            • S
                              sudhakar @Surya 1 last edited by

                              @rajanprabu , Can you help with any sample code , API guide is use less,it does not have anything.

                              A S 2 Replies Last reply Reply Quote 0
                              • A
                                admin @sudhakar last edited by

                                @sudhakar please provide us your contact details to assist you.

                                1 Reply Last reply Reply Quote 0
                                • S
                                  Surya 1 @sudhakar last edited by

                                  @sudhakar

                                  @admin and team is very helpful and they will reach out to you.

                                  @James-Bond has compiled some nice set of instructions and codes here which you can look

                                  https://smartapi.angelbroking.com/topic/325/faq-smart-api/5

                                  I will write a complete page with sample scripts sometime soon..

                                  N 1 Reply Last reply Reply Quote 0
                                  • D
                                    djdenish @James Bond last edited by

                                    @James-Bond
                                    No I am not getting any error on code provided above.
                                    Its working fine.

                                    1 Reply Last reply Reply Quote 0
                                    • N
                                      neeluru @Surya 1 last edited by

                                      @rajanprabu
                                      Any idea on below error. Using google.colab

                                      92cf3176-9985-4fd2-888a-b49740475963-image.png

                                      S 1 Reply Last reply Reply Quote 0
                                      • S
                                        Surya 1 @neeluru last edited by

                                        @neeluru

                                        I don't use google colab so its difficult to comment. which versions of python sdk r u using ??

                                        N 1 Reply Last reply Reply Quote 0
                                        • N
                                          neeluru @Surya 1 last edited by

                                          @rajanprabu
                                          OK..

                                          Is code is correct one ?

                                          have used ss.connect() instead ws.connect(). Have changed it but no use at all

                                          S 1 Reply Last reply Reply Quote 0
                                          • S
                                            Surya 1 @neeluru last edited by

                                            @neeluru

                                            Please share which version of SDK you are using.. I can only check the code depending on that..

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