A complete guide to automating historical stock data collection from the Indian stock market.
Introduction
For retail investors, traders, data scientists, and finance students, access to clean and structured historical stock data is invaluable.
While many APIs exist, few are as convenient and free as Yahoo Finance, especially when paired with Python’s powerful yfinance
library.
In this article, I’ll walk you through how to:
- Fetch all NSE-listed stocks’ End of Day (EOD) data
- Use EQUITY_L.csv, an official list of NSE stocks
- Download 20 years of data and save them to CSVs for local use
It is useful:
If you’re:
- A quant trying to backtest trading strategies
- A developer building financial dashboards
- A student working on a finance project
- Or just a curious investor…
Then automating the retrieval of 20 years of historical EOD data for all NSE stocks is a powerful capability.
Step 1: Download EQUITY_L.csv
from NSE
Visit the official NSE website and download the full list of equity securities: https://www.nseindia.com/market-data/securities-available-for-trading
Look for a file named :
Securities available for Equity segment (.csv)
This file contains all actively listed equities on NSE, including their SYMBOLs, which are required by yfinance
.
After downloading, save this file as EQUITY_L.csv
in your project folder.
Step 2: Fetch EOD Data with yfinance
Install yfinance
if you haven’t:
pip install yfinance
Then use the following Python script
import yfinance as yf
import pandas as pd
import os
# Create directory to store EOD files
os.makedirs("EOD", exist_ok=True)
# Load NSE equity symbols
equity_details = pd.read_csv("EQUITY_L.csv")
# Loop through each symbol
for name in equity_details.SYMBOL:
try:
print(f"Fetching {name}...")
data = yf.download(f"{name}.NS", period="20y")
if not data.empty:
data.to_csv(f"./EOD/{name}.csv")
else:
print(f"No data for {name}")
except Exception as e:
print(f"{name} ===> {e}")
What’s Happening Behind the Scenes?
- We’re looping through every stock symbol in the
EQUITY_L.csv
file. .NS
is appended to each symbol to match Yahoo Finance’s naming convention for NSE stocks.yf.download()
fetches 20 years of daily OHLCV (Open, High, Low, Close, Volume) data.- Each stock’s data is saved as a CSV file inside a local folder named
EOD
.
Folder Structure After Execution
project_folder/
│
├── EQUITY_L.csv
├── fetch_stocks.py
└── EOD/
├── INFY.csv
├── RELIANCE.csv
├── TCS.csv
└── ...
What Can You Build Next?
- Backtest trading strategies with historical data
- Train ML models for stock forecasting
- Build dashboards in Streamlit or Dash
- Perform data cleaning and feature engineering
Conclusion
In just a few lines of Python, you’ve automated the collection of 20 years of EOD data for all NSE stocks. This project forms the foundation of many advanced financial applications, from algorithmic trading to AI-driven market analytics.
Learn more How to Download All NSE EOD Stock Data Using Python and YFinance