INNER CODE UNIT · Python

close_position

flukelaster/ai-trading-agent · mt5_bridge/main.py:354

async def close_position(ticket: int):
    if not ensure_connected():
        return mt5_response(False, error="MT5 not connected")

    position = mt5.positions_get(ticket=ticket)
    if not position:
        return mt5_response(False, error=f"Position {ticket} not found")

    pos = position[0]
    close_type = mt5.ORDER_TYPE_SELL if pos.type == mt5.ORDER_TYPE_BUY else mt5.ORDER_TYPE_BUY
    tick = mt5.symbol_info_tick(pos.symbol)
    price = tick.bid if pos.type == mt5.ORDER_TYPE_BUY else tick.ask

    request = {
        "action": mt5.TRADE_ACTION_DEAL,
        "symbol": pos.symbol,
        "volume": pos.volume,
        "type": close_type,

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…