MT5 Close All function

johnnybegoode

Trader
Jul 19, 2016
56
0
22
47
Do you have a better close all function for MT5?

The following function fail to close everything.
It was executed in the expert but at the journal, shows that 2 positions does not exist.
I have 4 opened trades and it only closes 2.

MQL5:
void CloseAll3()
{
   for (int i=PositionsTotal()-1;i>=0; i--)
   { 
      {                
         if(!trade.PositionClose(PositionGetSymbol(i)))
         {
      //--- failure message
      Print(PositionGetSymbol(i), "PositionClose() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
         }
 
         else
         {
      Print(PositionGetSymbol(i), "PositionClose() method executed successfully. Return code=",trade.ResultRetcode(),
            " (",trade.ResultRetcodeDescription(),")");
         }
      }
   }  
}
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,607
1,366
144
Odesa
www.earnforex.com
I have tested it in RoboForex MT5 Hedging account and it worked perfectly. I have tried it with 4 positions on the same currency pair, and 2/2 on two different currency pairs - the result is the same - it closes all positions.
 

johnnybegoode

Trader
Jul 19, 2016
56
0
22
47
I know why the last time it only close the 2 out 4 positions.
It is because the function keep looping the first 2 opened positions.

This time, I only have 2 opened positions and as seen below, the function seems to loop the 2 positions a few times. (five in total)

Expert Log:
ˇ˛FS 0 01:17:04.236 StatArbSMI (EURUSD,M1) GBPUSDPositionClose() method executed successfully. Return code=10008 (placed)

IE 0 01:17:04.267 StatArbSMI (EURUSD,M1) EURUSDPositionClose() method executed successfully. Return code=10008 (placed)

EE 0 01:17:04.288 StatArbSMI (EURUSD,M1) GBPUSDPositionClose() method executed successfully. Return code=10008 (placed)

MG 0 01:17:04.289 StatArbSMI (EURUSD,M1) EURUSDPositionClose() method executed successfully. Return code=10008 (placed)

RF 0 01:17:04.400 StatArbSMI (EURUSD,M1) EURUSDPositionClose() method executed successfully. Return code=10008 (placed)

Journal Log:
ˇ˛IP 0 01:17:04.191 Trades '555606': exchange sell 0.10 GBPUSD at market, close #103160545 buy 0.10 GBPUSD 1.24330

CF 0 01:17:04.267 Trades '555606': exchange buy 0.10 EURUSD at market, close #103160544 sell 0.10 EURUSD 1.08160

HD 0 01:17:04.288 Trades '555606': exchange sell 0.10 GBPUSD at market, close #103160545 buy 0.10 GBPUSD 1.24330

KJ 0 01:17:04.289 Trades '555606': exchange buy 0.10 EURUSD at market, close #103160544 sell 0.10 EURUSD 1.08160

OK 0 01:17:04.367 Trades '555606': accepted exchange sell 0.10 GBPUSD at market, close #103160545 buy 0.10 GBPUSD 1.24330

IJ 0 01:17:04.368 Trades '555606': deal #102568088 sell 0.10 GBPUSD at 1.24198 done (based on order #103162232)

NP 0 01:17:04.369 Trades '555606': order #103162232 sell 0.10 / 0.10 GBPUSD at 1.24198 done in 179.440 ms

IL 0 01:17:04.388 Trades '555606': exchange buy 0.10 EURUSD at market, close #103160544 sell 0.10 EURUSD 1.08160

FP 0 01:17:04.461 Trades '555606': accepted exchange buy 0.10 EURUSD at market, close #103160544 sell 0.10 EURUSD 1.08160

RM 0 01:17:04.462 Trades '555606': deal #102568089 buy 0.10 EURUSD at 1.07866 done (based on order #103162233)

OG 0 01:17:04.463 Trades '555606': order #103162233 buy 0.10 / 0.10 EURUSD at 1.07866 done in 196.268 ms

QJ 2 01:17:04.465 Trades '555606': failed exchange sell 0.10 GBPUSD at market [Position doesn't exist]

DI 2 01:17:04.465 Trades '555606': failed exchange buy 0.10 EURUSD at market [Position doesn't exist]

DN 2 01:17:04.555 Trades '555606': failed exchange buy 0.10 EURUSD at market [Position doesn't exist]
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,607
1,366
144
Odesa
www.earnforex.com
I am rather confused about what's going on with that cycle. However, I find it strange that a for cycle would loop unless you change i inside it.

What I would suggest in this case is to add some Print(i); inside the cycle and also add Sleep(1); to give your deals some execution time.