portfolio_risk
Calculate annualized portfolio volatility to assess investment risk using Monte Carlo simulations for quantitative finance analysis.
Instructions
Returns annualized volatility of the portfolio.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- tools/risk_engine.py:32-43 (handler)The main handler function for the 'portfolio_risk' tool. It calculates the annualized volatility of the portfolio by fetching historical price data, computing returns covariance matrix, and deriving portfolio variance using current position weights.def portfolio_risk() -> str: """Returns annualized volatility of the portfolio.""" data, weights = _get_portfolio_data() if data is None: return "Portfolio is empty." returns = data.pct_change().dropna() cov_matrix = returns.cov() * 252 port_variance = np.dot(weights.T, np.dot(cov_matrix, weights)) port_volatility = np.sqrt(port_variance) return f"Annualized Portfolio Volatility: {port_volatility:.2%}"
- server.py:380-382 (registration)Registers the portfolio_risk tool (and related risk tools) with the FastMCP server. The register_tools helper applies the @mcp.tool() decorator to each function, making them available as MCP tools.register_tools( [portfolio_risk, var, max_drawdown, monte_carlo_simulation], "Risk Engine"
- tools/risk_engine.py:10-30 (helper)Supporting helper function used by portfolio_risk to retrieve portfolio positions, download historical closing prices via yfinance, and compute market-value-based portfolio weights.def _get_portfolio_data(lookback: str = "1y"): portfolio = get_positions() positions = portfolio.get("positions", {}) if not positions: return None, None tickers = list(positions.keys()) weights = np.array(list(positions.values())) # This is qty, need value weights # Fetch data data = yf.download(tickers, period=lookback, progress=False)['Close'] if isinstance(data, pd.Series): data = data.to_frame(name=tickers[0]) # Calculate current value weights current_prices = data.iloc[-1] values = current_prices * pd.Series(positions) total_value = values.sum() weights = values / total_value return data, weights