Skip to main content
These three standalone scripts fetch comprehensive Futures & Options (F&O) market data. They are not included in the main pipeline by default and should be run manually when F&O reference data is needed.

Overview

ScriptOutput FileRecordsPurpose
fetch_fno_data.pyfno_stocks_response.json~207 stocksF&O-enabled stocks with live metrics
fetch_fno_lot_sizes.pyfno_lot_sizes_cleaned.json~207 instrumentsContract lot sizes per expiry month
fetch_fno_expiry.pyfno_expiry_calendar.json~2,000+ entriesExpiry dates for all derivatives

fetch_fno_data.py

What It Does

Fetches the complete list of F&O-eligible stocks from NSE with real-time market data, fundamental ratios, and technical indicators.

API Endpoint

URL: https://ow-scanx-analytics.dhan.co/customscan/fetchdt
Method: POST

Payload:
{
  "data": {
    "sort": "Mcap",
    "sorder": "desc",
    "count": 500,
    "fields": ["Isin", "DispSym", "Mcap", "Pe", "Ltp", "Sym", ...],
    "params": [
      {"field": "FnoFlag", "op": "", "val": "1"},
      {"field": "OgInst", "op": "", "val": "ES"}
    ],
    "pgno": 0
  }
}
Filter Logic:
  • FnoFlag = 1: Only stocks where derivatives trading is enabled
  • OgInst = ES: Equity Segment instruments

Output Format

File: fno_stocks_response.json (Array of ~207 stocks)
[
  {
    "Isin": "INE002A01018",
    "DispSym": "Reliance Industries Ltd.",
    "Sym": "RELIANCE",
    "Mcap": 1712345.67,
    "Pe": 25.3,
    "Ltp": 2543.80,
    "Volume": 5432100,
    "DaySMA50CurrentCandle": 2480.45,
    "DayRSI14CurrentCandle": 58.2,
    "High1Yr": 2750.00,
    "ROCE": 18.5,
    "Roe": 14.2
  }
]

Fields Captured

  • Isin, DispSym, Sym
  • Ltp (Last Traded Price)
  • Open, BcClose (Previous Close)
  • Mcap, Pe, Pb, DivYeild
  • Revenue, Year1RevenueGrowth
  • NetProfitMargin, EBIDTAMargin
  • ROCE, Roe
  • DaySMA50CurrentCandle, DaySMA200CurrentCandle
  • DayRSI14CurrentCandle
  • High1Yr, High3Yr, High5yr, High1Wk
  • RtAwayFrom5YearHigh, RtAwayFrom1MonthHigh
  • PricePerchng1week, PricePerchng2week, PricePerchng1mon
  • PricePerchng3mon, PricePerchng1year, PricePerchng3year, PricePerchng5year
  • Volume
  • Year1CAGREPSGrowth, YearlyEarningPerShare

Usage

python3 fetch_fno_data.py
Output:
Fetching F&O data (FnoFlag: 1) from https://ow-scanx-analytics.dhan.co/...
Successfully fetched 207 F&O stocks. Saved to fno_stocks_response.json

Use Cases

Derivatives Screener

Identify high-liquidity F&O stocks for options strategies

Volatility Analysis

Track RSI, SMA crossovers in derivative-eligible universe

F&O Watchlists

Build custom scanners for stocks with active derivative contracts

Risk Management

Monitor margin requirements for F&O portfolio positions

fetch_fno_lot_sizes.py

What It Does

Extracts lot sizes for all F&O contracts by parsing Dhan’s Next.js data API. Each instrument has different lot sizes per expiry month.

API Endpoint

Primary: https://dhan.co/_next/data/{buildId}/nse-fno-lot-size.json
Fallback: HTML scraping from __NEXT_DATA__ script tag
Dynamic buildId Extraction:
import re
response = requests.get("https://dhan.co/nse-fno-lot-size/")
match = re.search(r'"buildId":"([^"]+)"', response.text)
build_id = match.group(1)

Output Format

File: fno_lot_sizes_cleaned.json
[
  {
    "Symbol": "RELIANCE",
    "Name": "Reliance Industries Ltd.",
    "Lot_JAN": 250,
    "Lot_FEB": 250,
    "Lot_MAR": 250,
    "Lot_APR": 250
  },
  {
    "Symbol": "TCS",
    "Name": "Tata Consultancy Services Ltd.",
    "Lot_JAN": 125,
    "Lot_FEB": 125,
    "Lot_MAR": 125
  }
]

Lot Size Calculation

Lot sizes are set by NSE to maintain contract value around ₹5-7 lakhs:
Lot Size = ₹5,00,000 ÷ Stock Price

Example (RELIANCE @ ₹2,500):
Lot Size = 500000 ÷ 2500 = 200 shares
Lot sizes are adjusted quarterly by NSE. Always fetch fresh data before trading.

Usage

python3 fetch_fno_lot_sizes.py
Output:
Primary Fetch: Direct JSON via Next.js Data API...
Successfully extracted 207 instruments via Direct JSON.

Use Cases

  • Position Sizing: Calculate exact capital required for options strategies
  • Margin Computation: Estimate SPAN + Exposure margins for F&O trades
  • Risk Budgeting: Determine maximum loss per lot for defined-risk strategies

fetch_fno_expiry.py

What It Does

Fetches the complete F&O expiry calendar covering Stock Futures, Index Futures, Options, and Currency Derivatives.

API Endpoint

Primary: https://dhan.co/_next/data/{buildId}/fno-expiry-calendar.json
Fallback: HTML scraping from __NEXT_DATA__ script tag

Output Format

File: fno_expiry_calendar.json (~2,000+ entries)
[
  {
    "Exchange": "NSE",
    "Segment": "D",
    "InstrumentType": "OPTIDX",
    "SymbolName": "NIFTY",
    "ExpiryDate": "2024-03-28",
    "UnderlyingSecID": "13"
  },
  {
    "Exchange": "NSE",
    "Segment": "D",
    "InstrumentType": "FUTSTK",
    "SymbolName": "RELIANCE",
    "ExpiryDate": "2024-03-28",
    "UnderlyingSecID": "2885"
  },
  {
    "Exchange": "NSE",
    "Segment": "D",
    "InstrumentType": "OPTSTK",
    "SymbolName": "TCS",
    "ExpiryDate": "2024-03-28",
    "UnderlyingSecID": "3456"
  }
]

Instrument Types

CodeInstrumentExpiry Pattern
OPTIDXIndex Options (NIFTY, BANKNIFTY)Weekly (Thursday)
FUTIDXIndex FuturesMonthly (Last Thursday)
OPTSTKStock OptionsMonthly (Last Thursday)
FUTSTKStock FuturesMonthly (Last Thursday)

Expiry Rules

1

Weekly Expiries (Index Options)

NIFTY & BANKNIFTY expire every Thursday. If Thursday is a holiday, expiry moves to the preceding trading day.
2

Monthly Expiries (All Others)

Stock Futures/Options and Index Futures expire on the last Thursday of the month.
3

Holiday Adjustment

If expiry falls on a holiday, contracts expire on the previous trading day.

Usage

python3 fetch_fno_expiry.py
Output:
Primary Fetch: Direct JSON via Next.js Data API...
Successfully processed 2147 expiry entries.
Data saved to fno_expiry_calendar.json

Use Cases

Calendar Spreads

Identify expiry gaps for multi-leg strategies

Rollover Planning

Schedule position rollovers before contract expiry

Theta Decay

Track days to expiry for options time decay calculations

Settlement Alerts

Avoid physical settlement by closing positions before expiry

Comparison with Pipeline Integration

While these scripts are standalone, the pipeline includes F&O enrichment via enrich_fno_data.py:
Standalone ScriptsPipeline Integration (enrich_fno_data.py)
Fetches all F&O instrumentsEnriches only F&O-flagged stocks in master JSON
Outputs separate JSON filesAdds F&O Flag, Lot Size, Next Expiry fields
Run manually on-demandAuto-runs in Phase 4 of pipeline
Use for reference/analysisUse for live stock screener
If you need F&O data in all_stocks_fundamental_analysis.json, the pipeline’s enrich_fno_data.py will add these fields automatically. Run these standalone scripts only when you need the complete F&O universe outside the main dataset.

Error Handling

All three scripts include fallback mechanisms:
try:
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
except Exception as e:
    print(f"Error fetching data: {e}")

Next Steps

Indices & ETF Scripts

Fetch market indices and ETF data

OHLCV History

Download lifetime price history

Pipeline Flags

Configure FETCH_OHLCV and FETCH_OPTIONAL

Cleanup Options

Manage intermediate file cleanup

Build docs developers (and LLMs) love