Skip to main content

bhav.py — AmiBroker Export Generator

Generates daily AmiBroker-compatible data files from the PostgreSQL database.


generateamiformat(mdate: date)

SQL executed:

SELECT
sm.mname AS ticker,
sne.sdate AS date_ymd,
sne.sopen AS open,
sne.shigh AS high,
sne.slow AS low,
sne.sclose AS close,
sne.svolume AS volume,
sne.delqty AS openint
FROM scr_master sm
JOIN scr_nseeq_eod sne ON sm.masid = sne.masid
WHERE sne.sdate = :mdate
AND sm.typ <= 2
AND sne.sclose <> 0
AND sm.monthindex >= 0

Output file: {AMI_PATH}/YYYYMMDD.txt — CSV format, no index column.

Example: 20260522.txt

ticker,date_ymd,open,high,low,close,volume,openint
RELIANCE,20260522,2850.5,2880.0,2840.0,2865.0,5234567,1234567
NIFTY 50,20260522,22450.0,22510.0,22400.0,22480.0,0,0

AmiBroker imports this file via a data plugin that reads the openint column as delivery quantity for equities.


NSEDownloader.py — HTTP Downloader

Downloads bhav copy ZIPs from the new NSE archive format (post-April 2025).


Class: NseDownLoader

getBhavcopy(to_folder, mDate, mode="cm") → bool

ParameterValueDescription
to_folderstrDestination directory
mDatedateTrading date
mode"cm" | "fo"Equity or F&O

URLs used:

ModeURL Pattern
cmhttps://nsearchives.nseindia.com/content/cm/BhavCopy_NSE_CM_0_0_0_YYYYMMDD_F_0000.csv.zip
fohttps://nsearchives.nseindia.com/content/fo/BhavCopy_NSE_FO_0_0_0_YYYYMMDD_F_0000.csv.zip

Behavior:

  1. 3 retries with 1s sleep on failure
  2. Downloads ZIP → saves as bhavcopy.zip → extracts → deletes ZIP
  3. Returns True on success, False after all retries exhausted

Session headers mimic a Chrome browser to avoid NSE bot detection.