Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
The function "TimeHour()" does not exist in MQL5. It's from MQL4. Is this A.I. generated code?
Please, don't request help for ChatGPT (or other A.I.) generated code. It generates horrible invalid code, often mixing MQL4 and MQL5.
To learn MQL5 programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Book and Documentation
If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
Finally, you also have the option to hire a programmer in the Freelance section.
Hello, wondering if anyone could help out...
Inputs:
input group "---------------------------------------"; input group "Turn trading time filter on/off"; input bool UseTimer = true; input group "---------------------------------------"; input group "Use personal computer time to filter? ==> if false, broker time is used"; input bool UsePCtime = true; input group "---------------------------------------"; input group "Set time to enable trading ==> Intraday and overnight are supported"; input string StartTime = "21:00"; input group "---------------------------------------"; input group "Set time to disable trading"; input string StopTime = "16:00"; input group "---------------------------------------";
Variables on global scope:
datetime dLocalTime, dStartTime, dStopTime; ulong uLocalTime, uStartTime, uStopTime,
In OnTick():
dStartTime = StringToTime(StartTime); uStartTime = ulong(dStartTime); dStopTime = StringToTime(StopTime); uStopTime = ulong(dStopTime); if(UsePCtime == true) { dLocalTime = TimeLocal(); } else { dLocalTime = TimeCurrent(); } uLocalTime = ulong(dLocalTime); if(uStartTime < uStopTime) // intraday start trading time is earlier than intraday stop trading time { if(UseTimer == true) { if(uLocalTime >= uStartTime && uLocalTime < uStopTime) { runBot = true; if(timerPrinted != 1) { Print("Timer is ON. Current time is within set trading times. Bot is ON."); timerPrinted = 1; } } else { runBot = false; if(timerPrinted != 2) { Print("Timer is ON. Current time is outside of set trading times. Bot is OFF."); CancelOrder(); timerPrinted = 2; } } } } if(uStartTime > uStopTime) // intraday start trading time is later than intraday stop trading time { if(UseTimer == true) { if(uLocalTime >= uStopTime && uLocalTime < uStartTime) { runBot = false; if(timerPrinted != 2) { Print("Timer is ON. Current time is outside of set trading times. Bot is OFF."); timerPrinted = 2; } } else { runBot = true; if(timerPrinted != 1) { Print("Timer is ON. Current time is within set trading times. Bot is ON."); timerPrinted = 1; } } } } if(UseTimer == false) { runBot = true; if(timerPrinted != 3) { Print("Timer is OFF. Bot is ON."); timerPrinted = 3; } }
And then put your trading code inside:
if(runBot == true) { //all of your trading code }(I prefer to leave trade exits code outside of the time filter).
Nice , but i'd use TimeTradeServer() instead of TimeCurrent() - if i remember correctly time current is for the last tick
Thanks. If you set UsePCtime = true, then TimeLocal() is used.
I see that TimeTradeServer() is simulated on the local machine based on TimeCurrent(). That's clear as mud to me. How exactly does TimeTradeServer() differ from TimeLocal()... other than the additional time stucture required?
Thanks. If you set UsePCtime = true, then TimeLocal() is used.
I see that TimeTradeServer() is simulated on the local machine based on TimeCurrent(). That's clear as mud to me. How exactly does TimeTradeServer() differ from TimeLocal()... other than the additional time stucture required?
The offset of the server and the offset of the machine i guess.
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 an expanded snippet of the trading time filter code above in Post #3. Based on the idea of Lorentzos Roussos in Post #4, TimeTraderServer() has been added to the inputs as TimeForm==>SimPCtime.
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.
The first filtered out time block will auto-detect and overnight timespan (wraparound through midnight like the code in Post #3). The next two time blocks are strictly linear─chronological. 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 Saturdays 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.
//globals enum SELECTED_TIME { PCtime, SimPCtime, BrokerTime, }; //inputs input bool UseTimeFilter = true; input SELECTED_TIME TimeForm = PCtime; input string StartTimeOvernightA = "23:00"; input string StopTimeOvernightA = "00:00"; input string StartTimeB = "02:00"; input string StopTimeB = "17:00"; input string StartTimeC = "18:00"; input string StopTimeC = "20:00"; input bool SkipDays = true; input int DayA = 0; input bool SkipMonths = true; input int MonthA = 2; input int MonthB = 11; input int MonthC = 12; //globals 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; //OnInit switch(TimeForm) { case PCtime: dLocalTime = TimeLocal(); break; case SimPCtime: dLocalTime = TimeTradeServer(); break; case BrokerTime: dLocalTime = TimeCurrent(); break; } //OnTick 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. Bot is ON."); timerPrinted = 1; } } else { timeRunBotOvernightA = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times A. Bot is OFF."); 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. Bot is OFF."); timerPrinted = 2; } } else { timeRunBotOvernightA = true; if(timerPrinted != 1) { Print("Time Filter is ON. Current time is within set trading times A. Bot is ON."); timerPrinted = 1; } } } } if(UseTimeFilter == false) { timeRunBotOvernightA = true; if(timerPrinted != 3) { Print("Time Filter is OFF. Bot is ON."); 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. Bot is ON."); timerPrinted = 1; } } else { timeRunBotB = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times B. Bot is OFF."); timerPrinted = 2; } } } } if(UseTimeFilter == false) { timeRunBotB = true; if(timerPrinted != 3) { Print("Time Filter is OFF. Bot is ON."); 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. Bot is ON."); timerPrinted = 1; } } else { timeRunBotC = false; if(timerPrinted != 2) { Print("Time Filter is ON. Current time is outside of set trading times C. Bot is OFF."); timerPrinted = 2; } } } } if(UseTimeFilter == false) { timeRunBotC = true; if(timerPrinted != 3) { Print("Time Filter is OFF. Bot is ON."); 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; } } //--- trading conditions if((timeRunBotOvernightA == true || timeRunBotB == true || timeRunBotC == true) && dayRunBot == true && monthRunBot == true) { // your other trading conditions }
Critical update: There is a bug in my code posted in my Post #8, above. Time forms are defined inside OnInit() which causes them not to update in real time.
My corrected code is posted at:
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, wondering if anyone could help out, im trying to code my EA in Mql5 but i get this error, Any help on how to resolve this.
Thank you in Advance
Note: The starthour and endhour are already defined
Part of the Code concerning time...
Erro Returned...
...