OrderClose() not closing at specific time

Back to topics list To post a new topic, please log in or register
avatar
120
DeanDeV 2015.05.26 09:01
 

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. 

avatar
Moderator
3406
GumRai 2015.05.26 09:27 #
 
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? 

avatar
120
DeanDeV 2015.05.26 09:53 #
 
GumRai:

 What function?

Where is this placed in your code? 

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

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

avatar
12985
WHRoeder 2015.05.26 16:15 #
 
  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)

avatar
65
2cent 2015.05.26 16:34 #
 
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.


avatar
120
DeanDeV 2015.05.26 22:45 #
 
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? 

avatar
120
DeanDeV 2015.05.26 22:46 #
 
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...?
avatar
65
2cent 2015.05.26 23:40 #
 
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.

 

avatar
120
DeanDeV 2015.05.26 23:52 #
 
2cent:

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

 

Still not working? :( 
avatar
Moderator
3406
GumRai 2015.05.27 01:21 #
 
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
avatar
120
DeanDeV 2015.05.27 04:57 #
 
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(). 

Back to topics list  

To add comments, please log in or register