I am running KiteTicker in Amazon AWS (Ubuntu) EC2 instance and facing a weird issue.
KiteTicker simply stops responding abruptly(after 30mins -2 hours) without any exception. I have enabled debug for kite ticker - but was unable to get meaningful debug message -
Note: The same worked fine when running in the local system.
Please let me know if any configuration should be changed from my side.
def tickHandler(ticks, entry, target, stop, targetCheck): for tick in ticks: try: res = [sub['last_price'] for sub in ticks] ltp = res[0]
if side == 'LONG': if ltp <= stop: logging.info('Price Less Than %d', stop) sys.exit(1) if targetCheck: if ltp >= target: stop = entry targetCheck = False else: if ltp >= stop: sys.exit(1) if targetCheck: if ltp <= target: stop = entry targetCheck = False
if datetime.time(15, 10) <= datetime.datetime.now().time(): sys.exit(1) except SystemExit as sysExit: global slReturn slReturn = 202
My question : if my condition (say ltp > 100 ) met - what is the clean way to stop the websocket thread. I dont want to keep alive the websocket thread once I am done with my trading.
# Perform required data operation using tick data def on_ticks(ws, ticks): helper_method(ws, ticks)
def helper_method(ws, ticks): print(ticks) if datetime.time(20, 30) <= datetime.datetime.now().time(): print('Before Stopping') ws.close() global keepLooping keepLooping = False
print('After Stopping')
kws.on_ticks = on_ticks --------------------------------------------------------------------------------------------------- Question : is ws.stop() enough? do I need to unsubscribe or do anything else? Re-iterating my requirement: want to stop the kws thread once the condition is met.
-------------------------------------------------------------------------------------------------------------
def StoplossTicker(side, entry, target, stop, instrumentToken):
logging.info('Starting Stoploss Ticks')
logging.info('Entry - %f,stop - %f , target - %f', entry, stop, target)
apiKey = Constant.API_KEY
access_token = login.get_access_token()
kws = KiteTicker(apiKey, access_token, debug=True)
logging.info('For Detailed Ticks - Change the Log settings To Debug')
targetCheck = True
def tickHandler(ticks, entry, target, stop, targetCheck):
for tick in ticks:
try:
res = [sub['last_price'] for sub in ticks]
ltp = res[0]
if side == 'LONG':
if ltp <= stop:
logging.info('Price Less Than %d', stop)
sys.exit(1)
if targetCheck:
if ltp >= target:
stop = entry
targetCheck = False
else:
if ltp >= stop:
sys.exit(1)
if targetCheck:
if ltp <= target:
stop = entry
targetCheck = False
if datetime.time(15, 10) <= datetime.datetime.now().time():
sys.exit(1)
except SystemExit as sysExit:
global slReturn
slReturn = 202
def on_ticks(ws, ticks):
tickHandler(ticks, entry, target, stop, True)
def on_connect(ws, response):
ws.subscribe(instrumentToken)
ws.set_mode(ws.MODE_LTP, instrumentToken)
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect(disable_ssl_verification=True)
return slReturn
-----------------------------------------------------------------------------
2021-12-30 12:11:48,880 - [DEBUG] - [ticker.py)] - [_loop_ping] - [109] - ping =>
2021-12-30 12:11:48,880 - [DEBUG] - [ticker.py)] - [_loop_ping] - [111] - last ping was 2.502261161804199 seconds back.
2021-12-30 12:11:48,881 - [DEBUG] - [ticker.py)] - [onPong] - [96] - last pong was 2.502304792404175 seconds back.
2021-12-30 12:11:48,881 - [DEBUG] - [ticker.py)] - [onPong] - [101] - pong => b''
2021-12-30 12:11:51,381 - [DEBUG] - [ticker.py)] - [_loop_ping] - [109] - ping =>
2021-12-30 12:11:51,381 - [DEBUG] - [ticker.py)] - [_loop_ping] - [111] - last ping was 2.501481056213379 seconds back.
2021-12-30 12:11:51,382 - [DEBUG] - [ticker.py)] - [onPong] - [96] - last pong was 2.501494884490967 seconds back.
2021-12-30 12:11:51,382 - [DEBUG] - [ticker.py)] - [onPong] - [101] - pong => b''
2021-12-30 12:11:53,882 - [DEBUG] - [ticker.py)] - [_loop_ping] - [109] - ping =>
2021-12-30 12:11:53,882 - [DEBUG] - [ticker.py)] - [_loop_ping] - [111] - last ping was 2.500939130783081 seconds back.
2021-12-30 12:11:53,883 - [DEBUG] - [ticker.py)] - [onPong] - [96] - last pong was 2.500863552093506 seconds back.
2021-12-30 12:11:53,883 - [DEBUG] - [ticker.py)] - [onPong] - [101] - pong => b''
2021-12-30 12:11:56,386 - [DEBUG] - [ticker.py)] - [_loop_ping] - [109] - ping =>
2021-12-30 12:11:56,386 - [DEBUG] - [ticker.py)] - [_loop_ping] - [111] - last ping was 2.5038630962371826 seconds back.
2021-12-30 12:11:56,387 - [DEBUG] - [ticker.py)] - [onPong] - [96] - last pong was 2.5037951469421387 seconds back.
2021-12-30 12:11:56,387 - [DEBUG] - [ticker.py)] - [onPong] - [101] - pong => b''
2021-12-30 12:11:58,888 - [DEBUG] - [ticker.py)] - [_loop_ping] - [109] - ping =>
2021-12-30 12:11:58,888 - [DEBUG] - [ticker.py)] - [_loop_ping] - [111] - last ping was 2.501797676086426 seconds back.
My question : if my condition (say ltp > 100 ) met - what is the clean way to stop the websocket thread. I dont want to keep alive the websocket thread once I am done with my trading.
if condition:
ws.close()
----------------------------------------
def helper_method(ws,ticks):
.........
if datetime.time(14, 30) <= datetime.datetime.now().time():
ws.stop()
ERROR:kiteconnect.ticker:Connection closed: None - None
-----------------------------------------------
import datetime
import logging
from kiteconnect import KiteTicker
logging.basicConfig(level=logging.DEBUG)
kws = KiteTicker("APIKEY", "ACCESSTOKEN")
keepLooping = True
def on_ticks(ws, ticks):
print("Ticks: {}".format(ticks))
def on_connect(ws, response):
ws.subscribe([59307271])
ws.set_mode(ws.MODE_FULL, [59307271])
def on_close(ws, code, reason):
ws.stop()
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect(threaded=True)
while keepLooping:
# Perform required data operation using tick data
def on_ticks(ws, ticks):
helper_method(ws, ticks)
def helper_method(ws, ticks):
print(ticks)
if datetime.time(20, 30) <= datetime.datetime.now().time():
print('Before Stopping')
ws.close()
global keepLooping
keepLooping = False
print('After Stopping')
kws.on_ticks = on_ticks
---------------------------------------------------------------------------------------------------
Question : is ws.stop() enough? do I need to unsubscribe or do anything else? Re-iterating my requirement: want to stop the kws thread once the condition is met.