Durable instructions for Codex and other coding agents working in this repo.
- Backtester is a Python 3.11+ event-driven strategy backtesting engine with a FastAPI wrapper and a Next.js dashboard called Backtest Lab.
- Core Python package is in
backtester/; frontend source is isolated infrontend/. - The engine supports single-asset and multi-asset backtests, pluggable strategies, portfolio simulation, metrics, grid search, charts, CLI, and API responses.
- FastAPI currently exposes health, strategy metadata, and single-asset backtest endpoints.
- Backtest Lab currently exposes a polished single-asset research dashboard. Multi-asset support remains Python-side.
- The project intentionally avoids backtesting-specific libraries such as backtrader, zipline, quantstats, and empyrical.
- Core tests should remain deterministic and should not depend on live yfinance/network calls.
Python setup:
py -m venv .venv
.\.venv\Scripts\Activate
python -m pip install -r requirements.txtCreate .venv locally and activate it before running validation. Never commit .venv/, venv/, or generated environment files.
Python validation:
python -m pytest
python -m mypy backtester
python -m pytest --cov=backtesterImportant:
- Do not run bare
pytestormypycommands. - Always invoke them through
python -m ...because PATH is not reliable in this environment.
API:
python -m uvicorn backtester.api.main:app --reloadFrontend:
cd frontend
npm install
npm run dev
npm run lint
npm run typecheck
npm run buildBacktest Lab uses Next.js 15. Use a Node.js version compatible with Next's engine range: ^18.18.0, ^19.8.0, or >=20.0.0.
CLI/examples:
python -m backtester.cli --help
python -m backtester.cli run --ticker AAPL --strategy momentum --start 2020-01-01 --end 2023-12-31 --benchmark
python -m backtester.cli grid-search --ticker AAPL --start 2020-01-01 --end 2023-12-31 --fast-windows 5,10 --slow-windows 30,50
python examples/run_demo.py
python examples/grid_search_demo.py
python examples/multi_asset_demo.py- Keep
data,strategy,portfolio,metrics, andvizindependently testable. enginecomposes data, strategy, portfolio, and config.apicomposes schemas, service conversion, engine execution, and JSON responses.frontendis an API client and presentation layer. It must not reimplement backtesting, portfolio accounting, benchmark math, or performance metrics in TypeScript.- Preserve public APIs unless the user explicitly asks for a breaking change.
- Prefer small, well-scoped changes over broad rewrites.
- Keep strict typing for public Python functions/classes;
mypy backtestershould pass. - Pydantic schemas in
backtester/api/schemas.pyare the API contract for Backtest Lab. backtester/api/services.pyshould convert API requests into engine configs and convert engine results back to JSON-friendly responses.- Do not add auth, database persistence, live trading, broker integration, or paid data feeds unless explicitly requested.
- If API tests need data, use FastAPI
TestClientand monkeypatch service/loader behavior to avoid network calls. - yfinance-backed flows may be used in demos and manual local runs, but deterministic tests should use synthetic data or mocked yfinance/data loader behavior.
- Current stack: Next.js App Router, TypeScript, Tailwind CSS, Recharts, lucide-react.
- Avoid heavy UI libraries or state managers unless there is a clear, user-requested reason.
- Keep API calls isolated in
frontend/lib/api.ts. - Keep API request/response shapes typed in
frontend/lib/types.ts. - Use shared formatting helpers for currency, percentages, numbers, decimals, and dates.
- Keep validation user-friendly and inline; do not rely on
alert(). - Default
NEXT_PUBLIC_API_URLbehavior should remainhttp://localhost:8000unless explicitly changed. - Backtest Lab should remain desktop-first but usable on smaller screens without horizontal overflow.
- Numbers in metric cards, tables, percentages, dates, and chart tooltips should use the finance mono font stack.
- Do not add fake multi-page workflows or unsupported multi-asset UI. Disabled or coming-soon affordances are fine when honest.
- Add or update tests for behavior changes.
- For Python changes, run:
python -m pytestpython -m mypy backtester
- For frontend changes, run:
cd frontend && npm run lintcd frontend && npm run typecheckcd frontend && npm run build
- If a command fails because dependencies, PATH, network, or local services are unavailable, document that clearly in the handoff.
README.mdfor user-facing overview and run commands.docs/architecture.mdfor module map, data flow, and integration notes.docs/current-state.mdfor current implementation state and verified commands.docs/tasks.mdfor lightweight task tracking.docs/decisions/README.mdfor ADR guidance.frontend/README.mdfor Backtest Lab-specific setup and component map.backtester/api/for API contract and service conversion.frontend/app/,frontend/components/, andfrontend/lib/for dashboard implementation.
- Stage 7 strategy API uses full DataFrames plus
current_index; strategies must not read rows aftercurrent_index. - Multi-asset engine aligns tickers on the intersection of available dates and processes signals in config ticker order.
- The web dashboard currently exposes single-asset backtests only; multi-asset remains Python-side.
- FastAPI currently exposes only single-asset backtesting.
- The frontend must call the API and render returned data; do not duplicate metrics or engine behavior in TypeScript.
- yfinance calls may require network unless data is cached.
- API CORS defaults to localhost/127.0.0.1 port 3000. Other frontend origins can be configured with
BACKTESTER_CORS_ORIGINS. - Generated files such as
__pycache__/,.pytest_cache/,.mypy_cache/,frontend/node_modules/, andfrontend/.next/should not be committed.
At the end of a session, report:
- Files changed.
- Commands run and results.
- Tests or builds not run, with reasons.
- API assumptions made.
- Any known limitations or follow-up tasks added to
docs/tasks.md.