Close All MT5 trades quickly
-
This tutorial is how to close all trades at once (this only works on MT5).
1st click on custom, then click on create custom blocks to create a custom block.

This code is for the first section. This section is classed as on tick because the block is to be used in the on tick tab, it ends with a ~next~ to activate the next connected block:
startTime = GetMicrosecondCount(); // Record start time
sTrade.SetAsyncMode(true); // Enable asynchronous trade operationsCloseAllPositions(); // Call function to close all positions
PrintPerformance(startTime); // Monitor performance~next~
This code is for the Global variables, includes section:
#include <Trade/Trade.mqh>
ulong startTime; // To store the start time for performance measurement
CTrade sTrade; // Trade object used for operations
This code is for the custom functions section, when added the function will be named from the first line:
void CloseAllPositions() {
for (int cnt = PositionsTotal() - 1; cnt >= 0 && !IsStopped(); cnt--) {
if (PositionGetTicket(cnt)) {
sTrade.PositionClose(PositionGetInteger(POSITION_TICKET), 100);
uint code = sTrade.ResultRetcode();
Print("Close result code: ", IntegerToString(code));
}
}
}void PrintPerformance(ulong startTime) {
for (int i = 0; i < 100; i++) {
Print("Elapsed time: ", IntegerToString(GetMicrosecondCount() - startTime), " microseconds. Open positions: ", IntegerToString(PositionsTotal()));
if (PositionsTotal() <= 0) { break; }
Sleep(100);
}
} -
There is a block for this...
-
@bgvulk No there's not, this does not close 1 at a time till all is closed (which could take a long time), it closes them all at once...
-
Thanks for this tutorial and the help you gave me previously. That was really helpful.
By the way, I assume the Close All here works on all trades, regardless of the magic number as it imitates MT5 close all.
How can we restrict the Close All to one magic number or maybe if that is difficult, to a certain text in Comment. -
@jstap Do you have any way to turn ON/OFF Algo Trading in MT5 ?

-
@zentex74 To do this you can add a global variable, then in your EA modify the terminal variable with the magic number. I have not tested this but it should work.
On Tick Section
mql5
Copy code
startTime = GetMicrosecondCount(); // Record start time
sTrade.SetAsyncMode(true); // Enable asynchronous trade operationsClosePositionsByMagicNumber(magicNumberToClose); // Use the global variable for the magic number
PrintPerformance(startTime); // Monitor performance~next~
Global Variables/Includes Section
mql5
Copy code
#include <Trade/Trade.mqh>
ulong startTime; // To store the start time for performance measurement
CTrade sTrade; // Trade object used for operations
int magicNumberToClose = 12345; // Magic number for trades to close (change as needed)Custom Functions Section
mql5
Copy code
void ClosePositionsByMagicNumber(int magicNumber) {
for (int cnt = PositionsTotal() - 1; cnt >= 0 && !IsStopped(); cnt--) {
if (PositionSelectByIndex(cnt)) {
// Check if the position's magic number matches the desired one
if (PositionGetInteger(POSITION_MAGIC) == magicNumber) {
// Close the position
sTrade.PositionClose(PositionGetInteger(POSITION_TICKET), 100);
uint code = sTrade.ResultRetcode();
Print("Close result code: ", IntegerToString(code));
}
}
}
}void PrintPerformance(ulong startTime) {
for (int i = 0; i < 100; i++) {
Print("Elapsed time: ", IntegerToString(GetMicrosecondCount() - startTime), " microseconds. Open positions: ", IntegerToString(PositionsTotal()));
if (PositionsTotal() <= 0) { break; }
Sleep(100);
}
} -
@XDV You can find that here: https://fxdreema.com/forum/topic/19004/help-custom-mql-code?_=1718300737685
-
Greeting sir
Pls i wanted to create EA to open buy trade "on tick" without any condition, and when the trade hit stop_Loss or trailing_stop, Sell trade should open and when the Sell trade too hit stop loss or trailing stops the Buy should open.
pls can you help me out with that.
i dont want buy and sell trade should open at once but buy trade first and when sl or ts hit sell trade should open.
-
@JMaxFx This is not the post for that, start a fresh question, and add a shared link on what you have already done
-
OOhok Sir
-
@jstap Thank you very very much

-
please i want to know how to stop the robot from placing trade after making $20 profit daily. for example, "if the robot make $20 today, it should not execute any trade again for the day until the next day" i will appreciate your guide. Thanks
-
@creatingrobot Create a new post for these questions.
-
@creatingrobot This is the third time you asked for the same question. Please stop duplicating questions.
-
@jstap can you modify it with internal magicnumber, im sure we will forget to change magic number
-
Next question how do we change only close buy or only close sell?
-

i got this error using magic number
-
MT5生成器在哪里打开页面