# matheusneder/ai-trade-agent architecture

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

- Interactive diagram: https://gitdiagram.com/matheusneder/ai-trade-agent
- Repository: https://github.com/matheusneder/ai-trade-agent

## Overview

AI Trade Agent is an autonomous Binance Spot trading system, currently documented as paper trading. Its main workflow builds a market universe, evaluates profile-based technical signals and optional research, applies risk controls, then simulates or places protected orders and records outcomes. Separate branches handle news analysis, copy-trading leads, operator commands, reconciliation, and observability. The README also describes a Freqtrade-based backtesting and tape-replay lab. Some subsystem details are unsampled; edges are limited to indexed references or documented workflows.

Main entry points: [`src/trade_agent/app.py`](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/app.py) (Agent runtime), [`src/trade_agent/decision/engine.py`](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/decision/engine.py) (Decision cycle), [`src/trade_agent/execution/service.py`](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/execution/service.py) (Position service), [`src/trade_agent/research/service.py`](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/research/service.py) (Research service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_trading["Trading lifecycle"]
  node_app["Agent runtime<br/>[app.py]"]
  node_universe["Market universe<br/>[universe.py]"]
  node_candles["Candle data<br/>[candles.py]"]
  node_profiles["Strategy profiles<br/>[profiles.py]"]
  node_signals["Technical signals<br/>[local.py]"]
  node_decision["Decision cycle<br/>[engine.py]"]
  node_risk["Risk guard<br/>[guard.py]"]
  node_risk_conditions["Stop conditions<br/>[conditions.py]"]
  node_execution["Position service<br/>[service.py]"]
  node_order_builders["Order builders<br/>[orders.py]"]
  node_exchange_api["Binance API<br/>[api.py]"]
  node_exchange_models["Exchange models<br/>[models.py]"]
end

subgraph group_research["Research and leads"]
  node_research_service["Research service<br/>[service.py]"]
  node_research_store["Research store<br/>[research_store.py]"]
  node_analyst["Market analyst<br/>[analyst.py]"]
  node_lead_tracker["Lead tracker<br/>[tracker.py]"]
end

subgraph group_operations["Operations and persistence"]
  node_store["Trading store<br/>[store.py]"]
  node_database[("PostgreSQL<br/>[db.py]")]
  node_commands["Telegram commands<br/>[commands.py]"]
  node_metrics["Metrics<br/>[metrics.py]"]
  node_telemetry["Telemetry recorder<br/>[recorder.py]"]
  node_tracing["Tracing<br/>[tracing.py]"]
end

subgraph group_lab["Backtesting lab"]
  node_lab_strategy["Freqtrade strategy"]
  node_walk_forward["Walk-forward runs<br/>[walk_forward.py]"]
  node_paper_replay["Tape replay<br/>[paper.py]"]
end

node_operator(("Operator"))
node_binance{{"Binance Spot"}}
node_claude{{"Claude API"}}
node_telegram{{"Telegram"}}

node_operator -->|"starts agent"| node_app
node_app -->|"builds universe"| node_universe
node_app -->|"runs cycles"| node_decision
node_decision -->|"loads profiles"| node_profiles
node_decision -->|"reads candles"| node_candles
node_decision -->|"evaluates signals"| node_signals
node_decision -->|"checks risk"| node_risk
node_risk -->|"evaluates stops"| node_risk_conditions
node_decision -->|"requests trades"| node_execution
node_execution -->|"builds orders"| node_order_builders
node_execution -->|"sends orders"| node_exchange_api
node_order_builders -->|"uses order types"| node_exchange_models
node_exchange_api -->|"calls endpoints"| node_binance
node_universe -->|"fetches market data"| node_exchange_api
node_app -->|"schedules research"| node_research_service
node_research_service -->|"runs analysis"| node_analyst
node_research_service -->|"persists reports"| node_research_store
node_analyst -->|"analyzes news"| node_claude
node_app -->|"reads trader leads"| node_lead_tracker
node_app -->|"records outcomes"| node_store
node_research_store -->|"stores research"| node_database
node_store -->|"stores trading state"| node_database
node_operator -->|"sends commands"| node_commands
node_commands -->|"receives commands"| node_telegram
node_app -->|"records snapshots"| node_telemetry
node_telemetry -->|"stores snapshots"| node_database
node_app -.->|"reports metrics"| node_metrics
node_app -.->|"instruments components"| node_tracing
node_walk_forward -->|"runs backtests"| node_lab_strategy
node_lab_strategy -->|"reuses signals"| node_signals
node_paper_replay -->|"replays decisions"| node_signals

click node_app "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/app.py"
click node_universe "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/market/universe.py"
click node_candles "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/market/candles.py"
click node_profiles "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/strategy/profiles.py"
click node_signals "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/signals/local.py"
click node_decision "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/decision/engine.py"
click node_risk "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/risk/guard.py"
click node_risk_conditions "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/risk/conditions.py"
click node_execution "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/execution/service.py"
click node_order_builders "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/execution/orders.py"
click node_exchange_api "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/exchange/api.py"
click node_exchange_models "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/exchange/models.py"
click node_research_service "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/research/service.py"
click node_research_store "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/persistence/research_store.py"
click node_analyst "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/research/analyst.py"
click node_lead_tracker "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/leads/tracker.py"
click node_store "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/persistence/store.py"
click node_database "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/persistence/db.py"
click node_commands "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/notify/commands.py"
click node_metrics "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/metrics.py"
click node_telemetry "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/telemetry/recorder.py"
click node_tracing "https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/tracing.py"
click node_lab_strategy "https://github.com/matheusneder/ai-trade-agent/blob/main/lab/freqtrade/user_data/strategies/trade_agent_strategy.py"
click node_walk_forward "https://github.com/matheusneder/ai-trade-agent/blob/main/lab/walk_forward.py"
click node_paper_replay "https://github.com/matheusneder/ai-trade-agent/blob/main/lab/paper.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_app,node_universe,node_candles,node_profiles,node_signals,node_decision,node_risk,node_risk_conditions,node_execution,node_order_builders,node_exchange_api,node_exchange_models toneBlue
class node_research_service,node_research_store,node_analyst,node_lead_tracker toneAmber
class node_store,node_database,node_commands,node_metrics,node_telemetry,node_tracing,node_claude toneMint
class node_lab_strategy,node_walk_forward,node_paper_replay toneRose
class node_operator,node_binance,node_telegram toneIndigo
```

## Components

### Trading lifecycle

- [Agent runtime](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/app.py): `src/trade_agent/app.py`
- [Market universe](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/market/universe.py): `src/trade_agent/market/universe.py`
- [Candle data](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/market/candles.py): `src/trade_agent/market/candles.py`
- [Strategy profiles](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/strategy/profiles.py): `src/trade_agent/strategy/profiles.py`
- [Technical signals](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/signals/local.py): `src/trade_agent/signals/local.py`
- [Decision cycle](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/decision/engine.py): `src/trade_agent/decision/engine.py`
- [Risk guard](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/risk/guard.py): `src/trade_agent/risk/guard.py`
- [Stop conditions](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/risk/conditions.py): `src/trade_agent/risk/conditions.py`
- [Position service](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/execution/service.py): `src/trade_agent/execution/service.py`
- [Order builders](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/execution/orders.py): `src/trade_agent/execution/orders.py`
- [Binance API](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/exchange/api.py): `src/trade_agent/exchange/api.py`
- [Exchange models](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/exchange/models.py): `src/trade_agent/exchange/models.py`

### Research and leads

- [Research service](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/research/service.py): `src/trade_agent/research/service.py`
- [Research store](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/persistence/research_store.py): `src/trade_agent/persistence/research_store.py`
- [Market analyst](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/research/analyst.py): `src/trade_agent/research/analyst.py`
- [Lead tracker](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/leads/tracker.py): `src/trade_agent/leads/tracker.py`

### Operations and persistence

- [Trading store](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/persistence/store.py): `src/trade_agent/persistence/store.py`
- [PostgreSQL](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/persistence/db.py): `src/trade_agent/persistence/db.py`
- [Telegram commands](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/notify/commands.py): `src/trade_agent/notify/commands.py`
- [Metrics](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/metrics.py): `src/trade_agent/metrics.py`
- [Telemetry recorder](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/telemetry/recorder.py): `src/trade_agent/telemetry/recorder.py`
- [Tracing](https://github.com/matheusneder/ai-trade-agent/blob/main/src/trade_agent/tracing.py): `src/trade_agent/tracing.py`

### Backtesting lab

- [Freqtrade strategy](https://github.com/matheusneder/ai-trade-agent/blob/main/lab/freqtrade/user_data/strategies/trade_agent_strategy.py): `lab/freqtrade/user_data/strategies/trade_agent_strategy.py`
- [Walk-forward runs](https://github.com/matheusneder/ai-trade-agent/blob/main/lab/walk_forward.py): `lab/walk_forward.py`
- [Tape replay](https://github.com/matheusneder/ai-trade-agent/blob/main/lab/paper.py): `lab/paper.py`

### Other components

- **Operator**
- **Binance Spot**
- **Claude API**
- **Telegram**

## Connections

- Operator → Agent runtime: starts agent (evidence: `README.md`)
- Agent runtime → Market universe: builds universe (evidence: `src/trade_agent/app.py`)
- Agent runtime → Decision cycle: runs cycles (evidence: `src/trade_agent/app.py`)
- Decision cycle → Strategy profiles: loads profiles (evidence: `src/trade_agent/decision/engine.py`)
- Decision cycle → Candle data: reads candles (evidence: `src/trade_agent/decision/engine.py`)
- Decision cycle → Technical signals: evaluates signals (evidence: `src/trade_agent/decision/engine.py`)
- Decision cycle → Risk guard: checks risk (evidence: `src/trade_agent/decision/engine.py`)
- Risk guard → Stop conditions: evaluates stops (evidence: `src/trade_agent/risk/guard.py`)
- Decision cycle → Position service: requests trades (evidence: `src/trade_agent/decision/engine.py`)
- Position service → Order builders: builds orders (evidence: `src/trade_agent/execution/service.py`)
- Position service → Binance API: sends orders (evidence: `src/trade_agent/execution/service.py`)
- Order builders → Exchange models: uses order types (evidence: `src/trade_agent/execution/orders.py`)
- Binance API → Binance Spot: calls endpoints (evidence: `README.md`)
- Market universe → Binance API: fetches market data (evidence: `src/trade_agent/market/universe.py`)
- Agent runtime → Research service: schedules research (evidence: `README.md`)
- Research service → Market analyst: runs analysis (evidence: `src/trade_agent/research/service.py`)
- Research service → Research store: persists reports (evidence: `src/trade_agent/research/service.py`)
- Market analyst → Claude API: analyzes news (evidence: `README.md`)
- Agent runtime → Lead tracker: reads trader leads (evidence: `README.md`)
- Agent runtime → Trading store: records outcomes (evidence: `src/trade_agent/app.py`)
- Research store → PostgreSQL: stores research (evidence: `src/trade_agent/persistence/research_store.py`)
- Trading store → PostgreSQL: stores trading state (evidence: `src/trade_agent/persistence/store.py`)
- Operator → Telegram commands: sends commands (evidence: `README.md`)
- Telegram commands → Telegram: receives commands (evidence: `README.md`)
- Agent runtime → Telemetry recorder: records snapshots (evidence: `README.md`)
- Telemetry recorder → PostgreSQL: stores snapshots (evidence: `src/trade_agent/telemetry/recorder.py`)
- Agent runtime → Metrics: reports metrics (evidence: `src/trade_agent/app.py`)
- Agent runtime → Tracing: instruments components (evidence: `src/trade_agent/app.py`)
- Walk-forward runs → Freqtrade strategy: runs backtests (evidence: `README.md`)
- Freqtrade strategy → Technical signals: reuses signals (evidence: `README.md`)
- Tape replay → Technical signals: replays decisions (evidence: `README.md`)
