how to fix the issue
code in Green is what goes near your Order Information.
i am posting this incase anyone else wants to add a Time Filter to their EA in MQL5
void OnTick(){ MqlDateTime structTime; TimeCurrent(structTime); // Set the start time structTime.hour = TimeStartHour; structTime.min = TimeStartMin; datetime timeStart = StructToTime(structTime); // Set the end time structTime.hour = TimeEndHour; structTime.min = TimeEndMin; datetime timeEnd = StructToTime(structTime); // Check if TimeEndHour is on the next day if (TimeEndHour < TimeStartHour) { structTime.day++; timeEnd = StructToTime(structTime); } // Check if the current time is within the specified range bool isTime = TimeCurrent() >= timeStart && TimeCurrent() < timeEnd; // Check if it's past midnight if (TimeCurrent() >= timeEnd && TimeCurrent() < timeStart) { // Increment the day by 1 structTime.day++; // Set the start time to the next day structTime.hour = TimeStartHour; structTime.min = TimeStartMin; timeStart = StructToTime(structTime); // Set the end time to the next day structTime.hour = TimeEndHour; structTime.min = TimeEndMin; timeEnd = StructToTime(structTime); } // Check if the current time is within the specified range on the next day if (isTime == false && TimeCurrent() >= timeStart && TimeCurrent() < timeEnd) { isTime = true; } // Execute the desired code if the current time is within the specified range if (isTime) { // Your code here } }Code in yellow is what was missing now when it checks the time it knows to move it to the next day
code in Green is what goes near your Order Information.
i am posting this incase anyone else wants to add a Time Filter to their EA in MQL5
The issue I am having with now is the midnight function doesn’t work. When midnight happens it changes the start time even if I remove the section of code where it says to change the start time.
Ricks creations #: The issue I am having with now is the midnight function doesn’t work. When midnight happens it changes the start time even if I remove the section of code where it says to change the start time.
Your issue is that you are converting clock time to datetime for comparison with TimeCurrent(), thus you have to handle midnight. Just use clock time.
Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)
Not tested, not compiled, just typed.
void OnTick(){ int now = time(); int start = TimeStartHour * 3600 + TimeStartMin *60; int end = TimeEndHour * 3600 + TimeEndtMin *60; bool isValidTime = start < end ? start <= now && now < end : start <= now || now < end;
Not tested, not compiled, just typed.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
that the time it actually will start will be 21:00 and then go through midnight and the time it will end will be the next day at. 9am . (SERVER TIME )
and to check if its outputting correctly i do have a Print comment statement here is the clip of it below. but when i set it to the example variables i am talking about above. as the start time being 21 and then end time being 9
i notice. that on the print variable it is showing the same day and not rolling over to the next day.
should the updated code look like the following below using day_of_week from StructTime