Skip to main content

FoBhav.py — F&O Updater

Handles downloading and ingesting NSE Futures & Options bhav copies into scr_nsefo_eod.


Class: NseEodFoUpdater


update()

Main entry — iterates through all missing NSE working days for F&O.


update_a_day(mdate, th, updateonly=False, downloadonly=False) → bool

ParameterTypeDescription
mdatedateNSE trading date to process
thlist[date]Holiday-adjusted Thursday list (expiry correction)
updateonlyboolSkip download, process existing file only
downloadonlyboolDownload only, skip DB insert

Steps:

  1. Downloads F&O bhav ZIP via NseDownLoader.getBhavcopy(mode="fo")
  2. Calls utils.updatefobhav() which:
    • Parses futures and options into separate DataFrames
    • Resolves masid from scr_master (inserts new symbols if needed)
    • Builds mname = SYMBOL + expiry + strike + opttype for options
    • Inserts into scr_nsefo_eod
    • Moves processed file to BhavCopies/updated/

download_missingfoBhav()

Catch-up utility — finds all equity dates missing from F&O table:

SELECT sdate FROM scr_nseeq_eod
WHERE sdate > '2012-01-01'
EXCEPT
SELECT sdate FROM scr_nsefo_eod
ORDER BY sdate

Processes each missing date via update_a_day(). Used for initial bulk backfill only — not called in normal flow.


F&O Instrument Type Mapping

NSE CodeInternal TypeDescription
STF / FUTSTKtyp=4Stock Futures
IDF / FUTIDXtyp=3Index Futures
STO / OPTSTKtyp=6Stock Options
IDO / OPTIDXtyp=5Index Options

New-format codes (STO, STF, IDO, IDF) are mapped to old-format names on load.


F&O mname Construction

scr_master.mname is constructed as a unique key:

InstrumentPatternExample
FuturesSYMBOL + DDMONYYNIFTY29MAY25
OptionsSYMBOL + DDMONYY + STRIKE + CE/PENIFTY29MAY2524000CE
Adjusted FuturesSYMBOL + -I / -II / -III (Roman)NIFTY-I

Adjusted futures use Roman numeral suffix (-I = near month, -II = mid, -III = far) aligned to the last Thursday of each month.


Task Tracking (typ=5)

Failed downloads stored in tasks table same as equity (key = date string, value = retry info).