This Python tool was designed for fetching, analyzing, and visualizing option chain data from NSE India (National Stock Exchange of India). However, NSE India has updated their API endpoints to use dynamic URLs with additional security measures, making this implementation non-functional.
This repository is maintained for educational and reference purposes only.
NSE India API Changes: The National Stock Exchange of India has implemented significant changes to their API infrastructure:
- Dynamic Endpoints: The static API URLs this project relied on have been replaced with dynamic, session-based endpoints
- Enhanced Security: Additional authentication mechanisms and anti-bot measures have been implemented
- Rate Limiting: Stricter rate limiting and request validation
- Session Management: Complex session management requirements that require browser-like behavior
Real-time Data Fetching: β NO LONGER WORKS - NSE API endpoints deprecated- Advanced Analysis: β Still functional with mock/sample data
- Professional Visualizations: β Still functional with sample data
- Type-Safe: β Full type hints and static type checking with mypy
- Well-Tested: β Comprehensive test suite with pytest
- Modern Python: β Built for Python 3.12+ with modern practices
Visualize how implied volatility varies across different strike prices for a given expiry.
Analyze how implied volatility changes across different expiry dates for a specific strike.
View the distribution of open interest and changes in OI across the option chain.
# Clone the repository
git clone https://github.com/yourusername/OptionChain.git
cd OptionChain
# Install with uv
uv sync# Clone the repository
git clone https://github.com/yourusername/OptionChain.git
cd OptionChain
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Run the comprehensive example script (will show errors due to API issues)
uv run python main.py
# Or run the package directly (will show deprecation warnings)
uv run python -m optionchain
# Note: You'll see warnings that NSE API endpoints are deprecatedfrom optionchain import OptionChainAnalyzer
# Initialize the analyzer
analyzer = OptionChainAnalyzer()
# Fetch and analyze option chain data
option_chain = analyzer.analyze_symbol("NIFTY", oi_cutoff=100)
# Generate visualizations
analyzer.plot_volatility_skew(expiry_index=0) # Nearest expiry
analyzer.plot_term_structure() # ATM strike by default
analyzer.plot_open_interest_analysis() # OI distribution
# Get summary statistics
summary = analyzer.get_summary()
print(f"ATM Strike: {summary['atm_strike']}")
print(f"Total Call OI: {summary['total_call_oi']:,}")
print(f"Total Put OI: {summary['total_put_oi']:,}")from optionchain import OptionChain, NSEOptionFetcher
# Direct API access
fetcher = NSEOptionFetcher(timeout=30, max_retries=3)
option_chain = fetcher.fetch_option_chain("BANKNIFTY")
# Custom filtering
filtered_chain = option_chain.filter_by_oi(cutoff=500)
# Group by expiry
expiry_groups = option_chain.group_by_expiry()
for expiry_idx, data in expiry_groups.items():
print(f"Expiry {expiry_idx}: {len(data)} strikes")
# Group by strike
strike_groups = option_chain.group_by_strike()
atm_strike = option_chain.get_atm_strike()
print(f"ATM Strike: {atm_strike}")OptionChain/
βββ src/
β βββ optionchain/
β βββ __init__.py # Package exports
β βββ __main__.py # CLI entry point
β βββ analysis.py # Main analyzer class
β βββ data_fetcher.py # NSE API integration
β βββ models.py # Data models
β βββ visualization.py # Plotting utilities
βββ tests/
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
βββ main.py # Example implementation script
βββ pyproject.toml # Project configuration
βββ requirements.txt # Dependencies
βββ README.md # This file
# Clone and install with development dependencies
git clone https://github.com/yourusername/OptionChain.git
cd OptionChain
uv sync
# Install pre-commit hooks
uv run pre-commit install# Run all tests
uv run pytest
# Run with coverage
uv run pytest --cov=src --cov-report=html
# Run specific test file
uv run pytest tests/unit/test_models.py -v# Run linting
uv run ruff check src/ tests/
# Run type checking
uv run mypy src/
# Format code
uv run black src/ tests/
uv run isort src/ tests/# Generate API documentation
uv run pdoc --html --output-dir docs src/optionchainThe tool works with the following data structure:
@dataclass
class OptionData:
expiry: datetime
strike: float
call_oi: int
call_coi: int
call_iv: float
call_ltp: float
put_oi: int
put_coi: int
put_iv: float
put_ltp: float
@dataclass
class OptionChain:
security: str
underlying_price: float
data: pd.DataFrame
expiries: List[datetime]
strikes: List[float]The tool uses sensible defaults but can be customized:
# Custom timeout and retries
fetcher = NSEOptionFetcher(timeout=60, max_retries=5)
# Custom visualization settings
visualizer = OptionChainVisualizer(figsize=(20, 12))
# Analysis with custom OI cutoff
analyzer.analyze_symbol("NIFTY", oi_cutoff=1000)Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Run tests and ensure they pass
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Use type hints for all functions
- Follow PEP 8 style guidelines
- Write comprehensive docstrings
- Add tests for new features
- Ensure 80%+ test coverage
This project is licensed under the MIT License - see the LICENSE file for details.
- NSE India for providing the option chain data API
- The Python quantitative finance community
- Contributors and users of this tool
IMPORTANT: This project is NO LONGER FUNCTIONAL due to NSE India API changes. The data fetching capabilities do not work and will return errors.
Educational Use Only: This repository is maintained solely for educational purposes to demonstrate:
- Modern Python package structure and development practices
- Financial data analysis patterns and methodologies
- Option chain data modeling and visualization techniques
- Professional software engineering practices in quantitative finance
No Financial Advice: This tool was designed for educational and research purposes only. Always verify data accuracy and consult with financial professionals before making investment decisions. The authors are not responsible for any financial losses incurred through the use of this tool.
API Status: NSE India has implemented dynamic API endpoints with enhanced security measures that this static implementation cannot access.
Note: Development has been discontinued due to NSE API changes. The following features were planned but will not be implemented:
Add support for equity options (not just indices)β CancelledImplement options Greeks calculations (Delta, Gamma, Theta, Vega)β CancelledAdd real-time data streaming capabilitiesβ Cancelled (API no longer accessible)Create web interface with Streamlit/Dashβ CancelledAdd portfolio analysis featuresβ CancelledImplement backtesting frameworkβ CancelledSupport for international marketsβ Cancelled
For questions, suggestions, or collaborations, please open an issue on GitHub or contact the maintainers.
Note: This tool requires a stable internet connection to fetch data from NSE India. Data availability depends on market hours and NSE API status.