Download MetaTrader 5

Need Help for OrderClose Code

To add comments, please log in or register
voitl1979
107
voitl1979  
The code should close all orders but not the last opened.
Can anyone help me?
Thank you very much

My code ...

void CloseOrders()
  {

   bool close1=false;
   bool delite1=false;
   int closez;
   RefreshRates();

   for(closez=OrdersTotal()-2; closez>=0; closez--)
     {

      if(OrderSelect(closez,SELECT_BY_POS,MODE_TRADES))

      if(AllSymbols==false && OrderSymbol()!=Symbol())
        {
         continue;
        }

      if(OrderType()==OP_BUY)
        {
          close1=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
        }
      if(OrderType()==OP_SELL)
        {
          close1=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
        }

      if(PendingOrders)
        {
         if(OrderType()==OP_BUYLIMIT)
           {
            delite1=OrderDelete(OrderTicket());
           }
         if(OrderType()==OP_SELLLIMIT)
           {
            delite1=OrderDelete(OrderTicket());
           }
         if(OrderType()==OP_BUYSTOP)
           {
            delite1=OrderDelete(OrderTicket());
           }
         if(OrderType()==OP_SELLSTOP)
           {
            delite1=OrderDelete(OrderTicket());
           }
        }

     } // for

  }
whroeder1
15925
whroeder1  
voitl1979: The code should close all orders but not the last opened. Can anyone help me?
  1. Help you with what? You've stated no problem.
  2. if(OrderSelect(closez,SELECT_BY_POS,MODE_TRADES)) Do what here?
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
honest_knave
Moderator
2326
honest_knave  
voitl1979:
The code should close all orders but not the last opened.

void CloseOrders()
  {

   bool close1=false;
   bool delite1=false;
   int closez;
   RefreshRates();

   for(closez=OrdersTotal()-2; closez>=0; closez--)
     {

In addition to whroeder's comments - don't presume that the order with the highest index was the last opened.

You need to check the OrderOpenTime()  

To add comments, please log in or register