Skip to main content

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

MethodPathPurpose
POST/downloadTrigger a background sync — single date, range, or auto
GET/last-updateLatest synced sdate + next pending trading date
GET/statusLive progress/logs/history from mcx_stats.json
GET/filesList raw bhavcopy CSVs on disk
GET/file/{file_type}/{filename}Download one raw CSV
GET/ami-filesList AmiBroker export files
GET/ami-file/{filename}Download one AMI .txt file
POST/ami-downloadDownload one or more AMI files (zipped if >1)
POST/generate-amiRegenerate AMI files for a date/range without re-downloading

POST /download

Query params (all optional, mutually exclusive precedence: date > from_date+to_date > neither):

ParamTypeEffect
dateYYYY-MM-DDForce-sync exactly this date
from_date + to_dateYYYY-MM-DDSync 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 — lists Config.MCX_AMI_PATH, parsing YYYYMMDD.txt into a display date
  • GET /ami-file/{filename} — serves one file as text/plain
  • POST /ami-download — body {"files": ["20260804.txt", ...]}; a single file streams directly, multiple files are zipped in-memory (zipfile.ZIP_DEFLATED) and streamed as mcx_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.