Unofficial Python Api for NSE India stock exchange
Project description
💰 NseIndiaApi v2.0 - Enhanced Edition
An unofficial Python API for the NSE India stock exchange with modern features.
Python version: >= 3.8
⭐ NEW in v2.0: Async support, caching, CLI tool, better error handling, data export, and more!
🎯 Key Features
- ✅ Synchronous and Asynchronous API
- ✅ Built-in caching with TTL
- ✅ CLI tool for quick queries
- ✅ Export to CSV, JSON, Parquet
- ✅ Retry logic with exponential backoff
- ✅ Custom exceptions for better error handling
- ✅ Type hints and data models
- ✅ Comprehensive logging
- ✅ CI/CD pipeline
- ✅ 100% backward compatible with v1.x
📚 Documentation
https://bennythadikaran.github.io/NseIndiaApi
🚀 Quick Start
Installation
# Basic installation (local use)
pip install nse[local]
# Server environment (AWS, Azure, etc.)
pip install nse[server]
# All features (recommended)
pip install nse[all]
# Development
pip install nse[dev]
Basic Usage
from nse import NSE
with NSE(download_folder="./data") as nse:
# Get market status
status = nse.status()
# Get stock quote
quote = nse.equityQuote("HDFCBANK")
print(f"Price: {quote['close']}")
# Get top gainers
data = nse.listEquityStocksByIndex("NIFTY 50")
gainers = nse.gainers(data, count=5)
Async Usage (NEW!)
from nse.async_nse import AsyncNSE
import asyncio
async def main():
async with AsyncNSE("./data") as nse:
# Fetch multiple quotes concurrently
symbols = ["HDFCBANK", "INFY", "TCS", "RELIANCE"]
quotes = await nse.fetch_multiple_quotes(symbols)
for symbol, quote in quotes.items():
print(f"{symbol}: {quote['priceInfo']['lastPrice']}")
asyncio.run(main())
CLI Usage (NEW!)
# Get stock quote
nse quote HDFCBANK
# Market status
nse status
# Top gainers
nse gainers --index "NIFTY 50" --count 10
# Top losers
nse losers --index "NIFTY 50" --count 10
# Download bhavcopy
nse bhavcopy --date 2024-01-15 --type equity
Caching (NEW!)
from nse import NSE
from nse.cache import SimpleCache
from pathlib import Path
cache = SimpleCache(Path("./data"), default_ttl=300)
with NSE("./data") as nse:
symbol = "HDFCBANK"
# Try cache first
quote = cache.get(f"quote_{symbol}")
if not quote:
quote = nse.equityQuote(symbol)
cache.set(f"quote_{symbol}", quote, ttl=60)
print(quote)
Data Export (NEW!)
from nse import NSE
from nse.export import DataExporter
from pathlib import Path
with NSE("./data") as nse:
data = nse.listEquityStocksByIndex("NIFTY 50")
# Export to CSV
DataExporter.to_csv(data["data"], Path("nifty50.csv"))
# Export to JSON
DataExporter.to_json(data["data"], Path("nifty50.json"))
# Export to Parquet (requires pandas)
DataExporter.to_parquet(data["data"], Path("nifty50.parquet"))
Error Handling (NEW!)
from nse import NSE
from nse.exceptions import NSETimeoutError, NSEInvalidSymbol
with NSE("./data") as nse:
try:
quote = nse.quote("INVALID_SYMBOL")
except NSEInvalidSymbol as e:
print(f"Invalid symbol: {e}")
except NSETimeoutError as e:
print(f"Request timeout: {e}")
🆕 What's New in v2.0
1. Async Support
Fetch multiple quotes concurrently for better performance.
2. Caching Layer
Reduce API calls with built-in file-based cache with TTL.
3. CLI Tool
No coding required for basic operations.
4. Data Export
Export to CSV, JSON, and Parquet formats.
5. Better Error Handling
Custom exception classes for different error types.
6. Retry Logic
Automatic retry with exponential backoff.
7. Logging
Structured logging with file and console output.
8. Type Safety
Data models using dataclasses for better IDE support.
9. CI/CD Pipeline
Automated testing and code quality checks.
10. Enhanced Testing
Comprehensive test suite with async tests.
📊 Performance
- Async operations: 3-5x faster for bulk requests
- Caching: Reduces API calls by 60-80%
- Better connection pooling: Improved reliability
🔧 Development
# Clone repository
git clone https://github.com/BennyThadikaran/NseIndiaApi.git
cd NseIndiaApi
# Install in development mode
pip install -e .[dev]
# Run tests
pytest tests/ -v
# Code formatting
black src/
# Linting
ruff check src/
# Type checking
mypy src/
📝 API Limits
All requests through NSE are rate limited to 3 requests per second.
Best Practices:
- Download large reports after market hours
- Add 0.5-1 sec sleep between requests
- Save and reuse files instead of re-downloading
- Use caching for frequently accessed data
🔄 Migration from v1.x
Version 2.0 is 100% backward compatible. All v1.x code works without changes.
New features are additive and optional. See UPGRADE_NOTES.md for details.
📖 Examples
Check the examples/ directory for:
cache_example.py- Using cache to reduce API callsasync_example.py- Concurrent quote fetchingexport_example.py- Exporting data to various formats
🤝 Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new features
- Ensure all tests pass
- Submit a pull request
📄 License
GNU General Public License v3 (GPLv3)
🙏 Credits
Original Author & Creator: Benny Thadikaran
This project was originally created and maintained by Benny Thadikaran. All core functionality, API design, and original implementation are his work.
v2.0 Enhancements: Community contributions building upon Benny's excellent foundation
📞 Support
- Issues: GitHub Issues
- Documentation: Official Docs
If you ❤️ this project, please ⭐ the repo!
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 Distribution
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 nseindia_data-2.0.0.tar.gz.
File metadata
- Download URL: nseindia_data-2.0.0.tar.gz
- Upload date:
- Size: 32.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
686375e0a6ac5cf05656088253e94b395dc0ba5e6a26d24ab32928d8e6240452
|
|
| MD5 |
854e294f5b93f57050453ea481349d02
|
|
| BLAKE2b-256 |
449317f76031c341c48395eb9c6b84a1d8daeb0cce8f2a5e1cf04bbb6c7fe3c8
|
File details
Details for the file nseindia_data-2.0.0-py3-none-any.whl.
File metadata
- Download URL: nseindia_data-2.0.0-py3-none-any.whl
- Upload date:
- Size: 34.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a5a05d8498e9ddc39a93a331912c2fb2f0a159c69f5b6448c58205f174af07f
|
|
| MD5 |
c9aaf225bd09c0c0469fe707bce05144
|
|
| BLAKE2b-256 |
b3c7cfe728cafe0106d5d295f33f664ef4aa0c7f68014253ac611e537fc5b933
|