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
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()
close_process
method is getting called? Once you call it from out of the scope.So the close process is getting called.
on_close
method. You need to assign a callback with kws object for calling the same. Something like: