Skip to main content
Glama

var

Calculate Value at Risk (VaR) to quantify potential portfolio losses at a specified confidence level for risk management.

Instructions

Calculates Value at Risk (VaR).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
confidenceNo

Implementation Reference

  • The 'var' tool handler: computes portfolio Value at Risk (VaR) using historical returns percentile method.
    def var(confidence: float = 0.95) -> str: """Calculates Value at Risk (VaR).""" data, weights = _get_portfolio_data() if data is None: return "Portfolio is empty." returns = data.pct_change().dropna() # Portfolio historical returns port_returns = returns.dot(weights) # Parametric VaR mean = np.mean(port_returns) std = np.std(port_returns) var_val = np.percentile(port_returns, (1 - confidence) * 100) return f"Daily VaR ({confidence:.0%}): {var_val:.2%}"
  • server.py:380-383 (registration)
    Registration of the 'var' tool via register_tools which applies FastMCP @mcp.tool() decorator.
    register_tools( [portfolio_risk, var, max_drawdown, monte_carlo_simulation], "Risk Engine" )
  • Shared helper function to retrieve portfolio data and compute asset weights, used by 'var' and other risk tools.
    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