Skip to main content
Glama

trade_close

Close perpetual futures positions on supported DEXs by executing trades to exit existing market exposure after confirming impact preview.

Instructions

Close an existing position. IMPORTANT: Call trade_preview first with the opposite side to show impact, then get user confirmation.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
exchangeYesExchange: pacifica, hyperliquid, or lighter
symbolYesSymbol to close (e.g., BTC, ETH)

Implementation Reference

  • The `trade_close` MCP tool handler, which identifies the open position and executes a market order to close it.
      "trade_close",
      "Close an existing position. IMPORTANT: Call trade_preview first with the opposite side to show impact, then get user confirmation.",
      {
        exchange: z.string().describe("Exchange: pacifica, hyperliquid, or lighter"),
        symbol: z.string().describe("Symbol to close (e.g., BTC, ETH)"),
      },
      async ({ exchange, symbol }) => {
        try {
          const adapter = await getOrCreateAdapter(exchange);
          const positions = await adapter.getPositions();
          const pos = positions.find(p => p.symbol.toUpperCase().includes(symbol.toUpperCase()));
          if (!pos || Number(pos.size) === 0) {
            return { content: [{ type: "text", text: err(`No open position for ${symbol} on ${exchange}`) }], isError: true };
          }
    
          const closeSide = pos.side === "long" ? "sell" : "buy";
          const result = await adapter.marketOrder(symbol, closeSide, pos.size);
    
          return {
            content: [{
              type: "text",
              text: ok({
                closed: true,
                symbol,
                exchange,
                closedPosition: { side: pos.side, size: pos.size, entryPrice: pos.entryPrice, unrealizedPnl: pos.unrealizedPnl },
                result,
              }, { exchange, type: "close" }),
            }],
          };
        } catch (e) {
          return { content: [{ type: "text", text: err(e instanceof Error ? e.message : String(e)) }], isError: true };
        }
      },
    );

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/hypurrquant/perp-cli'

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