Skip to main content
Glama

metatrader-mcp-server

close_position.py1.56 kB
from typing import Union from ..types import TradeRequestActions from .send_order import send_order from .get_positions_by_id import get_positions_by_id def close_position(connection, id: Union[str, int]): """ Close a position by its ID. Args: connection: MetaTrader 5 connection object. id: The unique identifier of the position to close. Returns: A dictionary containing an error flag, a message, and the closed position data if successful. """ try: position_id = int(id) except ValueError: return { "error": True, "message": f"Invalid position ID '{id}', it should be a valid integer", "data": None, } positions = get_positions_by_id(connection, position_id) if positions.index.size == 0: return { "error": True, "message": f"Invalid position ID '{id}'", "data": None, } position = positions.iloc[0] response = send_order( connection, action=TradeRequestActions.DEAL, position=position_id, order_type="SELL" if position["type"] == "BUY" else "BUY", symbol=position["symbol"], volume=position["volume"], ) if response["success"] is False: return { "error": True, "message": response["message"], "data": None } data = response["data"] return { "error": False, "message": f"Close position {position_id} success at price {getattr(data, 'price', None)}", "data": data }

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/ariadng/metatrader-mcp-server'

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