CrewAI Stock Analysis System
repository·main·Indexed 41 days ago
https://github.com/liangdabiao/crewai_stock_analysis_systemAn intelligent stock analysis platform built on the CrewAI framework using a dual architecture of Crews for collaborative tasks and Flows for process orchestration. The system employs 9 specialized agents across DataCollectionCrew, AnalysisCrew, and DecisionCrew to provide fundamental, technical, and industry-level investment insights. It features a SmartInvestmentFlow for conditional routing based on data quality, a BatchStockAnalyzer for concurrent processing, and integrates AkShare for financial data retrieval.
What's inside CrewAI Stock Analysis System
- The CrewAI Stock Analysis System is an intelligent stock analysis platform built on the CrewAI framework. It utilizes a multi-agent collaborative architecture combining Crews (team mode) for collaborative tasks and Flows (flow mode) for process control. The system provides comprehensive investment services including fundamental, technical, and industry analysis, batch processing, real-time monitoring, and automated report generation.
Overview of the CrewAI Stock Analysis System Architecture
mainThe system is built on the CrewAI framework using a hybrid architecture that combines two primary patterns:
- Crews Mode: Utilizes specialized Agent teams to collaborate on specific tasks.
- Flows Mode: Provides intelligent process control and state management to orchestrate the analysis.
This hybrid approach allows the system to dynamically adjust the depth of analysis based on current market conditions.
Agent Team Structure and Roles
mainThe system organizes its 9 specialized agents into three functional crews:
1. DataCollectionCrew (Data Collection Team)
- Market Researcher: Collects market news, industry information, and company updates.
- Financial Data Expert: Retrieves financial statements and key financial metrics.
- Technical Analyst: Collects price data and technical indicators.
- Data Validation Expert: Verifies data quality and handles outliers.
2. AnalysisCrew (Analysis Team)
- Fundamental Analyst: Evaluates company fundamentals and financial health.
- Risk Assessor: Analyzes investment risks and identifies risk factors.
- Industry Expert: Analyzes industry position and the competitive environment.
3. DecisionCrew (Decision Team)
- Investment Advisor: Generates investment advice and formulates strategies.
- Report Generator: Produces detailed analysis reports.
- Quality Controller: Performs quality control and result verification.
Understand the dual architecture: Crews and Flows
mainThe system is built using two primary CrewAI architectural patterns:
- Crews (Team Mode): Used for collaborative task execution where multiple specialized agents work together to achieve a specific goal (e.g., a data collection crew).
- Flows (Flow Mode): Used for high-level process control and orchestration, managing the sequence and logic between different crews (e.g., moving from data collection to analysis to decision making).
Manage complex workflows with InvestmentFlow and @router
mainFor high-level orchestration of multiple crews, use the
InvestmentFlowpattern. This allows for intelligent routing and state management across the entire investment lifecycle.Key features include:
- Intelligent Routing: Use the
@routerdecorator to implement conditional branching (e.g., choosing different analysis strategies based on data quality). - State Management: Use
Pydanticmodels to maintain and pass state between different stages of the flow. - Error Handling: The system supports intelligent retry mechanisms and alternative paths (fallback routes) when a specific step fails.
@router async def route_analysis(self) -> str: if self.state.data_quality > 0.8: return "deep_analysis" else: return "quick_scan"- Intelligent Routing: Use the
Use collective decision-making mechanisms
mainThe system implements several patterns for multi-agent consensus and decision-making, particularly within the
DecisionCrew. When designing decision-making tasks, you can implement one of the following mechanisms:- Unanimous Decision: All agents must agree.
- Majority Rule: Decisions based on the majority vote.
- Weighted Voting: Votes are weighted based on agent expertise or role.
- Hierarchical Decision: Decisions are escalated through a chain of command (e.g., from analysts to a Portfolio Manager).
- Consensus Decision: Agents iterate until a common ground is reached.
Configure task context for multi-agent collaboration
mainWhen setting up collaborative crews like
AnalysisCreworDecisionCrew, you must establish relationships between tasks so agents can share context.Best Practice: Do not attempt to set task context dynamically during execution (e.g., inside an
_execute_...method). Instead, define the task context relationships at the time theCrewinstance is being created. This ensures that the dependencies and information flow between agents are correctly mapped before the workflow begins.System Architecture and Design Patterns
mainThe system is built on the following architectural principles:
- CrewAI Framework: Utilizes multi-agent collaboration.
- Crews Mode: Implements autonomous team collaboration patterns.
- Flows Mode: Uses intelligent process control for complex workflows.
- State Management: Employs
Pydantic(BaseModel) for robust state management across agents and flows. - Modular Design: Separates configuration (
agents.yaml,tasks.yaml,tools.yaml), logic (src/crews,src/flows), and tools (src/tools).
Use InvestmentFlow for intelligent routing and state management
mainThe
InvestmentFlowcomponent utilizes the CrewAI Flows pattern to manage complex conditional logic and state.Key Features:
- Intelligent Routing: Use the
@routerdecorator to implement conditional branching based on data quality or company characteristics. - State Management: Use
Pydanticmodels to maintain and track the state of the investment process across different stages. - Error Handling: The flow supports intelligent retry mechanisms and alternative execution paths if a specific branch fails.
@router def route_analysis_strategy(self) -> str: # Logic to decide the next path based on state if self.state.data_quality > 0.8: return "deep_analysis" return "basic_analysis"- Intelligent Routing: Use the
Important Investment Disclaimer
main⚠️ Disclaimer
Important Reminder: The analysis results and suggestions provided by this system are for reference only and do not constitute investment advice. Investing involves risk; decisions should be made with caution.
- The system is based on public data and AI model analysis and may contain errors.
- Stock markets are uncertain; historical performance does not guarantee future results.
- Users should make investment decisions based on their own risk tolerance.
- It is recommended to combine information from multiple sources and professional investment advisors.
- The developers are not responsible for any investment losses caused by the use of this system.
By using this system, you acknowledge and agree to this disclaimer.
Understand the Agent Team Structure
mainThe system uses a multi-agent architecture organized into specialized teams to handle different stages of the investment analysis lifecycle:
- Data Collection Team (4 Agents): Includes Market Researcher, Financial Data Expert, Technical Analyst, and Data Validation Expert. Responsible for gathering and cleaning data using tools like AkShare.
- Analysis Team (3 Agents): Includes Fundamental Analyst, Risk Assessment Expert, and Industry Expert. Responsible for deep-dive analysis.
- Decision Team (3 Agents): Includes Investment Strategy Consultant, Report Generator, and Quality Control Expert. Responsible for final output and strategy.
- Coordination Agent (1 Agent): Data Collection Coordinator, manages the flow of information between teams.
Implement collective decision-making mechanisms
mainThe system supports simulating an investment committee through a collective decision system. This allows multiple specialized agents (e.g., Risk Management Expert, Portfolio Manager, Market Strategist) to participate in a single decision process.
Supported Decision Types:
- Unanimous Consent: All participants must agree.
- Majority Rule: Decisions based on the majority of votes.
- Weighted Voting: Decisions based on votes weighted by the specific authority or importance of the agent/role.