Hello all, I am able to connect to the websocket and get LTP quotes: [{'instrument_token': 3465729, 'last_price': 468.8, 'mode': 'ltp', 'tradeable': True}
Can someone show me the way to parse and get the instrument and the last price? That is all I need. This is what I have: def on_tick(tick, ws): print tick
and it prints: [{'instrument_token': 3465729, 'last_price': 468.8, 'mode': 'ltp', 'tradeable': True}
How do I parse to get the last price into an double variable?
@stg the response is array of ticks for your subscribed instruments. For example if you have subscribed you may get ticks for one or more instruments at a given time and you should loop through the ticks array to get the individual instruments ticks.
# Callback for tick reception. def on_tick(tick, ws): print tick tickLength=len(tick) for i in range(0,tickLength): instName=tick[i]['instrument_token'] lastPrice=tick[i]['last_price'] print instName print lastPrice
def on_connect(ws): # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).738561, 5633 ws.subscribe([3465729]) ws.set_mode(ws.MODE_LTP, [3465729])
# Assign the callbacks. kws.on_tick = on_tick kws.on_connect = on_connect
#kws.on_tick = _parse_binary # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect()
and the response is: NameError: name 'tick' is not defined
# Callback for tick reception. def on_tick(ticks, ws): for tick in ticks: instName=tick['instrument_token'] lastPrice=tick['last_price'] print instName print lastPrice
def on_connect(ws): # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).738561, 5633 ws.subscribe([3465729]) ws.set_mode(ws.MODE_LTP, [3465729])
# Assign the callbacks. kws.on_tick = on_tick kws.on_connect = on_connect
#kws.on_tick = _parse_binary # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. kws.connect()
for i in range(0,tickLength):
instName=tick[i]['instrument_token']
lastPrice=tick[i]['last_price']
print instName
print lastPrice
NameError: name 'tickLength' is not defined
Did I put it in the correct place?