Kotak-Neo / kotak-neo-api

120 stars 112 forks source link

How to Extract the ltp data coming from websocket.? #199

Open kumarvipulkashyap opened 4 months ago

kumarvipulkashyap commented 4 months ago

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

crypt0inf0 commented 4 months ago

Ref https://github.com/Kotak-Neo/kotak-neo-api/issues/110, https://github.com/Kotak-Neo/kotak-neo-api/issues/84

kumarvipulkashyap commented 4 months ago

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

crypt0inf0 commented 4 months ago

Try client.on_message = on_message from https://github.com/Kotak-Neo/kotak-neo-api/pull/141 Maybe it will work.

Cloudman0011 commented 4 months ago

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..