Overview
Phase 1 establishes the data foundation for the entire pipeline. It fetches core market data and fundamental metrics for ~5000 NSE stocks, producing the criticalmaster_isin_map.json that ALL subsequent scripts depend on.
Scripts
1. fetch_dhan_data.py
Purpose: Fetches live market data for all NSE equities and creates the master ISIN → Symbol mapping. API Endpoint:Stock symbol (e.g., “RELIANCE”)
ISIN code (e.g., “INE002A01018”)
Security ID used by Dhan APIs
F&O eligibility: 1 = Yes, 0 = No
List of index memberships with Indexid and Name
2. fetch_fundamental_data.py
Purpose: Fetches detailed fundamental data (P&L, balance sheet, ratios) for all stocks using the master ISIN map. API Endpoint:- Loads all ISINs from
master_isin_map.json - Chunks ISINs into batches of 100
- Fetches each batch sequentially with 0.5s delay
incomeStat_cq - Quarterly P&L
incomeStat_cq - Quarterly P&L
Contains pipe-separated quarterly values (latest to oldest):
NET_PROFIT: Net profit in croresEPS: Earnings per shareSALES: Revenue in croresOPM: Operating profit margin (%)YEAR: Quarter labels (e.g., “Q3 FY25|Q2 FY25|…”)
"latest|prev|2back|3back|lastyearQ"incomeStat_cy - Annual P&L
incomeStat_cy - Annual P&L
Contains pipe-separated annual values:
EPS: Annual EPS for last 5 yearsSALES: Annual revenue for last 5 years
TTM_cy - Trailing Twelve Months
TTM_cy - Trailing Twelve Months
Single values for TTM metrics:
EPS: TTM earnings per shareOPM: TTM operating profit margin
CV - Company Variables
CV - Company Variables
Company classification and valuation:
INDUSTRY_NAME: Specific industrySECTOR: Broader sectorMARKET_CAP: Market capitalization (crores)STOCK_PE: Price-to-earnings ratio
roce_roe - Return Ratios
roce_roe - Return Ratios
Profitability metrics:
ROE: Return on equity (%)ROCE: Return on capital employed (%)
sHp - Shareholding Pattern
sHp - Shareholding Pattern
bs_c - Balance Sheet (Current)
bs_c - Balance Sheet (Current)
Pipe-separated quarterly balance sheet items:
NON_CURRENT_LIABILITIES: Long-term debtTOTAL_EQUITY: Shareholder equity
3. NSE Listing Dates CSV
Purpose: Downloads listing dates from NSE for calculating stock age. Source:Dependency Chain
Error Handling
fetch_dhan_data.py
fetch_fundamental_data.py
Performance
| Script | Avg Time | Records | Throughput |
|---|---|---|---|
| fetch_dhan_data.py | 3-5 sec | ~5000 | Single API call |
| fetch_fundamental_data.py | 2-3 min | ~5000 | ~2500 records/min (batched) |
| NSE CSV download | 1-2 sec | ~5000 | Direct download |
| Total Phase 1 | 2-4 min | ~5000 | - |
Next Steps
Phase 2: Data Enrichment
Explore how the master ISIN map is used to fetch enrichment data