close all open trades

 

Hi coders,

I seem to have a problem with this simple function I wrote to close all open trades. I keep getting an invalid price error but I can't see what I've done wrong. any help would be great

//+------------------------------------------------------------------+
//| close all open trades                                                                 |
//+------------------------------------------------------------------+
void exit()
      {
          for(int order=0; order<=OrdersTotal()-1; order++)
            {
               bool select=OrderSelect(order,SELECT_BY_POS);
   
                  if(OrderType()==OP_SELL && select==true)
   
                      //CLOSE sell Orders
                        {
                            bool closed = OrderClose(OrderTicket(),OrderLots(),Ask,30,clrRed);
                              if(closed==true) order--;
   
                           }
                             
                               if(OrderType()==OP_BUY && select==true)
   
                               //CLOSE buy Orders
                                   {
                                        bool closed = OrderClose(OrderTicket(),OrderLots(),Bid,30,clrRed);
                                       if(closed==true) order--;
         
                                           }
   
                      }
                   }
  
 
mql4 topic on mql4 section please.
 
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. You are using the predefined variables Ask/Bid. These are only market values for the current chart symbol, yet you are closing all orders on any symbol. Either get the correct price or just use OrderClosePrice()

  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 (non-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 programming forum
    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions. Your decrement of order will not work reliability.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

 
Thanks for your help
 
void exit()
{
   for(int order=OrdersTotal()-1; order>=0; order--)
     {
      if(OrderSelect(order,SELECT_BY_POS))
         if(OrderType()==OP_SELL || OrderType()==OP_BUY)
           {
            bool closed = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,clrRed);
            if(closed==false)
               Print("Error closing ticket #",OrderTicket(),". Error code ",GetLastError());
           }
     }
}

Topics concerning MT4 and MQL4 have their own section.

In future please post in the correct section.

I have moved your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:
void exit()

Code fails for US brokers (FIFO rules). See #2.3.2

 
William Roeder:

Code fails for US brokers (FIFO rules). See #2.3.2

As the code posted by the OP didn't have anything to cope with the moronic FIFO rules, neither did mine.

Reason: