What is the fastest way to close all positions?

 

MT5's 'Bulk Operations => Close All Positions' is much quicker compared to closing positions individually using a 'PositionsTotal()' loop.

Is there another way to close all positions as fast as possible?

Regards

 
Sajiro- Yoshizaki: MT5's 'Bulk Operations => Close All Positions' is much quicker compared to closing positions individually using a 'PositionsTotal()' loop. Is there another way to close all positions as fast as possible?

Loop and use asynchronous mode to make it faster — Documentation on MQL5: Trade Functions / OrderSendAsync

Forum on trading, automated trading systems and testing trading strategies

How To Implement Bulk Operations?

Fernando Carreiro, 2023.02.04 18:18

"Buik operations" are not available via MQL code. You can recreate an equivalente procedure using a loop and asynchronous mode.

 
Fernando Carreiro #:

Loop and use asynchronous mode to make it faster — Documentation on MQL5: Trade Functions / OrderSendAsync

Thank you for replying. I really appreciate it. I have created a sample code with Async-mode.
I am amazed that it is much faster than before.

Here is the sample script I used for testing.

#include <Trade/Trade.mqh>

void OnStart()
{
    ulong st = GetMicrosecondCount();

    CTrade sTrade;
    sTrade.SetAsyncMode(true);      // true:Async, false:Sync

    for(int cnt = PositionsTotal()-1; cnt >= 0 && !IsStopped(); cnt-- )
    {
        if(PositionGetTicket(cnt))
        {
            sTrade.PositionClose(PositionGetInteger(POSITION_TICKET),100);
            uint code = sTrade.ResultRetcode();
            Print(IntegerToString(code));
        }
    }
        
    for(int i=0; i<100; i++)
    {
        Print(IntegerToString(GetMicrosecondCount()-st) +"micro "+ IntegerToString(PositionsTotal()));
        if(PositionsTotal()<=0){ break; }
            Sleep(100);
    }
}

Please note that re-quotes may occur, so you may need to retry or take other measures.


Thanks