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
| Parameter | Type | Description |
|---|---|---|
mdate | date | NSE trading date to process |
th | list[date] | Holiday-adjusted Thursday list (expiry correction) |
updateonly | bool | Skip download, process existing file only |
downloadonly | bool | Download only, skip DB insert |
Steps:
- Downloads F&O bhav ZIP via
NseDownLoader.getBhavcopy(mode="fo") - Calls
utils.updatefobhav()which:- Parses futures and options into separate DataFrames
- Resolves
masidfromscr_master(inserts new symbols if needed) - Builds
mname=SYMBOL + expiry + strike + opttypefor 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 Code | Internal Type | Description |
|---|---|---|
STF / FUTSTK | typ=4 | Stock Futures |
IDF / FUTIDX | typ=3 | Index Futures |
STO / OPTSTK | typ=6 | Stock Options |
IDO / OPTIDX | typ=5 | Index 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:
| Instrument | Pattern | Example |
|---|---|---|
| Futures | SYMBOL + DDMONYY | NIFTY29MAY25 |
| Options | SYMBOL + DDMONYY + STRIKE + CE/PE | NIFTY29MAY2524000CE |
| Adjusted Futures | SYMBOL + -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).