Skip to main content

Backend Developer Guide

Technology Stack

  • FastAPI — Async Python web framework
  • Uvicorn — ASGI server
  • SQLAlchemy (async) — ORM for PostgreSQL
  • psycopg2 — Sync PostgreSQL driver (used in background tasks)
  • pandas — Data processing for NSE bhav copies
  • Pydantic v2 — Request/response validation

Project Structure

backend/
├── app/
│ ├── api/ # FastAPI route modules
│ │ ├── bhav_downloader.py
│ │ ├── stocks.py
│ │ ├── fo.py
│ │ └── ...
│ ├── database/
│ │ └── session.py # Async DB session factory
│ ├── config.py # Config class (APP_ENV, CSV_PATH, AMI_PATH, ...)
│ └── main.py # App factory, router registration
├── nseeod/ # NSE data ingestion package
│ ├── downloadbhav.py # BhavProcessor orchestrator
│ ├── EquityBhav.py # Equity segment updater
│ ├── FoBhav.py # F&O segment updater
│ ├── bhav.py # AMI format generator
│ ├── pgs.py # psycopg2 connection helpers
│ └── utils.py # NSE working days, holiday calendar
├── requirements.txt
└── .env

Key Modules

app/config.py

Reads environment variables to build paths:

class Config:
APP_ENV = os.getenv("APP_ENV", "LOCAL")
CSV_PATH = os.getenv("LOCAL_CSV_PATH") if APP_ENV == "LOCAL" else os.getenv("VPS_CSV_PATH")
AMI_PATH = os.getenv("LOCAL_AMI_PATH") if APP_ENV == "LOCAL" else os.getenv("VPS_AMI_PATH")

nseeod/downloadbhav.pyBhavProcessor

Orchestrates the full bhav download pipeline:

MethodDescription
run_update()Full catch-up sync for all missing dates
run_update_for_date(date)Force-sync a single specific date
_generate_ami(date)Generate AmiBroker export (never raises)
AddLog(msg, source)Append to stats.json log list
AddHistory(date, status, msg)Append to history list

Background Task Flow

POST /api/bhav-downloader/download
└── BackgroundTasks.add_task(processor.run_update)
└── Runs after HTTP response is sent
└── Writes progress to stats.json
└── Frontend polls GET /api/bhav-downloader/status

Running the Backend

cd backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

Adding a New API Route

  1. Create backend/app/api/mymodule.py with an APIRouter.
  2. Register in app/main.py:
    from app.api.mymodule import router as mymodule_router
    app.include_router(mymodule_router)

Running the NSE Module Standalone

Always run from the backend/ directory using module syntax:

cd backend
python -m nseeod.downloadbhav

Do not use python nseeod\downloadbhav.py — this adds the wrong folder to sys.path.