@jstap i tested the code in different ways its working fine , but for faster execution remove all the performance measurement, like below:
000000000000000000000000000000000000000000000000
for CloseAllPositions:-
{
sTrade.SetAsyncMode(true); // Enable asynchronous trade operations
CloseAllPositions(); // Call function to close all positions
}
In the global variables section:
#include <Trade/Trade.mqh>
CTrade sTrade; // Trade object used for operations
In the custom function section:
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));
}
}
}
000000000000000000000000000000000000000000000000
for close all buy:-
{
sTrade.SetAsyncMode(true); // Enable asynchronous trade operations
CloseBuyPositions(); // Call function to close only buy positions
}
// Global variables section:
#include <Trade/Trade.mqh>
CTrade sTrade; // Trade object used for operations
// Custom function section:
void CloseBuyPositions() {
for (int cnt = PositionsTotal() - 1; cnt >= 0 && !IsStopped(); cnt--) {
ulong ticket = PositionGetTicket(cnt);
if (ticket > 0) {
// Check if the position is a BUY
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) {
sTrade.PositionClose(ticket, 100);
uint code = sTrade.ResultRetcode();
Print("Close BUY result code: ", IntegerToString(code));
}
}
}
}
000000000000000000000000000000000000000000000000
for close all sell:-
// On tick section:
{
sTrade.SetAsyncMode(true); // Enable asynchronous trade operations
CloseSellPositions(); // Call function to close only sell positions
}
// Global variables section:
#include <Trade/Trade.mqh>
CTrade sTrade; // Trade object used for operations
// Custom function section:
void CloseSellPositions() {
for (int cnt = PositionsTotal() - 1; cnt >= 0 && !IsStopped(); cnt--) {
ulong ticket = PositionGetTicket(cnt);
if (ticket > 0) {
// Check if the position is a SELL
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) {
sTrade.PositionClose(ticket, 100);
uint code = sTrade.ResultRetcode();
Print("Close SELL result code: ", IntegerToString(code));
}
}
}
}
000000000000000000000000000000000000000000000000
thanks again for the code





