need help for coding a time window ! - page 2 12 New comment Peter Williams 2022.11.13 16:11 #11 Nikolay Georgiev #: Paul or Bir The code below would be suitable for MT5 (just decided to include this in my EA). Obviously comment out or delete from string msg through to MessageBox input bool TimeControl = true; // Filter time of traiding (true/false) input string TradingStart = "08:00"; // Start Trade input string TradingEnd = "22:00"; // End Trade int StartHours; int StartMinutes; int EndHours ; int EndMinutes; bool allowOpen; MqlDateTime stm; void OnInit() { TradingHoursCheck(); } void OnTick() { TradingHoursCheck(); if(!TimeControl) allowOpen=true; } void TradingHoursCheck() { allowOpen = false; TimeToStruct(TimeCurrent(),stm); StartHours = (int) StringToInteger(StringSubstr(TradingStart,0,2)); StartMinutes = (int) StringToInteger(StringSubstr(TradingStart,3,2)); EndHours = (int) StringToInteger(StringSubstr(TradingEnd,0,2)); EndMinutes = (int) StringToInteger(StringSubstr(TradingEnd,3,2)); if ( ( stm.hour > StartHours || ( stm.hour == StartHours && stm.min >= StartMinutes )) && ( stm.hour < EndHours || ( stm.hour == EndHours && stm.min <= EndMinutes ))) allowOpen=true; string msg = "TradingStart = "+TradingStart+" to TradingEnd = "+TradingEnd; msg = msg + "\nStartHours = "+IntegerToString(StartHours)+" StartMinutes = "+IntegerToString(StartMinutes); msg = msg + "\nEndHours = "+IntegerToString(EndHours)+" EndMinutes = "+IntegerToString(EndMinutes); msg = msg + "\nstm.hour = "+IntegerToString(stm.hour)+" stm.min = "+IntegerToString(stm.min); msg = msg + "\nallowOpen = "+allowOpen; MessageBox(msg); if(!TimeControl) allowOpen=true; } William Roeder 2022.11.13 17:08 #12 input string TradingStart = "08:00"; When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and fraction can be inputs. See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01) Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08) MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26) I need help with Timecurrent issue with my How to get data Peter Williams 2022.11.17 17:29 #13 Many thanks, I normally use int,ulong double but hadn't realised the optimisation implication 12 New comment
The code below would be suitable for MT5 (just decided to include this in my EA). Obviously comment out or delete from string msg through to MessageBox
When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and fraction can be inputs.
See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26)
Many thanks,
I normally use int,ulong double but hadn't realised the optimisation implication