# amitc3353/quantai architecture

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

- Interactive diagram: https://gitdiagram.com/amitc3353/quantai
- Repository: https://github.com/amitc3353/quantai

## Overview

QuantAI is an autonomous, cron-driven options-trading system with deterministic safety controls, broker execution, and operator alerts. The main workflow is scheduled agent evaluation, risk gating, broker order handling, and journal-based monitoring and learning. The README also describes Beta, Gamma, Sentinel, and a 13-state lifecycle; these are represented using the listed source tree, though their implementation details were not sampled. Legacy capabilities are grouped separately. Edges reflect README statements or shown references; unsampled subsystems are not given speculative internal call chains.

Main entry points: [`legacy/orchestrator`](https://github.com/amitc3353/quantai/tree/main/legacy/orchestrator) (Legacy trading agents), [`v2/shared-data/scripts`](https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts) (Deterministic gates), [`v2/shared-data/scripts/broker.py`](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/broker.py) (Broker adapter), [`v2/shared-data/scripts/lifecycle/journal_io.py`](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/lifecycle/journal_io.py) (Append-only journal)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_agents["Trading agents"]
  node_pipeline["Entry pipeline<br/>[run_pipeline.py]"]
  node_alpha["Alpha agent<br/>[run_pipeline.py]"]
  node_beta["Beta agent<br/>[beta_agent.py]"]
  node_beta_regime["Regime and strategies"]
  node_gamma["Gamma agent<br/>[gamma_agent.py]"]
  node_gamma_scan["Gamma scanner"]
  node_sentinel["Sentinel agent<br/>[sentinel_agent.py]"]
end

subgraph group_safety["Safety and execution"]
  node_gates["Deterministic gates"]
  node_risk["Agent risk engines<br/>[risk_engine.py]"]
  node_broker["Broker adapter<br/>[broker.py]"]
end

subgraph group_state["Trade state"]
  node_journal[("Append-only journal<br/>[journal_io.py]")]
  node_lifecycle["Order lifecycle FSM<br/>[trade_lifecycle.py]"]
  node_position_monitor["Position monitor"]
end

subgraph group_operations["Operations and learning"]
  node_intelligence["Market intelligence"]
  node_llm["LLM call layer<br/>[_llm_call.py]"]
  node_discord["Discord alerts<br/>[_discord.py]"]
  node_learning["Learning and review"]
end

subgraph group_legacy["Legacy system"]
  node_legacy_agents["Legacy trading agents"]
  node_legacy_scheduler["Legacy scheduler<br/>[scheduler.py]"]
  node_legacy_context["Legacy trade context<br/>[context_builder.py]"]
  node_legacy_market["Legacy market data<br/>[market_data.py]"]
  node_legacy_flow["Legacy flow analysis<br/>[flow_detector.py]"]
  node_legacy_memory[("Legacy trading memory<br/>[memory.py]")]
  node_legacy_broker["Legacy Alpaca client<br/>[alpaca_client.py]"]
  node_legacy_services["Legacy research services"]
  node_legacy_discord["Legacy Discord bot"]
  node_legacy_guards["Legacy guard service<br/>[guards.py]"]
end

node_operator(("Operator"))
node_ibkr["IBKR broker"]
node_alpaca["Alpaca broker"]
node_llm_service["LLM ingress"]

node_operator -->|"approves and receives"| node_discord
node_pipeline -->|"runs entries"| node_alpha
node_alpha -->|"submits candidates"| node_gates
node_beta -->|"submits candidates"| node_gates
node_gamma -->|"submits candidates"| node_gates
node_beta -->|"classifies and selects"| node_beta_regime
node_gamma -->|"scans candidates"| node_gamma_scan
node_gates -->|"checks risk"| node_risk
node_gates -->|"clears orders"| node_broker
node_broker -->|"routes orders"| node_ibkr
node_broker -.->|"fallback routes"| node_alpaca
node_broker -->|"records trades"| node_journal
node_journal -->|"reconciles positions"| node_position_monitor
node_journal -->|"feeds learning"| node_learning
node_lifecycle -->|"reads and writes"| node_journal
node_lifecycle -.->|"checks broker state"| node_ibkr
node_sentinel -.->|"reads health"| node_position_monitor
node_pipeline -->|"reads conditions"| node_intelligence
node_alpha -->|"requests judgment"| node_llm
node_llm -->|"routes metered calls"| node_llm_service
node_llm -->|"reports usage"| node_discord
node_sentinel -->|"requests triage"| node_llm
node_learning -.->|"sends summaries"| node_discord
node_legacy_scheduler -->|"schedules agents"| node_legacy_agents
node_legacy_scheduler -->|"requests context"| node_legacy_context
node_legacy_scheduler -->|"uses research services"| node_legacy_services
node_legacy_agents -->|"reads market data"| node_legacy_market
node_legacy_agents -->|"reads options flow"| node_legacy_flow
node_legacy_agents -->|"retrieves lessons"| node_legacy_memory
node_legacy_agents -.->|"executes trades"| node_legacy_broker
node_legacy_agents -.->|"checks proposals"| node_legacy_guards
node_legacy_discord -->|"uses trade memory"| node_legacy_memory

click node_pipeline "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/run_pipeline.py"
click node_alpha "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/run_pipeline.py"
click node_beta "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/beta_agent.py"
click node_beta_regime "https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts/beta"
click node_gamma "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/gamma_agent.py"
click node_gamma_scan "https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts/gamma"
click node_sentinel "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/sentinel_agent.py"
click node_gates "https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts"
click node_risk "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/beta/risk_engine.py"
click node_broker "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/broker.py"
click node_journal "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/lifecycle/journal_io.py"
click node_lifecycle "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/lifecycle/trade_lifecycle.py"
click node_position_monitor "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/position_monitor.py"
click node_intelligence "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/market_intelligence.py"
click node_llm "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/_llm_call.py"
click node_discord "https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/_discord.py"
click node_learning "https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts"
click node_legacy_agents "https://github.com/amitc3353/quantai/tree/main/legacy/orchestrator"
click node_legacy_scheduler "https://github.com/amitc3353/quantai/blob/main/legacy/orchestrator/scheduler.py"
click node_legacy_context "https://github.com/amitc3353/quantai/blob/main/legacy/services/context_builder.py"
click node_legacy_market "https://github.com/amitc3353/quantai/blob/main/legacy/services/market_data.py"
click node_legacy_flow "https://github.com/amitc3353/quantai/blob/main/legacy/services/flow_detector.py"
click node_legacy_memory "https://github.com/amitc3353/quantai/blob/main/legacy/discord-bot/memory.py"
click node_legacy_broker "https://github.com/amitc3353/quantai/blob/main/legacy/discord-bot/alpaca_client.py"
click node_legacy_services "https://github.com/amitc3353/quantai/tree/main/legacy/services"
click node_legacy_discord "https://github.com/amitc3353/quantai/tree/main/legacy/discord-bot"
click node_legacy_guards "https://github.com/amitc3353/quantai/blob/main/legacy/guard-engine/guards.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_pipeline,node_alpha,node_beta,node_beta_regime,node_gamma,node_gamma_scan,node_sentinel toneBlue
class node_gates,node_risk,node_broker toneAmber
class node_journal,node_lifecycle,node_position_monitor toneMint
class node_intelligence,node_llm,node_discord,node_learning toneRose
class node_legacy_agents,node_legacy_scheduler,node_legacy_context,node_legacy_market,node_legacy_flow,node_legacy_memory,node_legacy_broker,node_legacy_services,node_legacy_discord,node_legacy_guards,node_operator,node_ibkr,node_alpaca,node_llm_service toneIndigo
```

## Components

### Trading agents

- [Entry pipeline](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/run_pipeline.py): `v2/shared-data/scripts/run_pipeline.py`
- [Alpha agent](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/run_pipeline.py): `v2/shared-data/scripts/run_pipeline.py`
- [Beta agent](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/beta_agent.py): `v2/shared-data/scripts/beta_agent.py`
- [Regime and strategies](https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts/beta): `v2/shared-data/scripts/beta`
- [Gamma agent](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/gamma_agent.py): `v2/shared-data/scripts/gamma_agent.py`
- [Gamma scanner](https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts/gamma): `v2/shared-data/scripts/gamma`
- [Sentinel agent](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/sentinel_agent.py): `v2/shared-data/scripts/sentinel_agent.py`

### Safety and execution

- [Deterministic gates](https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts): `v2/shared-data/scripts`
- [Agent risk engines](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/beta/risk_engine.py): `v2/shared-data/scripts/beta/risk_engine.py`
- [Broker adapter](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/broker.py): `v2/shared-data/scripts/broker.py`

### Trade state

- [Append-only journal](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/lifecycle/journal_io.py): `v2/shared-data/scripts/lifecycle/journal_io.py`
- [Order lifecycle FSM](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/lifecycle/trade_lifecycle.py): `v2/shared-data/scripts/lifecycle/trade_lifecycle.py`
- [Position monitor](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/position_monitor.py): `v2/shared-data/scripts/position_monitor.py`

### Operations and learning

- [Market intelligence](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/market_intelligence.py): `v2/shared-data/scripts/market_intelligence.py`
- [LLM call layer](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/_llm_call.py): `v2/shared-data/scripts/_llm_call.py`
- [Discord alerts](https://github.com/amitc3353/quantai/blob/main/v2/shared-data/scripts/_discord.py): `v2/shared-data/scripts/_discord.py`
- [Learning and review](https://github.com/amitc3353/quantai/tree/main/v2/shared-data/scripts): `v2/shared-data/scripts`

### Legacy system

- [Legacy trading agents](https://github.com/amitc3353/quantai/tree/main/legacy/orchestrator): `legacy/orchestrator`
- [Legacy scheduler](https://github.com/amitc3353/quantai/blob/main/legacy/orchestrator/scheduler.py): `legacy/orchestrator/scheduler.py`
- [Legacy trade context](https://github.com/amitc3353/quantai/blob/main/legacy/services/context_builder.py): `legacy/services/context_builder.py`
- [Legacy market data](https://github.com/amitc3353/quantai/blob/main/legacy/services/market_data.py): `legacy/services/market_data.py`
- [Legacy flow analysis](https://github.com/amitc3353/quantai/blob/main/legacy/services/flow_detector.py): `legacy/services/flow_detector.py`
- [Legacy trading memory](https://github.com/amitc3353/quantai/blob/main/legacy/discord-bot/memory.py): `legacy/discord-bot/memory.py`
- [Legacy Alpaca client](https://github.com/amitc3353/quantai/blob/main/legacy/discord-bot/alpaca_client.py): `legacy/discord-bot/alpaca_client.py`
- [Legacy research services](https://github.com/amitc3353/quantai/tree/main/legacy/services): `legacy/services`
- [Legacy Discord bot](https://github.com/amitc3353/quantai/tree/main/legacy/discord-bot): `legacy/discord-bot`
- [Legacy guard service](https://github.com/amitc3353/quantai/blob/main/legacy/guard-engine/guards.py): `legacy/guard-engine/guards.py`

### Other components

- **Operator**
- **IBKR broker**
- **Alpaca broker**
- **LLM ingress**

## Connections

- Operator → Discord alerts: approves and receives
- Entry pipeline → Alpha agent: runs entries (evidence: `v2/shared-data/scripts/run_pipeline.py`)
- Alpha agent → Deterministic gates: submits candidates (evidence: `v2/shared-data/scripts/run_pipeline.py`)
- Beta agent → Deterministic gates: submits candidates
- Gamma agent → Deterministic gates: submits candidates
- Beta agent → Regime and strategies: classifies and selects
- Gamma agent → Gamma scanner: scans candidates
- Deterministic gates → Agent risk engines: checks risk
- Deterministic gates → Broker adapter: clears orders
- Broker adapter → IBKR broker: routes orders
- Broker adapter → Alpaca broker: fallback routes
- Broker adapter → Append-only journal: records trades
- Append-only journal → Position monitor: reconciles positions
- Append-only journal → Learning and review: feeds learning (evidence: `v2/shared-data/scripts/lifecycle/trade_lifecycle.py`)
- Order lifecycle FSM → Append-only journal: reads and writes (evidence: `v2/shared-data/scripts/lifecycle/trade_lifecycle.py`)
- Order lifecycle FSM → IBKR broker: checks broker state
- Sentinel agent → Position monitor: reads health
- Entry pipeline → Market intelligence: reads conditions (evidence: `v2/shared-data/scripts/run_pipeline.py`)
- Alpha agent → LLM call layer: requests judgment
- LLM call layer → LLM ingress: routes metered calls
- LLM call layer → Discord alerts: reports usage (evidence: `v2/shared-data/scripts/_llm_call.py`)
- Sentinel agent → LLM call layer: requests triage
- Learning and review → Discord alerts: sends summaries (evidence: `v2/shared-data/scripts/run_pipeline.py`)
- Legacy scheduler → Legacy trading agents: schedules agents (evidence: `legacy/orchestrator/scheduler.py`)
- Legacy scheduler → Legacy trade context: requests context (evidence: `legacy/orchestrator/scheduler.py`)
- Legacy scheduler → Legacy research services: uses research services (evidence: `legacy/orchestrator/scheduler.py`)
- Legacy trading agents → Legacy market data: reads market data (evidence: `legacy/orchestrator/agent1_iron_condor.py`)
- Legacy trading agents → Legacy flow analysis: reads options flow (evidence: `legacy/orchestrator/agent1_iron_condor.py`)
- Legacy trading agents → Legacy trading memory: retrieves lessons (evidence: `legacy/orchestrator/agent2_covered_call.py`)
- Legacy trading agents → Legacy Alpaca client: executes trades
- Legacy trading agents → Legacy guard service: checks proposals
- Legacy Discord bot → Legacy trading memory: uses trade memory (evidence: `legacy/discord-bot/cogs/trading.py`)
