Getting error for script thread_ticker.py

tusharwagh10
tusharwagh10 edited July 2021 in Python client
I am getting following error. My script is attached in the attachment section.

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/kiteconnect/ticker.py in subscribe(self, instrument_tokens)
569 self.ws.sendMessage(
--> 570 six.b(json.dumps({"a": self._message_subscribe, "v": instrument_tokens}))
571 )

7 frames
TypeError: Object of type int64 is not JSON serializable

During handling of the above exception, another exception occurred:

Exception Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/autobahn/websocket/protocol.py in sendClose(self, code, reason)
1989 if reason is not None:
1990 if code is None:
-> 1991 raise Exception("close reason without close code")
1992
1993 if type(reason) != six.text_type:

Exception: close reason without close code
  • rakeshr
    We tested your same code at our end, it's working fine as desired. Are you trying to use pandas data type as input in Websocket subscription?
  • tusharwagh10
    I am not using pandas datatype. I am using list. But values in list are taken from pandas dataframe.
    In attachment code, subscription list is hardcoded (given directly) but in real it is generated by following code:
    please use this code for generating subscription list and then try.
    exchange_segments = "NSE:NIFTY BANK"
    quote_data = kite.quote(exchange_segments)[exchange_segments]
    last_price = quote_data["last_price"]
    current_strike = int(round(last_price, -2))

    instruments = pd.read_csv("https://api.kite.trade/instruments")
    strike_instrument_token_CE = instruments[(instruments.segment == 'NFO-OPT') & (instruments.name == 'BANKNIFTY') & (instruments.strike == current_strike) & (instruments.instrument_type == "CE")].sort_values('expiry').iloc[0]["instrument_token"]
    strike_instrument_token_PE = instruments[(instruments.segment == 'NFO-OPT') & (instruments.name == 'BANKNIFTY') & (instruments.strike == current_strike) & (instruments.instrument_type == "PE")].sort_values('expiry').iloc[0]["instrument_token"]

    subscription_list = [strike_instrument_token_CE,strike_instrument_token_PE]
  • rakeshr
    subscription_list = [strike_instrument_token_CE,strike_instrument_token_PE]
    I am not using pandas datatype
    You are using NumPy datatype (numpy.int64) item inside subscription list. It should be int data type instead. You need to typecast these numpy dtype to int. Something like:
    subscription_list = [int(strike_instrument_token_CE), int(strike_instrument_token_PE)]
This discussion has been closed.