Skip to main content
Glama
laukikk

Alpaca Trading MCP Server

by laukikk

close_position

Close an open trading position for a specified stock symbol to manage portfolio risk and lock in profits or losses.

Instructions

Close an open position for a specific symbol.

Args: symbol: Stock symbol to close position for

Returns: Confirmation of position closure

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYes

Implementation Reference

  • The handler function decorated with @mcp.tool() that implements the close_position tool. It validates the existence of a position for the given symbol and closes it using the Alpaca trading client, returning appropriate success or error messages.
    @mcp.tool()
    def close_position(symbol: str) -> str:
        """
        Close an open position for a specific symbol.
        
        Args:
            symbol: Stock symbol to close position for
        
        Returns:
            Confirmation of position closure
        """
        try:
            # First check if position exists
            position = calls.get_position(trading_client, symbol)
            if not position:
                return f"No open position found for {symbol}."
            
            # Close the position
            trading_client.close_position(symbol)
            return f"Position for {symbol} has been successfully closed."
        except Exception as e:
            return f"Error closing position for {symbol}: {str(e)}"

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/laukikk/alpaca-mcp'

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