Why my orders not closing at this specific time ?

 

My orders not closing at 5 o'clock , why ?

//+------------------------------------------------------------------+
//|                                                     LETS TRY.mq4 |
//|                                                          LETSTRY |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "LETSTRY"
#property link      "http://www.metaquotes.net"
int total, i;



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()

  {
 int cnt, ticket, total;
   int ct;
   
total=OrdersTotal();
if(total>1) return(0);



{
if(DayOfWeek()==1 || DayOfWeek()==0) return(0);
{


if(Hour()== 00 && Minute()==00 && IsTradeAllowed()==true) 

 {
 ticket=OrderSend(Symbol(),OP_BUY,1,Ask,0,0,0); 
 

OrderModify(ticket,OrderOpenPrice(),Bid-99999*Point,Ask+300*Point,0,Blue);

 }
}
}



if(DayOfWeek()==1 || DayOfWeek()==0) return(0);
{


if(Hour()== 00 && Minute()==00 && IsTradeAllowed()==true) 

 {
 ticket=OrderSend(Symbol(),OP_SELL,1,Bid,0,0,0);
 
OrderModify(ticket,OrderOpenPrice(),Ask+99999*Point,Bid-300*Point,0,Blue);

 
}
}





if(Hour()==5 && Minute()==00)

    // while there are open orders...
    while (OrdersTotal() > 0) {
        // iterate the orders
        for (int i=OrdersTotal()-1;i>=0;i--) {
            // select the order
            OrderSelect(i,SELECT_BY_POS);
            // close or kill depending on order type
            Print("Closing order ", OrderTicket());
            switch(OrderType()) {
                case OP_BUY:  OrderClose(OrderTicket(),OrderLots(),Bid,100); break;
                case OP_SELL: OrderClose(OrderTicket(),OrderLots(),Ask,100); break;
                default:      OrderDelete(OrderTicket());
            }    
        }
    
}


 return(1);
  }
  


 

Thanks 

 
Leopardos:

My orders not closing at 5 o'clock , why ?


If you have more than one Order open . . . 

total=OrdersTotal();
if(total>1) return(0);

 return(0)  exits from start()

 

Any idea how to make it close on time preventing multiple trades in the same second? without it I get 20 trades in the same second when 00:00 hit ...

 

I would appreciate your help .. Thanks 

 
Leopardos:

Any idea how to make it close on time preventing multiple trades in the same second? without it I get 20 trades in the same second when 00:00 hit ...

Count the number of Orders that the EA has placed,  you can even check the time they were placed and use this information to decide if you want to place another Order before you actually place it. 
Reason: