I recently created a new scalping EA. When I say "new," I really mean new because I've strictly been an algorithmic swing trader up until now. Anyway, I found myself in need of a higher resolution time filter because scalping EA's trade more frequently than swing trading EA's.So...
Here is a snippet of my new trading time filter code. You can select one of three forms of time in the TimeForm input:
- SimPCtime ─ simulated real time based on the ticking of the clock in your local device;
- PCtime ─ time based on the arrival of data ticks "shifted" to the time zone of your local device; and
- BrokerTime ─ real time based on your broker-dealer's server time.
The first filtered out time block will auto-detect an overnight timespan (wraparound through midnight), and can use a regular chronological time span if you don't trade overnight. The second and third time blocks only use regular chronological timespans (because there is only one midnight per day). Set the hours and minutes of the timespan during which you want your EA to trade.
A daily filter consisting of one settable day, and a monthly filter consisting of three settable months, have also been added. If you need to edit the code to filter out additional days, it should be fairly self-explanatory. Sunday is 0, Monday is 1... and so on up to Saturday which is 6. If you only need to filter out one or two months, setting any skipped month to 0 will allow that because months are 1 to 12 (there is no month 0). Set the months and day that you do not want your EA to trade.
If your set current time is within an allowed timespan when you first run the code, you may get a message showing that trading is outside of set times immediately followed by a message showing that trading is within set times─printed to the Experts tab of MT5. This is intended. The corresponding Print() statements are inside each timespan's conditions, respectively, for the purpose of tracking what happened when and where.
Of course, you can edit the code to use a "blanket" message in the actual trading conditions instead, but I'd personally rather not be printing strings while trading conditions are being evaluated. If you need more filtered out timespans, you might consider creating a custom function or functions so that you can "recycle" the calculations therein.
In the global scope of your code, insert:
enum SELECTED_TIME { PCtime, SimPCtime, BrokerTime, }; input bool UseTimeFilter = true; input SELECTED_TIME TimeForm = PCtime; input string FridayExitTime = "16:45"; input string StartTimeOvernightA = "01:00"; input string StopTimeOvernightA = "16:00"; input string StartTimeB = "18:00"; input string StopTimeB = "20:00"; input string StartTimeC = "22:00"; input string StopTimeC = "23:00"; input bool SkipDays = true; input int DayA = 0; input bool SkipMonths = false; input int MonthA = 0; input int MonthB = 0; input int MonthC = 0; datetime dLocalTime, dStartTimeOvernightA, dStopTimeOvernightA, dStartTimeB, dStopTimeB, dStartTimeC, dStopTimeC; ulong uLocalTime, uStartTimeOvernightA, uStopTimeOvernightA, uStartTimeB, uStopTimeB, uStartTimeC, uStopTimeC; bool timeRunBotOvernightA, timeRunBotB, timeRunBotC, dayRunBot, monthRunBot; int timerPrinted, dayPrinted, monthPrinted;
In OnInit(), insert:
switch(TimeForm) { case PCtime: dLocalTime = TimeLocal(); break; case SimPCtime: dLocalTime = TimeTradeServer(); break; case BrokerTime: dLocalTime = TimeCurrent(); break; }
In OnTick(), insert:
dStartTimeOvernightA = StringToTime(StartTimeOvernightA); uStartTimeOvernightA = ulong(dStartTimeOvernightA); dStopTimeOvernightA = StringToTime(StopTimeOvernightA); uStopTimeOvernightA = ulong(dStopTimeOvernightA); dStartTimeB = StringToTime(StartTimeB); uStartTimeB = ulong(dStartTimeB); dStopTimeB = StringToTime(StopTimeB); uStopTimeB = ulong(dStopTimeB); dStartTimeC = StringToTime(StartTimeC); uStartTimeC = ulong(dStartTimeC); dStopTimeC = StringToTime(StopTimeC); uStopTimeC = ulong(dStopTimeC); uLocalTime = ulong(dLocalTime); // overnight trading times. Any times including midnight, 00:00, (if any) must be set in overnight times inputs--auto detection supported if(uStartTimeOvernightA < uStopTimeOvernightA) // intraday start trading time is set earlier than intraday stop trading time (linearly) { if(UseTimeFilter == true && timeRunBotB == false && timeRunBotC == false) { if(uLocalTime >= uStartTimeOvernightA && uLocalTime < uStopTimeOvernightA) { timeRunBotOvernightA = true; if(timerPrinted != 1) { Print("Time Filter is ON. Current time is within set trading times A."); timerPrinted = 1; } } else { timeRunBotOvernightA = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times A."); timerPrinted = 2; } } } } if(uStartTimeOvernightA > uStopTimeOvernightA) // intraday start trading time is set later than intraday stop trading time (wrap-around) { if(UseTimeFilter == true && timeRunBotB == false && timeRunBotC == false) { if(uLocalTime >= uStopTimeOvernightA && uLocalTime < uStartTimeOvernightA) { timeRunBotOvernightA = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times A."); timerPrinted = 2; } } else { timeRunBotOvernightA = true; if(timerPrinted != 1) { Print("Time Filter is ON. Current time is within set trading times A."); timerPrinted = 1; } } } } if(UseTimeFilter == false) { timeRunBotOvernightA = true; if(timerPrinted != 3) { Print("Time Filter is OFF."); timerPrinted = 3; } } // trading times B. Trading times set in B times inputs must be linear (not overnight) if(uStartTimeB < uStopTimeB) // intraday start trading time is set earlier than intraday stop trading time (linearly) { if(UseTimeFilter == true && timeRunBotOvernightA == false && timeRunBotC == false) { if(uLocalTime >= uStartTimeB && uLocalTime < uStopTimeB) { timeRunBotB = true; if(timerPrinted != 1) { Print("Time Filter is ON. Current time is within set trading times B."); timerPrinted = 1; } } else { timeRunBotB = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times B."); timerPrinted = 2; } } } } if(UseTimeFilter == false) { timeRunBotB = true; if(timerPrinted != 3) { Print("Time Filter is OFF."); timerPrinted = 3; } } // trading times C. Trading times set in C times inputs must be linear (not overnight) if(uStartTimeC < uStopTimeC) // intraday start trading time is set earlier than intraday stop trading time (linearly) { if(UseTimeFilter == true && timeRunBotOvernightA == false && timeRunBotB == false) { if(uLocalTime >= uStartTimeC && uLocalTime < uStopTimeC) { timeRunBotC = true; if(timerPrinted != 1) { Print("Time Filter is ON. Current time is within set trading times C."); timerPrinted = 1; } } else { timeRunBotC = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times C."); timerPrinted = 2; } } } } if(UseTimeFilter == false) { timeRunBotC = true; if(timerPrinted != 3) { Print("Time Filter is OFF."); timerPrinted = 3; } } datetime tm = dLocalTime; MqlDateTime stm; TimeToStruct(tm, stm); if(SkipDays == true && stm.day_of_week != DayA) { dayRunBot = true; if(dayPrinted != 1) { Print("Daily Filter is ON. Current day is within set trading days."); dayPrinted = 1; } } else { dayRunBot = false; if(dayPrinted != 2) { Print("Daily Filter is ON. Current day is outside of set trading days."); dayPrinted = 2; } } if(SkipDays == false) { dayRunBot = true; if(dayPrinted != 3) { Print("Daily Filter is OFF."); dayPrinted = 3; } } if(SkipMonths == true && (stm.mon != MonthA || MonthA == 0) && (stm.mon != MonthB || MonthB == 0) && (stm.mon != MonthC || MonthC == 0)) { monthRunBot = true; if(monthPrinted != 1) { Print("Monthly Filter is ON. Current month is within set trading months."); monthPrinted = 1; } } if(SkipMonths == true && (stm.mon == MonthA || stm.mon == MonthB || stm.mon == MonthC)) { monthRunBot = false; if(monthPrinted != 2) { Print("Monthly Filter is ON. Current month is outside of set trading months."); monthPrinted = 2; } } if(SkipMonths == false) { monthRunBot = true; if(monthPrinted != 3) { Print("Monthly Filter is OFF."); monthPrinted = 3; } } // and in your EA's trading conditions, insert: if((timeRunBotOvernightA == true || timeRunBotB == true || timeRunBotC == true) && dayRunBot == true && monthRunBot == true) { // your trading conditions/logic here }


![[iVISTscalp5]: A Laboratory for Market Behavior Research Through Time [iVISTscalp5]: A Laboratory for Market Behavior Research Through Time](https://c.mql5.com/6/1005/splash-preview-770124.jpg)