TradingAgents-CN: Architecture and Design Analysis
TradingAgents-CN is a Chinese-enhanced version of TauricResearch/TradingAgents — a multi-agent, LLM-driven stock analysis system. It is positioned as an educational and research platform, supporting A-shares, Hong Kong, and US markets, and explicitly does not provide live trading signals.
Key points
- Architecture evolution: v0.1.x was a Streamlit monolith; v1.0.0-preview migrates to FastAPI + Vue 3 (Element Plus, Vite) with RESTful API, WebSocket, and SSE, backed by MongoDB and Redis, deployed via multi-architecture Docker (amd64/ARM64) with GitHub Actions CI/CD.
- Multi-agent design: A four-tier agent hierarchy orchestrated by LangGraph —
- *Analysis layer*: Market, Fundamentals, News, and Social Media Analysts
- *Research layer*: Bull and Bear Researchers engaged in a debate mechanism, resolved by a Research Manager
- *Decision layer*: a Trader agent
- *Risk layer*: Risky/Safe/Neutral Analysts feeding a Risk Judge
- State is managed via
AgentState,InvestDebateState, andRiskDebateState; vector memory is based on ChromaDB. - Data layer: A strategy-pattern unified interface (
dataflows/interface.py) auto-selects sources (Tushare → AKShare → BaoStock, plus yfinance for US stocks) via a data source registry (constants/data_sources.py). A three-tier cache (Redis → MongoDB → CSV/JSON file) uses anAdaptiveCachewith automatic fallback, TTL management, and cache warming. - LLM integration: An adapter pattern (
create_llm_by_providerintrading_graph.py) supports 10+ providers including OpenAI, Google AI, Alibaba DashScope, DeepSeek, Zhipu AI, Anthropic, SiliconFlow, OpenRouter, and Ollama. A notable hybrid mode lets quick-think and deep-think tasks use different providers (e.g.,qwen-turbofor fast tasks,gpt-4for deep reasoning) for cost and resilience. - Configuration: Legacy JSON file config is deprecated in favor of MongoDB-backed config with live updates, versioning, and multi-tenant support; secrets follow 12-Factor via environment variables.
- Design patterns applied: Adapter (LLM providers), Strategy (data sources, caching), Factory (agent creation), Observer (progress callbacks via
_send_progress_update), and Decorator (logging/monitoring). - Performance: Async LangGraph streaming (
astream), connection pooling, cache warmup for hot stocks, LRU/TTL eviction, LLM batching, exponential-backoff retries, and per-node timing reports (sample analysis run: 12 nodes, ~35s total). - Security & compliance: JWT auth with RBAC, rate limiting, AES-256 encrypted key storage with runtime rotation, data isolation at collection/key level, log redaction, and explicit disclaimers that results are for research only.
- Outstanding: completing migration from the deprecated JSON config system, reducing duplicated data-source code, and expanding test coverage.
- Future directions: reinforcement learning for agent decisions, a financial knowledge graph, real-time streaming market data, federated learning, and integrated backtesting.
- Engine:
tradingagents/graph/trading_graph.py,graph/setup.py,graph/conditional_logic.py - Agents:
tradingagents/agents/__init__.py,agents/analysts/,agents/researchers/,agents/risk_mgmt/ - Data:
tradingagents/dataflows/interface.py,dataflows/data_source_manager.py,dataflows/cache/ - LLM adapters:
tradingagents/llm_adapters/(base:openai_compatible_base.py) - Config:
tradingagents/default_config.py(legacyconfig/config_manager.pydeprecated)