Navigation

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

    Websocket 2.0

    Python SDK
    0
    17
    747
    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.
    • S
      Surya 1 @Surya 1 last edited by

      @admin and Team,

      I had spent sometime in python SDK to see whats the issue. In the SmartWebSocketV2 file in GitHub (https://github.com/angel-one/smartapi-python/blob/AMX-3393-Smart-Api-Python-Library/SmartApi/smartWebSocketV2.py )

      line 310 that is converting byte string to token is not working. If I simply comment it out and give the token number directly it can now print Ticks.

       def _parse_binary_data(self, binary_data):
      
              try:
                  parsed_data = {
                      "subscription_mode": self._unpack_data(binary_data, 0, 1, byte_format="B")[0],
                      "exchange_type": self._unpack_data(binary_data, 1, 2, byte_format="B")[0],
                      #"token": SmartWebSocketV2._parse_token_value(binary_data[2:27]),
                       "token": 234876,
                      "sequence_number": self._unpack_data(binary_data, 27, 35, byte_format="q")[0],
                      "exchange_timestamp": self._unpack_data(binary_data, 35, 43, byte_format="q")[0],
                      "last_traded_price": self._unpack_data(binary_data, 43, 51, byte_format="q")[0]
                  }
      

      lines 353 to 360. This functions needs a re look.

          @staticmethod
          def _parse_token_value(binary_packet):
              token = ""
              for i in range(len(binary_packet)):
                  if binary_packet[i] == b'\x00':
                      return token
                  token += binary_packet[i].encode("UTF-8")
              return token
      

      I could re write the function but I would request the team to have a look .

      A J Z A 4 Replies Last reply Reply Quote 3
      • A
        admin @Surya 1 last edited by admin

        Hi @rajanprabu , thank you so much for the fix. Request you to please raise the PR, I'll get my tech team to review and approve the same. Open source contributions from experts and loyal SmartAPI users like you will help us to maintain the SDKs. We are exploring few options on the support for SDKs and shall restore the support soon. Thank you for your contribution again.

        K 1 Reply Last reply Reply Quote 1
        • J
          J88913 @Surya 1 last edited by

          @rajanprabu sir can u share the correct function so that we can use it? Waiting for @admin to correct it will take a long time for them as their support is very bad.

          1 Reply Last reply Reply Quote 0
          • V
            vivekjha05 last edited by vivekjha05

            Seems the token variable is not coming in the right data type, hence the issue. It did work for me with some tweaking, checking the changes that I tried 🙂

            V 1 Reply Last reply Reply Quote 0
            • V
              vivekjha05 @vivekjha05 last edited by vivekjha05

              @vivekjha05 Here's the changes:

              def _parse_token_value(binary_packet):
                  token = ""
                  for i in range(len(binary_packet)):
                      if chr(binary_packet[i]) == '\x00':
                          return token
                      token += chr(binary_packet[i])
                  return token
              
              1 Reply Last reply Reply Quote 0
              • Z
                zubuco @Surya 1 last edited by

                @rajanprabu
                not working

                1 Reply Last reply Reply Quote 0
                • K
                  Karthikeyan @admin last edited by

                  @admin Hi @admin

                  I have updated the code like below to convert the bytes to string and I can see the response.

                  def _parse_binary_data(self, binary_data):
                      try:
                          parsed_data = {
                              "subscription_mode": self._unpack_data(binary_data, 0, 1, byte_format="B")[0],
                              "exchange_type": self._unpack_data(binary_data, 1, 2, byte_format="B")[0],
                              # "token": SmartWebSocketV2._parse_token_value(binary_data[2:27]),
                              "token": binary_data[2:27].decode("utf-8").replace("\x00",""),
                              "sequence_number": self._unpack_data(binary_data, 27, 35, byte_format="q")[0],
                              "exchange_timestamp": self._unpack_data(binary_data, 35, 43, byte_format="q")[0],
                              "last_traded_price": self._unpack_data(binary_data, 43, 51, byte_format="q")[0]
                          }
                  

                  Got connected to server...
                  on open
                  Ticks: {'subscription_mode': 3, 'exchange_type': 1, 'token': '26009', 'sequence_number': 6346734, 'exchange_timestamp': 1668667371000, 'last_traded_price': 4245320, 'subscription_mode_val': 'SNAP_QUOTE', 'last_traded_quantity': 0, 'average_traded_price': 0, 'volume_trade_for_the_day': 0, 'total_buy_quantity': 0.0, 'total_sell_quantity': 0.0, 'open_price_of_the_day': 0, 'high_price_of_the_day': 0, 'low_price_of_the_day': 0, 'closed_price': 4253530, 'last_traded_timestamp': 0, 'open_interest': 31252900, 'open_interest_change_percentage': 4626580563041346420, 'upper_circuit_limit': 0, 'lower_circuit_limit': 0, '52_week_high_price': 0, '52_week_low_price': 0, 'best_5_buy_data': [{'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}], 'best_5_sell_data': [{'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}]}
                  Ticks: {'subscription_mode': 3, 'exchange_type': 1, 'token': '26009', 'sequence_number': 6347286, 'exchange_timestamp': 1668667372000, 'last_traded_price': 4245150, 'subscription_mode_val': 'SNAP_QUOTE', 'last_traded_quantity': 0, 'average_traded_price': 0, 'volume_trade_for_the_day': 0, 'total_buy_quantity': 0.0, 'total_sell_quantity': 0.0, 'open_price_of_the_day': 0, 'high_price_of_the_day': 0, 'low_price_of_the_day': 0, 'closed_price': 4253530, 'last_traded_timestamp': 0, 'open_interest': 31252900, 'open_interest_change_percentage': 4626580563041346420, 'upper_circuit_limit': 0, 'lower_circuit_limit': 0, '52_week_high_price': 0, '52_week_low_price': 0, 'best_5_buy_data': [{'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 0, 'quantity': 0, 'price': 0, 'no of orders': 0}], 'best_5_sell_data': [{'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}, {'flag': 1, 'quantity': 0, 'price': 0, 'no of orders': 0}]}

                  S D 2 Replies Last reply Reply Quote 1
                  • A
                    ashokkmr22 @Surya 1 last edited by

                    @rajanprabu

                    Below code will work. Use it for time being.

                    @staticmethod
                        def _parse_token_value(binary_packet):
                            token = ""
                            token_data = binary_packet.split(b'\x00')
                            for split in token_data:
                                if split:
                                    token = split.decode("UTF-8")
                            return token
                    
                    D 1 Reply Last reply Reply Quote 1
                    • A
                      ashokkmr22 last edited by

                      Below code will work use it for time being

                      @staticmethod
                          def _parse_token_value(binary_packet):
                              token = ""
                              token_data = binary_packet.split(b'\x00')
                              for split in token_data:
                                  if split:
                                      token = split.decode("UTF-8")
                              return token
                      
                      T 1 Reply Last reply Reply Quote 1
                      • S
                        smitshahfo @Karthikeyan last edited by

                        @Karthikeyan
                        @rajanprabu
                        @ashokkmr22

                        Thank you for the fix,

                        Can you please help me to get time stamp in datetime format.
                        'exchange_timestamp': 1668667371000.
                        Also prices (OHLC) into their required formats.

                        Thanks in advance.

                        punit T 2 Replies Last reply Reply Quote 0
                        • punit
                          punit @smitshahfo last edited by

                          @vrkhyd commented you on another thread

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

                            @rajanprabu same here now. any solution??

                            1 Reply Last reply Reply Quote 1
                            • T
                              tanmoydev @ashokkmr22 last edited by

                              @ashokkmr22 thanks for your convenience

                              1 Reply Last reply Reply Quote 0
                              • T
                                tanmoydev @smitshahfo last edited by

                                @vrkhyd

                                import datetime

                                timestamp = 1671097663
                                dt_obj = datetime.datetime.fromtimestamp(timestamp)

                                print("date_time:",dt_obj)
                                print("type of dt:",type(dt_obj))

                                1 Reply Last reply Reply Quote 0
                                • D
                                  deepu1977 @ashokkmr22 last edited by

                                  @ashokkmr22 said in Websocket 2.0:

                                  split.decode("UTF-8")
                                  return token

                                  I tried this and did not worked. Can you please suggest alternate option?

                                  1 Reply Last reply Reply Quote 0
                                  • D
                                    dharm0072 @Karthikeyan last edited by

                                    @Karthikeyan this is not working
                                    please help

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