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.
- v88: I assume it could be a timezone issue
Your start and stop times must be in your broker's timezone.
- 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 }
-
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)
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
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 } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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