Skip to main content

NSEEOD Package — Overview

backend/nseeod/ is a custom-built Python package that handles the full lifecycle of NSE end-of-day data: downloading bhav copies from NSE archives, parsing them, merging with the security master, and persisting to PostgreSQL.


Package File Map

FileRole
downloadbhav.pyOrchestratorBhavProcessor class + standalone utilities
EquityBhav.pyEquity, Index, and Delivery Volume updater classes
FoBhav.pyFutures & Options updater class
NSEDownloader.pyHTTP downloader for new-format NSE archives
utils.pyShared helpers — date utils, NSE file downloads, DB write helpers
pgs.pyPostgreSQL connection and CRUD helpers (psycopg2 + SQLAlchemy)
bhav.pyAmiBroker export file generator
check_bhav.pyDebug utility — compares F&O DB vs raw file
ut_Classes.pyLegacy / research — holiday correction, intraday fill
Indexinfo.csvStatic NSE index name mapping (Old name → New name → typ)

High-Level Architecture


Data Flow — Full Sync


Download Modes

ModeMethodTrigger
Full syncrun_update()POST /api/bhav-downloader/download
Single daterun_update_for_date(date)POST /api/bhav-downloader/download?date=YYYY-MM-DD
F&O catch-updownload_missingfoBhav()Manual / CLI only

NSE File Format History

NSE changed the bhav copy format in April 2025. The code supports both:

EraFormat flagEquity filenameF&O filename
Old (pre-2025)newformat = FalsecmDDMMMYYYYbhav.csvfoDDMMMYYYYbhav.csv
New (2025+)newformat = TrueBhavCopy_NSE_CM_0_0_0_YYYYMMDD_F_0000.csvBhavCopy_NSE_FO_0_0_0_YYYYMMDD_F_0000.csv

BhavProcessor.newformat = True by default (new format active).


Key Database Tables

TableDescriptionPrimary Key
scr_masterSecurity master — all symbols (EQ, FUT, OPT, Index)masid
scr_nseeq_eodEquity EOD OHLCV + delivery(masid, sdate)
scr_nsefo_eodF&O EOD OHLCV + OI(masid, sdate)
scr_indexpepbIndex PE / PB / Div Yield(masid, sdate)
scr_missing_bhavFailed download tracker(sdate, typ)
scr_missing_eodMissing EOD tracker(sdate, typ)
holidaysNSE holiday calendarsdate
specialdaysMuhurat trading sessionssdate
tasksRetry task queue (JSONB)typ
scr_configSystem configuration storemname

Running Standalone

# Always run as a module from backend/ directory
cd backend
python -m nseeod.downloadbhav
Never run as a script directly
# WRONG — breaks relative imports
python nseeod/downloadbhav.py