Facing the error builtins.typeError: 'tuple' object is not callable

ishaanagni
ishaanagni edited November 27 in Python client
I am using a process manager (pm2) to restart the KiteTicker service.
This seems to be an error in the on_close function.
kite connect version is
Name: kiteconnect
Version: 5.0.1
Summary: The official Python client for the Kite Connect trading API
Home-page: https://kite.trade
Author: Zerodha Technology Pvt. Ltd. (India)
Author-email: [email protected]
License: MIT
Location: /root/kite_sync_algo/venv/lib/python3.8/site-packages
Requires: autobahn, pyOpenSSL, python-dateutil, requests, service-identity, six
Required-by:

Traceback:
2024-11-27T11:10:36: Unhandled Error
2024-11-27T11:10:36: Traceback (most recent call last):
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/python/log.py", line 96, in callWithLogger
2024-11-27T11:10:36: return callWithContext({"system": lp}, func, *args, **kw)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/python/log.py", line 80, in callWithContext
2024-11-27T11:10:36: return context.call({ILogContext: newCtx}, func, *args, **kw)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/python/context.py", line 117, in callWithContext
2024-11-27T11:10:36: return self.currentContext().callWithContext(ctx, func, *args, **kw)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/python/context.py", line 82, in callWithContext
2024-11-27T11:10:36: return func(*args, **kw)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/internet/tcp.py", line 510, in connectionLost
2024-11-27T11:10:36: self._commonConnection.connectionLost(self, reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/internet/tcp.py", line 328, in connectionLost
2024-11-27T11:10:36: protocol.connectionLost(reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/protocols/tls.py", line 409, in connectionLost
2024-11-27T11:10:36: ProtocolWrapper.connectionLost(self, reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/twisted/protocols/policies.py", line 114, in connectionLost
2024-11-27T11:10:36: self.wrappedProtocol.connectionLost(reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/autobahn/twisted/websocket.py", line 288, in connectionLost
2024-11-27T11:10:36: self._connectionLost(reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/autobahn/websocket/protocol.py", line 3422, in _connectionLost
2024-11-27T11:10:36: WebSocketProtocol._connectionLost(self, reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/autobahn/websocket/protocol.py", line 1133, in _connectionLost
2024-11-27T11:10:36: self._onClose(self.wasClean, WebSocketProtocol.CLOSE_STATUS_CODE_ABNORMAL_CLOSE, "connection was closed uncleanly (%s)" % self.wasNotCleanReason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/autobahn/twisted/websocket.py", line 331, in _onClose
2024-11-27T11:10:36: self.onClose(wasClean, code, reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/kiteconnect/ticker.py", line 80, in onClose
2024-11-27T11:10:36: self.factory.on_close(self, code, reason)
2024-11-27T11:10:36: File "/root/kite_sync_algo/venv/lib/python3.8/site-packages/kiteconnect/ticker.py", line 659, in _on_close
2024-11-27T11:10:36: self.on_close(self, code, reason)
2024-11-27T11:10:36: builtins.TypeError: 'tuple' object is not callable
2024-11-27T11:10:36:
In the logfile, I am getting the following logs:
 
[2024-11-27 11:10:36,806: ERROR] Connection error: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
[2024-11-27 11:10:36,806: ERROR] Errored [1006]: connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)
[2024-11-27 11:10:36,806: ERROR] Connection closed: 1006 - connection was closed uncleanly (peer dropped the TCP connection without previous WebSocket closing handshake)

This is the code

from kiteconnect import KiteTicker
import json
import logging
import os
import datetime
from sync import send_sync_order
import time

def on_connect(ws, response):
logging.info(f'Connected: {response}')

# def on_close(ws, code, reason):
# logging.error(f'Socket was closed [{code}]: {reason}')
# ws.close()

def on_error(ws, code, reason):
logging.error(f'Errored [{code}]: {reason}')

def on_order_update(ws, data):
logging.debug(f'Recieved Order: {data}')
try: send_sync_order(data)
except Exception as e: logging.exception(e)

def order_stream(api_key, access_token):
kws = KiteTicker(api_key, access_token)
# kws.on_close = on_close,
kws.on_connect = on_connect
kws.on_error = on_error
kws.on_order_update = on_order_update
kws.connect()

if __name__ == '__main__':

log_dir = 'Logs/'
if not os.path.exists(log_dir): os.makedirs(log_dir)
log_file = log_dir + f'{str(datetime.date.today())}.log'
logging.basicConfig(level=logging.DEBUG,filename = log_file ,filemode = 'a',
format="[%(asctime)s: %(levelname)s] %(message)s",force=True)

config = json.load(open('config.json','r'))
api_key = config['credentials']['api_key']
access_token = config['credentials']['access_token']

logging.info(f'Starting order stream: api_key: {api_key}, access_token: {access_token}')
print('Starting Kite sync...')
while True:
try: order_stream(api_key, access_token)
except Exception as e: logging.exception(f"error in order stream: {e}")
time.sleep(3)


Sign In or Register to comment.