English

## Multiple Orders in EA - Problem while decrease counters

 31 leourb_ 2015.09.15 16:48   I have a problem regarding the number of orders that EA can open simultaneously. I set the code as it follows:```sinput string MAXORDERS; // Maximum Number of orders open simultaneously extern int buys=5; // Number of buys extern int sells=5; // Number of sell orders opened simultaneously int gBuyTicket, gSellTicket; //... int OnInit()   { //---    double minLot=MarketInfo(_Symbol,MODE_MINLOT); // Check if the input lot is lower than minimum market size    if(LOTS0; order--)         {          if(OrderSelect(order,SELECT_BY_POS))            {             if(OrderType()==OP_SELL && OrderMagicNumber()==MAGIC)               {                if(OrderClosePrice()<=OrderTakeProfit()) countSell--;                if(/*CLOSING CONDITIONS*/)                  {                   // Close Order                   bool closed=OrderClose(OrderTicket(),OrderLots(),Ask,slippage,clrGreen);                   if(closed) countSell--;                  }               }             // Close Buy Order if condition is violated             if(OrderType()==OP_BUY && OrderMagicNumber()==MAGIC)               {                if(OrderClosePrice()>=OrderTakeProfit()) countBuy--;                if(/*CLOSING CONDITIONS*/)                  {                   // Close Order                   bool closed=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrRed);                   if(closed) countBuy--;                  }               }            }         }      }   }``` It works partially because it opens orders but it does not decrease them! So once reached 5 trades for buys and 5 for sells it does not trade anymore. I have commented inside the loop for orders to see if it effectively loops and "order" variable keeps stack on 1. I cannot figure out why. Could you help me? Thanks!
 13001 WHRoeder 2015.09.15 18:08 #   EA's must be coded to recover. If you restart the terminal, crash, power glitch, BSOD, your counts are bogus. `if(OrderClosePrice()>=OrderTakeProfit()) countBuy--;`This will never be true. IF the order has closed you won't find it in the OrderSelect loop. Move the loop to above the open code, then count buys and sells that you don't close.```         gBuyTicket=OrderSend(_Symbol,OP_BUY,LOTS,Ask,slippage,0,TP,"Market Buy Order Opened",MAGIC,0,clrGreen);          countBuy++; ```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