NSE Option Chain Analysis
Relevant source files
The following files were used as context for generating this wiki page:
The NSE Option Chain Analysis feature in stolgo provides utilities for fetching and processing derivative market data from the National Stock Exchange (NSE). This system allows developers to retrieve option chain data, which includes strike prices, premiums, and open interest for various indices and stocks.
The implementation facilitates data retrieval through multiple providers, such as Samco or direct NSE data fetching, and supports exporting this data to formats like Excel for further analysis.
Sources: setup.py:35, tests/test_nse_option_chain.py:4-5
Architecture and Data Retrieval
The project utilizes a modular architecture to handle web requests and data parsing. The core data fetching logic is built upon the requests library, with specialized wrappers to handle retries and timeouts.
Request Handling
The RequestUrl class manages sessions and implements robust HTTP GET/POST methods. For environments where standard Python requests might be restricted, the Curl class provides a subprocess-based alternative using system curl commands.
Samco Integration
The Samco module provides a specific implementation for fetching option chains via the Stocknote API. It requires a session token obtained through a login process involving user credentials and year of birth (YOB).
| Endpoint Type | URL Variable | Purpose |
|---|---|---|
| Login | LOGIN_URL | Authenticate and retrieve session token |
| Option Chain | OPTION_CHAIN_URL | Fetch derivative data for a symbol |
Sources: lib/stolgo/samco.py:22-35
Key Components and Functions
Data Export and NSE Utilities
The primary interface for NSE-specific data is NseData. It provides high-level methods to interact with option chain data.
- get_option_chain_excel(symbol, expiry): Fetches the option chain for a given symbol (e.g., 'BANKNIFTY') and expiry date, then exports it to an Excel file.
- get_optionchain(symbol): Retrieves raw option chain data as a JSON/dictionary structure via a broker interface.
Sources: tests/test_nse_option_chain.py:5, lib/stolgo/samco.py:166-169
Supported Indices
The system recognizes a wide variety of Indian indices for analysis. The validation of these symbols is handled by is_ind_index in the helper module.
IND_INDICES = [
"NIFTY 50", "NIFTY BANK", "NIFTY FIN SERVICE",
"NIFTY AUTO", "NIFTY FMCG", "NIFTY IT",
"NIFTY METAL", "NIFTY PHARMA", "INDIA VIX"
]
Sources: lib/stolgo/common.py:1-16, lib/stolgo/helper.py:11-13
Data Flow for Option Chain Retrieval
The following sequence diagram illustrates the process of retrieving option chain data through the Samco provider.
Sources: lib/stolgo/samco.py:73-90, 166-169
Configuration and Requirements
The NSE analysis module depends on several third-party libraries for data processing and export.
| Dependency | Purpose |
|---|---|
pandas | Data manipulation and Resampling |
openpyxl | Excel file generation |
beautifulsoup4 / lxml | Parsing HTML/XML data from NSE |
requests | HTTP communication |
Sources: setup.py:24-30, lib/stolgo/helper.py:101-104
Summary
The NSE Option Chain Analysis module within stolgo streamlines the acquisition of derivative data. By abstracting the complexities of session management, retries, and exchange-specific URL constructions, it allows traders and developers to focus on strategy and data interpretation rather than data plumbing. The system supports both indices and individual stocks, providing outputs in both programmatic (Pandas/JSON) and user-friendly (Excel) formats.