Kotak-Neo / kotak-neo-api

111 stars 100 forks source link

How to store live data in dictionary on_messege and my below code is not working #211

Open Jignesh108108 opened 2 months ago

Jignesh108108 commented 2 months ago

instrument_tokens = [{"instrument_token": "65490", "exchange_segment": "nse_fo"}, {"instrument_token": "65489", "exchange_segment": "nse_fo"}] client.quotes(instrument_tokens = instrument_tokens, quote_type="ltp", isIndex=False, session_token="", sid="",server_id="") client.subscribe(instrument_tokens = instrument_tokens, isIndex=False, isDepth=False) live_data = {} def on_message(message): global live_data try: print("Received message: (message}") for i in message: live_data[i['tk']] = i['ltp']

except Exception as e:
    print("Error in on_message: {e}")

After subscribing it is fetching live data and showing on jyupeter output but when I call live_data dictionary is it always empty dictionary that means its not storing live data in dictionary. I want to create 1 minute timeframe candlestick with from ltp so please help.

Cloudman0011 commented 2 months ago

Try without using the client.quotes and just use client.subscribe method.

Jignesh108108 commented 2 months ago

Get Quote details. #instrument_token=pSymbol

instrument_tokens = [{"instrument_token": "65490", "exchange_segment": "nse_fo"}, {"instrument_token": "65489", "exchange_segment": "nse_fo"},] client.subscribe(instrument_tokens=instrument_tokens, isIndex=False, isDepth=False) live_data = {} def on_message(message): global live_data try: print("Received message: (message}") for i in message: live_data[i['tk']] = i['ltp']

except Exception as e:
    print("Error in on_message: {e}")

Above code also not storing anything in live_data and it is showing empty dictionary only

Cloudman0011 commented 2 months ago

Check this if it works: live_data = message['data']

and then print live_data.. If it works??

Jignesh108108 commented 2 months ago

kotak Its not working sir. Please check attached screenshot. Please help sir I want ltp and tk live data to build 1 minutes timeframe candle data and SMA Crossover strategy for entry and exit. Please guide is there any other way to execute this strategy with live data.

Cloudman0011 commented 2 months ago

There is an extra ',' at the end before ] in instrument_tokens I did this and it works for me:

def on_message(message):
    global live_data
    live_data = message['data']
Jignesh108108 commented 2 months ago

Still not working sir kotak

Cloudman0011 commented 1 month ago

Remove everything after try. Just put this below code and then try printing live_data. If it is printing properly then you can perform whatever you want to do that with that data.. I also tried the above code, but it was not working for me.. def on_message(message): global live_data live_data = message['data']

Jignesh108108 commented 1 month ago

its still not working kotak and when I try with declaring live_data = {} before as below code its still giving empty {} only live_data = {} def on_message(message): global live_data live_data = message['data'] print(live_data)

sudhanshupati commented 1 month ago

have you linked your on message method to client i don't see the below line in your code

client.on_message = on_message

Jignesh108108 commented 1 month ago

yes it is there in above cell but still not working. I dont know how others are doing

Cloudman0011 commented 1 month ago

Declare live_data = [] instead of live_data = {} and then try.. That's the only difference I can see in my code.. And declare this variable where you are importing all the packages and not in any function..

Jignesh108108 commented 1 month ago

not working sir still it giving [] empty list only kotak

Yokai-2510 commented 1 month ago

Use threading , run the websocket in a separate thread and use a while true loop inside the thread to keep it running.