I want your support with Expert coding (MT4)

 

This my first expert to code . The expert is open trade based on signal . The signal work correctly . But  I want to add section called Monitoring Section for this expert . 


The function of this code is opening Pending Stop orders in opposite direction for the last open trade . IF the pend order become market order then the expert will do the same as before . Means will create Pending Stop order in opposite  direction .

The Distance of the Pending order will be same distance of Take Profit but in opposite direction . The lot =2XLot(0) ( user input ) . Take Profit  will be the same for all  . Note That the lot size is same for all pending order which is Double of the first order created by the signal .

The Monitoring will stop at 2 condition Only . IF ( Hour()>22 ( user input ) OR any of the running trade Hit Take Profit ) Then close all running orders and delete all Pending orders and wait until new day come means Hour>=00:00

This is the section As I try to code . Thank you in advanced 

/*=================================================================================================================================*/ 
/*Create the Function that check the last open alive trade to create pending order in oppisite direction */
int lastOpenTime = 0, needleTicket = 0;
   
  int OpenBuyOrders=0;
  int OpenSellOrders=0;
  int totalPend=0;

    total=OrdersTotal();
   if(total>1)// if no trade existes then not open any pending order 
     {
            //Count Pending Stop Orders
            for(int j=0;j<OrdersTotal(); j++ )
            {
                  if(OrderSelect(j, SELECT_BY_POS)==true)
                  {
                  if (OrderType()==OP_BUYSTOP)
                  OpenBuyOrders++;
                  if (OrderType()==OP_SELLSTOP)
                  OpenSellOrders++; 
                  }
            totalPend=OpenBuyOrders + OpenSellOrders; 
            }
           // Comment ("buy_stop=",OpenBuyOrders," sell_stop=",OpenSellOrders," total=",total);
            
   if(totalPend<1 )
 {
   for(int i = (OrdersTotal()-1); i >= 0; i --)
   {
      ticket=OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        if(ticket<0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("Error opening SELL order : ",GetLastError());
           }     
         
      int curOpenTime = OrderOpenTime();
      
      if(curOpenTime > lastOpenTime)
      {
         lastOpenTime = curOpenTime;
         needleTicket = OrderTicket();
      }
         
   }
   Print("NeedleTicket : ",needleTicket);
 if(OrderSelect(needleTicket,SELECT_BY_TICKET,MODE_TRADES)==True)
 {
     
      if(OrderType()==OP_BUY) // long position is opened
       {
        ticket=OrderSend(Symbol(),OP_SELLSTOP,2*Lots,OrderOpenPrice()-TakeProfit*Point,3,0,OrderOpenPrice()-2.2*TakeProfit*Point,"ATR Hyper",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
             //  Print("SELL order opened : ",OrderOpenPrice());
               Print("Ticket Number : ",ticket);
           }
         else
            Print("Error opening SELL Pending order : ",GetLastError());
        }
        
      if(OrderType()==OP_SELL) // long position is opened
       {
        ticket=OrderSend(Symbol(),OP_BUYSTOP,2*Lots,OrderOpenPrice()+TakeProfit*Point,3,0,OrderOpenPrice()+2.2*TakeProfit*Point,"ATR Hyper",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("BUY  order opened : ",OrderOpenPrice());
           }
         else
            Print("Error opening BUY Pending order : ",GetLastError());
       
      }
  } 
  }
   }// end of Total order first brackets 
/*=================================================================================================================================*/