Close All Open Orders Based On Gain Percentage Not Working

 
int start()
{
   if ((100+TakeProfitPct)*AccountBalance()/100<=AccountEquity())
   {
      bClose= true;
   }
   
   if (bClose)
   {
      CloseAll();
      bClose= false;

   }
}


void CloseAll()
{
   for(int i = OrdersTotal() - 1; i >= 0; i--)
      if (OrderSelect(i, SELECT_BY_POS))
         if (OrderMagicNumber() == MagicNumber)
         {
            Log("close #"+OrderTicket());
            double closeprice = 0.0;
            if (OrderType() == OP_BUY)
               closeprice = Bid;
            if (OrderType() == OP_SELL)
               closeprice = Ask;
            if (!OrderClose(OrderTicket(), OrderLots(), closeprice, slippage))
              {
               Log("error");
               return(0);
              }
         }
}


This code will work, but it seems to take awhile to trigger and close the orders after the TakeProfitPCT has been hit for a few minutes. How do I get it to be as close to instant as possible?

 
thili55:


This code will work, but it seems to take awhile to trigger and close the orders after the TakeProfitPCT has been hit for a few minutes. How do I get it to be as close to instant as possible?

It looks like it should happen straight away, add some debugging Print() statements and find out what is happening and when . . .
 
Why haven't you used GetLastError() ??
 
            if (!OrderClose(OrderTicket(), OrderLots(), closeprice, slippage))
              {
               Log("error");
               return(0);
              }

What value have you assigned to slippage?

Reason: