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
| Name | Required | Description | Default |
|---|---|---|---|
| exchange | Yes | Exchange: pacifica, hyperliquid, or lighter | |
| symbol | Yes | Symbol to close (e.g., BTC, ETH) |
Implementation Reference
- src/mcp-server.ts:1499-1533 (handler)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 }; } }, );