Christian CortezPlay the 3D version

03 / 05Systems project · May 2026

Hindsight

A self-hosted research platform for reproducible portfolio backtesting and risk analytics, with background jobs on Redis/RQ workers.

Fig. 03Hindsight request and job flow. Text version
  1. Browser
  2. Next.js web
  3. FastAPI
  4. Redis
  5. PostgreSQL
  6. Parquet versions
  7. RQ workers
  8. Reaper
Role
Sole author
Status
Release candidate for private, self-hosted use
Stack
Python, FastAPI, PostgreSQL, Redis / RQ, Docker, GitHub Actions
OHLCV records indexed
1M+
bundled data is synthetic
Redis/RQ workers
1–4
recorded scaling runs in the repo

The problem

Backtests are easy to write and easy to get quietly wrong: a signal peeks at tomorrow's price, the universe only holds companies that survived, or nobody can regenerate last month's result.

Hindsight treats each of those failures as an engineering requirement with a test, and runs every long computation as a tracked background job.

Architecture

Hindsight request and job flow

The browser talks to a Next.js front end, which proxies API calls to FastAPI. Routes never run simulations: they record a job in PostgreSQL and enqueue only its id on Redis. RQ workers run the domain library, read versioned Parquet datasets through DuckDB, and write results back to PostgreSQL. A reaper redelivers jobs whose worker died. Fig. 03 draws it.

List all 8 connections
  • Browser to Next.js web
  • Next.js web to FastAPI (/api/*)
  • FastAPI to PostgreSQL (record job)
  • FastAPI to Redis (enqueue id)
  • Redis to RQ workers (deliver)
  • RQ workers to Parquet versions (read)
  • RQ workers to PostgreSQL (results)
  • Reaper to PostgreSQL (scan)
  • The domain library imports no web or database code, so every numerical routine is testable in isolation.
  • Redis only delivers job ids. Job state lives in PostgreSQL with database-time leases and fencing tokens, so a stale worker cannot overwrite a newer attempt.
  • Every result carries a manifest: dataset version, engine, input hash and output fingerprint.

Decisions and trade-offs

  1. Routes never compute

    HTTP handlers validate, record a job and enqueue its id; workers do the work.

    Trade-off: Every analysis becomes asynchronous, but the API stays responsive and long jobs survive a web restart.

  2. Undefined is reported as undefined

    Ratios that cannot be computed are shown as undefined with a reason, never printed as zero.

    Trade-off: The output is less tidy, and much harder to misread.

  3. Content-addressed datasets

    Market data versions are immutable Parquet files addressed by content.

    Trade-off: Storage grows with each version, and any result can be regenerated exactly from its manifest.

How it is tested

  • An independent reference engine, written from the methodology without importing the engine, must agree to 1e-9 on 50 seeded synthetic universes.
  • Mutation tests inject 36 known defects, such as a one-bar look-ahead or a delisting a day early; the suites must catch each one.
  • Integration tests run against real PostgreSQL and Redis, and Playwright end-to-end tests include an axe accessibility audit.

Reproducibility as a toy: a backtest on synthetic prices whose rule only reads past closes, run against immutable dataset versions, with a manifest per run. Because the data never changes and the rule is pure, re-running a version reproduces its manifest by construction; another version gives another. The same model runs on the Hindsight bench in the 3D version; it is not Hindsight's code.

Backtest, re-runnable

run 1 · dataset v1 · manifest 4f3d6c9b

Equity over 32 days on dataset v1: 4.9% return, largest drawdown 4.7%. Amber days sit below the running peak (in a drawdown, not necessarily a loss); the rest are green when the rule held the asset and grey when it was out of the market.
  1. run 1 · v1 · 4f3d6c9b

Re-run on the same dataset version and the manifest comes back identical; the next version gives a different one.

Where it stands

  • Backtests compute volatility, beta, Sharpe ratio, drawdown, value at risk and expected shortfall, with a research integrity report on each experiment.
  • The release candidate was verified from a fresh clone.

Scope: Hindsight is research infrastructure, not a trading bot. All bundled data is synthetic, results are never investment advice, and it is not publicly deployed.