How to pause after trades close

 

Hello

I use the code below to close all trades at a percentage of balance, I would like there to be a pause of say 5 minutes before the ea starts trading again.

Can I put a delay in the code below after it is executed.

Thanks for your help

void CheckDrawdown()
{
double totalProfit = 0;
int i;
for(i=0; i <= OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber)
{
totalProfit=totalProfit+OrderProfit();
}
}
if(totalProfit<0 && MathAbs(totalProfit) >= (DrawdownPercentage/100.0*AccountBalance()))
{
for(i=0; i <= OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 5);
}
if (OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 5);
}
}
}
}
}

 
Sleep(xxxxx)
 
n8937g:

Sleep(xxxxx)

There are repeated posts on this forum about Sleep() not working in the backtester (e.g. https://www.mql5.com/en/forum/108222). As a result, it's generally better to record a timestamp and wait for n seconds after the timestamp before allowing the EA to carry out any further action. This is also more generally flexible: Sleep() suspends the EA's execution entirely, whereas there are scenarios where you might only want to disable part of an EA's execution for a specified period.

 

Thanks for the reply jjc could you supply some sample code to work with.

 
build1team:

Thanks for the reply jjc could you supply some sample code to work with.

There's example code in the earlier post I linked to before: https://www.mql5.com/en/forum/108222


If you want another example, try backtesting the following. It will open new buy orders one hour apart (strictly speaking, on the first tick after an hour has elapsed since the last order placement):


int start()
{
   static datetime LastOrderPlacement = 0;
   
   // Is it one hour (3,600 seconds) since the last order? Also runs on the very first call to start() because
   // LastOrderPlacement will contain zero.
   if (TimeCurrent() - LastOrderPlacement >= 3600) {
      LastOrderPlacement = TimeCurrent();
      OrderSend(Symbol(), OP_BUY, 0.01, Ask, 999, 0, 0, "");
   }
}