# erfaniaa/llm-auto-trader architecture

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

- Interactive diagram: https://gitdiagram.com/erfaniaa/llm-auto-trader
- Repository: https://github.com/erfaniaa/llm-auto-trader

## Overview

#### Purpose and entry points
LLM-Auto-Trader is a Python crypto-trading system where an LLM proposes actions and deterministic controls approve, resize, reject, and execute them. Primary live/paper orchestration is `src/bot.py`, launched through `scripts/paper_trade.py`; scheduled Claude Code operation is `scripts/live_cycle.sh`. Research/backtest entry points center on `scripts/prepare_snapshots.py`, `scripts/dispatch_decisions.py`, and `scripts/run_cpcv.py`.

#### Configuration and operating modes
Layered environment-driven settings define exchange, universe, LLM, risk, cadence, storage, and notifications in `config/settings.py`; symbol resolution/fallbacks live in `config/symbols.py`. The key boundary is `BOT_MODE`: paper mode simulates execution, while live mode requires exchange credentials and submits real orders. Exchange-specific fees, maintenance margin, and market conventions are centralized in `src/exchange/profile.py`.

#### Market-data and feature pipeline
Data adapters collect exchange market/derivatives data and supplemental information, then aggregate it into a decision-ready market state. Core ingestion boundaries are `src/data/market.py`, `src/data/aggregator.py`, and `src/data/websocket.py`; external/specialized feeds include news, on-chain, sentiment, prediction markets, and top-trader signals under `src/data/`. Analysis transforms inputs into indicators, statistical features, and market/risk regimes via `src/analysis/indicators.py`, `src/analysis/market_state.py`, and `src/analysis/regime.py`.

#### LLM decision subsystem
`src/llm/context_builder.py` composes token-budgeted context from features, positions, and memory. `src/llm/client.py` selects a provider through `src/llm/providers/factory.py`; supported external services are Anthropic, OpenAI, Google, and OpenAI-compatible endpoints. `src/llm/response_parser.py` converts structured model output into actions, while `src/llm/cost_tracker.py` enforces token-spend accounting. Prompt assets are versioned under `prompts/`, including `prompts/decide_system_prompt.md`.

#### Validation, risk, and execution boundary
Before any order crosses into execution, response validation and deterministic risk controls gate it. `src/live/decision_validator.py` validates candidate decisions; `src/risk/risk_manager.py` coordinates sizing, leverage/correlation constraints, circuit breakers, margin checks, stops, cooldowns, and volatility targeting using components such as `src/risk/circuit_breakers.py` and `src/risk/position_sizer.py`. Approved actions pass to `src/execution/executor.py` and `src/execution/position_manager.py`, which interact with CCXT-supported exchanges or paper simulation.

#### Persistence and reporting
SQLite is the default durable boundary, implemented in `src/storage/database.py`, with lifecycle cleanup in `src/storage/retention.py`. It stores operational records such as decisions, trades, prompts, and data according to retention policy. Notifications are optional and isolated in `src/telegram/reporter.py`, targeting the Telegram Bot API; console/logging remains local.

#### Historical evaluation architecture
The backtest system separates historical inputs, point-in-time snapshots, cached LLM decisions, and simulation. `src/backtest/snapshot_builder.py` prevents prompt lookahead; `src/backtest/decision_cache.py` and `src/backtest/vote_cache.py` make decisions reusable; `src/backtest/runner.py` applies fills, funding, costs, and portfolio logic. CPCV/walk-forward evaluation resides in `src/backtest/cpcv.py` and `src/backtest/walk_forward.py`, with results persisted as committed research artifacts under `runs/`. This isolates expensive or nondeterministic LLM generation from repeatable portfolio simulation and robustness analysis.

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ops["Operations"]
  node_paper_entry["Paper trade launcher<br/>entrypoint<br/>[paper_trade.py]"]
  node_bot["Trading orchestrator<br/>Python service<br/>[bot.py]"]
  node_settings["Environment settings<br/>configuration boundary<br/>[settings.py]"]
  node_database[("SQLite operational store<br/>persistence<br/>[database.py]")]
  node_telegram["Telegram reporter<br/>notification adapter<br/>[reporter.py]"]
end

subgraph group_data["Market data &amp; analysis"]
  node_market_data["Market data adapters<br/>data ingestion<br/>[market.py]"]
  node_market_aggregation["Market-state aggregation<br/>data pipeline<br/>[aggregator.py]"]
  node_analysis["Features &amp; regimes<br/>analysis pipeline<br/>[market_state.py]"]
end

subgraph group_decision["LLM decisioning"]
  node_context_builder["LLM context builder<br/>prompt assembly<br/>[context_builder.py]"]
  node_prompt["Decision system prompt<br/>prompt asset"]
  node_llm_client["LLM client &amp; providers<br/>LLM gateway<br/>[client.py]"]
  node_response_parser["Structured response parser<br/>decision parser<br/>[response_parser.py]"]
end

subgraph group_control["Risk &amp; execution"]
  node_decision_validator["Decision validator<br/>validation gate"]
  node_risk_manager["Risk manager<br/>deterministic control<br/>[risk_manager.py]"]
  node_executor["Order executor<br/>execution boundary<br/>[executor.py]"]
  node_position_manager["Position manager<br/>portfolio state"]
end

subgraph group_research["Historical evaluation"]
  node_snapshot_builder["Point-in-time snapshots<br/>historical data stage"]
  node_decision_cache[("Cached LLM decisions<br/>research cache<br/>[decision_cache.py]")]
  node_backtest_runner["Portfolio simulation<br/>backtest engine<br/>[runner.py]"]
  node_cpcv["CPCV &amp; walk-forward<br/>robustness evaluation<br/>[cpcv.py]"]
  node_runs[("Research results<br/>committed artifacts")]
end

node_paper_entry -->|"launches"| node_bot
node_settings -.->|"configures mode"| node_bot
node_bot -->|"requests feeds"| node_market_data
node_market_data -->|"raw market inputs"| node_market_aggregation
node_market_aggregation -->|"aggregated state"| node_analysis
node_analysis -->|"features and regimes"| node_context_builder
node_position_manager -->|"positions"| node_context_builder
node_prompt -.->|"instructions"| node_context_builder
node_context_builder -->|"decision request"| node_llm_client
node_llm_client -->|"structured output"| node_response_parser
node_response_parser -->|"candidate actions"| node_decision_validator
node_decision_validator -->|"validated actions"| node_risk_manager
node_risk_manager -->|"approved/resized orders"| node_executor
node_executor -->|"fills and order state"| node_position_manager
node_bot -->|"operational records"| node_database
node_executor -->|"trades"| node_database
node_bot -.->|"optional reports"| node_telegram
node_snapshot_builder -->|"historical contexts"| node_decision_cache
node_decision_cache -->|"reusable decisions"| node_backtest_runner
node_backtest_runner -->|"simulation outcomes"| node_cpcv
node_cpcv -->|"evaluation artifacts"| node_runs

click node_paper_entry "https://github.com/erfaniaa/llm-auto-trader/blob/main/scripts/paper_trade.py"
click node_bot "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/bot.py"
click node_settings "https://github.com/erfaniaa/llm-auto-trader/blob/main/config/settings.py"
click node_market_data "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/data/market.py"
click node_market_aggregation "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/data/aggregator.py"
click node_analysis "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/analysis/market_state.py"
click node_context_builder "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/llm/context_builder.py"
click node_prompt "https://github.com/erfaniaa/llm-auto-trader/blob/main/prompts/decide_system_prompt.md"
click node_llm_client "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/llm/client.py"
click node_response_parser "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/llm/response_parser.py"
click node_decision_validator "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/live/decision_validator.py"
click node_risk_manager "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/risk/risk_manager.py"
click node_executor "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/execution/executor.py"
click node_position_manager "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/execution/position_manager.py"
click node_database "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/storage/database.py"
click node_telegram "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/telegram/reporter.py"
click node_snapshot_builder "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/snapshot_builder.py"
click node_decision_cache "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/decision_cache.py"
click node_backtest_runner "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/runner.py"
click node_cpcv "https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/cpcv.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_paper_entry,node_bot,node_settings,node_database,node_telegram toneBlue
class node_market_data,node_market_aggregation,node_analysis toneAmber
class node_context_builder,node_prompt,node_llm_client,node_response_parser toneMint
class node_decision_validator,node_risk_manager,node_executor,node_position_manager toneRose
class node_snapshot_builder,node_decision_cache,node_backtest_runner,node_cpcv,node_runs toneIndigo
```

## Components

### Operations

- [Paper trade launcher](https://github.com/erfaniaa/llm-auto-trader/blob/main/scripts/paper_trade.py): entrypoint · Launches live/paper bot orchestration. · `scripts/paper_trade.py`
- [Trading orchestrator](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/bot.py): Python service · Coordinates the decision-to-execution cycle. · `src/bot.py`
- [Environment settings](https://github.com/erfaniaa/llm-auto-trader/blob/main/config/settings.py): configuration boundary · Defines BOT_MODE, credentials, cadence, risk, and storage settings. · `config/settings.py`
- [SQLite operational store](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/storage/database.py): persistence · Stores decisions, trades, prompts, and market data. · `src/storage/database.py`
- [Telegram reporter](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/telegram/reporter.py): notification adapter · Optionally sends operational reporting to Telegram. · `src/telegram/reporter.py`

### Market data & analysis

- [Market data adapters](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/data/market.py): data ingestion · Collects exchange, derivatives, and specialized external feeds. · `src/data/market.py`
- [Market-state aggregation](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/data/aggregator.py): data pipeline · Aggregates incoming feeds into decision-ready state. · `src/data/aggregator.py`
- [Features & regimes](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/analysis/market_state.py): analysis pipeline · Produces indicators, statistical features, and market/risk regimes. · `src/analysis/market_state.py`

### LLM decisioning

- [LLM context builder](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/llm/context_builder.py): prompt assembly · Builds token-budgeted context from features, positions, and memory. · `src/llm/context_builder.py`
- [Decision system prompt](https://github.com/erfaniaa/llm-auto-trader/blob/main/prompts/decide_system_prompt.md): prompt asset · `prompts/decide_system_prompt.md`
- [LLM client & providers](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/llm/client.py): LLM gateway · Selects Anthropic, OpenAI, Google, or compatible providers. · `src/llm/client.py`
- [Structured response parser](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/llm/response_parser.py): decision parser · Converts model output into candidate trading actions. · `src/llm/response_parser.py`

### Risk & execution

- [Decision validator](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/live/decision_validator.py): validation gate · Validates candidate decisions before risk evaluation. · `src/live/decision_validator.py`
- [Risk manager](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/risk/risk_manager.py): deterministic control · Applies sizing, margin, stops, circuit breakers, and exposure constraints. · `src/risk/risk_manager.py`
- [Order executor](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/execution/executor.py): execution boundary · Routes approved actions to CCXT exchanges or paper simulation. · `src/execution/executor.py`
- [Position manager](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/execution/position_manager.py): portfolio state · `src/execution/position_manager.py`

### Historical evaluation

- [Point-in-time snapshots](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/snapshot_builder.py): historical data stage · Builds snapshots without prompt lookahead. · `src/backtest/snapshot_builder.py`
- [Cached LLM decisions](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/decision_cache.py): research cache · Separates expensive decision generation from repeatable simulation. · `src/backtest/decision_cache.py`
- [Portfolio simulation](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/runner.py): backtest engine · Applies fills, funding, costs, and portfolio logic. · `src/backtest/runner.py`
- [CPCV & walk-forward](https://github.com/erfaniaa/llm-auto-trader/blob/main/src/backtest/cpcv.py): robustness evaluation · Runs cross-validation and walk-forward analysis. · `src/backtest/cpcv.py`
- **Research results**: committed artifacts

## Connections

- Paper trade launcher → Trading orchestrator: launches
- Environment settings → Trading orchestrator: configures mode
- Trading orchestrator → Market data adapters: requests feeds
- Market data adapters → Market-state aggregation: raw market inputs
- Market-state aggregation → Features & regimes: aggregated state
- Features & regimes → LLM context builder: features and regimes
- Position manager → LLM context builder: positions
- Decision system prompt → LLM context builder: instructions
- LLM context builder → LLM client & providers: decision request
- LLM client & providers → Structured response parser: structured output
- Structured response parser → Decision validator: candidate actions
- Decision validator → Risk manager: validated actions
- Risk manager → Order executor: approved/resized orders
- Order executor → Position manager: fills and order state
- Trading orchestrator → SQLite operational store: operational records
- Order executor → SQLite operational store: trades
- Trading orchestrator → Telegram reporter: optional reports
- Point-in-time snapshots → Cached LLM decisions: historical contexts
- Cached LLM decisions → Portfolio simulation: reusable decisions
- Portfolio simulation → CPCV & walk-forward: simulation outcomes
- CPCV & walk-forward → Research results: evaluation artifacts
