Need a way to establish two kiteticker instances one in threaded mode and one in non threaded mode

SITPL
Hello,

We want to set up two kiteticker instances one in threaded mode to listen to the order updates so that while we wait for feting details like LTP, orders, etc. we will not lose the order callbacks

We also need a second kiteticker to process the incoming ticks to determine the current state of positions

What is currently happening is that after we call connect() on the second kiteconnect instance. Main thread simply terminates

program simply terminates with exit status 0 after call to positionManager.monitorPositions()

Code:
class PositionManager:
def __init__(self):


self.orderListener = KiteTicker(API_KEY, ACCESS_TOKEN)
self.orderListener.on_order_update = self.logOrderData
self.orderListener.connect(threaded=True)

def logOrderData(self, ws, data):
logger.info('Received Order Update')
logger.info('\nData :\n{}'.format(pformat(data)))

def monitorPositions(self):
if (self.kiteTicker is None):
self.kiteTicker = kiteSessionManager.getKiteTickerSession()
kiteTicker = self.kiteTicker
kiteTicker.on_connect = self.kiteTickerOnConnect
kiteTicker.on_ticks = self.kiteTickerOnTicks
kiteTicker.on_order_update = self.kiteTickerOnOrderUpdate
kiteTicker.on_reconnect = self.kiteTickerOnConnect
kiteTicker.on_noreconnect = self.kiteTickerOnNoReconnect
kiteTicker.on_error = self.kiteTickerOnError
kiteTicker.on_close = self.kiteTickerOnClose

kiteTicker.connect()

Driver Script:
positionManager = PositionManager()
positionManager.monitorPositions()

pyKiteConnect Version: kiteconnect==3.9.0
  • SITPL
    Also when using the ticker in threaded mode, how to do the error handling as it is a different thread

    Is there any functionality to add callback on exception similar to

    concurrent.futures.add_done_callback where we can check the error and handle it
  • rakeshr
    @SITPL
    kiteTicker.on_ticks = self.kiteTickerOnTicks
    Don't see kiteTickerOnTicks method detail above.
    Are you performing any computataion inside on_tick?
    Make sure, you are not blocking the ticker thread. This thread should help.
  • SITPL
    SITPL edited March 2021
    Indeed we do some computations onside on_tick callback but those are for just pnl calculation which is basic arithmatic and some sleep in certain scenarios like checking the status of a recent order.
    What we want is to run tickers in both threaded and non threaded mode so that while processing tics we do not miss the order update while we wait.
    We dropped few updates as the ticks were being processed hence want to change the working model to thread-based order event listening
  • SITPL
    In the example shared can you please explain why do we need to write two on_tick functions.
    Also, how will the last line get executed while the main thread is in an infinite loop?
    Sorry to ask silly question but wanted to know this
  • rjamitsharm
    kiteTicker.connect(threaded=True)

    it will help you as it will not block main thread
  • SITPL
    What we have experienced is that calling kiteTicker.connect(threaded=True) first time starts the ticker in separate thread as expected.
    However when we call kiteTicker.connect() second time to run ticker in main thread, it is starting it in separate thread again which is daemon.
    Post calling kiteTicker.connect(), main thread ends and so does the application
Sign In or Register to comment.