cancel_order
Cancel an open Alpaca order by providing its order ID to manage trading positions and adjust strategies.
Instructions
Cancels a specific open order.
Args:
order_id: The Alpaca order ID to cancel
Returns:
Confirmation message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| order_id | Yes |
Implementation Reference
- tools/execution.py:140-161 (handler)The primary handler function for the 'cancel_order' MCP tool. It takes an order_id, uses the Alpaca broker to cancel the order, and returns a success or error message.def cancel_order(order_id: str) -> str: """ Cancels a specific open order. Args: order_id: The Alpaca order ID to cancel Returns: Confirmation message """ if broker is None: return "ERROR: Alpaca broker not initialized." try: logger.info(f"Cancelling order: {order_id}") broker.cancel_order(order_id) logger.info(f"Order {order_id} cancelled successfully") return f"✅ Order {order_id} cancelled successfully" except Exception as e: logger.error(f"Cancel order failed: {e}", exc_info=True) return f"ERROR: Failed to cancel order - {str(e)}"
- server.py:375-378 (registration)Registers the cancel_order function (imported from tools.execution) as an MCP tool using FastMCP's mcp.tool() decorator via the register_tools helper.register_tools( [place_order, cancel_order, get_positions, flatten, get_order_history], "Execution" )
- tools/alpaca_broker.py:259-275 (helper)Supporting method in the AlpacaBroker class that performs the actual API call to cancel an order by ID. Called by the execution handler.def cancel_order(self, order_id: str) -> bool: """ Cancel a pending order. Args: order_id: Order ID to cancel Returns: True if successful """ try: self.trading_client.cancel_order_by_id(order_id) logger.info(f"Order {order_id} cancelled") return True except Exception as e: logger.error(f"Failed to cancel order {order_id}: {e}") raise