# vuongnq10/ai_agent_project architecture

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

- Interactive diagram: https://gitdiagram.com/vuongnq10/ai_agent_project
- Repository: https://github.com/vuongnq10/ai_agent_project

## Overview

1. This repo is split into two runtime boundaries: a Python backend in `bot-trading/` and a React + TypeScript frontend in `fe_chat/`. The backend owns market analysis, agent orchestration, and trade execution; the frontend owns charting, chat UI, and user controls.

2. The backend is a FastAPI service started from `bot-trading/main.py`. It wires routers, loads configuration from `config.py`, and exposes the API surface used by the browser. The main external dependencies are Binance Futures, Telegram, and LLM provider APIs.

3. The core backend domain service is `services/smc_service.py`. It fetches OHLCV data from Binance Futures REST and computes Smart Money Concepts plus classic indicators: trend, BOS/CHoCH, order blocks, fair value gaps, liquidity, premium/discount zones, ATR, EMAs, Bollinger Bands, and RSI. This service is the source of truth for all market structure analysis.

4. Agent execution is organized under `bot-trading/agents/` with provider-specific wrappers for Gemini, Claude, and ChatGPT. Each provider has an `agent.py` model wrapper and an `agentic_agent.py` LangGraph state machine, but they share the same high-level flow: classify intent, analyze market context, decide trade action, optionally invoke tools, then generate a final response.

5. Tooling for the agents lives in `tools/cx_connector.py`. It exposes two main callable actions: `smc_analysis()` to retrieve the full SMC payload, and `create_order()` to place a trade. This is the bridge between the reasoning layer and side effects.

6. Order execution is handled by `connectors/binance_v2.py`. It wraps the Binance futures connector and creates bracket trades: a LIMIT entry plus TAKE_PROFIT_MARKET and STOP_MARKET orders. It also manages leverage settings, which are exposed through the trading router.

7. Notifications are pushed through `connectors/telegram.py`. When an order is placed, the system can asynchronously notify a Telegram chat, so trading events are observable outside the web UI.

8. The backend API is split into two routers: `routers/trading.py` for normal JSON endpoints and `routers/stream.py` for the streaming chat endpoint. The streaming endpoint returns Server-Sent Events so the agent’s response can be delivered character-by-character to the browser.

9. The frontend is a single-page application built around a chart-and-chat workspace. `src/App/index.tsx` composes the layout: a candlestick chart area, indicator/SMC overlays, a coin list, leverage controls, and a chat sidebar. The chart stack uses `lightweight-charts` with custom SMC drawings and indicator panels.

10. Frontend state is split into hooks and services. `useCandles`, `useTicker`, and `useLeverage` manage market data and trading controls; `useChat` manages SSE chat streaming. Service modules isolate backend calls: `chatService.ts` for model streaming, `tradingService.ts` for SMC and leverage APIs, and `binanceService.ts` for market data access.

11. A key data-flow boundary is `smcQueryService.ts`. Before a chat message is sent, it requests SMC analysis for multiple timeframes (1h, 2h, 4h), formats the results into structured markdown, and prepends that context to the user prompt. This means the LLM receives precomputed market structure rather than raw candles.

12. The end-to-end interaction path is: user input in the browser → multi-timeframe SMC context assembly → SSE request to the backend agent endpoint → LangGraph analysis/decision/tool execution → optional Binance order placement and Telegram alert → streamed textual result back to the chat UI. That is the primary architecture to draw.

13. There is also a small test boundary under `bot-trading/tests/`, focused on order creation behavior. The repo’s `.claude` and `.codex` folders are agent/operator metadata, not product runtime components, so they do not affect the deployed system architecture.

Main entry points: [`bot-trading/routers/trading.py`](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/routers/trading.py) (Trading router), [`bot-trading/connectors/binance_v2.py`](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/connectors/binance_v2.py) (Binance executor), [`bot-trading/main.py`](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/main.py) (FastAPI app), [`fe_chat/src/App/index.tsx`](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/index.tsx) (App shell)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Python backend"]
  node_backend_main["FastAPI app<br/>entrypoint<br/>[main.py]"]
  node_backend_config["Config<br/>settings<br/>[config.py]"]
  node_backend_trading_router["Trading router<br/>http api<br/>[trading.py]"]
  node_backend_stream_router["Stream router<br/>sse api<br/>[stream.py]"]
  node_backend_smc_service[("SMC service<br/>market analysis<br/>[smc_service.py]")]
  node_backend_market_data["Market data<br/>binance fetch"]
  node_backend_agents["Agent stack<br/>llm orchestration"]
  node_backend_tools["Tool bridge<br/>agent tools<br/>[cx_connector.py]"]
  node_backend_binance_exec["Binance executor<br/>trade execution<br/>[binance_v2.py]"]
  node_backend_telegram["Telegram alerts<br/>notification<br/>[telegram.py]"]
  node_backend_tests["Order tests<br/>test boundary"]
end

subgraph group_frontend["React frontend"]
  node_frontend_app["App shell<br/>spa entrypoint<br/>[index.tsx]"]
  node_frontend_chart["Chart workspace<br/>ui composition<br/>[index.tsx]"]
  node_frontend_chat["Chat panel<br/>ui composition<br/>[Input.tsx]"]
  node_frontend_controls["Trading controls<br/>ui controls<br/>[index.tsx]"]
  node_frontend_candles["Candle hook<br/>state hook<br/>[useCandles.ts]"]
  node_frontend_chat_hook["Chat hook<br/>state hook<br/>[useChat.ts]"]
  node_frontend_smc_query["SMC query<br/>context assembly<br/>[smcQueryService.ts]"]
  node_frontend_services["API services<br/>backend clients<br/>[tradingService.ts]"]
end

node_backend_main -->|"loads"| node_backend_config
node_backend_main -->|"mounts"| node_backend_trading_router
node_backend_main -->|"mounts"| node_backend_stream_router
node_backend_trading_router -->|"uses"| node_backend_smc_service
node_backend_trading_router -->|"orders"| node_backend_binance_exec
node_backend_stream_router -->|"invokes"| node_backend_agents
node_backend_agents -->|"calls"| node_backend_tools
node_backend_tools -->|"analyzes"| node_backend_smc_service
node_backend_tools -->|"executes"| node_backend_binance_exec
node_backend_binance_exec -->|"notifies"| node_backend_telegram
node_backend_smc_service -->|"fetches"| node_backend_market_data
node_backend_tests -.->|"verifies"| node_backend_binance_exec
node_frontend_app -->|"composes"| node_frontend_chart
node_frontend_app -->|"composes"| node_frontend_chat
node_frontend_app -->|"composes"| node_frontend_controls
node_frontend_chart -->|"feeds"| node_frontend_candles
node_frontend_chat -->|"binds"| node_frontend_chat_hook
node_frontend_chat_hook -->|"prepends"| node_frontend_smc_query
node_frontend_smc_query -->|"requests"| node_frontend_services
node_frontend_services -->|"api"| node_backend_trading_router
node_frontend_services -->|"api"| node_backend_stream_router

click node_backend_main "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/main.py"
click node_backend_config "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/config.py"
click node_backend_trading_router "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/routers/trading.py"
click node_backend_stream_router "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/routers/stream.py"
click node_backend_smc_service "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/services/smc_service.py"
click node_backend_market_data "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/services/binance_market_data_service.py"
click node_backend_agents "https://github.com/vuongnq10/ai_agent_project/tree/master/bot-trading/agents"
click node_backend_tools "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/tools/cx_connector.py"
click node_backend_binance_exec "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/connectors/binance_v2.py"
click node_backend_telegram "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/connectors/telegram.py"
click node_backend_tests "https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/tests/test_create_orders.py"
click node_frontend_app "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/index.tsx"
click node_frontend_chart "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/components/Chart/index.tsx"
click node_frontend_chat "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/components/Chat/Input.tsx"
click node_frontend_controls "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/components/LeveragePanel/index.tsx"
click node_frontend_candles "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/hooks/useCandles.ts"
click node_frontend_chat_hook "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/hooks/useChat.ts"
click node_frontend_smc_query "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/services/smcQueryService.ts"
click node_frontend_services "https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/services/tradingService.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_backend_main,node_backend_config,node_backend_trading_router,node_backend_stream_router,node_backend_smc_service,node_backend_market_data,node_backend_agents,node_backend_tools,node_backend_binance_exec,node_backend_telegram,node_backend_tests toneBlue
class node_frontend_app,node_frontend_chart,node_frontend_chat,node_frontend_controls,node_frontend_candles,node_frontend_chat_hook,node_frontend_smc_query,node_frontend_services toneAmber
```

## Components

### Python backend

FastAPI service for market analysis, agent orchestration, and trade execution.

- [FastAPI app](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/main.py): entrypoint · Bootstraps the API, loads config, and wires routers. · `bot-trading/main.py`
- [Config](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/config.py): settings · Central backend configuration and environment wiring. · `bot-trading/config.py`
- [Trading router](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/routers/trading.py): http api · JSON endpoints for market data, leverage, and order actions. · `bot-trading/routers/trading.py`
- [Stream router](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/routers/stream.py): sse api · Server-sent event endpoint for streamed agent responses. · `bot-trading/routers/stream.py`
- [SMC service](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/services/smc_service.py): market analysis · Computes Smart Money Concepts and classic indicators from OHLCV data. · `bot-trading/services/smc_service.py`
- [Market data](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/services/binance_market_data_service.py): binance fetch · Fetches candle and ticker data from Binance Futures. · `bot-trading/services/binance_market_data_service.py`
- [Agent stack](https://github.com/vuongnq10/ai_agent_project/tree/master/bot-trading/agents): llm orchestration · Provider-specific wrappers and shared LangGraph decision flow. · `bot-trading/agents`
- [Tool bridge](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/tools/cx_connector.py): agent tools · Exposes SMC analysis and order creation to agent workflows. · `bot-trading/tools/cx_connector.py`
- [Binance executor](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/connectors/binance_v2.py): trade execution · Places bracket orders and manages leverage on Binance Futures. · `bot-trading/connectors/binance_v2.py`
- [Telegram alerts](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/connectors/telegram.py): notification · Sends trade notifications to Telegram chats. · `bot-trading/connectors/telegram.py`
- [Order tests](https://github.com/vuongnq10/ai_agent_project/blob/master/bot-trading/tests/test_create_orders.py): test boundary · Focused tests for order creation behavior. · `bot-trading/tests/test_create_orders.py`

### React frontend

Single-page chart and chat workspace for user interaction.

- [App shell](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/index.tsx): spa entrypoint · Composes the chart, controls, and chat workspace. · `fe_chat/src/App/index.tsx`
- [Chart workspace](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/components/Chart/index.tsx): ui composition · Candlestick chart, overlays, indicators, and market panels. · `fe_chat/src/App/components/Chart/index.tsx`
- [Chat panel](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/components/Chat/Input.tsx): ui composition · Chat input and streamed message display. · `fe_chat/src/App/components/Chat/Input.tsx`
- [Trading controls](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/App/components/LeveragePanel/index.tsx): ui controls · Coin list, leverage panel, and header controls. · `fe_chat/src/App/components/LeveragePanel/index.tsx`
- [Candle hook](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/hooks/useCandles.ts): state hook · Manages candle loading and chart updates. · `fe_chat/src/hooks/useCandles.ts`
- [Chat hook](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/hooks/useChat.ts): state hook · Handles SSE chat streaming and message state. · `fe_chat/src/hooks/useChat.ts`
- [SMC query](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/services/smcQueryService.ts): context assembly · Fetches multi-timeframe SMC data and formats prompt context. · `fe_chat/src/services/smcQueryService.ts`
- [API services](https://github.com/vuongnq10/ai_agent_project/blob/master/fe_chat/src/services/tradingService.ts): backend clients · Frontend clients for market data, chat streaming, and trading actions. · `fe_chat/src/services/tradingService.ts`

## Connections

- FastAPI app → Config: loads · Initializes the service from configuration.
- FastAPI app → Trading router: mounts · Registers standard JSON endpoints.
- FastAPI app → Stream router: mounts · Registers the SSE chat endpoint.
- Trading router → SMC service: uses · Serves computed market structure to clients.
- Trading router → Binance executor: orders · Creates and configures futures trades.
- Stream router → Agent stack: invokes · Streams agent reasoning and responses.
- Agent stack → Tool bridge: calls · Uses tools during analysis and decision making.
- Tool bridge → SMC service: analyzes · Retrieves full SMC payload for reasoning.
- Tool bridge → Binance executor: executes · Places trades when the agent decides to act.
- Binance executor → Telegram alerts: notifies · Sends alerts after order placement.
- SMC service → Market data: fetches · Pulls OHLCV data for structure analysis.
- Order tests → Binance executor: verifies · Covers order creation behavior.
- App shell → Chart workspace: composes · Renders the chart and overlays.
- App shell → Chat panel: composes · Renders the conversational panel.
- App shell → Trading controls: composes · Renders market and leverage controls.
- Chart workspace → Candle hook: feeds · Consumes candle state for rendering.
- Chat panel → Chat hook: binds · Streams chat messages and response state.
- Chat hook → SMC query: prepends · Builds prompt context before sending a message.
- SMC query → API services: requests · Calls backend APIs for SMC and chat flow.
- API services → Trading router: api · Uses trading and market endpoints.
- API services → Stream router: api · Uses streaming endpoint for chat responses.
