Summary
I identified and fixed a long-standing limitation in the nsepython derivative fetching logic.
The library has now been migrated from the restrictive getOptionChainData API to the comprehensive **getSymbolDerivativesData API`.
This change resolves issues with multi-expiry calculations (for example pcr(payload, 1) returning zero) and significantly improves both data availability and performance.
The Problem: “The Expiry Wall”
Previously, nse_optionchain_scrapper(symbol) was hardcoded to fetch only the nearest expiry by default.
API Used
getOptionChainData
Limitation
The API returns data for only one expiry date at a time.
Result
Functions such as:
pcr(payload, 1)
nse_optionchain_ltp(payload, strike, type, 1)
would fail because the second expiry data did not exist in the payload.
To analyze multiple expiries (for example weekly vs monthly), developers had to make multiple network calls, which:
The Solution: “God Mode” API Integration
I rewrote nse_optionchain_scrapper to use the getSymbolDerivativesData endpoint.
1. Unified Fetching
A single network call now retrieves the entire derivative universe for a symbol in one JSON response.
This includes:
-
All Strike Prices
-
All Expiry Dates (Weekly, Monthly, Yearly)
-
All Option Types (CE / PE)
-
All Future Contracts
2. Intelligent Transformation Layer
To ensure all existing strategies continue to work without modification (such as art_main.py, heatmap.py, maxpain.py), I implemented a Normalization Bridge.
This bridge converts the raw flat response from NSE into the legacy option chain structure expected by existing functions.
# New payload structure (compatible with legacy functions)
payload = {
"records": {
"data": [ ... combined CE/PE objects ... ],
"expiryDates": [ ... sorted list of all expiries ... ],
"timestamp": "...",
"underlyingValue": 22500.0
}
}
This ensures no breaking changes for existing strategy scripts.
New Function: nse_quote_derivatives
This function is now the recommended method for developers who want raw access to all derivative contracts.
Function Signature
nse_quote_derivatives(symbol)
Parameters
| Parameter |
Type |
Description |
symbol |
str |
FNO symbol such as "NIFTY", "RELIANCE", "INFY" |
Returns
A dictionary containing:
| Key |
Description |
data |
List of dictionaries representing derivative contracts |
timestamp |
Last update time from NSE |
Example Usage
from rahu import nse_quote_derivatives
# Fetch every derivative contract for NIFTY
raw_payload = nse_quote_derivatives("NIFTY")
# Filter all Call Options for a specific expiry
nifty_calls = [
d for d in raw_payload["data"]
if d["expiryDate"] == "10-Mar-2026" and d["optionType"] == "CE"
]
print(f"Total Call Strikes found: {len(nifty_calls)}")
Verification and Benchmarks
Tests were performed inside the pinakin Docker container to ensure environment consistency.
NIFTY Multi-Expiry PCR Test
| Expiry Index |
Expiry Date |
Previous Result |
New Result |
Status |
inp=0 (Nearest) |
10-Mar-2026 |
0.6767 |
0.6767 |
Stable |
inp=1 (Next) |
17-Mar-2026 |
0.0000 |
0.6099 |
Fixed |
Legacy Function Compatibility
Core legacy functions now correctly retrieve data for non-zero expiry indices.
Test
nse_optionchain_ltp(payload, 24250, "CE", 1)
Result
513.85
This confirms that the function correctly retrieves LTP from the second expiry.
Status: Fully compatible.