Getting ticks in QUOTE mode intermittently instead of FULL mode

prajwal52520

Hi, I am using latest pykiteconnect. I am trying to subscribe to the tokens through the websocket in MODE_FULL.
But sometimes (like 25% of the time), I get all the ticks in QUOTE mode instead of FULL mode. This is true when the quotes are of size nearing 3000; 2930 in my case.
This happens at the start of the program and not after some time has passed. I mean in some of the runs of the program I don't see any ticks with FULL mode.

Another observation is that when quotes come in QUOTE mode instead of FULL mode I receive another tick in FULL mode i.e. 2 separate ticks in QUOTE and FULL mode. To me, it looks like a bug. This is the code I am using.
(looks like indentation is not appearing in the code below, hence I have attached the file itself)



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()




In the above code, I am using multiprocessing. I have also noted that the observation I shared has nothing to do with multiprocessing. It's just that I have a higher chance of getting the quotes in FULL mode if I run with multiprocessing.

These are the logs

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


I have attached the code with all instruments as a file. I guess you can reproduce this behavior if you run the above code multiple times.
Also, please tell me how to highlight the syntax in this editor.

Thanks.
  • prajwal52520
    @sujith @rakeshr could you please help?
  • rakeshr
    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)
    By default subscription mode is MODE_QUOTE(Quote Mode), assigned at the subscribe call here ws.subscribe(tokens). You can refer to the code base here.
    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.
  • prajwal52520
    @rakeshr The strange thing is 2 web sockets get created one with the mode quote and another with mode full when the token size is large with a single kws connection. I am sure that I have not set the mode or called subscribe method twice resulting in the websocket getting created twice.
    I have set the process = 1, the results are the same.
Sign In or Register to comment.