Open Jignesh108108 opened 4 months ago
Try without using the client.quotes and just use client.subscribe method.
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
Check this if it works: live_data = message['data']
and then print live_data.. If it works??
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.
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']
Still not working sir
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']
its still not working 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)
have you linked your on message method to client i don't see the below line in your code
client.on_message = on_message
yes it is there in above cell but still not working. I dont know how others are doing
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..
not working sir still it giving [] empty list only
Use threading , run the websocket in a separate thread and use a while true loop inside the thread to keep it running.
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']
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.