LLM agent for market research and trading.
This project is an AI-powered market research assistant that leverages a multi-agent system to provide comprehensive analysis of financial markets. It can analyze market trends, assess portfolio performance, gather key statistics, and consolidate news to generate a cohesive summary for the user. The system is built using LangGraph for orchestration and a suite of specialized agents for data collection and analysis.
The application is designed with a multi-layered architecture to separate concerns and enhance modularity.
graph TD
%% Define Layers and Components
subgraph "UI Layer"
UI[User Interface]
end
subgraph "Orchestration Layer"
Orchestrator(LangGraph Orchestrator)
end
subgraph "Agent Layer"
direction LR
TrendingAgent
PortfolioAgent
KeyStatsAgent
NewsAgent
SummaryAgent
end
subgraph "Data Layer"
DataSources[(External APIs & Data Files)]
end
%% Define Workflow Connections
UI -- "User Query" --> Orchestrator
Orchestrator -- "Dispatch Initial Tasks" --> TrendingAgent & PortfolioAgent
TrendingAgent & PortfolioAgent -- "Fetch" --> DataSources
Orchestrator -- "Fan-Out" --> KeyStatsAgent & NewsAgent
KeyStatsAgent & NewsAgent -- "Fetch" --> DataSources
Orchestrator -- "Aggregate" --> SummaryAgent
SummaryAgent -- "Final Report" --> Orchestrator
Orchestrator -- "Stream Final Report" --> UI
The core of this application is a team of specialized AI agents that work together to fulfill the user's request. The LangGraph orchestrator manages the workflow, ensuring that agents are called in the correct order and that data flows smoothly between them.
Here are the agents involved:
-
TrendingAgent (
fetch_trending_stocks_node)- Role: Identifies stocks that are relevant to the user's query.
- Action: Kicks off the workflow by finding trending stocks based on the initial prompt.
-
PortfolioAgent (
fetch_portfolio_holdings_node)- Role: Gathers information about the user's existing investments.
- Action: Runs in parallel with the
TrendingAgentto load the user's portfolio fromportfolio.json.
-
KeyStatsAgent (
fetch_key_stats_node)- Role: Collects key financial metrics for the identified stocks.
- Action: Fetches statistics (e.g., P/E ratio, market cap) for both trending and portfolio stocks in parallel to speed up the process.
-
NewsAgent (
fetch_news_node)- Role: Gathers the latest news for each stock.
- Action: Runs in parallel with the
KeyStatsAgent, fetching and summarizing recent news articles for all relevant stocks.
-
SummaryAgent (
generate_summary_node)- Role: The final assembler of the report.
- Action: Once all the other agents have completed their tasks, this agent takes all the collected data—trending stocks, portfolio analysis, key stats, and news—and generates a single, cohesive report.
The orchestrator.py script uses LangGraph to define and execute the workflow. The process is as follows:
-
Initiation: The graph starts with two parallel tasks:
fetch_trending_stocks: Identifies trending stocks based on the user's query.fetch_portfolio_holdings: Loads the user's current portfolio.
-
Fan-Out Data Collection: Once the initial stock lists are ready, the orchestrator fans out to collect detailed information in parallel:
fetch_key_stats: Gathers financial statistics for all identified stocks.fetch_news: Fetches and summarizes the latest news for the same stocks.
-
Aggregation and Summary: After all data collection is complete, the
generate_summarynode is triggered. This final node aggregates all the information—trending stocks, portfolio data, key stats, and news—to produce a comprehensive report.
This parallelized, multi-step approach ensures that the analysis is both thorough and efficient.
- Python 3.10+
- Pip for package management
-
Clone the repository:
git clone <your-repo-url> cd mcp-trading
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate -
Install the dependencies:
pip install -r requirements.txt
The primary way to run the research workflow is through the web interface, which provides a real-time, interactive experience.
This project also includes a web interface to provide a more interactive experience. The web server is built with FastAPI and uses Server-Sent Events (SSE) to stream the analysis results in real-time.
-
Start the server:
python server.py
-
Open your browser:
Navigate to http://localhost:8000. You will see a simple interface where you can input your research query.
server.py: This script launches a FastAPI web server that serves the mainindex.htmlpage.templates/index.html: This file contains the HTML and JavaScript for the user interface. It sends the user's query to the/stream-analysisendpoint.- Server-Sent Events (SSE): The
/stream-analysisendpoint maintains an open connection to the browser and streams updates as they are generated by the agentic workflow. This allows you to see the analysis unfold in real-time. mcp_server.py: This script runs a separate server that exposes the individual agent functionalities as tools via the Model Context Protocol (MCP). While not directly used by the web interface, it's a core component for more advanced integrations and direct tool-based interactions.
Here are a few examples of prompts you can use to get started. You can copy and paste these directly into the web interface or use them with the command-line script.
I am looking to invest 100k for 6 months. I have a very high risk tolerance. I would like to see mix of growth and dividend stocks from different sectors and industries.
I have a high risk tolerance and would like to invest 50K for the duration of 6 months. I am looking for mix of growth and dividend stocks.
I would like to invest 100K for 2 years horizon in a low risk high dividend stocks. I would like to diversify investment across different industries.
Analyze the current state of the renewable energy sector. I have a moderate risk tolerance and a 5-year investment horizon.
What are the top-performing tech stocks over the last quarter? I'm interested in large-cap companies.
I want to invest in healthcare stocks, but I want to avoid pharmaceutical companies. What are my options?
Provide a comparative analysis between NVDA and AMD.