Skip to main content
Glama

flatten

Closes all open trading positions to manage risk and lock in current portfolio status. Returns a summary of closed positions for review.

Instructions

Immediately closes all open positions. Returns: Summary of closed positions

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The flatten tool handler: closes all open positions via Alpaca broker and returns confirmation message or error.
    def flatten() -> str: """ Immediately closes all open positions. Returns: Summary of closed positions """ if broker is None: return "ERROR: Alpaca broker not initialized." try: result = broker.close_all_positions() if result['closed_count'] == 0: return "No positions to flatten." msg = [f"✅ Flattened {result['closed_count']} positions:"] for pos in result['positions_closed']: msg.append(f" - {pos['symbol']}: {pos['qty']} shares ({pos['status']})") return "\n".join(msg) except Exception as e: logger.error(f"Flatten failed: {e}") return f"ERROR: Failed to flatten positions - {str(e)}"
  • server.py:375-378 (registration)
    Registration of the flatten tool (among execution tools) to the MCP server using register_tools which applies @mcp.tool() decorator.
    register_tools( [place_order, cancel_order, get_positions, flatten, get_order_history], "Execution" )
  • Supporting method in AlpacaBroker class that performs the actual closing of all positions, called by the flatten handler.
    def close_all_positions(self) -> Dict[str, Any]: """ Close all open positions (emergency flatten). Returns: Dict with closed position count and details """ try: closed = self.trading_client.close_all_positions(cancel_orders=True) logger.info(f"Closed {len(closed)} positions") return { "closed_count": len(closed), "positions_closed": [ { "symbol": pos.symbol, "qty": float(pos.qty) if pos.qty else 0, "status": pos.status.value } for pos in closed ] } except Exception as e: logger.error(f"Failed to close all positions: {e}") raise

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/N-lia/MonteWalk'

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