Strategy Tester: Can anyone make sense of this?

 

I've worked through alot with MetaTrader, but this one I really don't get. The Tester iterates a function several times - it's not that the function fails - but then it just freezes with the Stop button remaining active.

1. int MyFunction {

2. for{i=1;i<=5;i++) {

3. if(!MyArray[i]) {do something} // Comment this out and the Tester's fine; Leave it in and the Tester freezes up after several iterations.

4. if(!2==3) {do something} // Tester's fine with this without line 3 - the do something executes normally

5. } // end for

6. for{i=1;i<=5;i++) {

7. if(!MyArray[i]||AnotherArray[i]) {do something} // SAME MyArray AS LINE 3 being tested but OK if without line 3.

8. } // end for

9.} // end function

Why is one test of this array OK but not the other? In the same function. Does this make sense to anyone? Has anyone else run into this?

 

What does if(!2==3) mean in MQL exactly? I may be exposing you to my ignorance here but I'm not familiar with the logic used in that if statement.

 

Does your array have 6 elements in it? Your loop goes from 1 to 5 indexes but array starts numeration from 0 thus if you have array of 5 elements you need to run loop from 0 to 4 --> for ( i = 0; i < 5; i ++ ) ...

And you have two things to check (line 7) while line 3 has only 1 check...

 
1005phillip:

What does if(!2==3) mean in MQL exactly?

It's a valid expression, although not a very useful one. But something's not right here... The operator '!' is supposed to have higher precedence than '==' according to https://docs.mql4.com/basis/operations/rules. The integers 2 and 3 are type cast to True. So the expression should evaluate to False, but I checked it - it evaluates to True! WTF?


Edit: corrected bellow.

 
Lou:

[...] but then it just freezes with the Stop button remaining active.

This happens if u enter an endless loop... 'Time' in the Tester is discrete, it only passes between ticks, but not while in the same tick. Hence, the Tester seems to be 'stuck'.

There's not enough in the code sample to see where that happens. Show us your code.

 
gordon:
It's a valid expression, although not a very useful one. But something's not right here... The operator '!' is supposed to have higher precedence than '==' according to https://docs.mql4.com/basis/operations/rules. The integers 2 and 3 are type cast to True. So the expression should evaluate to False, but I checked it - it evaluates to True! WTF?

gordon I believe you when you say it is valid, but what is the expression?

I know what if(2==3) means...if 2 = 3 is true then execute the loop (but 2!=3, so the expression 2==3 is false and the if is not executed) but what does adding a ! to the front of 2==3 do to the logic tree? I don't follow.
 
1005phillip:

gordon I believe you when you say it is valid, but what is the expression?

I know what if(2==3) means...if 2 = 3 is true then execute the loop (but 2!=3, so the expression 2==3 is false and the if is not executed) but what does adding a ! to the front of 2==3 do to the logic tree? I don't follow.

I made a mistake in my last post (wasn't thinking)... There is no type casting...


The expression:

!2==3

is exactly the same as:

!(2==3)

because of precedence rules. Since '2==3' evaluates false, the whole expression evaluates True.


As to what it means... Nothing. Since 2 and 3 are constants, it basically means the 'IF' is not there... As I said - not very useful.

 
gordon:

I made a mistake in my last post (wasn't thinking)... There is no type casting...


The expression:

!2==3

is exactly the same as:

!(2==3)

because of precedence rules. Since '2==3' evaluates false, the whole expression evaluates True.


As to what it means... Nothing. Since 2 and 3 are constants, it basically means the 'IF' is not there... As I said - not very useful.



OK, first I swear to you I have not been drinking, much, and I am not just jerking your chain here...but I can't get my mind wrapped around this.

Let's say the expression was something less specific. Instead of explicitly stated constants like 2 and 3 lets use integer variables like A and B, allowing for A and B to change value over time.

In this situation, wth does if(!A==B) mean? I know what if(A!=B) does, and I know what if(A==B) does, but what is the logic tree for an expression of the sort if(!A==B)? I don't get what adding the "not" qualifier in front of variable A does to the logic tree for evaluating the expression that decides whether the expression itself is true or false.
 
1005phillip:
OK, first I swear to you I have not been drinking, [...]

Well, I have been drinking... (last night... woke up with a massive hangover...).


Anyway, the expression '!A==B' equals the expression '!(A==B)' which also equals the expression 'A!=B'. When I say equal, I mean equal when it comes to the expression's value for a given input, and not necessarily how that input is calculated to result in that value.


In simplistic terms, if the possible inputs are 0 and 1 only (or false and true...):

A
B
!A==B
A!=B
0
0
0
0
1
0
1
1
0
1
1
1
1
1
0
0
 

gordon thank you for indulging my repetitious requests for clarification. You've made it perfectly clear now. So !A==B is the equivalent to coding A!=B in this situation.

So the "not" in front of A==B is like an XOR gate. First the valuation is performed as to whether or not A=B...and then it takes the opposite bool for the output of the expression.

If A=0 and B=0 then A==B is true, and !(A==B) become false because !(true) = not true = false = 0.

I understand now. I had not realized I could code boolean logic this way in MQL, oh the tortured schemes I've programmed to simply get to the point of logic that a simple if(!(...)) would have deftly implemented!

 

Ya... Bottom line, u can use the '!' operator with ANY expression... And basically it negates whatever that expression evaluates to. As usual - u should either use brackets or pay attention to precedence rules when using this operator (in our case, the '!' operator has higher precedence than the '==' operator, hence no need for brackets).