Getting List of All FNO Stocks Using Python Zerodha API

Although We can use the oneliner fnolist() function from NSEPython API, Lets use Zerodha API to get list of all FNO stocks as a diversification. In case You want to see fnolist() function – 

Downloading Entire Instruments List

First, let’s download the entire instrument list from Zerodha and save it as a portable database. We will use the pandas library for this task, making data manipulation seamless. The instrument list is available at https://api.kite.trade/instruments in CSV format. So, let’s declare a variable named “instrumentList” to harness the power of pandas in managing this crucial trading data.
				
					import pandas as pd

instrumentList = pd.read_csv("https://api.kite.trade/instruments")
print(instrumentList)
				
			
The output will be –
				
					Triggered at	Count	Stocks (new stocks are highlighted)	Trigger Date	Trigger Time
0	Fri Apr 21 2023, 10:07 am	1	APOLLOTYRE	2023-04-21	10:07:00
1	Fri Apr 21 2023, 9:58 am	1	ASIANPAINT	2023-04-21	09:58:00
2	Thu Apr 20 2023, 3:21 pm	1	TATACONSUM	2023-04-20	15:21:00
3	Thu Apr 20 2023, 2:16 pm	1	BAJAJ-AUTO	2023-04-20	14:16:00
4	Thu Apr 20 2023, 12:49 pm	1	CUB	2023-04-20	12:49:00
...	...	...	...	...	...
775	Tue Sep 13 2022, 10:03 am	3	DIXON	2022-09-13	10:03:00
776	Tue Sep 13 2022, 10:03 am	3	DRREDDY	2022-09-13	10:03:00
777	Tue Sep 13 2022, 10:03 am	3	HEROMOTOCO	2022-09-13	10:03:00
778	Tue Sep 13 2022, 10:01 am	2	DRREDDY	2022-09-13	10:01:00
779	Tue Sep 13 2022, 10:01 am	2	HEROMOTOCO	2022-09-13	10:01:00
780 rows × 5 columns
				
			

Filtering F&O Stocks

				
					fno_stocks = list(set(instrumentList[instrumentList['segment'] == 'NFO-FUT']["name"].to_list()))

				
			
  • instrumentList[instrumentList['segment'] == 'NFO-FUT'] filters the DataFrame to only include rows where the ‘segment’ column has the value ‘NFO-FUT’. This filtering step narrows down the DataFrame to F&O stocks in the NSE F&O Futures segment.
  • .to_list() converts the filtered DataFrame column into a Python list.
  • list(set(...)) is used to remove any duplicate entries and ensure that each stock name appears only once in the list. The result is assigned to the variable fno_stocks.

Building the Symbol List

				
					symbol_list = list(set(instrumentList[instrumentList['segment'] == 'NFO-FUT']["tradingsymbol"].to_list()))
symbol_list
				
			
  • This code initializes an empty list called symbol_list.
  • It then iterates through each stock name in fno_stocks. Zerodha follows a certain nomenclauture, so it is adhering to it. Now one can write the same above code in single line too! 
				
					symbol_list = ['NSE:' + {'NIFTY': 'NIFTY 50', 'BANKNIFTY': 'NIFTY BANK', 'FINNIFTY': 'NIFTY FIN SERVICE'}.get(stocks.upper(), stocks.upper()) for stocks in fno_stocks]

				
			
Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

×Close