Skip to content

Ultraing/stock-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– StockAgent โ€” LLM-Powered Multi-Agent Stock Analysis System

Python 3.11+ License: MIT

An intelligent stock analysis system that combines multi-source data fusion, multi-dimensional scoring, and LLM-powered reasoning to provide actionable market insights. Designed as a multi-agent architecture where specialized agents handle data collection, analysis, screening, and reporting.

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     User Interface Layer                        โ”‚
โ”‚            CLI โ€ข WeChat Bot โ€ข Web Dashboard โ€ข API               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                           โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     Agent Orchestrator                          โ”‚
โ”‚         Task Scheduling โ€ข Memory โ€ข Context Management           โ”‚
โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ”‚          โ”‚          โ”‚          โ”‚          โ”‚
โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”
โ”‚ Data  โ”‚ โ”‚Analysisโ”‚ โ”‚Screen โ”‚ โ”‚Report โ”‚ โ”‚Monitorโ”‚
โ”‚ Agent โ”‚ โ”‚ Agent  โ”‚ โ”‚ Agent โ”‚ โ”‚ Agent โ”‚ โ”‚ Agent โ”‚
โ”‚       โ”‚ โ”‚        โ”‚ โ”‚       โ”‚ โ”‚       โ”‚ โ”‚       โ”‚
โ”‚Multi- โ”‚ โ”‚Technic.โ”‚ โ”‚Full   โ”‚ โ”‚Daily  โ”‚ โ”‚Real-  โ”‚
โ”‚Source โ”‚ โ”‚Capital โ”‚ โ”‚Market โ”‚ โ”‚Brief  โ”‚ โ”‚time   โ”‚
โ”‚Fusion โ”‚ โ”‚Sentim. โ”‚ โ”‚Scan   โ”‚ โ”‚Gener. โ”‚ โ”‚Alert  โ”‚
โ”‚       โ”‚ โ”‚Fundam. โ”‚ โ”‚17+    โ”‚ โ”‚LLM    โ”‚ โ”‚       โ”‚
โ”‚5+ API โ”‚ โ”‚Market  โ”‚ โ”‚Factor โ”‚ โ”‚Poweredโ”‚ โ”‚Cron   โ”‚
โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜
    โ”‚         โ”‚         โ”‚         โ”‚         โ”‚
โ”Œโ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Data Source Layer                            โ”‚
โ”‚  Tencent โ€ข Sina โ€ข EastMoney โ€ข THS โ€ข AkShare โ€ข Tushare โ€ข YFinanceโ”‚
โ”‚  Fallback Chain โ€ข Cache โ€ข Rate Limiting                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โœจ Key Features

๐Ÿ”„ Multi-Source Data Fusion with Fallback Chain

Real-time quotes from 5+ providers (Tencent, Sina, EastMoney, THS, AkShare) with automatic failover. If one source is down, the system seamlessly switches to the next.

# Data sources with automatic fallback
sources = [TencentRealtimeSource(), SinaRealtimeSource(), EastMoneyRealtimeSource()]
chain = FallbackChain(sources)
quote = await chain.get_quote("002475")  # Tries each source until one succeeds

๐Ÿ“Š Multi-Dimensional Scoring Engine (17+ Factors)

Every stock gets scored across 5 dimensions:

Dimension Factors Weight
Technical MA trends, RSI, consecutive candles, price position 25%
Capital Flow Main force net flow, outer/inner ratio, volume ratio 25%
Sentiment News polarity, market heat, social media signals 20%
Fundamental PE, PB, revenue growth, ROE 20%
Market Sector strength, index correlation 10%

๐Ÿ” Full Market Screening

Scan 5000+ A-share stocks in seconds with configurable filters:

  • Bowlong anti-chase rules (avoid overbought stocks)
  • Fundamental risk gating (ST, delisting, insider selling)
  • Event-driven scoring (news catalysts)
  • Holdings overlap detection

๐Ÿ“ฐ LLM-Powered Analysis

Feed market data to LLM for natural language analysis:

  • Daily morning/evening reports
  • Stock-specific deep dives
  • Portfolio risk assessment
  • Market sentiment summary

โฐ Automated Monitoring

  • Scheduled market scans (pre-market, mid-day, post-market)
  • Real-time price alerts via WeChat/Telegram
  • Portfolio tracking with stop-loss monitoring

๐Ÿš€ Quick Start

# Install
pip install -e .

# Single stock analysis
stock-agent analyze 002475

# Full market scan
stock-agent scan --top 20

# Market overview
stock-agent market

# Generate daily report
stock-agent report morning

๐Ÿ“ Project Structure

stock-agent/
โ”œโ”€โ”€ src/stock_agent/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ __main__.py          # CLI entry point
โ”‚   โ”œโ”€โ”€ tools/               # Agent tools (callable by LLM)
โ”‚   โ”‚   โ”œโ”€โ”€ stock_analysis.py
โ”‚   โ”‚   โ”œโ”€โ”€ market_scan.py
โ”‚   โ”‚   โ”œโ”€โ”€ daily_report.py
โ”‚   โ”‚   โ””โ”€โ”€ price_alert.py
โ”‚   โ”œโ”€โ”€ data/                # Multi-source data layer
โ”‚   โ”‚   โ”œโ”€โ”€ sources/         # Data source adapters
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ base.py      # Abstract base + FallbackChain
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ tencent.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ sina.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ eastmoney.py
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ths.py
โ”‚   โ”‚   โ”œโ”€โ”€ manager.py       # Unified data manager
โ”‚   โ”‚   โ””โ”€โ”€ cache.py         # TTL cache layer
โ”‚   โ”œโ”€โ”€ analysis/            # Scoring & analysis engine
โ”‚   โ”‚   โ”œโ”€โ”€ scoring.py       # Multi-dimensional scoring
โ”‚   โ”‚   โ”œโ”€โ”€ technical.py     # Technical indicators
โ”‚   โ”‚   โ”œโ”€โ”€ capital_flow.py  # Capital flow analysis
โ”‚   โ”‚   โ”œโ”€โ”€ sentiment.py     # News sentiment (LLM-powered)
โ”‚   โ”‚   โ””โ”€โ”€ fundamental.py   # PE/PB/ROE analysis
โ”‚   โ”œโ”€โ”€ agent/               # LLM agent integration
โ”‚   โ”‚   โ”œโ”€โ”€ tools.py         # OpenAI function-calling schemas
โ”‚   โ”‚   โ”œโ”€โ”€ prompts.py       # System prompts
โ”‚   โ”‚   โ””โ”€โ”€ orchestrator.py  # Multi-agent coordinator
โ”‚   โ””โ”€โ”€ reporting/           # Report generation
โ”‚       โ”œโ”€โ”€ morning.py       # Pre-market brief
โ”‚       โ”œโ”€โ”€ evening.py       # Post-market summary
โ”‚       โ””โ”€โ”€ templates.py     # Report templates
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_scoring.py
โ”‚   โ”œโ”€โ”€ test_data_sources.py
โ”‚   โ””โ”€โ”€ test_technical.py
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ LICENSE

๐Ÿ”ง Agent Tools (LLM Function Calling)

Each tool is designed to be called by an LLM agent via function calling:

# tools/stock_analysis.py
TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "stock_analysis",
            "description": "Analyze a single stock with multi-dimensional scoring",
            "parameters": {
                "type": "object",
                "properties": {
                    "code": {"type": "string", "description": "Stock code, e.g. '002475'"},
                    "include_sentiment": {"type": "boolean", "default": True},
                },
                "required": ["code"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "market_scan",
            "description": "Scan the full A-share market and return top candidates",
            "parameters": {
                "type": "object",
                "properties": {
                    "top_n": {"type": "integer", "default": 20},
                    "min_pe": {"type": "number"},
                    "max_pe": {"type": "number"},
                    "exclude_sectors": {"type": "array", "items": {"type": "string"}},
                }
            }
        }
    }
]

๐Ÿ“ˆ Sample Output

{
  "code": "002475",
  "name": "็ซ‹่ฎฏ็ฒพๅฏ†",
  "price": 71.29,
  "change_pct": 2.55,
  "total_score": 55.0,
  "signal": "WATCH",
  "technical": {
    "score": 55,
    "signals": ["5่ฟž้˜ณ+5"],
    "consecutive_up_candles": 5
  },
  "capital": {
    "score": 60,
    "signals": ["ไธปๅŠ›ๅๅผบ +28.3ไธ‡", "ๅค–็›˜ๅๅผบ+8"]
  },
  "fundamental": {
    "score": 58,
    "pe": 30.17,
    "pb": 5.88
  }
}

๐Ÿ›ก๏ธ Risk Management Rules

Built-in anti-chase and risk control rules:

  • โŒ No buying stocks with PE > 50 (unless strong catalyst)
  • โŒ No chasing stocks up > 5% in a single day
  • โŒ No buying stocks that overlap with existing holdings
  • โŒ Must have clear stop-loss level before entry
  • โœ… Prefer stocks with PE < 30 and positive capital flow
  • โœ… Require multi-day confirmation before entry

๐Ÿ™ Acknowledgments

  • OpenClaw โ€” Agent orchestration framework
  • AkShare โ€” Financial data API
  • Tushare โ€” A-share data provider

License

MIT

About

LLM-Powered Multi-Agent Stock Analysis System for A-Share Market

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages