How to avoid multiple Limit orders

 

How to avoid opening multiple limit orders instead of one at a time?

extern double PriceLimit=3;
extern int MAPeriod=7;

int MagicNumber=90;
double BuyGridTrade=0;
double SellGridTrade=0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int b=OrdersTotal()-1;b>=0;b--)
  {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY||OrderType()==OP_SELL)
   Count++;
  }
  
  return(Count);
 }
 
int CountPending()
 {
  int Pending=0;
  for(int d=OrdersTotal()-1;d>=0;d--)
  {
   if(OrderSelect(d,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)
   if(OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT)
   Pending++;
  }
  return(Pending); 
 } 
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==2||Digit==3||Digit==5){PipPoint=Point*10;}
  
  return(PipPoint);
 }
 
void Buy()
 {
  double MAClose=iMA(Symbol(),0,MAPeriod,0,MODE_LWMA,PRICE_CLOSE,0);
  
  if(Open[0]>MAClose&&Bid>MAClose)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,"Test",MagicNumber,0,clrNONE);
  } 
  
 }

void Sell()
 {
  double MAClose=iMA(Symbol(),0,MAPeriod,0,MODE_LWMA,PRICE_CLOSE,0);

  if(Open[0]<MAClose&&Bid<MAClose)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0,"Test",MagicNumber,0,clrNONE);
  }   

 }
 
void BuyGridLimit()
 {
  for(int y=OrdersTotal()-1;y>=0;y--)
  {
   if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(Ask<=BuyGridTrade)
    {
     int BuyLimitTrade=OrderSend(Symbol(),OP_BUYLIMIT,0.01,Ask-PriceLimit*Pips(),0,0,0,"Test",MagicNumber,0,clrDarkOrange);
    }
   }
  }

 } 
 
void SellGridLimit()
 {
  for(int x=OrdersTotal()-1;x>=0;x--)
  {
   if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(Bid>=SellGridTrade)
    { 
     int SellLimitTrade=OrderSend(Symbol(),OP_SELLLIMIT,0.01,Bid+PriceLimit*Pips(),0,0,0,"Test",MagicNumber,0,clrDarkCyan); 
    }
   }
  }
   
 } 
 
void CloseAll()
 {
  for(int x=OrdersTotal()-1;x>=0;x--)
  {
   if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    bool CloseTrade=false;
    int Type=OrderType();
    switch(Type)
    {
     case OP_BUY:CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     case OP_SELL:CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
    }
   }
  }
 
 } 

void ProfitHedge()
 {
  if(AccountProfit()>=2)
  {
   for(int w=OrdersTotal()-1;w>=0;w--)
   {
    if(OrderSelect(w,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    {
     bool CloseTrade=false;
     CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);
    }
   }
  }
    
 }    

void OnTick()
 {
  if(CountTrades()<1){Buy();}
  else if(CountPending()<1){BuyGridLimit();}

  if(CountTrades()<1){Sell();}
  else if(CountPending()<1){SellGridLimit();}
  
  ProfitHedge();
   
 }
 
int CountPending()
 {
  int Pending=0;
  for(int d=OrdersTotal()-1;d>=0;d--)
  {
   if(OrderSelect(d,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYSTOP||OrderType()==OP_SELLSTOP)
   if(OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT)
   Pending++;
  }
  return(Pending); 
 } 

If it is a buy stop or a sellstop it will never be a buy limit or a sell limit. Your function always returns zero. Fix and simplify:

int CountPending()
 {
  int Pending=0;
  for(int d=OrdersTotal()-1;d>=0;d--) if(
      OrderSelect(d,SELECT_BY_POS,MODE_TRADES)
   && OrderMagicNumber()==MagicNumber
   &&(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
   && OrderSymbol()==Symbol()
   ){
      Pending++;
   }
   return(Pending); 
 } 
 
William Roeder #:

If it is a buy stop or a sellstop it will never be a buy limit or a sell limit. Your function always returns zero. Fix and simplify:

Minor mistakes, I'll just remove the stop functions. 

int CountPending()
 {
  int Pending=0;
  for(int d=OrdersTotal()-1;d>=0;d--)
  {
   if(OrderSelect(d,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT)
   Pending++;
  }
  return(Pending); 
 } 

As for the multiple orders I solved it by adding this function:

void BuyGridLimit()
 {
  for(int y=OrdersTotal()-1;y>=0;y--)
  {
   if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(Ask<=BuyGridTrade)
    {
     int BuyLimitTrade=OrderSend(Symbol(),OP_BUYLIMIT,0.01,Ask-PriceLimit*Pips(),0,0,0,"Test",MagicNumber,0,clrDarkOrange);
     BuyGridTrade=Ask-PriceLimit*Pips();
    }
   }
  }

 } 
 
void SellGridLimit()
 {
  for(int x=OrdersTotal()-1;x>=0;x--)
  {
   if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(Bid>=SellGridTrade)
    { 
     int SellLimitTrade=OrderSend(Symbol(),OP_SELLLIMIT,0.01,Bid+PriceLimit*Pips(),0,0,0,"Test",MagicNumber,0,clrDarkCyan); 
     SellGridTrade=Bid+PriceLimit*Pips();
    }
   }
  }
   
 } 

Thank you William