Python defenition code to get instrument token by exchange name and symbol:
def get_instrument_by_symbol(exchange, symbol): global symbol_dict,u,token_dict,global_instrument_var if (exchange not in symbol_dict.keys()): symbol_dict[exchange]={}
if (symbol in symbol_dict[exchange].keys()): token_dict[symbol_dict[exchange][symbol]]= symbol return symbol_dict[exchange][symbol] else: #global_instrument_var = u.instruments(exchange) instrument_token = next((sub for sub in global_instrument_var if sub['tradingsymbol'] == symbol), None)['instrument_token'] symbol_dict[exchange][symbol] = instrument_token token_dict[instrument_token]=symbol return instrument_token
.265256UPDATE: Nifty50's instrument token is 256265.
256265,1001,NIFTY 50,NIFTY 50,0.0,,0.0,0.0,0,EQ,NSE-INDICES,NSE
263177 NIFTY SERV SECTOR NIFTY SERV SECTOR
258057 LIX 15 LIX 15
258313 CPSE INDEX CPSE INDEX
262665 NIFTY PSE NIFTY PSE
264969 INDIA VIX INDIA VIX
260873 NIFTY MIDCAP 50 NIFTY MIDCAP 50
257545 NIFTY CONSUMPTION NIFTY CONSUMPTION
263689 NIFTY METAL NIFTY METAL
260361 NIFTY JUNIOR NIFTY JUNIOR
260617 NIFTY 100 NIFTY 100
258569 NI15 NI15
265481 NV20 NV20
261641 NIFTY ENERGY NIFTY ENERGY
256265 NIFTY 50 NIFTY 50
256777 NIFTY MIDCAP 100 NIFTY MIDCAP 100
259337 NIFTY50 TR 2X LEV NIFTY50 TR 2X LEV
261897 NIFTY FMCG NIFTY FMCG
258825 NIFTY50 PR 2X LEV NIFTY50 PR 2X LEV
259081 NIFTY50 PR 1X INV NIFTY50 PR 1X INV
260105 NIFTY BANK NIFTY BANK
262921 NIFTY PSU BANK NIFTY PSU BANK
261129 NIFTY REALTY NIFTY REALTY
259849 NIFTY IT NIFTY IT
257289 NIFTY COMMODITIES NIFTY COMMODITIES
262409 NIFTY PHARMA NIFTY PHARMA
257033 NIFTY DIV OPPS 50 NIFTY DIV OPPS 50
263433 NIFTY AUTO NIFTY AUTO
263945 NIFTY MEDIA NIFTY MEDIA
264713 HANGSENG BEES-NAV HANGSENG BEES-NAV
257801 NIFTY FIN SERVICE NIFTY FIN SERVICE
264457 NIFTY 200 NIFTY 200
262153 NIFTY MNC NIFTY MNC
259593 NIFTY50 TR 1X INV NIFTY50 TR 1X INV
265225 NIFTY50 DIV POINT NIFTY50 DIV POINT
261385 NIFTY INFRA NIFTY INFRA
264201 NIFTY SMALL 100 NIFTY SMALL 100
exchange as NSE
andsegment field as INDICES
from the instrument dump.def get_instrument_by_symbol(exchange, symbol):
global symbol_dict,u,token_dict,global_instrument_var
if (exchange not in symbol_dict.keys()):
symbol_dict[exchange]={}
if (symbol in symbol_dict[exchange].keys()):
token_dict[symbol_dict[exchange][symbol]]= symbol
return symbol_dict[exchange][symbol]
else:
#global_instrument_var = u.instruments(exchange)
instrument_token = next((sub for sub in global_instrument_var if sub['tradingsymbol'] == symbol), None)['instrument_token']
symbol_dict[exchange][symbol] = instrument_token
token_dict[instrument_token]=symbol
return instrument_token
extracting Bank nifty LTP and line of code for same is
NIFTY_BANK_LTP = u.ltp(get_instrument_by_symbol('NSE', 'NIFTY BANK'))[str(get_instrument_by_symbol('NSE', 'NIFTY BANK'))]['last_price']