Subscribed instruments missing in the ticks

kapilaggr
kapilaggr edited October 2019 in Python client
Hi All,

I am trying to get the price of two symbols from the same tick using the following code(the instruments appear in the ticks in the order of subscription)-
inst_token = [13206274,13260546]
def on_connect(ws, response):
ws.subscribe(inst_token)
ws.set_mode(ws.MODE_FULL, inst_token)


def on_ticks(ws, ticks):
if (ticks[0]['instrument_token'] == 13206274):
symobl1_price = ticks[0]['last_price'] ##### get symbol1 tick price
print(symobl1_price)
symobl1_sell_price= ticks[0]['depth']['buy'][0]['price']
symobl1_buy_price = ticks[0]['depth']['sell'][0]['price']
if (ticks[1]['instrument_token'] == 13260546):
symobl2_price = ticks[1]['last_price'] ##### get symbol2 tick price
print(symobl2_price)
symobl2_sell_price = ticks[1]['depth']['buy'][0]['price']
symobl2_buy_price = ticks[1]['depth']['sell'][0]['price']

This code runs fine for a few ticks,but soon gives an error in this line-

symobl2_price = ticks[1]['last_price'] ,list index out of range.

Does this mean that the tick is showing the price of only one symbol?I have subscribed to two instruments in the on_connect function,why does this then give the data of only one symbol in the tick?

To counter this problem,I used the following approach--
 if len(ticks) == 2:
if (ticks[0]['instrument_token'] == 13206274):
symobl1_price = ticks[0]['last_price'] ##### get axis tick
print(symobl1_price)
symobl1_sell_price= ticks[0]['depth']['buy'][0]['price']
symobl1_buy_price = ticks[0]['depth']['sell'][0]['price']
if (ticks[1]['instrument_token'] == 13260546):
symobl2_price = ticks[1]['last_price'] ##### get icici tick
print(symobl2_price)
symobl2_sell_price = ticks[1]['depth']['buy'][0]['price']
symobl2_buy_price = ticks[1]['depth']['sell'][0]['price']

else:
print('Hi from else')
When i run this code,i can see that it goes into the else block frequently.

Any idea why this happens,and a better way achieve the same output.
  • sujith
    sujith edited October 2019
    This is not the right approach. If you have four instruments tomorrow then you will end up writing 4 if statements.
    Once you subscribe for a tick, the first time you will get a cached tick for every subscribed token if a tick exists at the Kite Ticker. Post that, you will receive ticks only when there is a change in any of the values.

    An ideal way is to check the length of the incoming array of ticks and then loop those many times. Each tick object will have an instrument token value using which you can do the operations.
  • sujith
    You can take a look at the documentation to know more.
  • kapilaggr
    Hi Sujith,

    Thank you for your response.I will update the code accordingly.

    Thanks a lot!
Sign In or Register to comment.