Hi All, I am new to kite API. I have been referring few codes to convert tick data to candle data.While running the below line of code ,I am getting a keyerror. My question is does tick data has a timestamp attribute?. How can we extract the time associated with a tick data. ltt=tick["timestamp"]
In the python client each tick received is a list of dictionaries as denoted in https://kite.trade/docs/pykiteconnect/v4/#kiteconnect.KiteTicker. Scroll down and search/look for the section below the line "Tick structure (passed to the on_ticks callback)" which looks something like the following on_ticks(ws, ticks) - Triggered when ticks are recevied.
ticks - List of tick object. Check below for sample structure.
So when you receive a ticks list object, a way to print/access the timestamp would be for eachtick in ticks: ltt = eachtick[ 'timestamp' ] instrumentToken = eachtick[ 'instrument_token' ]
Please note that if you do any of this within on_ticks method, you may end up losing ticks or getting disconnected because the on_ticks is blocked. Within on_ticks callback method you should only be receiving the ticks and pushing them to a queue/database. And any processing should be done in a separate thread or program.
Thanks very much. I tested the below lode in live market. I am able to get the instrument token and LTP but for timestamp its throwing an error. def main(): while True: try: event = EventQ.get(False) #If False means that the operation should not block, the method may return immediately with the next item from the queue if there is one, or it may raise an exception or return a specific value indicating that the queue is empty. except queue.Empty: continue else: if event.type=='TICK': ticks = event.data #Remember ticks are extracted from EventQ for tick in ticks: try: instrument=tick["instrument_token"] last_traded_price = tick["last_price"] ltt=tick["timestamp"] print(tick)
except Exception as e: print(e)
below is the tick I am getting .I don't see any timestamp property. Please help me to resolve this issue.
mode': 'quote' Try setting the mode to full. I use the full mode and I get timestamp as well as last trade time. I havent tried but I think in quote mode timestamp is not sent. I remember reading about this on some other thread.
Scroll down and search/look for the section below the line "Tick structure (passed to the on_ticks callback)"
which looks something like the following
on_ticks(ws, ticks) - Triggered when ticks are recevied.
ticks - List of tick object. Check below for sample structure.
[{
'instrument_token': 53490439,
'mode': 'full',
'volume': 12510,
'last_price': 4084.0,
'average_price': 4086.55,
'last_quantity': 1,
'buy_quantity': 2356
'sell_quantity': 2440,
'change': 0.46740467404674046,
'last_trade_time': datetime.datetime(2018, 1, 15, 13, 16, 54),
'timestamp': datetime.datetime(2018, 1, 15, 13, 16, 56),
'oi': 21845,
'oi_day_low': 0,
'oi_day_high': 0,
'ohlc': {
'high': 4093.0,
'close': 4065.0,
'open': 4088.0,
'low': 4080.0
},
'tradable': True,
'depth': {
'sell': [{
'price': 4085.0,
'orders': 1048576,
'quantity': 43
}, {
'price': 4086.0,
'orders': 2752512,
'quantity': 134
}, {
'price': 4087.0,
'orders': 1703936,
'quantity': 133
}, {
'price': 4088.0,
'orders': 1376256,
'quantity': 70
}, {
'price': 4089.0,
'orders': 1048576,
'quantity': 46
}],
'buy': [{
'price': 4084.0,
'orders': 589824,
'quantity': 53
}, {
'price': 4083.0,
'orders': 1245184,
'quantity': 145
}, {
'price': 4082.0,
'orders': 1114112,
'quantity': 63
}, {
'price': 4081.0,
'orders': 1835008,
'quantity': 69
}, {
'price': 4080.0,
'orders': 2752512,
'quantity': 89
}]
}
},
{<next tick data>},
{<next tick data>},
{<next tick data>},
...,
...]
So when you receive a ticks list object, a way to print/access the timestamp would be
for eachtick in ticks:
ltt = eachtick[ 'timestamp' ]
instrumentToken = eachtick[ 'instrument_token' ]
Please note that if you do any of this within on_ticks method, you may end up losing ticks or getting disconnected because the on_ticks is blocked.
Within on_ticks callback method you should only be receiving the ticks and pushing them to a queue/database. And any processing should be done in a separate thread or program.
def main():
while True:
try:
event = EventQ.get(False) #If False means that the operation should not block, the method may return immediately with the next item from the queue if there is one, or it may raise an exception or return a specific value indicating that the queue is empty.
except queue.Empty:
continue
else:
if event.type=='TICK':
ticks = event.data #Remember ticks are extracted from EventQ
for tick in ticks:
try:
instrument=tick["instrument_token"]
last_traded_price = tick["last_price"]
ltt=tick["timestamp"]
print(tick)
except Exception as e:
print(e)
below is the tick I am getting .I don't see any timestamp property. Please help me to resolve this issue.
{'tradable': True, 'mode': 'quote', 'instrument_token': 1270529, 'last_price': 1016.6, 'last_traded_quantity': 12, 'average_traded_price': 1022.44, 'volume_traded': 9916849, 'total_buy_quantity': 425161, 'total_sell_quantity': 1149720, 'ohlc': {'open': 1020.0, 'high': 1032.0, 'low': 1014.65, 'close': 1009.95}, 'change': 0.6584484380414849}
mode': 'quote'
Try setting the mode to full. I use the full mode and I get timestamp as well as last trade time. I havent tried but I think in quote mode timestamp is not sent. I remember reading about this on some other thread.tick['exchange_timestamp']