OrderClose() not closing at specific time

 

Hi gurus,

I seem to be having a problem with my OrderClose() function. I want to close all orders at a specific time. I used the code from RaptorUK's thread:  https://forum.mql4.com/48352

When it reaches the specific time it doesn't run the function and does not close orders; any ideas? Below is the code:

if(Hour()==23 && Minute()>=0)
  {
   int PositionIndex;    
   int TotalNumberOfOrders;   
   TotalNumberOfOrders = OrdersTotal();    
   
   for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
     if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue; 
   
     if( OrderMagicNumber() == MagicNumber      
      && OrderSymbol() == Symbol()         
      && ( OrderType() == OP_BUY        
      ||   OrderType() == OP_SELL ) )   
   
      if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 5 ) )          
         Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() ); 
      
   }
}

 There are no values printed in tester.

Thanks in advance. 

 
DeanDeV:

Hi gurus,

I seem to be having a problem with my OrderClose() function. I want to close all orders at a specific time. I used the code from RaptorUK's thread:  https://forum.mql4.com/48352

When it reaches the specific time it doesn't run the function and does not close orders; any ideas? Below is the code:

 There are no values printed in tester.

Thanks in advance. 

 What function?

Where is this placed in your code? 

 
GumRai:

 What function?

Where is this placed in your code? 

 if(Hour()==23 && Minute()>=0)

It is placed in the Start() function...? 

 
  1. If you placed a print statement before the if, you would know whether the code is even being called.
  2. If the EA opens multiple orders, you need a RefreshRates before using OrderClosePrice.
  3. When can minute ever be negative?
     if(Hour()==23 && Minute()>=0)

 
DeanDeV:

It is placed in the Start() function...? 

If you have a #property strict in the code the start() function will not get called on the tick.

 
WHRoeder:
  1. If you placed a print statement before the if, you would know whether the code is even being called.
  2. If the EA opens multiple orders, you need a RefreshRates before using OrderClosePrice.
  3. When can minute ever be negative?

I used this because I want it to check from the first second of the first minute of the hour for a tick.

if(Hour()==23 && Minute()>=0)

Added RefreshRates(); thanks!

There seems to be a problem with the above time if() statement. It is not calling it at all. If I remove the time code it closes all orders. Is there another way to represent this time as something else? 

 
2cent:

If you have a #property strict in the code the start() function will not get called on the tick.


I have other code that gets called within the Start() function...?
 
DeanDeV:

There seems to be a problem with the above time if() statement. It is not calling it at all. If I remove the time code it closes all orders. Is there another way to represent this time as something else? 

Yes, use if(TimeHour(TimeCurrent())==23) instead.

 
2cent:

Yes, use if(TimeHour(TimeCurrent())==23) instead.

 

Still not working? :( 
 
DeanDeV:
Still not working? :( 
Probably because you have a return(0) before the code can be executed or whatever symbol you are using stops trading before 23:00 hours
 
GumRai:
Probably because you have a return(0) before the code can be executed or whatever symbol you are using stops trading before 23:00 hours

Thank you good sir!

I had a return(1) in my OrderDelete() function that was called before OrderClose(). I now have just added expiration time to all pending orders which would have been deleted by the OrderDelete(). 

Reason: