ranjanrak/optionchainstream

library raises key error because API response is changed

Parth971 opened this issue · 1 comments

2023-04-03 11:07:44+0530 [KiteTickerClientProtocol (TLSMemoryBIOProtocol),client] Unhandled Error
        Traceback (most recent call last):
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/python/log.py", line 96, in callWithLogger
            return callWithContext({"system": lp}, func, *args, **kw)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/python/log.py", line 80, in callWithContext
            return context.call({ILogContext: newCtx}, func, *args, **kw)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/python/context.py", line 117, in callWithContext
            return self.currentContext().callWithContext(ctx, func, *args, **kw)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/python/context.py", line 82, in callWithContext
            return func(*args, **kw)
        --- <exception caught here> ---
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/internet/posixbase.py", line 487, in _doReadOrWrite
            why = selectable.doRead()
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/internet/tcp.py", line 248, in doRead
            return self._dataReceived(data)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/internet/tcp.py", line 253, in _dataReceived
            rval = self.protocol.dataReceived(data)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/protocols/tls.py", line 329, in dataReceived
            self._flushReceiveBIO()
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/protocols/tls.py", line 295, in _flushReceiveBIO
            ProtocolWrapper.dataReceived(self, bytes)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/twisted/protocols/policies.py", line 110, in dataReceived
            self.wrappedProtocol.dataReceived(data)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/twisted/websocket.py", line 291, in dataReceived
            self._dataReceived(data)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/websocket/protocol.py", line 1213, in _dataReceived
            self.consumeData()
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/websocket/protocol.py", line 1225, in consumeData
            while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/websocket/protocol.py", line 1591, in processData
            fr = self.onFrameEnd()
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/websocket/protocol.py", line 1713, in onFrameEnd
            self._onMessageEnd()
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/twisted/websocket.py", line 319, in _onMessageEnd
            self.onMessageEnd()
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/websocket/protocol.py", line 634, in onMessageEnd
            self._onMessage(payload, self.message_is_binary)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/autobahn/twisted/websocket.py", line 322, in _onMessage
            self.onMessage(payload, isBinary)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/kiteconnect/ticker.py", line 71, in onMessage
            self.factory.on_message(self, payload, is_binary)
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/kiteconnect/ticker.py", line 679, in _on_message
            self.on_ticks(self, self._parse_binary(payload))
          File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/websocket.py", line 47, in on_ticks
            'last_price':tick['last_price'], 'volume':tick['volume'], 'change':tick['change'],
        builtins.KeyError: 'volume'
        
2023-04-03 11:07:44+0530 [-] Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
2023-04-03 11:07:44+0530 [-] ERROR:root:closed connection on error: 1006 connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
2023-04-03 11:07:44+0530 [-] ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
2023-04-03 11:07:44+0530 [-] ERROR:root:closed connection on close: 1006 connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
2023-04-03 11:07:44+0530 [KiteTickerClientProtocol (TLSMemoryBIOProtocol),client] <twisted.internet.tcp.Connector instance at 0x7fa53da843a0 disconnected IPv4Address(type='TCP', host='ws.kite.trade', port=443)> will retry in 2 seconds
2023-04-03 11:07:44+0530 [-] Stopping factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x7fa5545cd390>
Process Process-2:
Traceback (most recent call last):
  File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/redis_instrument.py", line 70, in fetch_option_data
    token_data = json.loads(self.conn.get(optionContractKey))
  File "/usr/lib/python3.10/json/__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/websocket.py", line 31, in form_option_chain
    complete_option_data = self.instrumentClass.generate_optionChain(self.token_list)
  File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/instrument_file.py", line 117, in generate_optionChain
    optionData = self.redis_db.fetch_option_data(optionInstrument['symbol'], instrumentToken)
  File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/redis_instrument.py", line 72, in fetch_option_data
    raise Exception('Error - {}'.format(e))
Exception: Error - the JSON object must be str, bytes or bytearray, not NoneType
2023-04-03 11:07:47+0530 [-] Starting factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x7fa5545cd390>
^C2023-04-03 11:07:48+0530 [-] Received SIGINT, shutting down.
Traceback (most recent call last):
  File "/home/root366/Dump/websocket streaming/test.py", line 12, in <module>
    for data in StreamData:
  File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/option_chain.py", line 45, in create_option_chain
    yield self.socketClient.q.get()
  File "/usr/lib/python3.10/multiprocessing/queues.py", line 103, in get
    res = self._recv_bytes()
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 216, in recv_bytes
2023-04-03 11:07:48+0530 [-] ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    buf = self._recv_bytes(maxlength)
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 414, in _recv_bytes
2023-04-03 11:07:48+0530 [-] ERROR:root:closed connection on error: 1006 connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    buf = self._recv(4)
  File "/usr/lib/python3.10/multiprocessing/connection.py", line 379, in _recv
2023-04-03 11:07:48+0530 [-] ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
    chunk = read(handle, remaining)
2023-04-03 11:07:48+0530 [-] ERROR:root:closed connection on close: 1006 connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
KeyboardInterrupt
2023-04-03 11:07:48+0530 [KiteTickerClientProtocol (TLSMemoryBIOProtocol),client] <twisted.internet.tcp.Connector instance at 0x7fa53da843a0 disconnected IPv4Address(type='TCP', host='ws.kite.trade', port=443)> will retry in 2 seconds
2023-04-03 11:07:48+0530 [-] Stopping factory <kiteconnect.ticker.KiteTickerClientFactory object at 0x7fa5545cd390>
2023-04-03 11:07:48+0530 [-] Main loop terminated.

Can you please update code as we can clearly see in websocket.py, volume key is causing this issue.

File "/home/root366/enviroments/websocket streaming/lib/python3.10/site-packages/optionchain_stream/websocket.py", line 47, in on_ticks 'last_price':tick['last_price'], 'volume':tick['volume'], 'change':tick['change'], builtins.KeyError: 'volume'

you need to remove the volume key from optionData dict.

source: #5 (comment)