# johnbrick123/sp500-data architecture

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

- Interactive diagram: https://gitdiagram.com/johnbrick123/sp500-data
- Repository: https://github.com/johnbrick123/sp500-data

## Overview

This repository builds a point-in-time S&P 500 and ETF historical dataset, then provides a backtesting library that consumes it. The data workflow covers universe and membership construction, price collection and recovery, adjustment, verification, and local or hosted database loading. In the backtesting workflow, a user runs a strategy through the engine, which uses aligned market data and indicators to produce results and reports. The README documents the dataset stages; sampled source shows the backtester’s internal links. Other recovery and collection links are omitted where their callers were not shown.

Main entry points: [`qbt/engine.py`](https://github.com/johnbrick123/sp500-data/blob/main/qbt/engine.py) (Backtest engine), [`fetch_perma.py`](https://github.com/johnbrick123/sp500-data/blob/main/fetch_perma.py) (Tiingo recovery), [`strategies/momentum_with_stops.py`](https://github.com/johnbrick123/sp500-data/blob/main/strategies/momentum_with_stops.py) (Trading strategy), [`qbt/data.py`](https://github.com/johnbrick123/sp500-data/blob/main/qbt/data.py) (Market panels)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_universe["Universe and membership"]
  node_builder["Build universe<br/>[build_universe.py]"]
  node_bridge["Bridge membership"]
  node_membership[("Membership history<br/>[build_universe.py]")]
end

subgraph group_collection["Price collection"]
  node_prices["Fetch prices<br/>[fetch_prices.py]"]
  node_crsp["CRSP data<br/>[fetch_crsp.py]"]
  node_wiki["WIKI recovery<br/>[fetch_wiki.py]"]
  node_perma["Tiingo recovery<br/>[fetch_perma.py]"]
  node_perma_names["Name-based recovery"]
  node_salvage["Split salvage<br/>[salvage_split.py]"]
end

subgraph group_release["Adjustment and release"]
  node_adjust["Compute adjustments"]
  node_verify["Verify dataset<br/>[verify.py]"]
  node_dataset[("Historical dataset")]
  node_localdb["DuckDB loading<br/>[build_db.py]"]
  node_hosteddb["MotherDuck loading<br/>[load_motherduck.py]"]
end

subgraph group_backtest["Backtesting"]
  node_strategy["Trading strategy"]
  node_engine["Backtest engine<br/>[engine.py]"]
  node_marketdata["Market panels<br/>[data.py]"]
  node_indicators["Indicators<br/>[indicators.py]"]
  node_report["Results and reports<br/>[report.py]"]
end

node_user(("Backtest user"))

node_builder -->|"builds"| node_membership
node_bridge -->|"extends history"| node_membership
node_prices -->|"collects prices"| node_dataset
node_crsp -->|"provides history"| node_dataset
node_wiki -->|"recovers prices"| node_dataset
node_perma -.->|"recovers members"| node_dataset
node_perma_names -->|"reuses helpers"| node_perma
node_salvage -.->|"repairs splits"| node_dataset
node_adjust -->|"derives adjusted prices"| node_dataset
node_verify -->|"checks quality"| node_dataset
node_dataset -->|"loads locally"| node_localdb
node_dataset -->|"loads remotely"| node_hosteddb
node_user -.->|"chooses"| node_strategy
node_strategy -->|"implements callbacks"| node_engine
node_engine -->|"loads market data"| node_marketdata
node_marketdata -->|"reads release files"| node_dataset
node_engine -->|"uses indicators"| node_indicators
node_engine -->|"computes results"| node_report

click node_builder "https://github.com/johnbrick123/sp500-data/blob/main/build_universe.py"
click node_bridge "https://github.com/johnbrick123/sp500-data/blob/main/bridge_membership.py"
click node_membership "https://github.com/johnbrick123/sp500-data/blob/main/build_universe.py"
click node_prices "https://github.com/johnbrick123/sp500-data/blob/main/fetch_prices.py"
click node_crsp "https://github.com/johnbrick123/sp500-data/blob/main/fetch_crsp.py"
click node_wiki "https://github.com/johnbrick123/sp500-data/blob/main/fetch_wiki.py"
click node_perma "https://github.com/johnbrick123/sp500-data/blob/main/fetch_perma.py"
click node_perma_names "https://github.com/johnbrick123/sp500-data/blob/main/fetch_perma_names.py"
click node_salvage "https://github.com/johnbrick123/sp500-data/blob/main/salvage_split.py"
click node_adjust "https://github.com/johnbrick123/sp500-data/blob/main/compute_adjusted.py"
click node_verify "https://github.com/johnbrick123/sp500-data/blob/main/verify.py"
click node_localdb "https://github.com/johnbrick123/sp500-data/blob/main/build_db.py"
click node_hosteddb "https://github.com/johnbrick123/sp500-data/blob/main/load_motherduck.py"
click node_strategy "https://github.com/johnbrick123/sp500-data/blob/main/strategies/momentum_with_stops.py"
click node_engine "https://github.com/johnbrick123/sp500-data/blob/main/qbt/engine.py"
click node_marketdata "https://github.com/johnbrick123/sp500-data/blob/main/qbt/data.py"
click node_indicators "https://github.com/johnbrick123/sp500-data/blob/main/qbt/indicators.py"
click node_report "https://github.com/johnbrick123/sp500-data/blob/main/qbt/report.py"

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_builder,node_bridge,node_membership,node_user toneBlue
class node_prices,node_crsp,node_wiki,node_perma,node_perma_names,node_salvage toneAmber
class node_adjust,node_verify,node_dataset,node_localdb,node_hosteddb toneMint
class node_strategy,node_engine,node_marketdata,node_indicators,node_report toneRose
```

## Components

### Universe and membership

- [Build universe](https://github.com/johnbrick123/sp500-data/blob/main/build_universe.py): `build_universe.py`
- [Bridge membership](https://github.com/johnbrick123/sp500-data/blob/main/bridge_membership.py): `bridge_membership.py`
- [Membership history](https://github.com/johnbrick123/sp500-data/blob/main/build_universe.py): `build_universe.py`

### Price collection

- [Fetch prices](https://github.com/johnbrick123/sp500-data/blob/main/fetch_prices.py): `fetch_prices.py`
- [CRSP data](https://github.com/johnbrick123/sp500-data/blob/main/fetch_crsp.py): `fetch_crsp.py`
- [WIKI recovery](https://github.com/johnbrick123/sp500-data/blob/main/fetch_wiki.py): `fetch_wiki.py`
- [Tiingo recovery](https://github.com/johnbrick123/sp500-data/blob/main/fetch_perma.py): `fetch_perma.py`
- [Name-based recovery](https://github.com/johnbrick123/sp500-data/blob/main/fetch_perma_names.py): `fetch_perma_names.py`
- [Split salvage](https://github.com/johnbrick123/sp500-data/blob/main/salvage_split.py): `salvage_split.py`

### Adjustment and release

- [Compute adjustments](https://github.com/johnbrick123/sp500-data/blob/main/compute_adjusted.py): `compute_adjusted.py`
- [Verify dataset](https://github.com/johnbrick123/sp500-data/blob/main/verify.py): `verify.py`
- **Historical dataset**
- [DuckDB loading](https://github.com/johnbrick123/sp500-data/blob/main/build_db.py): `build_db.py`
- [MotherDuck loading](https://github.com/johnbrick123/sp500-data/blob/main/load_motherduck.py): `load_motherduck.py`

### Backtesting

- [Trading strategy](https://github.com/johnbrick123/sp500-data/blob/main/strategies/momentum_with_stops.py): `strategies/momentum_with_stops.py`
- [Backtest engine](https://github.com/johnbrick123/sp500-data/blob/main/qbt/engine.py): `qbt/engine.py`
- [Market panels](https://github.com/johnbrick123/sp500-data/blob/main/qbt/data.py): `qbt/data.py`
- [Indicators](https://github.com/johnbrick123/sp500-data/blob/main/qbt/indicators.py): `qbt/indicators.py`
- [Results and reports](https://github.com/johnbrick123/sp500-data/blob/main/qbt/report.py): `qbt/report.py`

### Other components

- **Backtest user**

## Connections

- Build universe → Membership history: builds
- Bridge membership → Membership history: extends history
- Fetch prices → Historical dataset: collects prices
- CRSP data → Historical dataset: provides history
- WIKI recovery → Historical dataset: recovers prices (evidence: `fetch_wiki.py`)
- Tiingo recovery → Historical dataset: recovers members
- Name-based recovery → Tiingo recovery: reuses helpers (evidence: `fetch_perma_names.py`)
- Split salvage → Historical dataset: repairs splits
- Compute adjustments → Historical dataset: derives adjusted prices
- Verify dataset → Historical dataset: checks quality
- Historical dataset → DuckDB loading: loads locally
- Historical dataset → MotherDuck loading: loads remotely
- Backtest user → Trading strategy: chooses
- Trading strategy → Backtest engine: implements callbacks (evidence: `strategies/momentum_with_stops.py`)
- Backtest engine → Market panels: loads market data (evidence: `qbt/engine.py`)
- Market panels → Historical dataset: reads release files (evidence: `qbt/data.py`)
- Backtest engine → Indicators: uses indicators (evidence: `qbt/engine.py`)
- Backtest engine → Results and reports: computes results (evidence: `qbt/engine.py`)
