Problems with Time()

 
Hello MQL4 Community!

This is my first post. First, please not that I am not an MQL developer. I am a Trader, who uses a tool for helping me build EAs. That tool allows the use of some MQL structure, but it does not execute on explicit MQL functions. It simply takes MQL code, evaluates whether the MQL based condition is true/false, and then executes a custom function such as OpenBuy, OpenSell, CloseBuy, CloseSell, DeletePendingBuy, DeletePendingSell, etc. But, it does not run MQL code explicitly. For example, you can use the Print or Comment function to get something to appear on the chart window of MT4 (just one example).

So, I know a tiny bit about MQL, which is just enough to enable me to use the EA development tool that I use to build my EAs. Thus far, I have built 10 EAs using the tool and have abandoned 7 of them due to non-profitability, which is about 30% better than my successful prototype ratio that I used to get with building trading systems in Excel. I typically expect that 7-8 ideas out of 10 will not be as profitable as expected, leaving 2-3 designs profitable enough to begin the optimization process. Ok, enough about me and how I roll.

Here's my problem and I hope that I came to the right place for help!

I'm running this code [b]to close trades:[/b]

Day() == 1 || Day() == 2 || Day() == 3 || Day() == 4 && TimeHour(TimeCurrent()) >=23 && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57

Note: The problem is that all trades remain open Monday through Thursday, through 23:57. Also, all trades remain open on Friday, through 21:57.


I'm also running this code through an MQL block [b]to open trades:[/b]

TimeHour(TimeCurrent()) == 00 && TimeMinute(TimeCurrent()) <= 05

Note: The only trade that opens between 00:00 and 00:05, is the very first trade of the back-test, when clicking on the Tester Start button. That trade opens precisely at 00:00. However, none of the other trades open between 00:00 and 00:05, later in the week.

In summary:

- One trade opens after clicking on the Tester Start button at 00:00.
- No trade is ever subsequently closed at 23:57 (M-Thu), or 21:57 (Fri).
- No trade is ever then subsequently opened between 00:00 through 00:05.

All times shown above have ample available tick flow. So, there were no gaps in the data being used by Tester.

Any help would be appreciated. Why are these trades failing to close at the coded time? Why are no trades being executed when there is always a trade signal available between 00:00 through 00:05, Monday through Friday?

Thank you!
cfx
 

What debugging Print statements have you added to your code to find out what is and isn't being executed ? for example, is the OrderClose() failing or is it the code that leads to the OrderClose() ? do you take notice of the return values and Print any relevant errors ?

What are Function return values ? How do I use them ?

Does Day() work correctly in the Strategy Tester ? have to tested that it works ? you can use TimeDayOfWeek() instead . .

 

>I'm running this code [b]to close trades:[/b]

Day() == 1 || Day() == 2 || Day() == 3 || Day() == 4 && TimeHour(TimeCurrent()) >=23 && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57

>Note: The problem is that all trades remain open Monday through Thursday, through 23:57. Also, all trades remain open on Friday, through 21:57.

This is a very common mistake with beginners; trying to fit the entire logical expression on one line. It is impossible to debug.

The trick is to break the test down into little pieces and check each bit (with Print statements or otherwise).

Can you get it to close after 23:57 on any day? There is no need to worry about days of the week with that test. Once you get one test working correctly you can get more adventurous. But use several lines where possible to allow Print statements in between for debugging. When it works remove the Print statements but leave the code on separate lines. The code is actually more efficient when written on many lines (runs faster).

 
  1. Day() == 1 || Day() == 2 || Day() == 3 || Day() == 4 && TimeHour(TimeCurrent()) >=23 && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57
    which of these did you mean?
    Day() == 1 || Day() == 2 || Day() == 3 || (Day() == 4 && TimeHour(TimeCurrent()) >=23) && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57
    (Day() == 1 || Day() == 2 || Day() == 3 || Day() == 4) && TimeHour(TimeCurrent()) >=23 && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57
    Always parentheses completely or simplify and self document
    #define HR2157 79020 // 21 * 3600 + 57 * 60
    #define HR2357 86220
    datetime now = TimeCurrent();
    int      tod = now % 86400;
    bool     isFriday = TimeDayOfWeek(now) == 5;
    if (isFriday) int todClose = HR215700;
    else              todClose = HR235700
    if (now >= todClose) ...
    

  2. https://www.mql5.com/en/forum/127483 reported DayOfWeek() always returns 5 in the tester, so I only use Timexxx() versions.
 
WHRoeder:
  1. which of these did you mean?
The OP obviously means to OR all the weekdays except Friday. Clearly he is trying to force a close before the end of the trading day, but with Friday being shorter. He has failed to realise that the test of using the OR of the four days is redundant since we can always close if the time is after 23:57. Friday doesn't need to be excluded!
 
RaptorUK:

What debugging Print statements have you added to your code to find out what is and isn't being executed ? for example, is the OrderClose() failing or is it the code that leads to the OrderClose() ? do you take notice of the return values and Print any relevant errors ?

What are Function return values ? How do I use them ?

Does Day() work correctly in the Strategy Tester ? have to tested that it works ? you can use TimeDayOfWeek() instead . .


I've used no Print statements for such a purpose. I made a typo in my original post. The tool that I'm using [b]cannot[/b] (I originally typed "can") use MQL functions such as Print, or Comment. So, I apologize for the typo, which impacted your reply.

However, I can easily look at the Tester Journal and see that the EA is not preparing any trades when it should (at 00:00), and it is not instantiating the Close of any trades when it should (Mon - Thurs at 23:59, or Fri at 21:57). Essentially, I can see the EA loading at Tester run-time, the obligatory information about the Demo Account, and finally, I can see that all Time Frames for the indicator(s) used by the EA are being loaded properly and without error. I typically don't get errors of any kind with my EAs, whatsoever. And, I routinely test Multi-Time Frame EAs all the time.

I only seem to be having a problem with the Time() function.

Since the only position that ever gets opened, is the very first trade that the EA sees AFTER clicking on the Tester Start button, I can't tell you whether or not Day() is working or not from output that comes from Print, or Comment. However, as a random test, I removed the line [b]TimeMinute(TimeCurrent()) <= 05[/b] from the code segment that controls the Opening of Positions at 00:00, Mon - Fri. When I did that, the following code started working as designed:

Day() == 1 || Day() == 2 || Day() == 3 || Day() == 4 && TimeHour(TimeCurrent()) >=23 && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57

This tells me that in fact, Day() is indeed working by deduction. In fact, that entire segment of code that Closes positions, does work. But, some reason, not when [b]TimeMinute(TimeCurrent()) <= 05[/b] is used in the segment of code that controls the Opening of positions. That irony makes no sense to me - I can't figure out why this would be the case.

Thanks!

cfx

 
dabbler:

>I'm running this code [b]to close trades:[/b]

>Note: The problem is that all trades remain open Monday through Thursday, through 23:57. Also, all trades remain open on Friday, through 21:57.

This is a very common mistake with beginners; trying to fit the entire logical expression on one line. It is impossible to debug.

The trick is to break the test down into little pieces and check each bit (with Print statements or otherwise).

Can you get it to close after 23:57 on any day? There is no need to worry about days of the week with that test. Once you get one test working correctly you can get more adventurous. But use several lines where possible to allow Print statements in between for debugging. When it works remove the Print statements but leave the code on separate lines. The code is actually more efficient when written on many lines (runs faster).


Thanks to everyone for the replies, by the way!


The Close Control works. When I removed the [b]TimeMinute(TimeCurrent()) <= 05[/b] from the Open Control, it does indeed work. The very first position that got executed AFTER clicking on the Tester Start button, does indeed get closed at 23:57 (Mon-Thurs). The problem then becomes that fact that no new positions are ever opened at 00:00, as the Open Control requires by design ([b]TimeHour(TimeCurrent()) == 00 && TimeMinute(TimeCurrent()) <= 05[/b]. Even when I remove the [b]TimeMinute(TimeCurrent() <=5[/b] function as just a sanity check.

I thought making the code as compact as possible, would be the best alternative, but I'll try expanding each function, segment, component, etc., to see if that helps.


Thanks!

cfx

 
WHRoeder:
  1. which of these did you mean?
    Always parentheses completely or simplify and self document
  2. https://www.mql5.com/en/forum/127483 reported DayOfWeek() always returns 5 in the tester, so I only use Timexxx() versions.

I meant:

(Day() == 1 || Day() == 2 || Day() == 3 || Day() == 4) && TimeHour(TimeCurrent()) >=23 && TimeMinute(TimeCurrent()) >=57 || Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57


There seems to be a parenthesis missing from this code segment. If you can correct it for me, I'd appreciate it. The tool I use let's me know when a paren is missing, but it does not tell me where.


Thanks!

 
dabbler:
The OP obviously means to OR all the weekdays except Friday. Clearly he is trying to force a close before the end of the trading day, but with Friday being shorter. He has failed to realise that the test of using the OR of the four days is redundant since we can always close if the time is after 23:57. Friday doesn't need to be excluded!

I have to isolate Friday, else there will be zero distinction between Monday - Thursday closing times and that of Friday's closing time. Both closing times are distinguished by the Day of Week they occur and not merely the Time of Day they occur.

Thus, I need to close Monday - Thursday at 23:57, and Friday at 21:57, respectively. If I include Friday's close with Monday - Thursday, then all trades will be closed at the earliest time seen by the EA, which will be 21:57, a full two hours before the required closing times of Monday through Thursday.

Please clarify your statement. Thanks!

cfx

 

LOL!


I'm starting to think that a majority of the "Time" based functions in MT4 are seriously flawed.


I mean, if I reset all of my position control logic to something as mundane as: TimeHour(TimeCurrent()) == 23 && TimeHour(TimeCurrent()) == 59, and not be able to get a solitary trade closed at all (the EA simply ignores the code!), then I would say that MT4 has some issues that need to be corrected with respect to these types of functions.


I also ran through a series of tests using the following: DayOfWeek() != 0 && DayOfWeek() !=, and not a single trade would open Monday through Friday! In fact, not a solitary trade opened at all. But, what do you think happened to the EA when I completely removed all Time based functions? Of course, it runs as designed without error. I'm not an MQL developer, but I'm really good at developing logical constructs and I can detect a systemic problem when I sense one. I sense that MetaQuotes has a problem with their Time() functions, period. I can't be this clueless on something so easily coded and I've seen other people that do code in MQL, have difficulty with Time based functions in MT4.

I don't exactly know what the problem is, but when remove TimeMinute(TimeCurrent()) <= 05, from the Opening Control logic (that piece of code that controls the opening of all trades), and I get ONLY the first trade to close at the correct time, yet ALL OTHER trades driven by the exact same Opening Control logic are summarily ignored by the EA subsequently, then I know that something is systemically wrong with the function itself.

The code can't be more simple! I'm instructing the EA to open the damn trade between 00:00 and 00:05. That is not a difficult thing to code. A six year old could code that. Here it is yet again: [b]TimeHour(TimeCurrent()) == 00 && TimeHour(TimeCurrent()) <= 05[/b].


There. I just coded it. I then drop that code into the tool I'm using and the tool tells me that the Code's Syntax is Correct! Ok, now what? Well, I compile the damn .ex4 file for the MILLIONTH time and run the M4 Tester. Ok, now what? Well, the very FIRST trade launches at 00:00. Awesome, right? NOOOOOO! Why? Because at 23:59 on a Monday, the damn position is STILL open. Then what? Well, at 00:00 of the very next trading session (the next D1 bar), good ole [b]TimeHour(TimeCurrent()) == 00 && TimeHour(TimeCurrent()) <= 05[/b] will pull through where the Closing Control failed, right? Wrong! Why? Because, at 00:00 through 00:05, no freaking trade is ever opened by the EA!!!

I spent two (2) days on this simple code and nobody has been able to tell me why it won't work - not even the MQL experts! This tells me that MetaQuotes has a problem.

Ok, I'm done venting now. That was two days worth of pent-up frustration and I had to let it out.

I NEVER have problem with me EAs and I've been able to put together some fairly sophisticated designs using all time frames except W1 and MN. All of my EAs use iCustom designs that I built and researched from scratch.

All of a sudden, after running like a fine oiled and well tuned V8 engine, I start using Time() functions and all hell breaks loose? Something is just not quite right about that. I'm using these Time based functions in the most simple way possible, and they still don't work? 48 hours on something like this was an enormous amount of time for me to waste. Frustrated and p'd off, I will try to live without "Time," a if that is possible.

I'm going to have to learn how to develop trade logic around Time() functions.

Unreal.

 

CFx 2012.05.31 03:41

LOL!


I'm starting to think that a majority of the "Time" based functions in MT4 are seriously flawed.


I mean, if I reset all of my position control logic to something as mundane as: TimeHour(TimeCurrent()) == 23 && TimeHour(TimeCurrent()) == 59, and not be able to get a solitary trade closed at all (the EA simply ignores the code!), then I would say that MT4 has some issues that need to be corrected with respect to these types of functions.


I also ran through a series of tests using the following: DayOfWeek() != 0 && DayOfWeek() !=, and not a single trade would open Monday through Friday! In fact, not a solitary trade opened at all. But, what do you think happened to the EA when I completely removed all Time based functions? Of course, it runs as designed without error. I'm not an MQL developer, but I'm really good at developing logical constructs and I can detect a systemic problem when I sense one. I sense that MetaQuotes has a problem with their Time() functions, period. I can't be this clueless on something so easily coded and I've seen other people that do code in MQL, have difficulty with Time based functions in MT4.

I don't exactly know what the problem is, but when remove TimeMinute(TimeCurrent()) <= 05, from the Opening Control logic (that piece of code that controls the opening of all trades), and I get ONLY the first trade to close at the correct time, yet ALL OTHER trades driven by the exact same Opening Control logic are summarily ignored by the EA subsequently, then I know that something is systemically wrong with the function itself.

The code can't be more simple! I'm instructing the EA to open the damn trade between 00:00 and 00:05. That is not a difficult thing to code. A six year old could code that. Here it is yet again: [b]TimeHour(TimeCurrent()) == 00 && TimeHour(TimeCurrent()) <= 05[/b].


There. I just coded it. I then drop that code into the tool I'm using and the tool tells me that the Code's Syntax is Correct! Ok, now what? Well, I compile the damn .ex4 file for the MILLIONTH time and run the M4 Tester. Ok, now what? Well, the very FIRST trade launches at 00:00. Awesome, right? NOOOOOO! Why? Because at 23:59 on a Monday, the damn position is STILL open. Then what? Well, at 00:00 of the very next trading session (the next D1 bar), good ole [b]TimeHour(TimeCurrent()) == 00 && TimeHour(TimeCurrent()) <= 05[/b] will pull through where the Closing Control failed, right? Wrong! Why? Because, at 00:00 through 00:05, no freaking trade is ever opened by the EA!!!

I spent two (2) days on this simple code and nobody has been able to tell me why it won't work - not even the MQL experts! This tells me that MetaQuotes has a problem.

Ok, I'm done venting now. That was two days worth of pent-up frustration and I had to let it out.

I NEVER have problem with me EAs and I've been able to put together some fairly sophisticated designs using all time frames except W1 and MN. All of my EAs use iCustom designs that I built and researched from scratch.

All of a sudden, after running like a fine oiled and well tuned V8 engine, I start using Time() functions and all hell breaks loose? Something is just not quite right about that. I'm using these Time based functions in the most simple way possible, and they still don't work? 48 hours on something like this was an enormous amount of time for me to waste. Frustrated and p'd off, I will try to live without "Time," a if that is possible.

I'm going to have to learn how to develop trade logic around Time() functions.

Unreal.

LOL. You are the one who unreal. This code of yours ...

if (TimeHour(TimeCurrent()) == 00 && TimeHour(TimeCurrent()) <= 05)

... will never returns true.

You're the one who need to further develop "your" logical construct.

And construct this ...

if (TimeHour(TimeCurrent()) >= 00 && TimeHour(TimeCurrent()) <= 05)
Reason: