Parsing streaming data from Websocket

stg
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}

I have read the docs and have been trying for a while to parse the incoming data(above).
https://kite.trade/docs/connect/v1/#connecting-to-the-websocket

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?

Thanks for your help in advance.
  • gully
    tickLength=len(tick)
    for i in range(0,tickLength):
    instName=tick[i]['instrument_token']
    lastPrice=tick[i]['last_price']
    print instName
    print lastPrice
  • stg
    stg edited September 2016
    Thanks for the quick reply. I put your code like this:
    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
    and the response was this:
    NameError: name 'tickLength' is not defined

    Did I put it in the correct place?
  • gully
    the code looks good..try again..see if you are getting the same error.
  • Vivek
    @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.
  • stg
    stg edited September 2016
    from kiteconnect import WebSocket

    kws = WebSocket("")

    # 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

    Still cant get the data to parse, any thoughts?
  • Vivek
    Please try this.
    from kiteconnect import WebSocket

    kws = WebSocket("")

    # 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()
  • stg
    Thank you very much, that works very well.
This discussion has been closed.