# boscaleb/ai-trading-bot architecture

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

- Interactive diagram: https://gitdiagram.com/boscaleb/ai-trading-bot
- Repository: https://github.com/boscaleb/ai-trading-bot

## Overview

This repository implements an automated trading desk with a browser dashboard, scheduled trading and reporting endpoints, strategy evaluation, broker execution, risk controls, and backtesting. The principal workflow runs a timeframe-specific cron request through the trading engine, which evaluates market data and risk rules before submitting broker orders. The dashboard separately requests a snapshot assembled from broker state and market evaluations, then presents account, market, order, equity, and risk views. The README is empty; some route and UI relationships are supported only by the source index, so those links are marked unverified where appropriate.

Main entry points: [`lib/trading/engine.ts`](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/engine.ts) (Trade cycle), [`lib/trading/snapshot.ts`](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/snapshot.ts) (Desk snapshot), [`app/api/cron/report/[type]/route.ts`](https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/cron/report/%5Btype%5D/route.ts) (Report endpoint), [`lib/backtest/engine.ts`](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/engine.ts) (Backtest engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["API and dashboard"]
  node_trade_route["Trade endpoint<br/>[route.ts]"]
  node_report_route["Report endpoint<br/>[route.ts]"]
  node_dashboard_api["Dashboard API<br/>[route.ts]"]
  node_reports_api["Reports API<br/>[route.ts]"]
  node_dashboard_ui["Trading dashboard<br/>[dashboard.tsx]"]
  node_dashboard_views["Dashboard panels"]
  node_button["UI button<br/>[button.tsx]"]
  node_dashboard_auth["Dashboard access<br/>[dashboard-auth.ts]"]
  node_cron_auth["Cron authorization<br/>[cron-auth.ts]"]
  node_format["Display formatting<br/>[format.ts]"]
end

subgraph group_trade["Trading runtime"]
  node_snapshot["Desk snapshot<br/>[snapshot.ts]"]
  node_engine["Trade cycle<br/>[engine.ts]"]
  node_strategies["Strategy dispatch<br/>[index.ts]"]
  node_strategy_impls["Trading strategies"]
  node_indicators["Market indicators<br/>[indicators.ts]"]
  node_markets["Market configuration<br/>[markets.ts]"]
  node_risk["Risk and sizing<br/>[risk.ts]"]
  node_protection["Stop protection<br/>[protection.ts]"]
  node_types["Trading data types<br/>[types.ts]"]
end

subgraph group_report["Reports and backtests"]
  node_analyst["Analyst reports<br/>[analyst.ts]"]
  node_backtest["Backtest engine<br/>[engine.ts]"]
  node_history["Market history<br/>[history.ts]"]
  node_metrics["Backtest metrics<br/>[metrics.ts]"]
end

subgraph group_integrations["External services"]
  node_broker["Broker adapter<br/>[alpaca.ts]"]
  node_alpaca["Alpaca"]
  node_sms["SMS notification<br/>[sms.ts]"]
  node_sms_service["SMS provider"]
  node_ai_service["Text generation service"]
end

node_scheduler(("Cron scheduler"))
node_trader(("Trading user"))

node_scheduler -.->|"triggers"| node_trade_route
node_scheduler -.->|"triggers"| node_report_route
node_trade_route -.->|"checks access"| node_cron_auth
node_trade_route -.->|"runs cycle"| node_engine
node_report_route -.->|"checks access"| node_cron_auth
node_report_route -.->|"requests state"| node_snapshot
node_report_route -.->|"generates report"| node_analyst
node_dashboard_api -.->|"builds state"| node_snapshot
node_reports_api -.->|"requests state"| node_snapshot
node_reports_api -.->|"generates report"| node_analyst
node_trader -.->|"views desk"| node_dashboard_ui
node_dashboard_ui -.->|"requests snapshot"| node_dashboard_api
node_dashboard_ui -->|"renders"| node_dashboard_views
node_dashboard_views -.->|"uses controls"| node_button
node_dashboard_views -.->|"formats values"| node_format
node_dashboard_auth -.->|"guards access"| node_dashboard_ui
node_engine -->|"reads and trades"| node_broker
node_engine -.->|"evaluates signals"| node_strategies
node_engine -->|"filters and sizes"| node_risk
node_engine -->|"checks stops"| node_protection
node_strategies -->|"dispatches"| node_strategy_impls
node_strategy_impls -->|"uses indicators"| node_indicators
node_snapshot -->|"reads account data"| node_broker
node_snapshot -->|"evaluates markets"| node_engine
node_snapshot -->|"computes exposure"| node_risk
node_broker -->|"uses market config"| node_markets
node_broker -.->|"requests data and orders"| node_alpaca
node_report_route -.->|"sends notification"| node_sms
node_sms -.->|"delivers message"| node_sms_service
node_analyst -->|"generates text"| node_ai_service
node_backtest -.->|"uses bars"| node_history
node_backtest -->|"replays strategy"| node_strategies
node_backtest -->|"sizes positions"| node_risk
node_backtest -->|"computes results"| node_metrics

click node_trade_route "https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/cron/trade/%5Btimeframe%5D/route.ts"
click node_report_route "https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/cron/report/%5Btype%5D/route.ts"
click node_dashboard_api "https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/dashboard/route.ts"
click node_reports_api "https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/reports/route.ts"
click node_dashboard_ui "https://github.com/boscaleb/ai-trading-bot/blob/main/components/dashboard/dashboard.tsx"
click node_dashboard_views "https://github.com/boscaleb/ai-trading-bot/tree/main/components/dashboard"
click node_button "https://github.com/boscaleb/ai-trading-bot/blob/main/components/ui/button.tsx"
click node_dashboard_auth "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/dashboard-auth.ts"
click node_cron_auth "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/cron-auth.ts"
click node_snapshot "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/snapshot.ts"
click node_engine "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/engine.ts"
click node_strategies "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/strategies/index.ts"
click node_strategy_impls "https://github.com/boscaleb/ai-trading-bot/tree/main/lib/trading/strategies"
click node_indicators "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/indicators.ts"
click node_markets "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/markets.ts"
click node_risk "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/risk.ts"
click node_protection "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/protection.ts"
click node_types "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/types.ts"
click node_broker "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/broker/alpaca.ts"
click node_sms "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/notify/sms.ts"
click node_analyst "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/reports/analyst.ts"
click node_backtest "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/engine.ts"
click node_history "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/history.ts"
click node_metrics "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/metrics.ts"
click node_format "https://github.com/boscaleb/ai-trading-bot/blob/main/lib/format.ts"

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_trade_route,node_report_route,node_dashboard_api,node_reports_api,node_dashboard_ui,node_dashboard_views,node_button,node_dashboard_auth,node_cron_auth,node_format,node_trader toneBlue
class node_snapshot,node_engine,node_strategies,node_strategy_impls,node_indicators,node_markets,node_risk,node_protection,node_types toneAmber
class node_analyst,node_backtest,node_history,node_metrics toneMint
class node_broker,node_alpaca,node_sms,node_sms_service,node_ai_service,node_scheduler toneRose
```

## Components

### API and dashboard

- [Trade endpoint](https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/cron/trade/%5Btimeframe%5D/route.ts): `app/api/cron/trade/[timeframe]/route.ts`
- [Report endpoint](https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/cron/report/%5Btype%5D/route.ts): `app/api/cron/report/[type]/route.ts`
- [Dashboard API](https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/dashboard/route.ts): `app/api/dashboard/route.ts`
- [Reports API](https://github.com/boscaleb/ai-trading-bot/blob/main/app/api/reports/route.ts): `app/api/reports/route.ts`
- [Trading dashboard](https://github.com/boscaleb/ai-trading-bot/blob/main/components/dashboard/dashboard.tsx): `components/dashboard/dashboard.tsx`
- [Dashboard panels](https://github.com/boscaleb/ai-trading-bot/tree/main/components/dashboard): `components/dashboard`
- [UI button](https://github.com/boscaleb/ai-trading-bot/blob/main/components/ui/button.tsx): `components/ui/button.tsx`
- [Dashboard access](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/dashboard-auth.ts): `lib/dashboard-auth.ts`
- [Cron authorization](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/cron-auth.ts): `lib/cron-auth.ts`
- [Display formatting](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/format.ts): `lib/format.ts`

### Trading runtime

- [Desk snapshot](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/snapshot.ts): `lib/trading/snapshot.ts`
- [Trade cycle](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/engine.ts): `lib/trading/engine.ts`
- [Strategy dispatch](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/strategies/index.ts): `lib/trading/strategies/index.ts`
- [Trading strategies](https://github.com/boscaleb/ai-trading-bot/tree/main/lib/trading/strategies): `lib/trading/strategies`
- [Market indicators](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/indicators.ts): `lib/trading/indicators.ts`
- [Market configuration](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/markets.ts): `lib/trading/markets.ts`
- [Risk and sizing](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/risk.ts): `lib/trading/risk.ts`
- [Stop protection](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/protection.ts): `lib/trading/protection.ts`
- [Trading data types](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/trading/types.ts): `lib/trading/types.ts`

### Reports and backtests

- [Analyst reports](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/reports/analyst.ts): `lib/reports/analyst.ts`
- [Backtest engine](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/engine.ts): `lib/backtest/engine.ts`
- [Market history](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/history.ts): `lib/backtest/history.ts`
- [Backtest metrics](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/backtest/metrics.ts): `lib/backtest/metrics.ts`

### External services

- [Broker adapter](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/broker/alpaca.ts): `lib/broker/alpaca.ts`
- **Alpaca**
- [SMS notification](https://github.com/boscaleb/ai-trading-bot/blob/main/lib/notify/sms.ts): `lib/notify/sms.ts`
- **SMS provider**
- **Text generation service**

### Other components

- **Cron scheduler**
- **Trading user**

## Connections

- Cron scheduler → Trade endpoint: triggers
- Cron scheduler → Report endpoint: triggers
- Trade endpoint → Cron authorization: checks access (evidence: `app/api/cron/trade/[timeframe]/route.ts`)
- Trade endpoint → Trade cycle: runs cycle (evidence: `app/api/cron/trade/[timeframe]/route.ts`)
- Report endpoint → Cron authorization: checks access (evidence: `app/api/cron/report/[type]/route.ts`)
- Report endpoint → Desk snapshot: requests state (evidence: `app/api/cron/report/[type]/route.ts`)
- Report endpoint → Analyst reports: generates report (evidence: `app/api/cron/report/[type]/route.ts`)
- Dashboard API → Desk snapshot: builds state (evidence: `app/api/dashboard/route.ts`)
- Reports API → Desk snapshot: requests state (evidence: `app/api/reports/route.ts`)
- Reports API → Analyst reports: generates report (evidence: `app/api/reports/route.ts`)
- Trading user → Trading dashboard: views desk
- Trading dashboard → Dashboard API: requests snapshot
- Trading dashboard → Dashboard panels: renders (evidence: `components/dashboard/dashboard.tsx`)
- Dashboard panels → UI button: uses controls (evidence: `components/dashboard/reports-panel.tsx`)
- Dashboard panels → Display formatting: formats values (evidence: `components/dashboard/market-card.tsx`)
- Dashboard access → Trading dashboard: guards access
- Trade cycle → Broker adapter: reads and trades (evidence: `lib/trading/engine.ts`)
- Trade cycle → Strategy dispatch: evaluates signals (evidence: `lib/trading/engine.ts`)
- Trade cycle → Risk and sizing: filters and sizes (evidence: `lib/trading/engine.ts`)
- Trade cycle → Stop protection: checks stops (evidence: `lib/trading/engine.ts`)
- Strategy dispatch → Trading strategies: dispatches (evidence: `lib/trading/strategies/index.ts`)
- Trading strategies → Market indicators: uses indicators (evidence: `lib/trading/strategies/mean-reversion.ts`)
- Desk snapshot → Broker adapter: reads account data (evidence: `lib/trading/snapshot.ts`)
- Desk snapshot → Trade cycle: evaluates markets (evidence: `lib/trading/snapshot.ts`)
- Desk snapshot → Risk and sizing: computes exposure (evidence: `lib/trading/snapshot.ts`)
- Broker adapter → Market configuration: uses market config (evidence: `lib/broker/alpaca.ts`)
- Broker adapter → Alpaca: requests data and orders
- Report endpoint → SMS notification: sends notification (evidence: `app/api/cron/report/[type]/route.ts`)
- SMS notification → SMS provider: delivers message
- Analyst reports → Text generation service: generates text (evidence: `lib/reports/analyst.ts`)
- Backtest engine → Market history: uses bars
- Backtest engine → Strategy dispatch: replays strategy (evidence: `lib/backtest/engine.ts`)
- Backtest engine → Risk and sizing: sizes positions (evidence: `lib/backtest/engine.ts`)
- Backtest engine → Backtest metrics: computes results (evidence: `lib/backtest/engine.ts`)
