# Multiple Orders in EA - Problem while decrease counters

86

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 sells=5; // Number of sell orders opened simultaneously

//...

int OnInit()
{
//---
double minLot=MarketInfo(_Symbol,MODE_MINLOT); // Check if the input lot is lower than minimum market size
if(LOTS<minLot) LOTS=minLot;
MAX_LOSS=-MAX_LOSS;
if(leverage<1) leverage=1;
if(sells<1) sells=1;

//---
return(INIT_SUCCEEDED);
}

//...

void OnTick()
{
if (Volume[0]==1)
{

//...

{
}

// Sell Order Condition
{
// Open sell order
gSellTicket=OrderSend(_Symbol,OP_SELL,LOTS,Bid,slippage,0,TP,"Market Sell Order Opened",MAGIC,0,clrRed);
countSell++;
}

// Select and Evaluate Orders
for(int order=OrdersTotal()-1; order>0; order--)
{
if(OrderSelect(order,SELECT_BY_POS))
{
if(OrderType()==OP_SELL && OrderMagicNumber()==MAGIC)
{
if(OrderClosePrice()<=OrderTakeProfit()) countSell--;
if(/*CLOSING CONDITIONS*/)
{
// Close Order
if(closed) countSell--;
}
}
// Close Buy Order if condition is violated
{
if(/*CLOSING CONDITIONS*/)
{
// Close Order
bool closed=OrderClose(OrderTicket(),OrderLots(),Bid,slippage,clrRed);
}
}
}
}
}
}```

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!

16096

1. EA's must be coded to recover. If you restart the terminal, crash, power glitch, BSOD, your counts are bogus.
2. `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.
3. ```         gBuyTicket=OrderSend(_Symbol,OP_BUY,LOTS,Ask,slippage,0,TP,"Market Buy Order Opened",MAGIC,0,clrGreen);
```
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
86

Ok.. Mistery solved!

One last question.. How could I select OrderClosingPrice() for each close order then? I should switch to MODE_HISTORY?

Thanks!

121

leourb_:

Ok.. Mistery solved!

One last question.. How could I select OrderClosingPrice() for each close order then? I should switch to MODE_HISTORY?

Thanks!

Yes.
86

Thnaks!