Built-in Risk Management: Applying Intelligent Safeguards
This section explains a fundamental and automatic feature of the AI Hedge Fund platform: its intelligent risk management system. This system is designed to ensure that all simulated investment strategies operate safely within predefined risk boundaries before any final trading decisions are made.
Always Active: Automated Risk Assessment
A crucial aspect of this platform is that a dedicated Risk Manager agent is always active within every investment strategy you build. Even if you, as the user, do not explicitly add a risk management step in your visual strategy design, the system automatically makes sure the Risk Manager is activated. It runs its analysis right before the “Portfolio Manager” agent makes any final decision. This guarantees that every strategy you create has a consistent and non-negotiable layer of risk assessment.
How the Risk Manager Works: An Objective Safety Guardian
The Risk Manager functions as an objective, data-driven “safety guardian” for your portfolio. It does not make subjective judgments about a company’s value or market sentiment. Instead, it performs several key analyses based purely on historical market data to determine appropriate risk levels and limits:
- Understanding “Price Swings” (Volatility Checks): The Risk Manager analyzes how much an asset’s price has typically moved up and down over time. This is called volatility. If a stock’s price often changes dramatically, it has high volatility, meaning it carries more risk. The agent calculates how “active” or “unstable” a stock is.
- Identifying “Bundled Risks” (Correlation Analysis): The agent doesn’t look at each stock in isolation. It examines how different assets in your simulated portfolio tend to move in relation to each other. If several stocks you hold tend to go up or down at the same time (high correlation), it identifies a “bundled risk.” This means if the market turns against one, others might follow, increasing your overall loss potential.
- Setting “Smart Position Limits” (Deterministic Rules): Based on the volatility and correlation analysis, the Risk Manager calculates and imposes deterministic position limits. These are precise, unbreakable rules that define the maximum quantity or value of a specific asset you can hold in your simulated portfolio.
- For example, a stock with high volatility or one that’s highly correlated with your other holdings will automatically be assigned a lower position limit. This prevents you from putting “all your eggs in one basket” or overexposing your portfolio to risky or interdependent assets.
- These limits are calculated as a percentage of your total simulated portfolio value and then converted into actual dollar amounts or share quantities, always considering your available cash.
What Happens After Risk Assessment? The Mandatory Influence
After the Risk Manager completes its analysis, it does not simply provide a suggestion. The critical part is how its output is used by the system:
- Outputting Strict Limits: The Risk Manager updates the shared “memory space” (
AgentState
) with these calculated strict risk limits for each asset. This information includes the precise “remaining position limit” (how much more can be bought/sold safely for each stock) and detailed metrics on volatility and correlation. - Mandatory Influence on Decisions: This output mandatorily influences the final investment decisions made by other agents, specifically the “Portfolio Manager.” No subsequent agent, including the Portfolio Manager, can override these risk boundaries. This ensures that every simulated trading decision rigorously adheres to the predefined risk parameters, regardless of bullish signals from other AI experts.
- Capital Preservation: By strictly enforcing these limits, the system prioritizes capital preservation. It helps to prevent excessive exposure to risky assets and ensures that strategies remain disciplined, even when other AI agents might suggest aggressive trades.
This built-in risk management system is essential for creating simulated AI-driven investment strategies that are both robust and responsible. It introduces a layer of mathematical discipline, ensuring that the AI platform’s decisions are always made with safety and capital preservation as a top priority.
Related Code Files
Feature/Responsibility | Code File | Purpose and Implementation Details |
---|---|---|
Agent State Definition | src/graph/state.py | Defines the AgentState TypedDict, which acts as the shared memory for all agents, containing messages , data , and metadata fields for information exchange. |
Warren Buffett Agent Logic | src/agents/warren_buffett.py | Implements the specific logic for the Warren Buffett-inspired agent, including data fetching, quantitative analysis functions (e.g., analyze_fundamentals , analyze_moat ), intrinsic value calculation, and the LLM prompting structure. |
Sentiment Analyst Logic | src/agents/sentiment.py | Implements the logic for the Sentiment Analyst agent, including fetching insider trades and company news, analyzing sentiment from both sources, and combining signals with weighted proportions. |
Risk Manager Logic | src/agents/risk_manager.py | Implements the deterministic logic for the Risk Manager agent, including fetching historical prices, calculating volatility and correlation metrics, and computing volatility- and correlation-adjusted position limits. |
Volatility Calculation | src/agents/risk_manager.py (within calculate_volatility_metrics ) | Calculates daily and annualized volatility, as well as volatility percentiles, for assets based on historical price movements. |
Correlation Calculation | src/agents/risk_manager.py (within main agent logic) | Calculates the correlation matrix between asset returns to identify “bundled risks” within the portfolio. |
Position Limit Adjustment | src/agents/risk_manager.py (within calculate_volatility_adjusted_limit and calculate_correlation_multiplier ) | Computes how much the position limit should be adjusted based on the asset’s volatility and its correlation with other assets. |
Portfolio Manager Logic | src/agents/portfolio_manager.py | Implements the logic for the Portfolio Manager agent, responsible for synthesizing all agent signals, strictly adhering to risk constraints, deterministically calculating allowed actions, and generating final simulated trading decisions using an LLM. |
Financial Data Tools | src/tools/api.py | Contains functions like get_financial_metrics , search_line_items , and get_market_cap which are used by agents to fetch real-time and historical financial data from external APIs. |
LLM Interaction Utility | src/utils/llm.py | Provides the call_llm function, which handles sending prompts to the Large Language Model and parsing its structured outputs based on Pydantic models (e.g., WarrenBuffettSignal ). |
LangGraph Orchestration | app/backend/services/graph.py | Contains the create_graph function that dynamically builds the LangGraph state machine from user-defined nodes and edges, including the logic for mandatory insertion of the risk_management_agent . |