Trading within certain time period problems

 

Hi

I have some code that trades between a start hour and an end hour, eg 19:00 to 01:00 (Code as follows).

string s=(string)start+":00";
string e=(string)end+":00";

if (TimeCurrent()>=StringToTime(s) || TimeCurrent()<StringToTime(e))
{
        //Execute trade

}

else if (TimeCurrent()>=StringToTime(s) && TimeCurrent()<StringToTime(e))
{
        //Execute trade
}  
 

The only problem is that I see MT5 execute trades beyond the specified trading hours? I assume it could be a timezone issue but i've set my VPS to the broker timezone, so I'm not sure whats up? Broker timezone is GMT+3.

Any ideas?


Thanks

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
v88:

Hi

I have some code that trades between a start hour and an end hour, eg 19:00 to 01:00 (Code as follows).

The only problem is that I see MT5 execute trades beyond the specified trading hours? I assume it could be a timezone issue but i've set my VPS to the broker timezone, so I'm not sure whats up? Broker timezone is GMT+3.

Any ideas?


Thanks

You might like to try TimeLocal() which will read the workstation time (which I assume you are using) - TimeCurrent() gets the server time and this could be the reason you are having problems.

I have not used StringToTime() - I found TimeToStruc() really good for working with times. It is numeric so you can do exact comparisons of hrs, min, secs, etc - and it has worked well for me when setting up time dependent operations.

 
  1. v88: I assume it could be a timezone issue

    Your start and stop times must be in your broker's timezone.

  2. v88: I have some code that trades between a start hour and an end hour, eg 19:00 to 01:00
    if (TimeCurrent()>=StringToTime(s) || TimeCurrent()<StringToTime(e))

    Greater 19 or less than 01 is only valid if end is less than start. Otherwise, you have included all times. Code it correctly

    string s=(string)start+":00";
    string e=(string)end+":00";
    
    datetime begin = StringToTime(s), stop=StringToTime(e), now=TimeCurrent();
    bool isTime = stop <= begin ? now >= begin || now < stop : now >= begin && now < stop;
    if(isTime)
    {
            //Execute trade
    }  
    
  3. In addition, you can speed the code by dropping your int to string to time and increase the exactness of your range to seconds instead of hours:

    SECONDS begin=start*3600, stop=end*3600, tod=time();
    
              date/time (2017)
              Find bar of the same time one day ago - MQL4 programming forum (2017)

-
 

Thanks both for your replies.


William Roeder #:
  1. Your start and stop times must be in your broker's timezone.

On this point - just wanted to confirm whether the strategy tester in MT5 would take the Brokers timezone or would it take the local machine time?


Thanks.
 
v88 #: On this point - just wanted to confirm whether the strategy tester in MT5 would take the Brokers timezone or would it take the local machine time?

Chart times are broker times.

During testing in the strategy tester, TimeLocal() is always equal to TimeCurrent() simulated server time.
          TimeLocal - Date and Time - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

 
William Roeder #:

Chart times are broker times.

Thanks. So I took some time to test the code and see whether it trades correctly.


So I changed my code to the below based on your reply, but it's still trading at the wrong time? Any ideas?

   string s=(string)start+":00";
   string e=(string)finish+":00";
   
   datetime begin = StringToTime(s);
   datetime end = StringToTime(e);
   datetime now = TimeCurrent();
   

  if (finish <= start) {
         if (now >= begin || now < end)
         {
                //trade
                
         }   
  }
  else if(finish > start) {
         if (now >= begin && now < end)
         {
                //trade
         }      
  }
Reason: