Risk Management Agent
The Risk Management Agent serves as the final safeguard and quantitative strategist within the trading system. While other agents focus on price direction, the Risk Management Agent is responsible for capital preservation, position sizing, and the calculation of precise exit points based on market volatility.
Core Responsibilities
The agent evaluates market conditions and specific trade setups to provide:
- Volatility Assessment: Classifying market conditions as Low, Medium, or High.
- Position Sizing: Recommending capital allocation (Full, Half, Quarter, or None) based on risk-to-reward ratios.
- Mathematical Exit Targets: Calculating exact price levels for stop-losses and take-profits using technical tools.
- Safety Validation: Acting as a "veto" or modifier in the orchestration phase to prevent overexposure during high-volatility events.
Data Schema
The Risk Management Agent utilizes a structured Pydantic schema to ensure its output can be consumed programmatically by the execution engine.
RiskMetadata
This model contains the quantitative outputs derived from mathematical tools.
| Field | Type | Description |
| :--- | :--- | :--- |
| volatility_assessment | str | Low, Medium, or High volatility. |
| position_sizing | str | Suggested size: FULL, HALF, QUARTER, or NONE. |
| stop_loss_level | float | The exact price for the ATR-based stop-loss. |
| take_profit_level | float | The exact price for the target take-profit. |
RiskAnalysis
Inherits from the base AgentRecommendation and includes the RiskMetadata.
class RiskAnalysis(AgentRecommendation):
"""
Risk agent response with volatility and position sizing metadata.
"""
metadata: RiskMetadata
Calculation Logic & Tools
The agent utilizes specialized math tools to ensure calculations are precise and not hallucinated by the LLM.
ATR-Based Stop Loss
Instead of using fixed percentage drops, the agent uses Average True Range (ATR) to set stop-losses. This accounts for current market "noise."
- Logic: Typically sets the stop-loss at $1.5 \times \text{ATR}$ or $2.0 \times \text{ATR}$ from the entry price.
- Tool:
calculate_atr_stop_loss
Position Sizing
Allocation is determined by the "Confidence" score and the "Volatility Assessment."
- Full Position: High confidence + Low/Medium volatility.
- Half/Quarter Position: Low confidence or High volatility.
- None: Extreme volatility or negative risk-to-reward ratio.
Usage Example
The Risk Management Agent is typically invoked via the TradingOrchestrator. Below is an example of how the agent contributes to the final trade decision.
from agents.schemas import RiskAnalysis
# Example of a structured response from the Risk Agent
risk_output = RiskAnalysis(
recommendation="BUY",
confidence=85,
reasoning="Current volatility is low, allowing for a tight stop-loss. Upside potential outweighs risk.",
key_factors=["Low ATR", "Strong Support at $42k", "Healthy Risk/Reward"],
metadata={
"volatility_assessment": "LOW",
"position_sizing": "FULL",
"stop_loss_level": 41500.0,
"take_profit_level": 48000.0
}
)
print(f"Position Size: {risk_output.metadata.position_sizing}")
print(f"Set Stop Loss: ${risk_output.metadata.stop_loss_level}")
Knowledge Integration (RAG)
The agent is supported by a dedicated Risk Management Vector Store. This knowledge base contains:
- Standard risk management frameworks (Kelly Criterion, etc.).
- Historical "black swan" event data to help identify extreme volatility patterns.
- Curated markdown summaries of financial research papers on position sizing.
When an analysis is requested, the agent queries the risk domain in the vector store to ensure its reasoning aligns with established risk-reduction principles.