# aabishkar2/nepse-data architecture

> Architecture diagram of the GitHub repository aabishkar2/nepse-data, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aabishkar2/nepse-data
- Repository: https://github.com/aabishkar2/nepse-data

## Overview

#### Purpose and Deliverable
This is a data-collection repository for Nepal Stock Exchange (NEPSE) historical and current trading data. Its primary product is a versioned dataset: one ascending date-wise CSV per trading symbol under `data/company-wise/` (for example, `data/company-wise/NMB.csv` and `data/company-wise/NABIL.csv`).

#### Execution Entry Points
Scripts must run from `src/` because imports are relative to that directory:
- `src/discoverCompanies.py` refreshes the symbol-to-company-ID registry from the live NEPSE source.
- `src/allDataScrapper.py` performs historical backfill only for symbols lacking a CSV.
- `src/dailyDataScrapper.py` incrementally updates existing company CSVs without duplicating same-day rows.

Dependencies and expected Python runtime are defined in `src/requirements.txt` (README specifies Python 3.8).

#### Company Discovery and Registry
Company discovery establishes the identifier boundary between local symbols and the upstream API/site. `src/discoverCompanies.py` retrieves live listings and regenerates `src/constants/companyIdMap.py`; it merges rather than replaces the existing mapping so delisted symbols remain addressable. URL and request-header definitions are separated into `src/constants/url.py` and `src/config/headers.py`.

#### Historical Seeding Pipeline
`src/allDataScrapper.py` iterates the company registry and fetches historical trading records for symbols that have no destination file. This makes backfill non-destructive: existing datasets are not overwritten or duplicated. Retrieved records are normalized into the repository’s common CSV schema, whose column contract is represented by `src/constants/columns.py`, then persisted as `data/company-wise/<SYMBOL>.csv`.

#### Incremental Daily Update Pipeline
`src/dailyDataScrapper.py` is the operational refresh path. It targets only already-seeded CSVs, obtains current trading data, and appends only records absent from each file’s local history. History and duplicate/append decisions are isolated in `src/utils/history.py`; status derivation (`close` versus `open`) is handled through `src/utils/status.py`. This preserves an idempotent daily update boundary.

#### Data Transformation and Persistence
The persistent interface is the CSV collection under `data/company-wise/`. Each row carries date, OHLC prices, percentage change, quantity, turnover, and derived status. Response-shape normalization is supported by `src/utils/flatten.py`; CSV mutation behavior is centralized in `src/modified_csv.py`. The dataset is therefore both the pipeline output and the repository’s consumer-facing API.

#### Automation and External Dependencies
A scheduled GitHub Actions job at `.github/workflows/schedule-updater.yml` runs the daily scraper multiple times on trading days, providing retry-by-schedule rather than a persistent service runtime. The scraper depends on live NEPSE listing and market-data endpoints, accessed through HTTP/session logic in `src/utils/session.py` and parameter construction in `src/utils/params.py`. GitHub is the storage/distribution infrastructure: successful automation commits updated CSV artifacts back into the repository.

Main entry points: [`src/dailyDataScrapper.py`](https://github.com/aabishkar2/nepse-data/blob/main/src/dailyDataScrapper.py) (Daily updater), [`src/modified_csv.py`](https://github.com/aabishkar2/nepse-data/blob/main/src/modified_csv.py) (CSV persistence), [`src/allDataScrapper.py`](https://github.com/aabishkar2/nepse-data/blob/main/src/allDataScrapper.py) (Historical backfill), [`src/discoverCompanies.py`](https://github.com/aabishkar2/nepse-data/blob/main/src/discoverCompanies.py) (Company discovery)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_discovery["Company discovery"]
  node_headers["Request headers<br/>HTTP config<br/>[headers.py]"]
  node_urls["Endpoint definitions<br/>URL config<br/>[url.py]"]
  node_discovery["Company discovery<br/>Python script"]
  node_company_map["Company ID registry<br/>Python mapping<br/>[companyIdMap.py]"]
end

subgraph group_pipelines["Collection pipelines"]
  node_session["HTTP session<br/>HTTP utility<br/>[session.py]"]
  node_params["Request parameters<br/>HTTP utility<br/>[params.py]"]
  node_backfill["Historical backfill<br/>Python script<br/>[allDataScrapper.py]"]
  node_daily_update["Daily updater<br/>Python script"]
  node_flatten["Response normalization<br/>transform utility<br/>[flatten.py]"]
  node_history["History deduplication<br/>CSV history utility<br/>[history.py]"]
  node_status["Trading status<br/>derivation utility<br/>[status.py]"]
end

subgraph group_data["Versioned dataset"]
  node_columns["CSV schema<br/>column contract<br/>[columns.py]"]
  node_csv_mutation["CSV persistence<br/>CSV utility<br/>[modified_csv.py]"]
  node_company_csvs[("Company CSV collection<br/>versioned dataset")]
end

subgraph group_automation["Scheduled automation"]
  node_schedule["Scheduled updater<br/>GitHub Actions workflow"]
  node_github[("GitHub repository<br/>storage and distribution")]
end

node_nepse{{"NEPSE live endpoints<br/>external API"}}

node_headers -->|"configures"| node_discovery
node_urls -->|"defines listing endpoint"| node_discovery
node_nepse -->|"live listings"| node_discovery
node_discovery -->|"merges registry"| node_company_map
node_company_map -->|"symbols and IDs"| node_backfill
node_company_map -->|"symbols and IDs"| node_daily_update
node_session -->|"HTTP access"| node_nepse
node_params -->|"request parameters"| node_session
node_nepse -->|"historical records"| node_backfill
node_nepse -->|"current records"| node_daily_update
node_backfill -->|"normalizes responses"| node_flatten
node_daily_update -->|"normalizes responses"| node_flatten
node_company_csvs -->|"local history"| node_history
node_history -->|"missing rows only"| node_daily_update
node_daily_update -->|"derives status"| node_status
node_flatten -->|"normalized rows"| node_csv_mutation
node_status -->|"status values"| node_csv_mutation
node_columns -->|"schema"| node_csv_mutation
node_backfill -->|"seed rows"| node_csv_mutation
node_csv_mutation -->|"writes symbol CSVs"| node_company_csvs
node_schedule -->|"runs"| node_daily_update
node_company_csvs -->|"committed artifacts"| node_github

click node_headers "https://github.com/aabishkar2/nepse-data/blob/main/src/config/headers.py"
click node_urls "https://github.com/aabishkar2/nepse-data/blob/main/src/constants/url.py"
click node_discovery "https://github.com/aabishkar2/nepse-data/blob/main/src/discoverCompanies.py"
click node_company_map "https://github.com/aabishkar2/nepse-data/blob/main/src/constants/companyIdMap.py"
click node_session "https://github.com/aabishkar2/nepse-data/blob/main/src/utils/session.py"
click node_params "https://github.com/aabishkar2/nepse-data/blob/main/src/utils/params.py"
click node_backfill "https://github.com/aabishkar2/nepse-data/blob/main/src/allDataScrapper.py"
click node_daily_update "https://github.com/aabishkar2/nepse-data/blob/main/src/dailyDataScrapper.py"
click node_flatten "https://github.com/aabishkar2/nepse-data/blob/main/src/utils/flatten.py"
click node_history "https://github.com/aabishkar2/nepse-data/blob/main/src/utils/history.py"
click node_status "https://github.com/aabishkar2/nepse-data/blob/main/src/utils/status.py"
click node_columns "https://github.com/aabishkar2/nepse-data/blob/main/src/constants/columns.py"
click node_csv_mutation "https://github.com/aabishkar2/nepse-data/blob/main/src/modified_csv.py"
click node_schedule "https://github.com/aabishkar2/nepse-data/blob/main/.github/workflows/schedule-updater.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_headers,node_urls,node_discovery,node_company_map toneBlue
class node_session,node_params,node_backfill,node_daily_update,node_flatten,node_history,node_status toneAmber
class node_columns,node_csv_mutation,node_company_csvs toneMint
class node_schedule,node_github toneRose
class node_nepse toneNeutral
```

## Components

### Company discovery

- [Request headers](https://github.com/aabishkar2/nepse-data/blob/main/src/config/headers.py): HTTP config · `src/config/headers.py`
- [Endpoint definitions](https://github.com/aabishkar2/nepse-data/blob/main/src/constants/url.py): URL config · `src/constants/url.py`
- [Company discovery](https://github.com/aabishkar2/nepse-data/blob/main/src/discoverCompanies.py): Python script · Refreshes the symbol-to-ID registry while retaining delisted symbols. · `src/discoverCompanies.py`
- [Company ID registry](https://github.com/aabishkar2/nepse-data/blob/main/src/constants/companyIdMap.py): Python mapping · Identifier boundary between local symbols and NEPSE IDs. · `src/constants/companyIdMap.py`

### Collection pipelines

- [HTTP session](https://github.com/aabishkar2/nepse-data/blob/main/src/utils/session.py): HTTP utility · `src/utils/session.py`
- [Request parameters](https://github.com/aabishkar2/nepse-data/blob/main/src/utils/params.py): HTTP utility · `src/utils/params.py`
- [Historical backfill](https://github.com/aabishkar2/nepse-data/blob/main/src/allDataScrapper.py): Python script · Seeds only symbols without an existing CSV. · `src/allDataScrapper.py`
- [Daily updater](https://github.com/aabishkar2/nepse-data/blob/main/src/dailyDataScrapper.py): Python script · Updates only seeded symbol files idempotently. · `src/dailyDataScrapper.py`
- [Response normalization](https://github.com/aabishkar2/nepse-data/blob/main/src/utils/flatten.py): transform utility · `src/utils/flatten.py`
- [History deduplication](https://github.com/aabishkar2/nepse-data/blob/main/src/utils/history.py): CSV history utility · Determines rows absent from local symbol history. · `src/utils/history.py`
- [Trading status](https://github.com/aabishkar2/nepse-data/blob/main/src/utils/status.py): derivation utility · Derives open or close status. · `src/utils/status.py`

### Versioned dataset

- [CSV schema](https://github.com/aabishkar2/nepse-data/blob/main/src/constants/columns.py): column contract · Defines common date, OHLC, change, quantity, turnover, and status fields. · `src/constants/columns.py`
- [CSV persistence](https://github.com/aabishkar2/nepse-data/blob/main/src/modified_csv.py): CSV utility · Centralizes CSV mutation behavior. · `src/modified_csv.py`
- **Company CSV collection**: versioned dataset · One ascending date-wise CSV per trading symbol.

### Scheduled automation

- [Scheduled updater](https://github.com/aabishkar2/nepse-data/blob/main/.github/workflows/schedule-updater.yml): GitHub Actions workflow · Runs the daily scraper multiple times on trading days. · `.github/workflows/schedule-updater.yml`
- **GitHub repository**: storage and distribution · Commits and distributes refreshed CSV artifacts.

### Other components

- **NEPSE live endpoints**: external API · Listings and market trading data.

## Connections

- Request headers → Company discovery: configures
- Endpoint definitions → Company discovery: defines listing endpoint
- NEPSE live endpoints → Company discovery: live listings
- Company discovery → Company ID registry: merges registry
- Company ID registry → Historical backfill: symbols and IDs
- Company ID registry → Daily updater: symbols and IDs
- HTTP session → NEPSE live endpoints: HTTP access
- Request parameters → HTTP session: request parameters
- NEPSE live endpoints → Historical backfill: historical records
- NEPSE live endpoints → Daily updater: current records
- Historical backfill → Response normalization: normalizes responses
- Daily updater → Response normalization: normalizes responses
- Company CSV collection → History deduplication: local history
- History deduplication → Daily updater: missing rows only
- Daily updater → Trading status: derives status
- Response normalization → CSV persistence: normalized rows
- Trading status → CSV persistence: status values
- CSV schema → CSV persistence: schema
- Historical backfill → CSV persistence: seed rows
- CSV persistence → Company CSV collection: writes symbol CSVs
- Scheduled updater → Daily updater: runs
- Company CSV collection → GitHub repository: committed artifacts
