Skip to main content
Glama

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
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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"
  • 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

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/N-lia/MonteWalk'

If you have feedback or need assistance with the MCP directory API, please join our Discord server