Download MetaTrader 5

Close All Open Orders Based On Gain Percentage Not Working

To add comments, please log in or register
Your broker does not support mobile MetaTrader 5? Ask him to buy them!
thili55
113
thili55 2014.02.27 10:43 
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?

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.02.27 13:10  
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 . . .
Tjipke de Vries
6316
Tjipke de Vries 2014.02.27 13:20  
Why haven't you used GetLastError() ??
Keith Watford
Moderator
9648
Keith Watford 2014.02.27 13:55  
            if (!OrderClose(OrderTicket(), OrderLots(), closeprice, slippage))
              {
               Log("error");
               return(0);
              }

What value have you assigned to slippage?

To add comments, please log in or register