ExitTrade function question!!

 
void ExitTrade()
{
   for (int i=OrdersTotal(); i>=0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS))
      {
         if (OrderType()==OP_BUY && OrderMagicNumber() == MagicNumber)
         {
            while(true)
            {
              bool result = OrderClose(OrderTicket(),OrderLots(),Bid,30,Red);
              if (result != true)
              {
                 Print("Failed to close order, error =", GetLastError());
                 Sleep(1000);
                 RefreshRates();
                 i++;
              }
              else Print("Trade closed");
            }
         }
         if (OrderType()==OP_SELL && OrderMagicNumber() == MagicNumber)
         {
            while(true)
            {
              bool result = OrderClose(OrderTicket(),OrderLots(),Ask,30,Red);
              if (result != true)
              {
                 Print("Failed to close order, error =", GetLastError());
                 Sleep(1000);
                 RefreshRates();
                 i++;
              }
              else Print("Trade closed");
            }
         }
      }
      else (Print("When selecting a trade, error ",GetLastError(), "occurred"));
   }
}
 

Dear all, I just wondering if writing it this way, will the "Sleep, Refreshrate, and the i++" will work? 

Instead of leave some of the trade having error and not close, i wish all trades will be closed. So, will this function close all the trades for me?

Thank you for advice and suggestion.

 
sdahao36:

Dear all, I just wondering if writing it this way, will the "Sleep, Refreshrate, and the i++" will work? 

Instead of leave some of the trade having error and not close, i wish all trades will be closed. So, will this function close all the trades for me?

Thank you for advice and suggestion.

No. i++ will mess this all up because you will be cause an error by iterating it for no reason. This can also be shortened and simplified using some mql4 tricks. 


void ExitTrades()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
        if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == MagicNumber && OrderType() < 2)
            for(int j=0;j<3;j++) //Try three times
                if(OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 30, clrRed))
                {
                    Print("Trade closed");
                    break;
                }else{
                    Print("Failed to close order, error =", GetLastError());
                    Sleep(250);
                }
}
 
Emma Schwatson:

No. i++ will mess this all up because you will be cause an error by iterating it for no reason. This can also be shortened and simplified using some mql4 tricks. 


Thanks Emma, appreciate your reply. If I understand correctly, when j=3, the 2nd for loop will ceased and pass control back to first for loop, am i right?
 
I curious about the use of "OrderClosePrice()" function here. Can this function be used in place of Ask/Bid to close a trade?
 
Yes it can. #3
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
 
whroeder1:
Yes it can. #3
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
  1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
  2. and check OrderSelect in case earlier positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
Appreciate the links you have shared. It is very useful. 
Reason: