ig_close_position
Close an open trading position by specifying the deal ID. Facilitates efficient position management within the IG Trading MCP server for forex, indices, and commodities.
Instructions
Close an open position
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dealId | Yes | Deal ID of the position to close |
Input Schema (JSON Schema)
{
"properties": {
"dealId": {
"description": "Deal ID of the position to close",
"type": "string"
}
},
"required": [
"dealId"
],
"type": "object"
}
Implementation Reference
- src/services/ig-service.js:200-231 (handler)Core implementation of closing a position: fetches current positions, constructs opposite market close ticket, sends DELETE request to IG API /positions/otc, retrieves confirmation if available.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; } }
- src/services/mcp-service.js:214-227 (schema)Tool schema definition including name, description, and input validation requiring dealId.{ name: 'ig_close_position', description: 'Close an open position', inputSchema: { type: 'object', properties: { dealId: { type: 'string', description: 'Deal ID of the position to close', }, }, required: ['dealId'], }, },
- src/services/mcp-service.js:612-621 (registration)MCP tool dispatch/registration: handles CallToolRequest for ig_close_position by invoking igService.closePosition and formatting JSON response.case 'ig_close_position': const closeResult = await igService.closePosition(args.dealId); return { content: [ { type: 'text', text: JSON.stringify(closeResult, null, 2), }, ], };