How to Install Python on Windows 10

Do you want to learn how to install Python 3.10 on Windows 10 Computer in depth so you can install it in any computer without help. Then, you are at the right place, just keep reading till the end and you will be able to do it yourself. What is Python? Python is a powerful, […]

Continue reading...

Recent Post

31 May 2025

How to Create a Virtual Machine in VirtualBox and Install Ubuntu

Continue reading

Featured Post

25 May 2023

How to Fetch Stock’s Historical Data From NSE India in Python

Continue reading

If you’re working on financial analytics, trading strategies, or real-time dashboards, having access to updated index data like NIFTY 50, BANK NIFTY, NIFTY IT, etc., is essential. In this guide, we’ll show you how to fetch index data from NSE India using Python with a simple and effective script.

Prerequisites

Before we dive into the code, make sure you have installed Python 3.8+ with the required Python libraries:

pip install pandas requests

What This Script Does

This Python script connects to the official NSE India website, fetches live equity index data, and gives you the option to save it directly to a CSV file. You can retrieve:

  • NIFTY 50 and sectoral indices like NIFTY IT, NIFTY BANK, NIFTY PHARMA, etc.
  • Real-time data such as Open, High, Low, LTP (Last Traded Price), Change %, and more.
  • Export data to CSV with a custom filename.

Step-by-Step Breakdown of the Script

Step 1: Import Required Libraries

import pandas as pd
import requests
  • pandas: To store and export tabular index data.
  • requests: To send HTTP requests to NSE India servers.

Step 2: Create the IndexData Class

class IndexData:
    def __init__(self) -> None:
        self.session = requests.sessions.Session()
        self.session.headers['User-Agent'] = 'Mozilla/5.0 ...'
        self.session.get('https://nseindia.com/', timeout=10)
        self.url = 'https://www.nseindia.com/api/equity-stockIndices?index='
  • Initializes a session that mimics a browser request.
  • NSE requires a valid User-Agent to return data.

Step 3: Fetch Index Data from NSE

def fetch_index_from_nse(self, index_symbol):
    ...
  • Accepts index names like 'NIFTY 50', 'NIFTY IT', etc.
  • Calls the NSE API and normalizes JSON response into a DataFrame.
  • Returns a structured table of index constituents and values.

Step 4: Save the Data to CSV

def save_index_to_csv(self, index_symbol, csv_file_name=None, ...)
  • Allows you to save the DataFrame to a CSV file.
  • You can set custom delimiter, file name, and whether to include headers/index.

Step 5: Run the Script

if __name__ == '__main__':
    obj = IndexData()
    print(obj.fetch_index_from_nse('NIFTY 50'))
    if obj.save_index_to_csv('NIFTY IT'):
        print('CSV file saved')
    else:
        print('Unable to save CSV file')
  • Fetches data for NIFTY 50 and displays it.
  • Saves NIFTY IT index data to NIFTY IT.csv

The Complete Source Code

List of Popular NSE Index Symbols

Use these with the fetch_index_from_nse() method:

  • NIFTY 50
  • NIFTY NEXT 50
  • NIFTY BANK
  • NIFTY IT
  • NIFTY MIDCAP 100

Tips & Troubleshooting

  • Use valid User-Agent strings or you may get blocked.
  • Don’t send too many requests in a short time — NSE may temporarily block IPs.
  • Use try-except blocks to gracefully handle errors for production use.

Conclusion

Fetching real-time index data from NSE India using Python is a powerful capability for traders, analysts, and developers alike. With just a few lines of code, you can access detailed information about various indices such as NIFTY 50, BANK NIFTY, NIFTY IT, and more — directly from the source.

This method is efficient, reliable, and ideal for:

  • Creating automated dashboards
  • Performing technical or fundamental analysis
  • Exporting daily snapshots to CSV for record-keeping or backtesting
  • Automate CSV downloads for multiple indices
  • Visualize data using matplotlib or plotly

Whether you’re building an algo trading system or a market analysis tool, this script gives you the flexibility and accuracy you need.

Start customizing the script today to suit your own market insights and strategies!

Next you can learn “Fetch Future and Option data of any Index or Stock from NSE India in Python

If you are interest in more guides related to trading, you can visit here trading.

And for more python guides visit here Python.

You can explore more informative videos at my YouTube Channel.



About

In TutLogic, we want to share our knowledge with all of the Internet community and welcome your feedback as well. Whether you want to learn with videos or textual articles, it is all available for everyone at tutlogic.com and at our YouTube Channel.