on_close not working correctly.

AyushSharma
Whenever i try to close the connection it closes connection but when i again start the ticker it gives error like this -
127.0.0.1 - - [09/Nov/2021 17:36:18] "GET /startloop HTTP/1.1Exception in thread Thread-21:
Traceback (most recent call last):
File "c:\users\asayu\appdata\local\programs\python\python39\lib\threading.py", line 954, in _bootstrap_inner
" 302 -
self.run()
File "c:\users\asayu\appdata\local\programs\python\python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
self.startRunning(installSignalHandlers=installSignalHandlers)
File "G:\FazleAlgoTrading\env\lib\site-packages\twisted\internet\base.py", line 1299, in startRunning
ReactorBase.startRunning(cast(ReactorBase, self))
File "G:\FazleAlgoTrading\env\lib\site-packages\twisted\internet\base.py", line 843, in startRunning


Here what i want is, i have started a ticker and when i want to stop i should stop the ticker, so i have added a button in ui and when i click on that "close_process()" gets called but its not working.

prior to this i was using kws.stop() in close_process() but the above error is there can u help me how to do this ? How to close a ticker from outside any example. or u can help me by telling what is wrong in my code

start_loop.py


from kiteticker_func import on_close, on_connect, on_error, on_ticks, setData
from threading import Thread
from kite_instance import kiteTickerInstance

kws = None
threadname = None
def background_task():
global kws
kws.connect(threaded=True)

def order_process(data, acc_token):
global kws
kws = kiteTickerInstance(acc_token)
setData(data)
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
ticker_thread = Thread(target=background_task)
ticker_thread.daemon = True
ticker_thread.start()


def close_process():
on_close


kiteticker_func.py


from time import sleep
from place_order import place_order
from pprint import pprint
import logging

data = None

def setData(orders):
global data
data = orders

def on_ticks(ws, ticks):
for single_instrument in ticks:
ltp = single_instrument['last_price']
print(ltp)
i = 0
for order in data:
if order.ins_token == single_instrument['instrument_token']:
if order.action == 'long' and ltp <= order.trigger_price:
place_order(order)
i+=1
elif order.action == 'short' and ltp >= order.trigger_price:
place_order(order)
i+=1
data.pop(i)
pprint(ticks)

def on_connect(ws, response):
global data
tick_data = []
for order in data:
tick_data.append(order.ins_token)
print('tick data to be subscribed - ',tick_data)
ws.subscribe(tick_data)
ws.set_mode(ws.MODE_LTP,tick_data)

def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
ws.stop()

def on_error(ws, code, reason):
logging.error("closed connection on error: {} {}".format(code, reason))




  • rakeshr
    Can you check, if close_process method is getting called? Once you call it from out of the scope.
  • AyushSharma


    So the close process is getting called.
    1.png 652.4K
  • AyushSharma
    Hey any update can u tell me any example of how on_close can be called from outside
  • rakeshr
    def close_process():
    on_close
    This is not the correct way to call the on_close method. You need to assign a callback with kws object for calling the same. Something like:
    kws = kiteTickerInstance(acc_token)
    .....
    kws.on_close = on_close
Sign In or Register to comment.