Skip to main content

EquityBhav.py — Equity Classes

Contains three classes that handle equity EOD data, index data, and delivery volumes.


Class Relationships


NseEodEqUpdater

Handles equity bhav copy download and DB ingestion.

update()

Main entry — iterates through all missing NSE working days and processes each.

update_a_day(mdate, mdelfile, updateonly=False) → bool

Downloads and ingests equity data for a single date.

  1. Downloads bhav copy ZIP (new or old format via NseDownLoader or utils)
  2. Downloads index CSV (via NseEodIndexUpdater.downloadbhav)
  3. Deletes existing row for that date from scr_nseeq_eod
  4. Calls utils.new_process_and_update_EqBhav() or utils.process_and_update_EqBhav()
  5. Moves processed file to BhavCopies/updated/

Task Tracking (typ=2)

Failed downloads are stored in the tasks table as JSON:

{
"2026-05-20": {"retries": 1, "lasttry": "2026-05-21"},
"2026-05-21": {"retries": 0, "lasttry": "2026-05-21"}
}

repair()

Retries all pending failed download tasks. Removes successfully processed ones.


NseEodIndexUpdater

Downloads and ingests NSE index data (ind_close_all_DDMMYYYY.csv).

downloadbhav(mdate) → bool

Downloads index snapshot from niftyindices.com. If download fails, queues it in the tasks table (typ=1) for retry.

update(mdate, updateonly=False)

  1. Downloads the index CSV (unless updateonly=True)
  2. Calls utils.updateindexbhav() which:
    • Merges with Indexinfo.csv for standardised index names
    • Inserts new symbols into scr_master if needed
    • Writes OHLCV to scr_nseeq_eod (indices share the same table as equities)
    • Writes PE/PB to scr_indexpepb
    • Moves processed file to BhavCopies/updated/

NseEodDelValDownloader

Downloads delivery volume files (MTO_DDMMYYYY.DAT).

downloadbhav(mDate) → str

Downloads MTO_DDMMYYYY.DAT from NSE archives if not already present. Returns the filename string (or empty string on failure). Delivery data is merged into equity rows as the delqty column.


NSE File URL Patterns

File typeURL pattern
Equity bhav (new)https://nsearchives.nseindia.com/content/cm/BhavCopy_NSE_CM_0_0_0_YYYYMMDD_F_0000.csv.zip
Equity bhav (old)https://archives.nseindia.com/content/historical/EQUITIES/YYYY/MON/cmDDMONYYYYbhav.csv.zip
Index snapshothttps://www.niftyindices.com/Daily_Snapshot/ind_close_all_DDMMYYYY.csv
Delivery volumehttps://archives.nseindia.com/archives/equities/mto/MTO_DDMMYYYY.DAT
F&O bhav (new)https://nsearchives.nseindia.com/content/fo/BhavCopy_NSE_FO_0_0_0_YYYYMMDD_F_0000.csv.zip
F&O bhav (old)https://archives.nseindia.com/content/historical/DERIVATIVES/YYYY/MON/foDDMONYYYYbhav.csv.zip