Kotak-Neo / kotak-neo-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Extract the ltp data coming from websocket.?

kumarvipulkashyap opened this issue · comments

I want use ltp data from websocket to calculate live Pnl. Can someone explain it with an example code, i am unable do it.

Does that work after the update because i have done something similar to that and it hasn't worked for me.

Try client.on_message = on_message from #141 Maybe it will work.

Define a global variable live_data then go to the on_message function:

def on_message(message):
    global live_data
    live_data = message['data']

Now whenever you want to run check the live_data, run a loop over the data. Match the token of your instrument and if it matches then that's your ltp.

for data in live_data:               
                if data['tk'] == selected_token:
                             index_ltp = float(data['ltp'])

To calculate PNL you have to store the entry_price and other instrument details and calculate it with the live data..