Skip to main content
Glama
l4b4r4b4b4
by l4b4r4b4b4

run_monte_carlo

Optimizes portfolios by running Monte Carlo simulations that test random weight combinations and evaluate risk-return characteristics.

Instructions

Run Monte Carlo simulation to find optimal portfolios.

Generates random portfolio weight combinations and evaluates their risk/return characteristics to find optimal allocations.

Note: This is computationally intensive. For large num_trials, consider using the Efficient Frontier method instead which provides mathematically optimal solutions.

Args: name: The portfolio name. num_trials: Number of random portfolios to generate (default: 5000).

Returns: Dictionary containing: - num_trials: Number of simulations run - min_volatility_portfolio: Portfolio with minimum volatility - max_sharpe_portfolio: Portfolio with maximum Sharpe ratio - simulation_stats: Statistics about the simulation - sample_portfolios: Sample of generated portfolios

Example: result = run_monte_carlo(name="tech_stocks", num_trials=10000) best = result['max_sharpe_portfolio'] print(f"Best Sharpe: {best['sharpe_ratio']:.2f}") print(f"Optimal weights: {best['weights']}")

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYes
num_trialsNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The tool is registered as an MCP tool via @mcp.tool decorator on the run_monte_carlo function inside register_optimization_tools(). The registration is triggered from app/server.py line 139.
    @mcp.tool
    def run_monte_carlo(
  • The core handler function for run_monte_carlo tool. It takes a portfolio name and num_trials, fetches portfolio data from the store, runs Monte Carlo optimization via MonteCarloOpt from finquant, and returns min_volatility/max_sharpe portfolios, simulation stats, and sample portfolios.
    def run_monte_carlo(
        name: str,
        num_trials: int = 5000,
    ) -> dict[str, Any]:
        """Run Monte Carlo simulation to find optimal portfolios.
    
        Generates random portfolio weight combinations and evaluates
        their risk/return characteristics to find optimal allocations.
    
        Note: This is computationally intensive. For large num_trials,
        consider using the Efficient Frontier method instead which
        provides mathematically optimal solutions.
    
        Args:
            name: The portfolio name.
            num_trials: Number of random portfolios to generate (default: 5000).
    
        Returns:
            Dictionary containing:
            - num_trials: Number of simulations run
            - min_volatility_portfolio: Portfolio with minimum volatility
            - max_sharpe_portfolio: Portfolio with maximum Sharpe ratio
            - simulation_stats: Statistics about the simulation
            - sample_portfolios: Sample of generated portfolios
    
        Example:
            ```
            result = run_monte_carlo(name="tech_stocks", num_trials=10000)
            best = result['max_sharpe_portfolio']
            print(f"Best Sharpe: {best['sharpe_ratio']:.2f}")
            print(f"Optimal weights: {best['weights']}")
            ```
        """
        data = store.get(name)
        if data is None:
            return {
                "error": f"Portfolio '{name}' not found",
            }
    
        # Limit trials for performance
        if num_trials > 50000:
            return {
                "error": "num_trials exceeds maximum of 50000",
                "suggestion": "Use smaller num_trials or use optimize_portfolio() for exact solution",
            }
    
        # Rebuild portfolio
        prices_df = pd.DataFrame(
            data=data["prices"]["values"],
            index=pd.to_datetime(data["prices"]["index"]),
            columns=data["prices"]["columns"],
        )
        allocation_df = pd.DataFrame(
            data=data["allocation"]["values"],
            columns=data["allocation"]["columns"],
        )
        portfolio = build_portfolio(data=prices_df, pf_allocation=allocation_df)
        portfolio.risk_free_rate = data["settings"]["risk_free_rate"]
    
        # Calculate returns
        returns_df = daily_returns(prices_df).dropna()
    
        # Get initial weights
        initial_weights = np.array(
            [row[0] / 100.0 for row in data["allocation"]["values"]]
        )
    
        # Run Monte Carlo optimization
        mc = MonteCarloOpt(
            returns=returns_df,
            num_trials=num_trials,
            risk_free_rate=portfolio.risk_free_rate,
            freq=portfolio.freq,
            initial_weights=initial_weights,
        )
    
        opt_weights, opt_results = mc.optimisation()
    
        # Extract results
        symbols = prices_df.columns.tolist()
    
        min_vol_weights = opt_weights.loc["Min Volatility"].to_dict()
        max_sharpe_weights = opt_weights.loc["Max Sharpe Ratio"].to_dict()
    
        min_vol_results = opt_results.loc["Min Volatility"].to_dict()
        max_sharpe_results = opt_results.loc["Max Sharpe Ratio"].to_dict()
    
        # Get simulation statistics
        sim_returns = mc.df_results["Expected Return"]
        sim_volatility = mc.df_results["Volatility"]
        sim_sharpe = mc.df_results["Sharpe Ratio"]
    
        # Sample portfolios for visualization (take every nth)
        sample_size = min(100, num_trials)
        step = max(1, num_trials // sample_size)
        sample_portfolios = []
        for i in range(0, num_trials, step):
            if len(sample_portfolios) >= sample_size:
                break
            sample_portfolios.append(
                {
                    "expected_return": float(mc.df_results.iloc[i]["Expected Return"]),
                    "volatility": float(mc.df_results.iloc[i]["Volatility"]),
                    "sharpe_ratio": float(mc.df_results.iloc[i]["Sharpe Ratio"]),
                }
            )
    
        return {
            "portfolio_name": name,
            "num_trials": num_trials,
            "min_volatility_portfolio": {
                "weights": {s: float(min_vol_weights[s]) for s in symbols},
                "expected_return": float(min_vol_results["Expected Return"]),
                "volatility": float(min_vol_results["Volatility"]),
                "sharpe_ratio": float(min_vol_results["Sharpe Ratio"]),
            },
            "max_sharpe_portfolio": {
                "weights": {s: float(max_sharpe_weights[s]) for s in symbols},
                "expected_return": float(max_sharpe_results["Expected Return"]),
                "volatility": float(max_sharpe_results["Volatility"]),
                "sharpe_ratio": float(max_sharpe_results["Sharpe Ratio"]),
            },
            "simulation_stats": {
                "return_range": [float(sim_returns.min()), float(sim_returns.max())],
                "volatility_range": [
                    float(sim_volatility.min()),
                    float(sim_volatility.max()),
                ],
                "sharpe_range": [float(sim_sharpe.min()), float(sim_sharpe.max())],
                "return_mean": float(sim_returns.mean()),
                "volatility_mean": float(sim_volatility.mean()),
                "sharpe_mean": float(sim_sharpe.mean()),
            },
            "sample_portfolios": sample_portfolios,
            "completed_at": datetime.now().isoformat(),
        }
  • Input schema: name (str, required) and num_trials (int, default 5000, max 50000). Output structure documented in docstring and returned dict with keys: num_trials, min_volatility_portfolio, max_sharpe_portfolio, simulation_stats, sample_portfolios.
    def run_monte_carlo(
        name: str,
        num_trials: int = 5000,
    ) -> dict[str, Any]:
        """Run Monte Carlo simulation to find optimal portfolios.
    
        Generates random portfolio weight combinations and evaluates
        their risk/return characteristics to find optimal allocations.
    
        Note: This is computationally intensive. For large num_trials,
        consider using the Efficient Frontier method instead which
        provides mathematically optimal solutions.
    
        Args:
            name: The portfolio name.
            num_trials: Number of random portfolios to generate (default: 5000).
    
        Returns:
            Dictionary containing:
            - num_trials: Number of simulations run
            - min_volatility_portfolio: Portfolio with minimum volatility
            - max_sharpe_portfolio: Portfolio with maximum Sharpe ratio
            - simulation_stats: Statistics about the simulation
            - sample_portfolios: Sample of generated portfolios
    
        Example:
            ```
            result = run_monte_carlo(name="tech_stocks", num_trials=10000)
            best = result['max_sharpe_portfolio']
            print(f"Best Sharpe: {best['sharpe_ratio']:.2f}")
            print(f"Optimal weights: {best['weights']}")
            ```
        """
  • Uses MonteCarloOpt from finquant library to perform the Monte Carlo simulation. The optimisation() method returns optimal weights and results.
    mc = MonteCarloOpt(
        returns=returns_df,
        num_trials=num_trials,
        risk_free_rate=portfolio.risk_free_rate,
        freq=portfolio.freq,
        initial_weights=initial_weights,
    )
  • app/server.py:71-71 (registration)
    The tool is listed in the server's instructions/documentation as 'run_monte_carlo: Run Monte Carlo simulation for optimization'.
    - run_monte_carlo: Run Monte Carlo simulation for optimization
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Without annotations, the description carries full burden. It discloses computational intensity as a behavioral trait, but does not mention side effects, permissions, or data sources. Nonetheless, it provides sufficient context for a simulation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with an intro, a performance note, clear Arg/Returns sections, and an example. Every sentence adds value without redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity and the presence of an output schema, the description fully explains what the tool does, its performance implications, and the structure of its return values, including an example that clarifies usage. It is sufficient to differentiate from 25+ sibling tools.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has no descriptions for its parameters (0% coverage), but the description's Args block explains that 'name' is the portfolio name and 'num_trials' is the number of simulations with a default of 5000, adding essential meaning beyond the bare schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool runs Monte Carlo simulations to find optimal portfolios by generating random weight combinations and evaluating risk/return. It distinguishes itself from the Efficient Frontier method, implying this is for random sampling approaches, which differentiates it from sibling tools like 'optimize_portfolio' or 'get_efficient_frontier'.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly notes that the tool is computationally intensive and advises using the Efficient Frontier method for large num_trials, providing clear guidance on when not to use this tool and a specific alternative.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/l4b4r4b4b4/portfolio-mcp'

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