Skip to main content
Glama

max_drawdown

Calculate maximum drawdown to assess portfolio risk by measuring the largest peak-to-trough decline in value, helping identify potential losses during market downturns.

Instructions

Calculates Maximum Drawdown.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The core handler function implementing the max_drawdown tool. Computes the maximum drawdown of the portfolio using historical price data fetched via yfinance and position weights.
    def max_drawdown() -> str:
        """Calculates Maximum Drawdown."""
        data, weights = _get_portfolio_data()
        if data is None:
            return "Portfolio is empty."
        
        returns = data.pct_change().dropna()
        port_returns = returns.dot(weights)
        cumulative_returns = (1 + port_returns).cumprod()
        peak = cumulative_returns.expanding(min_periods=1).max()
        drawdown = (cumulative_returns / peak) - 1
        max_dd = drawdown.min()
        
        return f"Maximum Drawdown: {max_dd:.2%}"
  • Helper function that retrieves portfolio positions, downloads historical price data using yfinance, and computes value-based weights. Used by max_drawdown 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
  • server.py:380-383 (registration)
    Registration of the max_drawdown tool (along with related risk tools) to the FastMCP server using the register_tools helper.
    register_tools(
        [portfolio_risk, var, max_drawdown, monte_carlo_simulation],
        "Risk Engine"
    )
  • server.py:14-14 (registration)
    Import statement bringing the max_drawdown function into the MCP server module for registration.
    from tools.risk_engine import portfolio_risk, var, max_drawdown, monte_carlo_simulation

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