Problems with Time() - page 7

 
onewithzachy:

The tools. Tell us about the tools, don't be embarrassing, I learnt from RaptorUK, dabbler, and SDC's comments the other day. So, please, tell us .

:D

It seems the answer to your question is Molanis . . . http://www.molanis.com/forum/viewtopic.php?f=2&t=1450
 
RaptorUK:
It seems the answer to your question is Molanis . . . http://www.molanis.com/forum/viewtopic.php?f=2&t=1450

Interesting, as is this

https://www.mql5.com/en/forum/126224

It helps to know the background of what we are talking about!


I have been avoiding this thread because there is so much upset on all sides. It just seems so unnecessary.


Maybe the OP should start a new thread so respondents don't have to read 200 posts with 100% accuracy to figure out what is going on.

And maybe everybody could stop calling people names.

 

RaptorUK:

Day(), DayOfWeek(), TimeDay() and TimeDayOfWeek() all seem to work correctly in the Straegy Tester (build 427) . . . did you really mean to use Day() in your code or should your code building thing . . whatever it is that you use to code for you, should it have used DayOfWeek() ? the first, Day() gives a value 0 - 31, the second DayOfWeek() gives a value of 0 - 6 Sunday is 0

Dooh! I glossed over that code and didn't see that problem, and I have used those exact functions in my own code. Well spotted :-)
 
dabbler:

Interesting, as is this

https://www.mql5.com/en/forum/126224

It helps to know the background of what we are talking about!


I have been avoiding this thread because there is so much upset on all sides. It just seems so unnecessary.

It is . . . some people can't be helped though, they have a fixation in their head that their code is good and MT4 is garbage . . . I tried to help the OP, he obviously is mixing up Day() and DayOfWeek() and despite it being obvious from his first post that it was the case he wouldn't accept it . . . some people just can't be helped.
 
dabbler:
Dooh! I glossed over that code and didn't see that problem, and I have used those exact functions in my own code. Well spotted :-)
onewithzachy: spotted it too . . . but with all the activity in this thread I missed his post.
 

Another thing we all didnt notice, well I did notice but in the midst of this crazy thread, I didnt realize no one had mentioned it, the OP posted this:

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

You cant string together all those || || || || || && && || && && as a condition without using some brackets to break it down.

Looking at the last part of it:

|| Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57
I dont believe that would ever cause the condition to be true when the previous parts were false, unless use some brackets to separate it from the rest of the condition.
 
SDC:

Another thing we all didnt notice, well I did notice but in the midst of this crazy thread, I didnt realize no one had mentioned it, the OP posted this:

You cant string together all those || || || || || && && || && && as a condition without using some brackets to break it down.

Well interestingly I did check that and it seemed ok. MQL4 and C (and every other computer language) has a set of precedence rules which give an exact interpretation of a logical expression. However, the tool being used said such "complex expressions" were ambiguous! (For which we should read not correctly implemented). WHR did post earlier that the expressions should be put within parenthises. This is obviously good practice anyway since having to crack open the book to figure out which precedence rule follows which expression means the code is unreadable.
 

OK I had to check this now because I never wrote code like that and had it work the way I thought it would, so here is a simple script to test it.

I replaced each comparison in the original condition with corresponding comparisons of integers to make it easy to read/test/debug. All of it is false except the part after the last || operator.

int start()
  {
//----
   int a,b,c,d,e; 
   a=1;
   b=2;
   c=3;
   d=4;
   e=1;
//----
   if( a==b || b==c || c==d || d==e && a>=b && c>=d || a==e && b>=a && c>=a )
   Alert("condition true");
   else
   Alert("condition false");
//----
   return(0);
  }

That gives condition false which tells me:

|| Day() == 5 && TimeHour(TimeCurrent()) >=21 && TimeMinute(TimeCurrent()) >=57

As it was placed in the OP code, would never have made the condition become true even if Day() had been replaced with DayOfWeek() but if we put some parenthesis in there ....

int start()
  {
//----
   int a,b,c,d,e; 
   a=1;
   b=2;
   c=3;
   d=4;
   e=1;
//----
   if( (a==b || b==c || c==d || d==e && a>=b && c>=d) || (a==e && b>=a && c>=a) )
   Alert("condition true");
   else
   Alert("condition false");
//----
   return(0);
  }
Now it works and the condition becomes true.
 
SDC:

OK I had to check this now because I never wrote code like that and had it work the way I thought it would, so here is a simple script to test it.

Nice work. I also spotted that the MQL4 precedence rules ...

https://docs.mql4.com/basis/operations/rules

have logical OR higher than logical AND, in disagreement with K & R (2nd ed)

But like you, I never really use these to any great degree, I just put brackets around stuff or use separate lines so I can understand my own code!

 
SDC:
Now it works and the condition becomes true.

And here is my contribution (test script) ...

int start(){
   string str = "LOGIC";
   for( int N=0; N<2; N++ ){
      bool bN= (N==1);
      string strN= "false AND ";
      if( bN )
         strN = "true AND ";
      
      for( int M=0; M<2; M++ ){
         bool bM= (M==1);
         string strM= "false OR ";
         if( bM )
            strM = "true OR ";

         for( int P=0; P<2; P++ ){
            bool bP = (P==1);
            string strP= "false = ";
            if( bP )
               strP = "true = ";
            
            str = str + "\n" + strN + strM + strP;
            if( bN && bM || bP )
               str = str + "true";
            else
               str = str + "false";
         }
      }
   }
   
   Comment( str );

   return(0);
}

Which does this ...


Clearly showing that the logical OR tests are done first (higher priority) and then the logical AND tests are done ... just like it says in the MQL4 documentation in fact :-)

Reason: