Closing an order an order simultaneously opening an opposite order.

 

I am like super new to programing mql4. 
I am trying to make an EA, that will close say a buy order and simultaneously open a short position. 

I have the following functions

int Long()
   {
      if (OrderType() == OP_SELL)
      CloseShort();
      b_ticket=OrderSend(Symbol(),
                              OP_BUY,
                              LotsOptimized(),
                              Ask,
                              slip,
                              Ask-StopLoss,
                              Ask+TakeProfit,
                              comment,
                              Magic,0,Blue);
                              if(b_ticket>0)   {
                                 if(OrderSelect(b_ticket,SELECT_BY_TICKET,MODE_TRADES))
                                       {   Print(b_ticket);   }
                                 else Print("Error Opening BuyStop Order: ",GetLastError());
                              return(0);}
   }

//+--------------------------------------------------------------+
//|
//+--------------------------------------------------------------+

int Short()
   {
   if (OrderType() == OP_BUY)
     CloseLong();
      s_ticket=OrderSend(Symbol(),
                              OP_SELL,
                              LotsOptimized(),
                              Bid,
                              slip,
                              Bid+StopLoss,
                              Bid-TakeProfit,
                              comment,
                              Magic,0,Red);
                              if(s_ticket>0)   
                              {
                                 if(OrderSelect(s_ticket,SELECT_BY_TICKET,MODE_TRADES))
                                     {   Print(s_ticket);   }
                                 else Print("Error Opening SellStop Order: ",GetLastError());
                              return(0);}
   }
//+-----------------------------+
 //Close Long Positions
    void CloseLong()
                  {
                        int total = OrdersTotal();
                        for (int cnt = 0 ; cnt < total ; cnt++)
                           {
                                 OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
                                 if (OrderMagicNumber() == 0)
                                 if(OrderType()==OP_BUY)
                                 OrderClose(OrderTicket(),OrderLots(),Bid,3, Violet);                                

                           }
                  }
//+--------------------------------+
//Close Short Positions
    void CloseShort()
                  {
                        int total = OrdersTotal();
                        for (int cnt = 0 ; cnt < total ; cnt++)
                           {
                                 OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
                                 if (OrderMagicNumber() == 0)
                                 if(OrderType()==OP_SELL)
                                 OrderClose(OrderTicket(),OrderLots(),Ask,3, Violet);                                

                           }
                  }


then in my Event function I have called it as follows 

int start() //void OnTick() no difference at all
   {
      int total;
  
      total  = OrdersTotal();
        
        if(total < 5) 
      {
        if (temptrend == OP_BUY || tempsignal == OP_BUY) 
         {
            CloseShort();
            Long();
            
         }
        if(temptrend == OP_SELL || tempsignal == OP_SELL)
         {
           CloseLong();
            Short();
          }  
        
        
      } 
   }

I ended up inserting the CloseXXXX functions both in long() and in Short() then again in the Event function. 
But still its not closing. 

if I increase if (total < 5) to (total <9999) i will have both buys and sells open simultaneously. 

What is wrong with my code.

Reason: