I have a problem regarding the number of orders that EA can open simultaneously. I set the code as it follows:
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!
Inserted Code
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(LOTS<minLot) LOTS=minLot;
MAX_LOSS=-MAX_LOSS;
if(leverage<1) leverage=1;
if(buys<1) buys=1;
if(sells<1) sells=1;
//---
return(INIT_SUCCEEDED);
}
//...
void OnTick()
{
if (Volume[0]==1)
{
//...
if(/*TRADE CONDITIONS*/ && countBuy<buys)
{
// Open buy order
gBuyTicket=OrderSend(_Symbol,OP_BUY,LOTS,Ask,slippage,0,TP,"Market Buy Order Opened",MAGIC,0,clrGreen);
countBuy++;
}
// Sell Order Condition
if(/*TRADE CONDITIONS*/ && countSell<sells)
{
// 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
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!