A Python-based data downloader for NSE, India
Project description
PKNSETools
A comprehensive Python library for fetching stock market data from the National Stock Exchange (NSE) of India and NASDAQ.
Table of Contents
- What is PKNSETools?
- Installation
- Quick Start
- Architecture Overview
- Core Modules
- API Reference
- Index Maps
- Contributing
- Related Projects
What is PKNSETools?
PKNSETools provides tools for fetching and analyzing stock market data from NSE India. Key features include:
- ๐ Multi-Index Support - Nifty 50, Nifty Next 50, Nifty 500, and more
- ๐ Real-Time Intraday Data - Live market data during trading hours
- ๐ Historical Data - Up to 3 years of historical OHLCV data
- ๐ Multiple Sources - NSE official API, archives, and GitHub cache
- ๐ High-Performance Integration - Works with PKBrokers for real-time data
- ๐ฑ NASDAQ Support - Fetch NASDAQ index data
- โญ Morningstar Integration - Fair value and stock ratings
This library is part of the PKScreener ecosystem.
Installation
From PyPI
pip install PKNSETools
From Source
git clone https://github.com/pkjmesra/PKNSETools.git
cd PKNSETools
pip install -r requirements.txt
pip install -e .
Requirements
- Python 3.9+
- See
requirements.txtfor dependencies
Quick Start
Fetch Stock Data
from PKNSETools import nseStockDataFetcher
# Initialize fetcher
fetcher = nseStockDataFetcher()
# Fetch OHLCV data for a stock
df = fetcher.fetchStockData(
stockCode="RELIANCE",
period="1y", # 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, max
interval="1d" # 1m, 5m, 15m, 30m, 60m, 1d, 1wk, 1mo
)
print(df.head())
Get Index Constituents
from PKNSETools import nseStockDataFetcher
fetcher = nseStockDataFetcher()
# Get Nifty 50 stocks
nifty50_stocks = fetcher.fetchStockCodes(1) # 1 = Nifty 50
# Get all NSE stocks
all_stocks = fetcher.fetchStockCodes(12) # 12 = All equities
Use NSE API Directly
from PKNSETools.Benny.NSE import NSE
nse = NSE(download_folder="./data")
# Get stock quote
quote = nse.quote("RELIANCE")
print(f"LTP: {quote['priceInfo']['lastPrice']}")
# Get option chain
chain = nse.optionChain("NIFTY")
# Get market status
status = nse.marketStatus()
Architecture Overview
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PKNSETools Architecture โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Application Layer โ โ
โ โ PKScreener | Custom Applications โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ nseStockDataFetcher โ โ
โ โ (Unified data fetcher with source auto-selection) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ โ
โ โโโโโโผโโโโโ โโโโโโโโโผโโโโโโโโ โโโโโโโโโผโโโโโโโโ โ
โ โPKBrokersโ โ NSE API โ โ yfinance โ โ
โ โ(Real- โ โ (Official) โ โ (Fallback) โ โ
โ โ time) โ โ โ โ โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Core Modules โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ NSE (Benny) โ Intra_Day โ PKCompanyStock โ PKAllStocksโ โ
โ โ PKNasdaqIndex โ Morningstar โ PKCompanyGeneral โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Data Sources โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ NSE India API โ NSE Archives โ GitHub Cache โ yfinance โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Core Modules
1. NSE Stock Data Fetcher
The main interface for fetching stock data with automatic source selection.
from PKNSETools import nseStockDataFetcher
fetcher = nseStockDataFetcher()
fetchStockData(stockCode, period, interval, start, end)
Fetch OHLCV data for a stock.
Parameters:
stockCode(str): NSE symbol (e.g., "RELIANCE", "TCS")period(str): Data period - "1d", "5d", "1mo", "3mo", "6mo", "1y", "2y", "5y", "max"interval(str): Candle interval - "1m", "5m", "15m", "30m", "60m", "1d", "1wk", "1mo"start(datetime, optional): Start dateend(datetime, optional): End date
Returns: pd.DataFrame with Date, Open, High, Low, Close, Volume columns
Example:
# Last 1 year daily data
df = fetcher.fetchStockData("INFY", period="1y", interval="1d")
# Last 5 days 15-minute data
df = fetcher.fetchStockData("TCS", period="5d", interval="15m")
# Custom date range
from datetime import datetime
df = fetcher.fetchStockData(
"HDFC",
start=datetime(2024, 1, 1),
end=datetime(2024, 6, 30),
interval="1d"
)
Data Source Priority
- PKBrokers Real-time (if available and market is open)
- NSE Official API (primary source)
- yfinance (fallback)
fetchStockCodes(index, stockCode=None)
Fetch stock codes for an index.
Parameters:
index(int): Index identifier (see Index Maps)stockCode(str, optional): Filter for specific stock
Returns: List of stock codes
Example:
# Get Nifty 50 constituents
nifty50 = fetcher.fetchStockCodes(1)
print(f"Nifty 50 stocks: {len(nifty50)}")
# Get all NSE equities
all_stocks = fetcher.fetchStockCodes(12)
print(f"Total NSE stocks: {len(all_stocks)}")
# Get F&O stocks
fno_stocks = fetcher.fetchStockCodes(14)
fetchLatestNiftyDaily(proxyServer=None)
Fetch latest Nifty 50 index data.
Returns: pd.DataFrame with index data
fetchNiftyCodes(niftyIndex, proxyServer=None)
Fetch Nifty index constituents from GitHub cache.
2. NSE API (Benny)
Direct interface to NSE India's official API.
from PKNSETools.Benny.NSE import NSE
nse = NSE(download_folder="./data")
Stock Quotes
# Get detailed quote
quote = nse.quote("RELIANCE")
print(f"LTP: {quote['priceInfo']['lastPrice']}")
print(f"Change: {quote['priceInfo']['change']}")
print(f"% Change: {quote['priceInfo']['pChange']}%")
# Get quote with trade info
trade_info = nse.quote("TCS", trade_info=True)
Option Chain
# Get option chain for index
chain = nse.optionChain("NIFTY")
# Get option chain for stock
chain = nse.optionChain("RELIANCE", optionType="stock")
# Available indices: banknifty, nifty, finnifty, niftyit
Market Data
# Market status
status = nse.marketStatus()
# Trading holidays
holidays = nse.holidays()
# Advances/Declines
advances = nse.advanceDecline()
# Pre-open market data
preopen = nse.preOpen()
# Market turnover
turnover = nse.marketTurnover()
Index Data
# Get all indices
indices = nse.allIndices()
# Get specific index data
nifty50 = nse.indexData("NIFTY 50")
# Get index constituents
constituents = nse.indexStocks("NIFTY 50")
Block/Bulk Deals
# Block deals
block = nse.blockDeal()
# Bulk deals
bulk = nse.bulkDeal()
Historical Data
# Get historical data
history = nse.equityHistory(
symbol="RELIANCE",
series="EQ",
from_date="01-01-2024",
to_date="30-06-2024"
)
Download Reports
# Download bhavcopy
nse.bhavCopyFull(date="2024-12-20")
# Download index report
nse.indexReport(date="2024-12-20")
3. Intraday Data
Real-time intraday data during market hours.
from PKNSETools import Intra_Day
# Initialize for a stock
intraday = Intra_Day("RELIANCE")
# Get intraday data (9:00 AM to now)
timestamps, prices = intraday.intraDay()
# For NIFTY indices
nifty_intra = Intra_Day("NIFTY 50")
timestamps, prices = nifty_intra.nifty_intraDay()
Features
- Rate-limited (3 requests/second)
- Session-based with cookie handling
- Works during market hours (9:15 AM - 3:30 PM IST)
4. Historical Data
Fetch up to 3 years of historical data.
from PKNSETools import get_Company_History_Data, get_nifty_History_Data
# Company historical data
df = get_Company_History_Data(
company="RELIANCE",
from_date="01-01-2023",
to_date="31-12-2023"
)
# Nifty index historical data
df = get_nifty_History_Data(
indexName="NIFTY 50",
from_date="01-01-2023",
to_date="31-12-2023"
)
5. All Stocks Data
Fetch daily report for all stocks.
from PKNSETools import getTodayData
# Get today's data for all stocks
nifty_data, companies_data = getTodayData()
# Returns tuple:
# - nifty_data: NIFTY index performance
# - companies_data: All company data with OHLCV, volume, etc.
6. NASDAQ Index
Fetch NASDAQ index data.
from PKNSETools.Nasdaq.PKNasdaqIndex import PKNasdaqIndex
nasdaq = PKNasdaqIndex()
# Get NASDAQ-100 constituents
constituents = nasdaq.get_nasdaq100()
# Get NASDAQ Composite data
composite = nasdaq.get_nasdaq_composite()
7. Morningstar Tools
Integration with Morningstar for fundamental data.
from PKNSETools.morningstartools import PKMorningstarDataFetcher
# Initialize fetcher
ms = PKMorningstarDataFetcher()
# Get stock fair value
fair_value = ms.get_fair_value("RELIANCE")
# Get stock rating
rating = ms.get_stock_rating("TCS")
# Get mutual fund data
mf_data = ms.get_mutual_fund("HDFC Equity Fund")
Available Data
- Fair value estimates
- Star ratings
- Analyst reports
- Financial ratios
- Mutual fund performance
API Reference
Main Exports
from PKNSETools import (
# Stock Data
nseStockDataFetcher,
# Historical Data
get_Company_History_Data,
get_nifty_History_Data,
# Intraday
Intra_Day,
# All Stocks
getTodayData,
# Constants
NSE_INDEX_MAP,
REPO_INDEX_MAP,
)
from PKNSETools.Benny.NSE import NSE
from PKNSETools.Nasdaq.PKNasdaqIndex import PKNasdaqIndex
from PKNSETools.morningstartools import PKMorningstarDataFetcher
Module Structure
PKNSETools/
โโโ __init__.py # Main exports
โโโ PKAllStocks.py # All stocks daily data
โโโ PKCompanyGeneral.py # Company general info
โโโ PKCompanyStock.py # Company historical data
โโโ PKConstants.py # URL constants and headers
โโโ PKIntraDay.py # Intraday data
โโโ PKNSEStockDataFetcher.py # Main stock data fetcher
โโโ Benny/
โ โโโ __init__.py
โ โโโ NSE.py # NSE API wrapper
โโโ Nasdaq/
โ โโโ __init__.py
โ โโโ PKNasdaqIndex.py # NASDAQ index tools
โโโ morningstartools/
โโโ __init__.py
โโโ PKMorningstarDataFetcher.py
โโโ funds.py # Mutual fund data
โโโ stock.py # Stock fundamental data
โโโ security.py # Security data
โโโ search.py # Search functionality
โโโ NSEStockDB.py # Stock database
โโโ NSEStockFairValueDB.py # Fair value database
โโโ NSEStockMFIDB.py # MFI database
Index Maps
NSE_INDEX_MAP (Direct NSE URLs)
| Index | Description | URL |
|---|---|---|
| 1 | Nifty 50 | ind_nifty50list.csv |
| 2 | Nifty Next 50 | ind_niftynext50list.csv |
| 3 | Nifty 100 | ind_nifty100list.csv |
| 4 | Nifty 200 | ind_nifty200list.csv |
| 5 | Nifty 500 | ind_nifty500list.csv |
| 6 | Nifty Smallcap 50 | ind_niftysmallcap50list.csv |
| 7 | Nifty Smallcap 100 | ind_niftysmallcap100list.csv |
| 8 | Nifty Smallcap 250 | ind_niftysmallcap250list.csv |
| 9 | Nifty Midcap 50 | ind_niftymidcap50list.csv |
| 10 | Nifty Midcap 100 | ind_niftymidcap100list.csv |
| 11 | Nifty Midcap 150 | ind_niftymidcap150list.csv |
| 12 | All Equities | EQUITY_L.csv |
| 14 | F&O Stocks | NSE_FO_SosScheme.csv |
REPO_INDEX_MAP (GitHub Cache)
Same indices but fetched from PKScreener's GitHub repository for reliability.
Constants
from PKNSETools.PKConstants import (
_base_domain, # "https://www.nseindia.com"
_headers, # Default request headers
_head, # Headers with cookies
_quote_url_path, # Quote API path
_chart_data_open_url, # Chart data URL
)
Contributing
Development Setup
git clone https://github.com/pkjmesra/PKNSETools.git
cd PKNSETools
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -e .
Running Tests
pytest test/
Code Style
ruff check PKNSETools/
ruff format PKNSETools/
Related Projects
- PKScreener - Stock screening application
- PKDevTools - Common development tools
- PKBrokers - Broker integration and real-time data
License
MIT License - see LICENSE file.
Disclaimer
This library is for educational and research purposes only. Always verify data accuracy before making investment decisions. The authors are not responsible for any financial losses incurred through the use of this software.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pknsetools-0.1.20260505.152.tar.gz.
File metadata
- Download URL: pknsetools-0.1.20260505.152.tar.gz
- Upload date:
- Size: 88.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6358348312e23e00291bb26dcce83afd1fd92048fd8693188bf77b9d1a36da4
|
|
| MD5 |
5d6ad9752e1e7ab66e4ea754ee3ca206
|
|
| BLAKE2b-256 |
f917ce68aa9862b90dc4d3e4e4aeb07f2dece6771e08b086e1afb56236216ceb
|
File details
Details for the file pknsetools-0.1.20260505.152-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: pknsetools-0.1.20260505.152-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 101.1 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1ad65539412a3e50cf1ace1cce83df261bef27f1670ece88e80ab030932a212
|
|
| MD5 |
2cb5fbba8057e1781a962f25769a7a35
|
|
| BLAKE2b-256 |
7818ae39d6acc1627a4ffb541f3c92996ba6076a6196d16b30051825445b7e56
|
File details
Details for the file pknsetools-0.1.20260505.152-cp312-cp312-manylinux2014_x86_64.whl.
File metadata
- Download URL: pknsetools-0.1.20260505.152-cp312-cp312-manylinux2014_x86_64.whl
- Upload date:
- Size: 100.5 kB
- Tags: CPython 3.12
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a531784e0202e3170252464300413b5c26815f5a287b2062d55cc3e2c641ea03
|
|
| MD5 |
6e453f8ddd72140af4778af734872857
|
|
| BLAKE2b-256 |
bc844f573796b52a6357e9547bf47f025475c2dd11bbb94fca5356b8c9c488f1
|
File details
Details for the file pknsetools-0.1.20260505.152-cp310-cp310-macosx_13_0_arm64.whl.
File metadata
- Download URL: pknsetools-0.1.20260505.152-cp310-cp310-macosx_13_0_arm64.whl
- Upload date:
- Size: 100.5 kB
- Tags: CPython 3.10, macOS 13.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0d35f5adc60be2561384c74b8c8be1988c353d0cd5131f6fb8c2487b4ba1f7b
|
|
| MD5 |
6e9aff1c96c0e2242155888e29c0ccae
|
|
| BLAKE2b-256 |
0fbb6157199e13b23a3561054726a82800d1d77a0040162b6cb3126d6ea28061
|
File details
Details for the file pknsetools-0.1.20260505.152-cp310-cp310-macosx_10_9_x86_64.whl.
File metadata
- Download URL: pknsetools-0.1.20260505.152-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 100.5 kB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c06eacc862009b5c42c62e9dce5356fd72036c78db6e0a48935bf90ec55e086
|
|
| MD5 |
cf41a11147953d0d86eceee163dc8d49
|
|
| BLAKE2b-256 |
da2cafe13494c5d56fdeb6bd55b9a0a12115e9bfc3fd6d467a77a08f542b36d0
|