# Prepare token mapping instrument_dump = kite.instruments("NSE") token_map = {item['tradingsymbol']: item['instrument_token'] for item in instrument_dump if item['tradingsymbol'] in nifty50_stocks}
# File to save excel_filename = "nifty50_live_ticks.xlsx"
def on_ticks(ws, ticks): global live_data for tick in ticks: instrument_token = tick['instrument_token'] for tradingsymbol, token in token_map.items(): if token == instrument_token: last_price = tick['last_price'] live_data[tradingsymbol] = last_price
# Save live data to Excel if live_data: df = pd.DataFrame(list(live_data.items()), columns=["Stock", "LTP"]) df = df.sort_values("Stock") df.to_excel(excel_filename, index=False) print("Excel updated at", time.strftime("%H:%M:%S"))