Help needed: Implementing native MT5 "Close all positions" functionality in EA

 
Hello everyone,

I'm working on a Stream Deck Controller EA and I need help improving my CloseAllPositions function.

I'd like to replicate the native MT5 bulk operation "Close All Positions" (the one accessible via right-click -> bulk operations -> Close all positions) as it's much more efficient than my current implementation.


Here's my current code:

void CloseAllPositions()
{
    bool success = true;
    int total = PositionsTotal();

    for(int i = total-1; i >= 0; i--)
    {
        ulong ticket = PositionGetTicket(i);
        string position_symbol = PositionGetString(POSITION_SYMBOL);

        // If symbol specified, close only positions of that symbol
        if(SYMBOL != "" && position_symbol != SYMBOL)
            continue;

        if(!trade.PositionClose(ticket))
        {
            Print("Error closing position #", ticket, ": ", GetLastError());
            success = false;
        }
        else
        {
            Print("Position closed successfully: #", ticket);
        }
    }
}


This is what I could do by taking a look at the documentation. It's just a loop through all the positions, they get closed them one by one.

The native function in mt5 is just superior, it seems like all positions get closed all at once. Any idea?

 
Icaras12x:
Hello everyone,

I'm working on a Stream Deck Controller EA and I need help improving my CloseAllPositions function.

I'd like to replicate the native MT5 bulk operation "Close All Positions" (the one accessible via right-click -> bulk operations -> Close all positions) as it's much more efficient than my current implementation.


Here's my current code:


This is what I could do by taking a look at the documentation. It's just a loop through all the positions, they get closed them one by one.

The native function in mt5 is just superior, it seems like all positions get closed all at once. Any idea?

You can find new ways if you will read this article: https://www.mql5.com/en/docs/trading/ordersendasync

+ Also this: https://www.mql5.com/en/docs/constants/tradingconstants/enum_trade_request_actions

Documentation on MQL5: Trade Functions / OrderSendAsync
Documentation on MQL5: Trade Functions / OrderSendAsync
  • www.mql5.com
The OrderSendAsync() function is used for conducting asynchronous trade operations without waiting for the trade server's response to a sent...