close_position
Close an open trading position for a specific stock symbol using the Alpaca Trading MCP Server to manage your portfolio.
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
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes |
Implementation Reference
- src/server.py:523-544 (handler)The handler function decorated with @mcp.tool() that implements the close_position tool. It checks for an existing position and calls the Alpaca trading client to close it.@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)}"