I'm fetching quotes values by using Websocket, with two different code and getting different values.

1krishnagupta
My first Code is -
from kiteconnect import KiteConnect
import time
from kiteconnect import KiteTicker

kws = KiteTicker(zerodha_api_key,acc_tkn)

tokens = instrument_token #[instrumet_token List]

def conditions(ticks):
dataframes = []
for i in range(len(ticks)):
instrumnet_token = ticks[i]['instrument_token']
if instrumnet_token in tokens:
index_number = tokens.index(instrumnet_token)
strike_price = strike[index_number]

datetime = ticks[i]['exchange_timestamp']
bid_price = ticks[i]['depth']['buy'][0]['price']
ask_price = ticks[i]['depth']['sell'][0]['price']

print(instrumnet_token, " ", strike_price, " ", datetime, " ", bid_price, " ", ask_price)

output_df = pd.DataFrame(columns = ['Instrument_token', 'Strike', 'Datetime', 'Bid_price', 'Ask_price'])
c = 0
output_df.loc[c] = [instrumnet_token, strike_price, datetime, bid_price, ask_price]
dataframes.append(output_df)
else:
pass
print("-------------------------------------------------")
df = pd.concat(dataframes)
display(df)
print("\n\n")


def on_ticks(ws, ticks):
ticks = (ticks)
conditions(ticks)

def on_connect(ws, response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL,tokens)


kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect(threaded=True)
count=0
while True:
count+=1
if(count%2==0):
if kws.is_connected():
kws.set_mode(kws.MODE_FULL,tokens)
else:
if kws.is_connected():
kws.set_mode(kws.MODE_FULL,tokens)
time.sleep(1)
and Getting back to back output -


and my second code is this -
logging.basicConfig(level=logging.DEBUG)

kws = KiteTicker(zerodha_api_key,acc_tkn)

tokens = instrument_token
print(tokens)
print(strike)

def on_ticks(ws, ticks):
logging.debug("Ticks: {}".format(ticks))

def on_connect(ws, response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)

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 True:
#Perform required data operation using tick data
def on_ticks(ws, ticks):
# print(ticks, "\n\n\n")
helper_method(ticks)

def helper_method(ticks):
dataframes = []
for i in range(len(ticks)):
instrumnet_token = ticks[i]['instrument_token']
if instrumnet_token in tokens:
index_number = tokens.index(instrumnet_token)
strike_price = strike[index_number]

datetime = ticks[i]['exchange_timestamp']
bid_price = ticks[i]['depth']['buy'][0]['price']
ask_price = ticks[i]['depth']['sell'][0]['price']

# print(instrumnet_token, " ", strike_price, " ", datetime, " ", bid_price, " ", ask_price)

output_df = pd.DataFrame(columns = ['Instrument_token', 'Strike', 'Datetime', 'Bid_price', 'Ask_price'])
c = 0
output_df.loc[c] = [instrumnet_token, strike_price, datetime, bid_price, ask_price]
dataframes.append(output_df)
else:
pass
# print("-------------------------------------------------")
df = pd.concat(dataframes)
display(df)
print("-------------------------------------------------")
print("\n\n")

#Assign callback
kws.on_ticks=on_ticks
and I'm getting output this -


So I want to know why both values have different Datetime?
  • rakeshr
    A possible reason could be because of blocking of one ticker thread, adding a slight delay in ticker data consumption. You should try out the solution mentioned in your similar raised thread here.
This discussion has been closed.