Skip to main content

bse_infra.py — Infrastructure Bootstrap

Creates scr_bse_master, scr_bseeq_eod, scr_bsefo_eod, their indexes, and the modi_date trigger on scr_bse_master. Mirrors mcxeod/mcx_infra.py's structure exactly.

cd backend
python -m bseeod.bse_infra # create/verify + report (safe)
python -m bseeod.bse_infra --purge-scr-master # remove BSE rows wrongly in scr_master (destructive)

All DDL is CREATE TABLE IF NOT EXISTS / CREATE INDEX IF NOT EXISTS / CREATE OR REPLACE FUNCTION / DROP TRIGGER IF EXISTS + CREATE TRIGGER, so running it repeatedly is a no-op past the first call.


typ Numbering​

BSE's own range, clear of NSE's 1–11 and MCX's 20–24:

typMeaning
30EQ (equity cash)
31IDX (SENSEX/BANKEX spot)
32FUTIDX (index futures)
33OPTIDX (index options)

34+ is free for a later BSE instrument type without touching either existing exchange's range.


ensure_infrastructure(con=None) → dict​

Same pattern as mcx_infra.ensure_infrastructure(): accepts an existing connection (for running inside a caller's transaction) or opens/closes its own; rolls back and re-raises on failure.

scr_bse_master columns​

masid SERIAL PK, mname VARCHAR(100), scrip_code VARCHAR(20) UNIQUE, typ SMALLINT, underly INTEGER DEFAULT 0, edate DATE, strike REAL, opttyp CHAR(2), monthindex SMALLINT DEFAULT 0, isin VARCHAR(12), alia/tag1/tag2/tag3 VARCHAR(100), crea_date/modi_date TIMESTAMP.

scrip_code carries the UNIQUE constraint — not mname. A 2026-10-11 migration (026cf2bcd55f) moved the constraint off mname after BSE's company-name truncation was found to both merge unrelated companies and fragment single companies' histories across duplicate masids (see bse_equity.py for the full writeup). scrip_code is nullable — index/derivative root rows (typ 31–33) have no scrip code, and multiple NULLs don't violate a UNIQUE constraint in Postgres.

scr_bseeq_eod columns​

masid, sdate composite PK. sopen/shigh/slow/sclose/svolume/svalue/ prevclose/slast/delqty/avgprice — same shape as scr_nseeq_eod, minus the f5min/f15min/l5min/l15min reserved-for-intraday columns NSE's table has (no BSE intraday module exists yet). Holds both equity and SENSEX/BANKEX index-spot rows — see the Overview.

scr_bsefo_eod columns​

masid, sdate, sopen/shigh/slow/sclose/svolume/oi/coi/lotsize. No enforced primary key — same "delete the whole date, then insert" rule as scr_nsefo_eod (root CLAUDE.md rule 6).


find_bse_rows_in_scr_master / purge_bse_from_scr_master​

Same safety pattern as mcx_infra's find_mcx_rows_in_scr_master / purge_mcx_from_scr_master: identified by typ range (30–33), never by name — NSE genuinely lists tickers that could collide with a BSE-style name if matching were done by text. Dependent price rows (scr_nseeq_eod, scr_nsefo_eod, scr_indexpepb) are deleted before the master rows, inside one transaction, so a masid never ends up pointed-at by an orphaned price row. Requires confirm=True (CLI: --purge-scr-master); a plain call is a dry run that reports what would be deleted.


report_state()​

Read-only diagnostic — table existence, row counts, modi_date trigger presence, full index list, and the scr_master cross-contamination check. Running the module directly calls ensure_infrastructure() then report_state() in one command.


What This Does Not Touch​

  • scr_master / scr_mcx_master and their price tables — entirely separate from BSE's tables
  • Any row data — this only creates empty structure
  • holidays / specialdays (exch=3) — that calendar is seeded by bse_history.py, not this module