Kotak-Neo / kotak-neo-api

99 stars 84 forks source link

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

Open kumarvipulkashyap opened 1 week ago

kumarvipulkashyap commented 1 week 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 1 week ago

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

kumarvipulkashyap commented 1 week ago

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

crypt0inf0 commented 1 week ago

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

Cloudman0011 commented 1 week 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..