Help to correct my ordersend in sequence

 
double ordertotalb_0()    //countung buy orders
{
   double totalb=0;                         
   int i;                                
   for(i=OrdersTotal()-1; i>=0; i--)                                      
   {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       {
         if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
         {
         if(OrderMagicNumber()==MagicNumRevB)
         {           
         totalb+=OrderLots();
         }
         }
       }                         
   }
   return(totalb);                                                              
}

double ordertotals_0()    //countung sell orders
{
   double totals=0;                         
   int i;                                
   for(i=OrdersTotal()-1; i>=0; i--)                                      
   {
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
       {
         if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
         {
         if(OrderMagicNumber()==MagicNumRevS)
         {            
         totals+=OrderLots();
         }
         }
       }                         
   }
   return(totals);                                                              
}

double ordertotal_0=(ordertotalb_0()+ordertotals_0());

//----------
int reverse_0()
{
   if(ordertotal_0==0 && ordertotal_1==0 && Reverse==true)      //ordertotal_0 & ordertotal_1 are total lots of orders from this EA
   {
      if(getsignal_sar()=="buy")        //check signal 1 to buy
      {
         if(getsignal_rsi()=="buy")     //check signal 2 to buy
         {
            int i;
            for(i=0; i<TotalRevStart; i++)	//set totalrevstart=5      
            {
            OrderSend(Symbol(),OP_BUY,StartLot,Ask,2,0,0,"revbuy",MagicNumRevB,0,Blue);
            }
         }
      }
      
      if(getsignal_sar()=="sell")       //check signal 1 to sell
      {
          if(getsignal_rsi()=="sell")   //check signal 2 to sell
          {
             for(i=0; i<TotalRevStart; i++)
             {
             OrderSend(Symbol(),OP_SELL,StartLot,Bid,2,0,0,"revsell",MagicNumRevS,0,Red);
             }
          }
      }
   }
}

Hi all,

I want to write my own EA, but I encounter a problem while placing order. Is anyone could help? Here are my ideal processes as following

1. Check that there is no EA pending order on market, through ordertotal_0 and ordertotal_1 to confirm there is zero lots from this EA. 

2. Check signal, if signal_sar and signal_rsi are buy, then go to next step. If signals are sell, then also go next step.

3. After signal check, place multiple orders in the same time through loop command for(...). According to my setting, it would only place five sell orders or buy orders.

4. After that, because this processes would check orders from EA, if there is any pending order from EA, it won't place any order anymore.


BUT, while I try on my demo account, this EA keep placing the orders over than five, which is the target quantity I set. Is anything wrong in my code?

 

Is anyone can give me a hint?

I just wonder what kind of difference between my ideal mindset?


Many thanks for who could give me a help