# builderr-ai/builderr-trading-template architecture

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

- Interactive diagram: https://gitdiagram.com/builderr-ai/builderr-trading-template
- Repository: https://github.com/builderr-ai/builderr-trading-template

## Overview

#### Purpose and contract
This repository is a submission template and local validation kit for the Builderr Trading Agent Leaderboard. The deployable strategy entry point is `agent.py`, which must expose `decide(market_state, portfolio_state, cash) -> list[orders]`. It receives daily historical bars and current holdings, then emits long-only buy/sell orders. `AGENT_BRIEF.md` and `ANATOMY.md` define the fuller agent contract and strategy-design guidance.

Paths: `agent.py`, `AGENT_BRIEF.md`, `ANATOMY.md`

#### Submitted strategy
The checked-in agent is a forward-return rotation hybrid: it determines a risk regime from SPY/QQQ trend and QQQ volatility; selects defensive ETFs in risk-off periods; ranks broad, sector, semiconductor, and mega-cap instruments in risk-on periods; and conditionally uses modest 2x ETF exposure. Target sizing includes ticker, drift-rebalance, and beta-adjusted gross-exposure controls.

Paths: `agent.py`, `strategy_selftest.py`, `universe.json`

#### Decision data boundary
The strategy is designed to operate primarily on the supplied `market_state` bars, which are oldest-first daily OHLCV histories. `portfolio_state` supplies cash, open positions, and last prices for reconciliation and sizing. The returned order list is the hard boundary to the external execution/scoring engine; the repository does not contain that private engine.

Paths: `agent.py`, `preview.py`, `selfcheck.py`

#### Local evaluation stages
`preview.py` is the primary local preflight: it runs the agent over bundled public market windows and reports admission-like safety/performance results. `selfcheck.py` provides a faster synthetic smoke test for output shape and crash resistance. Strategy-specific assertions verify regime behavior and exposure caps before submission.

Paths: `preview.py`, `selfcheck.py`, `strategy_selftest.py`

#### Execution and safety model
The central platform enforces a long-only, finite-order contract, maximum trade rate, minimum hold interval, per-name concentration limit, and beta-adjusted gross limit. The local strategy proactively stays below those limits through capped target weights and beta-aware sizing. Its output may still be rejected or flattened by the external engine if constraints are breached.

Paths: `agent.py`, `fairness_tests.py`, `endpoint_security.py`

#### Submission modes and live integration
A repository submission is centrally evaluated on hidden historical regimes, then admitted agents run in a shared daily paper-trading sandbox and appear on a live leaderboard. Alternatively, an externally hosted HTTPS `/decide` endpoint can provide decisions; endpoint responses are captured before the market session and replayed for execution. Supporting scripts implement local endpoint replay/capture and leaderboard refresh/runner behavior.

Paths: `endpoint_replay.py`, `capture_endpoint_orders.py`, `live_runner.py`

#### Runtime and infrastructure
The project is Python-based, with dependencies declared in `requirements.txt`. GitHub Actions automation publishes or refreshes leaderboard-related artifacts. A macOS launchd plist and shell script support scheduled live leaderboard operation. Persistent local artifacts include the trade universe, historical/sample regimes, and leaderboard/result JSON files.

Paths: `requirements.txt`, `.github/workflows/leaderboard.yml`, `com.builderr.live-leaderboard.plist`

Main entry points: [`agent.py`](https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py) (Strategy entry point), [`live_runner.py`](https://github.com/builderr-ai/builderr-trading-template/blob/main/live_runner.py) (Live leaderboard runner), [`endpoint_replay.py`](https://github.com/builderr-ai/builderr-trading-template/blob/main/endpoint_replay.py) (Endpoint replay), [`capture_endpoint_orders.py`](https://github.com/builderr-ai/builderr-trading-template/blob/main/capture_endpoint_orders.py) (Endpoint order capture)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_strategy["Strategy"]
  node_brief["Agent contract<br/>documentation<br/>[AGENT_BRIEF.md]"]
  node_anatomy["Strategy guidance<br/>documentation<br/>[ANATOMY.md]"]
  node_agent["Strategy entry point<br/>Python agent<br/>[agent.py]"]
  node_regime["Regime detection<br/>strategy layer<br/>[agent.py]"]
  node_allocation["Rotation &amp; sizing<br/>strategy layer<br/>[agent.py]"]
  node_market[("Market history<br/>runtime input")]
  node_portfolio[("Portfolio state<br/>runtime input")]
  node_orders["Order list<br/>decision output"]
  node_universe["Trade universe<br/>market data<br/>[universe.json]"]
end

subgraph group_validation["Local Validation"]
  node_preview["Preflight evaluation<br/>Python runner<br/>[preview.py]"]
  node_selfcheck["Synthetic smoke test<br/>Python runner<br/>[selfcheck.py]"]
  node_strategy_test["Strategy assertions<br/>Python test"]
end

subgraph group_submission["Submission &amp; Live"]
  node_platform{{"Scoring &amp; execution engine<br/>external platform"}}
  node_endpoint{{"Hosted /decide endpoint<br/>external integration"}}
  node_endpoint_replay["Endpoint replay<br/>Python integration tool<br/>[endpoint_replay.py]"]
  node_capture["Endpoint order capture<br/>Python integration tool"]
  node_live_runner["Live leaderboard runner<br/>Python runner<br/>[live_runner.py]"]
end

subgraph group_operations["Operations"]
  node_automation["Leaderboard automation<br/>GitHub Actions workflow<br/>[leaderboard.yml]"]
  node_scheduler["macOS scheduler<br/>launchd service"]
  node_dependencies["Python dependencies<br/>dependency manifest<br/>[requirements.txt]"]
end

node_brief -->|"specifies"| node_agent
node_anatomy -.->|"guides"| node_agent
node_market -->|"daily bars"| node_agent
node_portfolio -->|"holdings and prices"| node_agent
node_universe -->|"tradable instruments"| node_allocation
node_agent -->|"derives"| node_regime
node_regime -->|"selects risk mode"| node_allocation
node_allocation -->|"target trades"| node_orders
node_orders -->|"submitted decisions"| node_platform
node_agent -.->|"evaluated by"| node_preview
node_agent -.->|"smoke tested by"| node_selfcheck
node_agent -.->|"asserted by"| node_strategy_test
node_endpoint -->|"responses"| node_capture
node_capture -->|"captured orders"| node_endpoint_replay
node_endpoint_replay -->|"replayed decisions"| node_platform
node_platform -->|"paper-trading results"| node_live_runner
node_scheduler -->|"schedules"| node_live_runner
node_live_runner -->|"leaderboard artifacts"| node_automation
node_dependencies -.->|"runtime packages"| node_agent

click node_brief "https://github.com/builderr-ai/builderr-trading-template/blob/main/AGENT_BRIEF.md"
click node_anatomy "https://github.com/builderr-ai/builderr-trading-template/blob/main/ANATOMY.md"
click node_agent "https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py"
click node_regime "https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py"
click node_allocation "https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py"
click node_universe "https://github.com/builderr-ai/builderr-trading-template/blob/main/universe.json"
click node_preview "https://github.com/builderr-ai/builderr-trading-template/blob/main/preview.py"
click node_selfcheck "https://github.com/builderr-ai/builderr-trading-template/blob/main/selfcheck.py"
click node_strategy_test "https://github.com/builderr-ai/builderr-trading-template/blob/main/strategy_selftest.py"
click node_endpoint_replay "https://github.com/builderr-ai/builderr-trading-template/blob/main/endpoint_replay.py"
click node_capture "https://github.com/builderr-ai/builderr-trading-template/blob/main/capture_endpoint_orders.py"
click node_live_runner "https://github.com/builderr-ai/builderr-trading-template/blob/main/live_runner.py"
click node_automation "https://github.com/builderr-ai/builderr-trading-template/blob/main/.github/workflows/leaderboard.yml"
click node_scheduler "https://github.com/builderr-ai/builderr-trading-template/blob/main/com.builderr.live-leaderboard.plist"
click node_dependencies "https://github.com/builderr-ai/builderr-trading-template/blob/main/requirements.txt"

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_brief,node_anatomy,node_agent,node_regime,node_allocation,node_market,node_portfolio,node_orders,node_universe toneBlue
class node_preview,node_selfcheck,node_strategy_test toneAmber
class node_platform,node_endpoint,node_endpoint_replay,node_capture,node_live_runner toneMint
class node_automation,node_scheduler,node_dependencies toneRose
```

## Components

### Strategy

- [Agent contract](https://github.com/builderr-ai/builderr-trading-template/blob/main/AGENT_BRIEF.md): documentation · Defines the required decide interface and order contract. · `AGENT_BRIEF.md`
- [Strategy guidance](https://github.com/builderr-ai/builderr-trading-template/blob/main/ANATOMY.md): documentation · `ANATOMY.md`
- [Strategy entry point](https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py): Python agent · Exports decide(market_state, portfolio_state, cash). · `agent.py`
- [Regime detection](https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py): strategy layer · Uses SPY/QQQ trend and QQQ volatility. · `agent.py`
- [Rotation & sizing](https://github.com/builderr-ai/builderr-trading-template/blob/main/agent.py): strategy layer · Ranks risk-on assets or defensive ETFs and caps beta-adjusted exposure. · `agent.py`
- **Market history**: runtime input · Oldest-first daily OHLCV bars.
- **Portfolio state**: runtime input · Positions, cash, and last prices for reconciliation.
- **Order list**: decision output · Long-only buy and sell orders.
- [Trade universe](https://github.com/builderr-ai/builderr-trading-template/blob/main/universe.json): market data · `universe.json`

### Local Validation

- [Preflight evaluation](https://github.com/builderr-ai/builderr-trading-template/blob/main/preview.py): Python runner · Runs bundled public market windows and reports safety/performance. · `preview.py`
- [Synthetic smoke test](https://github.com/builderr-ai/builderr-trading-template/blob/main/selfcheck.py): Python runner · Checks output shape and crash resistance. · `selfcheck.py`
- [Strategy assertions](https://github.com/builderr-ai/builderr-trading-template/blob/main/strategy_selftest.py): Python test · Verifies regime behavior and exposure caps. · `strategy_selftest.py`

### Submission & Live

- **Scoring & execution engine**: external platform · Enforces trading constraints and may reject or flatten orders.
- **Hosted /decide endpoint**: external integration · Optional HTTPS decision provider.
- [Endpoint replay](https://github.com/builderr-ai/builderr-trading-template/blob/main/endpoint_replay.py): Python integration tool · `endpoint_replay.py`
- [Endpoint order capture](https://github.com/builderr-ai/builderr-trading-template/blob/main/capture_endpoint_orders.py): Python integration tool · Captures endpoint responses for later execution replay. · `capture_endpoint_orders.py`
- [Live leaderboard runner](https://github.com/builderr-ai/builderr-trading-template/blob/main/live_runner.py): Python runner · Supports shared daily paper-trading and leaderboard refresh. · `live_runner.py`

### Operations

- [Leaderboard automation](https://github.com/builderr-ai/builderr-trading-template/blob/main/.github/workflows/leaderboard.yml): GitHub Actions workflow · Publishes or refreshes leaderboard artifacts. · `.github/workflows/leaderboard.yml`
- [macOS scheduler](https://github.com/builderr-ai/builderr-trading-template/blob/main/com.builderr.live-leaderboard.plist): launchd service · Schedules local live leaderboard operation. · `com.builderr.live-leaderboard.plist`
- [Python dependencies](https://github.com/builderr-ai/builderr-trading-template/blob/main/requirements.txt): dependency manifest · `requirements.txt`

## Connections

- Agent contract → Strategy entry point: specifies
- Strategy guidance → Strategy entry point: guides
- Market history → Strategy entry point: daily bars
- Portfolio state → Strategy entry point: holdings and prices
- Trade universe → Rotation & sizing: tradable instruments
- Strategy entry point → Regime detection: derives
- Regime detection → Rotation & sizing: selects risk mode
- Rotation & sizing → Order list: target trades
- Order list → Scoring & execution engine: submitted decisions
- Strategy entry point → Preflight evaluation: evaluated by
- Strategy entry point → Synthetic smoke test: smoke tested by
- Strategy entry point → Strategy assertions: asserted by
- Hosted /decide endpoint → Endpoint order capture: responses
- Endpoint order capture → Endpoint replay: captured orders
- Endpoint replay → Scoring & execution engine: replayed decisions
- Scoring & execution engine → Live leaderboard runner: paper-trading results
- macOS scheduler → Live leaderboard runner: schedules
- Live leaderboard runner → Leaderboard automation: leaderboard artifacts
- Python dependencies → Strategy entry point: runtime packages
