It looks like you're new here. If you want to get involved, click one of these buttons!
import logging
from multiprocessing import Process
from kiteconnect import KiteTicker
from helper import get_secrets
from src.option_chain import OptionChainCreator
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s.%(msecs)03d %(levelname)s %(process)d %(processName)s: %(funcName)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
log = logging.getLogger(__name__)
class OptionChainTicker:
def __init__(self):
self.tick_count = 0
self.failed_attempts = 0
def on_connect(self, ws, response):
log.info('on_connect')
# this is a list of 2930 tokens, please check the attached file
# for code with all tokens
self.all_tokens = []
ws.subscribe(self.all_tokens)
ws.set_mode(ws.MODE_FULL, self.all_tokens)
def on_ticks(self, ws, ticks):
log.info(f'on_ticks, size: {len(ticks)}')
if ticks[0]['mode'] != ws.MODE_FULL:
self.failed_attempts += 1
log.error(f'received tick in full mode..')
# ws.close()
return
def on_close(self, ws, code, response):
ws.stop()
if __name__ == '__main__':
process_list = []
process_count = 3
for i in range(0, process_count):
option_chain_creator = OptionChainCreator()
ticker = OptionChainTicker()
secrets = get_secrets()
kws = KiteTicker(secrets['api_key'], secrets['access_token'], debug=False)
kws.on_connect = ticker.on_connect
kws.on_ticks = ticker.on_ticks
kws.on_close = ticker.on_close
p = Process(target=kws.connect)
process_list.append(p)
p.start()
for p in process_list:
p.join()
2023-02-03 00:48:08.987 INFO 28704 Process-3: on_connect: on_connect
2023-02-03 00:48:09.005 INFO 28702 Process-1: on_connect: on_connect
2023-02-03 00:48:09.005 INFO 28703 Process-2: on_connect: on_connect
2023-02-03 00:48:09.949 INFO 28704 Process-3: on_ticks: on_ticks, size: 2930 <-------- tick1 in process3
2023-02-03 00:48:09.949 ERROR 28704 Process-3: on_ticks: received tick in full mode..
2023-02-03 00:48:11.029 INFO 28704 Process-3: on_ticks: on_ticks, size: 2930 <-------- tick2 in process3; why did I get a second tick, if I only subscribed once?
2023-02-03 00:48:11.357 INFO 28703 Process-2: on_ticks: on_ticks, size: 2930
2023-02-03 00:48:11.358 INFO 28702 Process-1: on_ticks: on_ticks, size: 2930
Once the
set_mode
gets called, it changes to the requested mode from Quote Mode. In generic, this set_mode is instant, and the first tick you receive is your set_mode type. But, in your case might be because of larger tokens, multi-processing, etc mode set is getting called after receiving the first default mode tick. It should be easy to handle on your end, by skipping none required mode ticks.I have set the process = 1, the results are the same.