Today on 21st june, at 9:17:58, Bank nifty index was around 33400. I had subscribed Option symbols: BANKNIFTY21JUN34000CE and BANKNIFTY21JUN34000PE along with other 15-20 symbols. I didn't receive at the money option tick data for couple of minutes. It's highly unlikely that there was no change in the LTP.
Below dictionary has the key as time and value has the duration for which mentioned tick data was absent
We can see at 9:17:58, there was no BANKNIFTY21JUN34000CE tick received for 2 minutes and 40 seconds. There have been multiple such instances.
I am also attaching today's websocket feed data which i have stored it. You can cross check the absent tick data.
A lot can happen in 2 minutes and 40 second. Any idea why this happened? Is it because of other 15-20 symbols i had subscribed? If yes, can i create two different KiteTicker/websocket connection with same APIkey and generated access_token for the day?
I had subscribed Option symbols: BANKNIFTY21JUN34000CE and BANKNIFTY21JUN34000PE along with other 15-20 symbols. I didn't receive at the money option tick data for couple of minutes.
We just checked chart historical data, which too uses the same WebSocket streaming to form candles. OHLCV value for the above-mentioned period(09:17 - 09:21) for BANKNIFTY21JUN34000CE, are showing fine.
You need to check, if there was websocket dis-connection for the mentioned period at your end or you are performing any thread block computation inside on_tick thread.
There was no websocket dis-connection and it was running on google cloud. I am not performing any computation inside on_tick thread. I am storing the tick data in Deque buffers first. Any processing i am doing on these buffers.
@rakeshr Thank you for the quick response. Can you please look at function coded in on_tick thread? Even today(22nd June), I didn't get BANKNIFTY21JUN35100PE tick data for 4 minutes at 9:30.
This has been happening consistently. I am trading short straddle, where one of the contract LTP doesn't update for 4 minutes and hence there is huge difference between expected profit/loss versus realized profit/loss.
With one API subscription, is it possible to create two KiteTicker instances on same PC. I am planning to use one ticker for traded two option contract tick and another ticker for storing 100s of banknifty option contracts tick.
I have written a code where i have subscribed 20 above & below banknifty weekly contracts w.r.t. Bank nifty index . I am not doing any calculation in the main thread. I have subscribed total 20(this week)+20(next week)+20(next to next week)=60 bank nifty option symbols.
out of these 60 subscribed option contracts, 10 will be ATM or near otm and their LTP is most likely to change every second. I am consistently getting at the max 5 ticks every second.
@sujith Thanks for your prompt reply. I have gone through the websocket related FAQs multiple times and the issue, which i have been facing, has not been discussed as of now.
Today(25th June), I had subscribed BANKNIFTY2170135100CE and at 11:07:25, I didn't received any ticks for next 1 minute 2 seconds. At that time, Bank nifty index was 35020. It's hard to believe that LTP didn't change for 1 minute as this was ATM option contract.
Following your recommendation, 1. I don't do any computation in on_tick thread. 2. Script was running on Mumbai based google cloud and websocket was not disconnected as I was receiving other option contract tick data. 3. No computation in main thread.
If you look at the previous comments, @rakeshr said that candle closed perfectly for him but not for me. Even after following your recommendation, traded tick is not coming for couple of minutes. I understand super OTM contract's price may not change for couple of minutes but this should not be the case with ATM option contracts.
What is the solution?
I have already attached full script [data_collection.rar] which I am using to store the tick data. You can find the script's main content below.
global u #login name_file_to_be_read="Data_collection" #data will be stored in this file api_key = api_key_value() access_token = access_token_value() u = KiteConnect(api_key=api_key) u.set_access_token(access_token) get_master_contract() print(INITIAL_BANKNIFTY_LTP()) bank_nifty_subscription_list = BANKNIFTY_sub_symbol_list(20) # list of option contracts subscribed.
for symbol_to_be_subscribed in bank_nifty_subscription_list: token_list_for_subscription.append(get_instrument_by_symbol('NFO', symbol_to_be_subscribed))
# token_list_for_subscription will be passed to websocket to start streaming these ticks token_list_for_subscription.append(get_instrument_by_symbol('NSE', 'NIFTY BANK')) token_list_for_subscription.append(get_instrument_by_symbol('NSE', 'INDIA VIX'))
# Parallel thread which will pop the data from deque buffer and write it in a file. t1 = Thread(target=write_to_file_from_file_buffer, args=(name_file_to_be_read + '.txt',)) t2 = Thread(target=write_raw_data_to_file_from_fast_buffer, args=(name_file_to_be_read + '_raw.txt',)) t1.start() t2.start() kws_data = KiteTicker(api_key, access_token, debug=False, root=None, reconnect=True, reconnect_max_tries=300, reconnect_max_delay=60, connect_timeout=30) # reconnection is enabled, it will start reconnecting at 2s,4s,8s,16s,32s,60s,60s till 300 times. each trial is timeout after 30s of connection attempt kws_data.on_ticks = read_from_socket_to_buffer kws_data.on_connect = on_connect kws_data.connect(threaded=True)
As, said above the WebSocket ticker is working absolutely fine, and historical data based on these are showing the same. We can't assist with your code as it is outside the scope of KiteConnect.
Today(25th June), I had subscribed BANKNIFTY2170135100CE and at 11:07:25, I didn't received any ticks for next 1 minute 2 seconds
kws =KiteTicker(api_key,access_token, debug=False, root=None, reconnect=True, reconnect_max_tries=300, reconnect_max_delay=60, connect_timeout=30)
kws.on_ticks=read_from_socket_to_buffer
kws.on_connect = on_connect
kws.connect(threaded=True)
def read_from_socket_to_buffer(ws,message):
global file_buffer, fast_buffer, hot_buffer # deque buffers from python Collections module
message1={}
message1['timestamp']=message[0]['timestamp']
message1['instrument_token'] = message[0]['instrument_token']
message1['last_price'] = message[0]['last_price']
message1['local_sys_time'] = time.ctime() # appending local system time
message1['symbol']=get_symbol_by_instrument_token(message[0]['instrument_token'])
#print(message1)
file_buffer.appendleft(message1)
fast_buffer.appendleft(message1)
hot_buffer.appendleft(message1)
def get_symbol_by_instrument_token(token):
global token_dict
return token_dict[token]
This has been happening consistently. I am trading short straddle, where one of the contract LTP doesn't update for 4 minutes and hence there is huge difference between expected profit/loss versus realized profit/loss.
With one API subscription, is it possible to create two KiteTicker instances on same PC. I am planning to use one ticker for traded two option contract tick and another ticker for storing 100s of banknifty option contracts tick.
It could be because you are blocking the main thread where ticks are received.
You can use three websocket connections per api_key.
I have written a code where i have subscribed 20 above & below banknifty weekly contracts w.r.t. Bank nifty index . I am not doing any calculation in the main thread.
I have subscribed total 20(this week)+20(next week)+20(next to next week)=60 bank nifty option symbols.
I am still seeing 4-5 option data every second.
I am also attaching my code for your reference.
You can check out FAQs about websockets here.
out of these 60 subscribed option contracts, 10 will be ATM or near otm and their LTP is most likely to change every second. I am consistently getting at the max 5 ticks every second.
Please read the FAQs here to know more about the Websocket API behavior.
Today(25th June), I had subscribed BANKNIFTY2170135100CE and at 11:07:25, I didn't received any ticks for next 1 minute 2 seconds. At that time, Bank nifty index was 35020. It's hard to believe that LTP didn't change for 1 minute as this was ATM option contract.
Following your recommendation,
1. I don't do any computation in on_tick thread.
2. Script was running on Mumbai based google cloud and websocket was not disconnected as I was receiving other option contract tick data.
3. No computation in main thread.
If you look at the previous comments, @rakeshr said that candle closed perfectly for him but not for me. Even after following your recommendation, traded tick is not coming for couple of minutes. I understand super OTM contract's price may not change for couple of minutes but this should not be the case with ATM option contracts.
What is the solution?
I have already attached full script [data_collection.rar] which I am using to store the tick data. You can find the script's main content below.
#main function starts here.
global u
#login
name_file_to_be_read="Data_collection" #data will be stored in this file
api_key = api_key_value()
access_token = access_token_value()
u = KiteConnect(api_key=api_key)
u.set_access_token(access_token)
get_master_contract()
print(INITIAL_BANKNIFTY_LTP())
bank_nifty_subscription_list = BANKNIFTY_sub_symbol_list(20) # list of option contracts subscribed.
for symbol_to_be_subscribed in bank_nifty_subscription_list:
token_list_for_subscription.append(get_instrument_by_symbol('NFO', symbol_to_be_subscribed))
# token_list_for_subscription will be passed to websocket to start streaming these ticks
token_list_for_subscription.append(get_instrument_by_symbol('NSE', 'NIFTY BANK'))
token_list_for_subscription.append(get_instrument_by_symbol('NSE', 'INDIA VIX'))
# Parallel thread which will pop the data from deque buffer and write it in a file.
t1 = Thread(target=write_to_file_from_file_buffer, args=(name_file_to_be_read + '.txt',))
t2 = Thread(target=write_raw_data_to_file_from_fast_buffer, args=(name_file_to_be_read + '_raw.txt',))
t1.start()
t2.start()
kws_data = KiteTicker(api_key, access_token, debug=False, root=None, reconnect=True, reconnect_max_tries=300, reconnect_max_delay=60, connect_timeout=30)
# reconnection is enabled, it will start reconnecting at 2s,4s,8s,16s,32s,60s,60s till 300 times. each trial is timeout after 30s of connection attempt
kws_data.on_ticks = read_from_socket_to_buffer
kws_data.on_connect = on_connect
kws_data.connect(threaded=True)
kws_data.connect()
# main function ends here