Skip to main content
Glama

ig_close_all_positions

Close all open trading positions in IG Trading accounts using the IG Trading MCP server, automating position management for forex, indices, and commodities.

Instructions

Close all open positions

Input Schema

NameRequiredDescriptionDefault

No arguments

Input Schema (JSON Schema)

{ "properties": {}, "type": "object" }

Implementation Reference

  • Main implementation of ig_close_all_positions: fetches all positions and closes them concurrently, returning successful and failed closures.
    async closeAllPositions() { try { const positions = await this.getPositions(); if (positions.positions.length === 0) { logger.info('No positions to close'); return []; } const closePromises = positions.positions.map(p => this.closePosition(p.position.dealId) ); const results = await Promise.allSettled(closePromises); const successful = results.filter(r => r.status === 'fulfilled').map(r => r.value); const failed = results.filter(r => r.status === 'rejected').map(r => r.reason); if (failed.length > 0) { logger.warn(`Failed to close ${failed.length} positions`); } return { successful, failed }; } catch (error) { logger.error('Failed to close all positions:', error.message); throw error; } }
  • Input schema definition for the tool (no parameters required).
    { name: 'ig_close_all_positions', description: 'Close all open positions', inputSchema: { type: 'object', properties: {}, }, },
  • MCP server tool execution handler that dispatches to IGService.closeAllPositions() and formats the response.
    case 'ig_close_all_positions': const closeAllResult = await igService.closeAllPositions(); return { content: [ { type: 'text', text: JSON.stringify(closeAllResult, null, 2), }, ], };
  • Helper method to close a single position, used by closeAllPositions.
    async closePosition(dealId) { try { const positions = await this.getPositions(); const position = positions.positions.find(p => p.position.dealId === dealId); if (!position) { throw new Error(`Position ${dealId} not found`); } const closeTicket = { dealId, direction: position.position.direction === 'BUY' ? 'SELL' : 'BUY', orderType: 'MARKET', size: position.position.size }; const response = await this.apiClient.delete('/positions/otc', closeTicket, 1); if (response.data.dealReference) { const confirmation = await this.getConfirmation(response.data.dealReference); return { position: response.data, confirmation }; } return response.data; } catch (error) { logger.error(`Failed to close position ${dealId}:`, error.message); throw error; } }
  • Helper method to fetch current open positions.
    async getPositions() { try { const response = await this.apiClient.get('/positions', 2); return response.data; } catch (error) { logger.error('Failed to get positions:', error.message); throw error; } }

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/kea0811/ig-trading-mcp'

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