An AI-powered portfolio planning system built with CrewAI that uses multiple specialized agents to analyze stocks and generate investment recommendations.
This project demonstrates how to build an intelligent portfolio planning system using CrewAI's multi-agent framework. The system consists of five specialized AI agents that work together to:
- Plan the portfolio strategy
- Analyze stock performance data
- Gather analyst opinions and ratings
- Validate the collected data
- Generate final portfolio recommendations
- PlannerAgent: Orchestrates the portfolio planning process and coordinates other agents
- StockPerformanceAgent: Analyzes historical stock performance and trends
- AnalystOpinionAgent: Gathers and summarizes expert analyst ratings
- ValidatorAgent: Validates data completeness and consistency
- PortfolioAdvisorAgent: Allocates budget and generates final recommendations
The system uses a custom MCP (Model Context Protocol) server that provides:
get_market_data(): Retrieves stock price and 1-year return dataget_analyst_insights(): Fetches analyst ratings and consensusvalidate_data(): Validates data structure and completenessgenerate_portfolio(): Creates portfolio allocation based on budget and preferences
- Python 3.10-3.13
- uv package manager (recommended)
-
Clone the repository
git clone <repository-url> cd portfolio_poc
-
Install dependencies
uv sync
Or using pip:
pip install -r requirements.txt
Run the portfolio planning system with default settings:
uv run portfolio_pocOr using the Python module:
python -m portfolio_poc.mainThe project provides several command-line interfaces:
uv run run_crewuv run train <iterations> <filename>uv run replay <task_id>uv run test <iterations> <eval_llm>-
Initialization: The PlannerAgent selects a set of commonly recommended stocks (AAPL, MSFT, JNJ, PFE)
-
Data Collection:
- StockPerformanceAgent retrieves 1-year returns and current prices
- AnalystOpinionAgent gathers buy/hold/sell ratings and consensus
-
Validation: ValidatorAgent ensures data completeness and consistency
-
Portfolio Generation: PortfolioAdvisorAgent allocates a $5000 budget across validated stocks, prioritizing those with positive analyst ratings
-
Output: Returns a detailed portfolio with share allocations, total cost, and leftover cash
portfolio_poc/
โโโ src/
โ โโโ portfolio_poc/
โ โ โโโ config/
โ โ โ โโโ agents.yaml # Agent configurations
โ โ โ โโโ tasks.yaml # Task definitions
โ โ โโโ crew.py # Main crew implementation
โ โ โโโ main.py # Entry points and CLI
โ โ โโโ tools/
โ โ โโโ custom_tool.py # Custom tools
โ โโโ servers/
โ โโโ portfolio_server.py # MCP server with portfolio tools
โโโ knowledge/
โ โโโ user_preference.txt # User preferences and context
โโโ tests/ # Test files
โโโ pyproject.toml # Project configuration
โโโ requirements.txt # Python dependencies
โโโ uv.lock # Lock file for dependencies
Each agent is configured with:
- Role: The agent's primary function
- Goal: What the agent aims to achieve
- Backstory: Context and expertise description
Tasks define the workflow with:
- Description: What the task does
- Expected Output: Expected result format
- Agent: Which agent performs the task
To analyze different stocks, modify the get_market_data() and get_analyst_insights() functions in src/servers/portfolio_server.py.
Update the budget parameter in the GeneratePortfolio task or modify the default value in the MCP server.
- Define the agent in
agents.yaml - Add the agent method in
crew.py - Create corresponding tasks in
tasks.yaml
Run tests using:
uv run test <iterations> <eval_llm>- crewai[tools]: Multi-agent framework
- mcp: Model Context Protocol for tool integration
- pydantic: Data validation
- fastmcp: Fast MCP server implementation
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Check the CrewAI documentation
- Review the configuration files
- Open an issue in the repository
- Real-time market data integration
- Risk assessment algorithms
- Portfolio rebalancing recommendations
- Historical performance tracking
- User preference learning
- Multi-asset class support (bonds, ETFs, etc.)