app/api/mcx_routes.py — REST API
FastAPI router mounted at /api/mcx-downloader, mirroring the shape of
nseeod's bhav-downloader endpoints. All mutating endpoints dispatch to
McxProcessor via BackgroundTasks — the HTTP response returns immediately
and the frontend polls /status.
Endpoints
| Method | Path | Purpose |
|---|---|---|
POST | /download | Trigger a background sync — single date, range, or auto |
GET | /last-update | Latest synced sdate + next pending trading date |
GET | /status | Live progress/logs/history from mcx_stats.json |
GET | /files | List raw bhavcopy CSVs on disk |
GET | /file/{file_type}/{filename} | Download one raw CSV |
GET | /ami-files | List AmiBroker export files |
GET | /ami-file/{filename} | Download one AMI .txt file |
POST | /ami-download | Download one or more AMI files (zipped if >1) |
POST | /generate-ami | Regenerate AMI files for a date/range without re-downloading |
POST /download
Query params (all optional, mutually exclusive precedence: date >
from_date+to_date > neither):
| Param | Type | Effect |
|---|---|---|
date | YYYY-MM-DD | Force-sync exactly this date |
from_date + to_date | YYYY-MM-DD | Sync every weekday in the range (see the weekday-filter caveat in mcx_history.py) |
| (none) | — | Auto-sync every missing date up to the latest MCX trading date |
Every branch calls a McxProcessor method that itself captures the rollback
baseline (run_update, run_update_for_date, run_range_download) — the
route never calls download_date() directly.
GET /last-update
Reads MAX(sdate) from scr_mcxfo_eod directly via the async session, and
computes next_trading_date with a plain weekend skip (not
MCXWorkingDays() — acceptable here since it's only advisory UI text, not a
download plan).
GET /status
Returns status, progress, last_log, logs, history straight from a
fresh McxProcessor().stats (i.e. mcx_stats.json on disk). Polled by the
frontend every 1500 ms, same cadence as the NSE bhav downloader.
GET /files / GET /file/{file_type}/{filename}
Lists/serves the raw per-day bhavcopy CSV snapshots under Config.MCX_CSV_PATH
(saved by McxProcessor.download_date() before any transform). file_type
must be "csv"; only os.path.basename()-sanitized filenames are served.
AMI file endpoints
_MCX_AMI_NAME_RE = r"^\d{8}\.txt$" — every AMI filename/path is validated
against this pattern before touching the filesystem (_safe_mcx_ami_path()),
rejecting path traversal and non-AMI filenames alike.
GET /ami-files— listsConfig.MCX_AMI_PATH, parsingYYYYMMDD.txtinto a displaydateGET /ami-file/{filename}— serves one file astext/plainPOST /ami-download— body{"files": ["20260804.txt", ...]}; a single file streams directly, multiple files are zipped in-memory (zipfile.ZIP_DEFLATED) and streamed asmcx_ami_data.zip
POST /generate-ami
Regenerates AMI .txt files from already-stored scr_mcxfo_eod rows,
without re-downloading. Accepts date, or from_date+to_date (weekday
filter again), or defaults to today. Runs
McxProcessor.generate_mcx_ami_format(d) per date in the background.
Useful after an AMI-export logic change, to backfill exports without
re-hitting MCX.
Related
- mcx_downloader.py —
McxProcessormethods this router dispatches to - Overview — Download Modes