Skip to main content
Glama
VaishnaviK23

Trading MCP Server

by VaishnaviK23

unrealized_gains

Calculate unrealized profit or loss for current stock holdings by comparing average buy prices against live market prices.

Instructions

Calculate unrealized gains for current holdings.

This tool calculates unrealized profit or loss by comparing the average buy price for currently held shares against the current market price fetched live using yfinance.

Returns: A dictionary mapping each stock symbol to its unrealized gain or loss in dollars.

Example: unrealized_gains() -> {"AAPL": 125.50, "GOOG": -22.15}

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The `unrealized_gains` MCP tool handler calculates the unrealized profit or loss for current stock holdings by comparing the average cost basis against the live market price.
    @mcp.tool()
    def unrealized_gains() -> dict[str, float]:
        """Calculate unrealized gains for current holdings.
    
        This tool calculates unrealized profit or loss by comparing the average
        buy price for currently held shares against the current market price
        fetched live using yfinance.
    
        Returns:
            A dictionary mapping each stock symbol to its unrealized gain or loss in dollars.
    
        Example:
            unrealized_gains() -> {"AAPL": 125.50, "GOOG": -22.15}
        """
        holdings = defaultdict(list)
    
        for _, row in df.iterrows():
            if row['type'] == 'Buy':
                holdings[row['symbol']].append((row['shares'], row['price_per_share']))
            else:
                to_sell = row['shares']
                while to_sell > 0 and holdings[row['symbol']]:
                    qty, price = holdings[row['symbol']][0]
                    matched = min(qty, to_sell)
                    if matched == qty:
                        holdings[row['symbol']].pop(0)
                    else:
                        holdings[row['symbol']][0] = (qty - matched, price)
                    to_sell -= matched
    
        results = {}
        for symbol, buys in holdings.items():
            total_cost = sum(q * p for q, p in buys)
            total_shares = sum(q for q, _ in buys)
            if total_shares == 0:
                continue
            avg_price = total_cost / total_shares
            current = get_live_price(symbol)
            results[symbol] = round((current - avg_price) * total_shares, 2)
        return results
Behavior4/5

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

With no annotations provided, the description carries the full burden. It successfully discloses key behavioral traits: the calculation methodology (average buy price vs. market price), the live data source (yfinance), and the return format (dictionary mapping symbols to dollar values). It lacks only operational details like error handling or rate limits.

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 excellently structured with a clear one-sentence summary followed by detailed calculation logic, a 'Returns:' section, and an example. Every sentence earns its place; no redundancy or fluff is present.

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 tool's simplicity (zero parameters, single operation) and the presence of an output explanation (including example), the description is complete. It adequately covers what the tool does, how it calculates results, and what it returns without needing additional operational context.

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

Parameters4/5

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

The input schema contains zero parameters. According to calibration rules, zero-parameter tools receive a baseline score of 4. The description correctly provides no parameter-related text since none exist.

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 'Calculate[s] unrealized gains for current holdings' and specifies the methodology (comparing average buy price against current market price). The phrase 'currently held shares' effectively distinguishes it from the sibling 'realized_gains' tool.

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

Usage Guidelines3/5

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

While the description implies usage by specifying 'currently held shares' (distinguishing from realized gains), it lacks explicit guidance on when to select this over siblings like 'pnl' or 'portfolio'. No 'when-not-to-use' or alternative recommendations are provided.

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/VaishnaviK23/Trading-MCP-Server'

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