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
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes |
Implementation Reference
- src/server.py:523-545 (handler)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)}"