Here is my code:
import sys
import json
from kiteconnect import KiteTicker
import time
from db import insert_ticks
api_key = ''
access_token = ''
kite = KiteConnect(api_key=api_key, access_token=access_token)
tokens = [6191105]
def on_tick(ticks, ws):
logging.info("on tick - {}".format(json.dumps(ticks)))
insert_ticks.delay(ticks, mapInsdf)
def on_connect(kws, response):
print("connected")
def on_close():
logging.info("con closed")
kws.on_tick = on_tick
kws.on_connect = on_connect
kws.on_close = on_close
kws.connect(threaded=True)
count = 0
while True:
logging.info("2s delay")
if count < len(tokens):
if kws.is_connected():
logging.info("Subscribing to: {}".format(tokens[count]))
kws.subscribe([tokens[count]])
kws.set_mode(kws.MODE_FULL, [tokens[count]])
count += 1
print('Subscribed successfully')
else:
logging.info("Connecting to WebSocket...")
time.sleep(2)
October 2021