Overview
| Script | Output Directory | Records | Update Mode |
|---|---|---|---|
fetch_all_ohlcv.py | ohlcv_data/ | ~2,775 CSV files | Incremental (2-5 min) |
fetch_indices_ohlcv.py | indices_ohlcv_data/ | ~194 CSV files | Incremental (30-60 sec) |
fetch_all_ohlcv.py
What It Does
Fetches lifetime daily OHLCV data for all NSE stocks, starting from the earliest available date (some stocks go back to 1990s). Uses smart incremental updates to only fetch missing dates.API Endpoint
START: Unix timestamp (default:215634600= Oct 31, 1976)- This forces the API to return all available history
INTERVAL:Dfor daily candles (useWfor weekly,Mfor monthly)SEC_ID: Security ID frommaster_isin_map.json(created byfetch_dhan_data.py)
Smart Incremental Logic
Output Format
Directory:ohlcv_data/
Files: One CSV per stock (e.g., RELIANCE.csv, TCS.csv)
Volume = 0 for some older dates (pre-2010) where NSE did not publish volume data.
Configuration
Edit these constants infetch_all_ohlcv.py (lines 11-16):
Performance Benchmarks
| Scenario | Time Taken | API Calls |
|---|---|---|
| First Run (Full History) | ~25-35 min | ~8,000 chunks |
| Daily Update (Next Day) | ~2-3 min | ~2,775 stocks |
| Weekly Update (7 Days) | ~2-5 min | ~2,775 stocks |
| Monthly Update (30 Days) | ~3-6 min | ~5,500 chunks |
Usage
Error Handling
fetch_indices_ohlcv.py
What It Does
Fetches lifetime daily OHLCV data for all NSE indices (NIFTY 50, NIFTY Bank, etc.) with the same incremental update logic.Differences from Stock OHLCV
| Feature | fetch_all_ohlcv.py | fetch_indices_ohlcv.py |
|---|---|---|
| Input File | dhan_data_response.json | all_indices_list.json |
| Output Directory | ohlcv_data/ | indices_ohlcv_data/ |
| Threads | 15 | 60 (indices are faster) |
| Chunk Size | 180 days | 120 days |
| Volume Data | Real trading volume | 0 (indices don’t have volume) |
| Filename | RELIANCE.csv | Nifty_50.csv (sanitized) |
API Endpoint
Filename Sanitization
Index names contain spaces/special chars, so filenames are sanitized:Output Format
Directory:indices_ohlcv_data/
Example: Nifty_50.csv
Volume is always 0 for indices because NSE does not publish index volume. For constituent volumes, use stock OHLCV data.
Configuration
Edit these constants infetch_indices_ohlcv.py (lines 18-19):
Performance Benchmarks
| Scenario | Time Taken | API Calls |
|---|---|---|
| First Run (Full History) | ~30-60 sec | ~600 chunks |
| Daily Update | ~10-15 sec | ~194 indices |
| Weekly Update | ~15-20 sec | ~300 chunks |
Usage
Pipeline Integration
Both scripts are optionally included in the pipeline based on theFETCH_OHLCV flag.
Enable OHLCV in Pipeline
Editrun_full_pipeline.py (line 64):
Pipeline Execution (Phase 2.5)
If
FETCH_OHLCV = False, Phase 2.5 is skipped, and fields like ADR, RVOL, % from ATH will be 0 in the final JSON.Why OHLCV Data is Critical
The OHLCV CSVs power 14 advanced fields in the final output:Volatility Metrics (4 fields)
Volatility Metrics (4 fields)
Calculated from High/Low ranges:
5 Days MA ADR(%)14 Days MA ADR(%)20 Days MA ADR(%)30 Days MA ADR(%)
Volume Metrics (5 fields)
Volume Metrics (5 fields)
Calculated from Volume column:
RVOL(Relative Volume vs 20-day avg)200 Days EMA Volume% from 52W High 200 Days EMA VolumeDaily Rupee Turnover 20/50/100(Cr.)30 Days Average Rupee Volume(Cr.)
All-Time High Tracking (2 fields)
All-Time High Tracking (2 fields)
% from ATH(Distance from all-time high)- Implicitly used in
Returns since Earnings(%)calculation
Earnings Performance (2 fields)
Earnings Performance (2 fields)
Requires OHLCV to calculate post-earnings returns:
Returns since Earnings(%)Max Returns since Earnings(%)
Technical Validation (1 field)
Technical Validation (1 field)
Cross-validates SMA/EMA levels from
fetch_advanced_indicators.pyData Quality Checks
Both scripts validate data integrity:Storage Requirements
| Dataset | Files | Avg Size/File | Total Size |
|---|---|---|---|
| Stocks OHLCV | ~2,775 | 50-150 KB | ~300-400 MB |
| Indices OHLCV | ~194 | 30-100 KB | ~10-20 MB |
| Total | ~2,969 | - | ~320-420 MB |
Troubleshooting
Error: master_isin_map.json not found
Error: master_isin_map.json not found
Cause:
fetch_dhan_data.py has not been run.Solution:Error: all_indices_list.json not found
Error: all_indices_list.json not found
Cause:
fetch_all_indices.py has not been run.Solution:High error count (>50 stocks failed)
High error count (>50 stocks failed)
Cause: API rate limiting or network issues.Solution:
- Reduce
MAX_THREADSfrom 15 to 5 - Increase
timeoutfrom 15s to 30s - Re-run script (incremental mode will fill gaps)
CSV has gaps in dates (weekends/holidays missing)
CSV has gaps in dates (weekends/holidays missing)
This is normal. NSE only publishes data for trading days. Use:
Next Steps
F&O Data Scripts
Fetch Futures & Options data
Indices & ETF Scripts
Fetch market indices and ETF data
Pipeline Flags
Configure FETCH_OHLCV and FETCH_OPTIONAL
Advanced Metrics
Learn how OHLCV powers ADR, RVOL, ATH