questions about &&

 

Hi,

Had an EA trade in clear violation of its rules and I can't see anything obvious in the code.  I use several MA's to identify trend an only trade in the direction of the trends.  I did notice the following in my if statement

if(xmma < xmpma &&flma < flpma)   //compare the MA of bar 1 with the prior bar

 I did not have a space between the && and the variable name.  Might that have confused the parser and produced something like the following???

if((xmma < xmpma) & (&flma < flpma))   //parenthesis added to clarify question

 It seems like a long shot, but the MA's that were "ignored or miscompared" in the system were the ones on either side of that particular && in the code where I had omitted following the && with a space.   Both moving average conditions had to have been "ignored or miscompared" for the trade to have gone through.

Thanks

 

As long as the && were together (no gap), it doesn't matter about the whitespace everywhere else.

The compiler would have thrown an error if it thought you had written the second line.

I suspect you need to be looking for another reason why the trade went through.

Consider if xmpma or flpma returning a 0 might have caused the logic to pass.

Or whether there was significant market volatility at the time of the trade.

If in doubt, print the variable values out at the the same time you open the order. 

 
Nathan:

Hi,

Had an EA trade in clear violation of its rules and I can't see anything obvious in the code.  I use several MA's to identify trend an only trade in the direction of the trends.  I did notice the following in my if statement

if(xmma < xmpma &&flma < flpma)   //compare the MA of bar 1 with the prior bar

 I did not have a space between the && and the variable name.  Might that have confused the parser and produced something like the following???

if((xmma < xmpma) & (&flma < flpma))   //parenthesis added to clarify question

 It seems like a long shot, but the MA's that were "ignored or miscompared" in the system were the ones on either side of that particular && in the code where I had omitted following the && with a space.   Both moving average conditions had to have been "ignored or miscompared" for the trade to have gone through.

Thanks

((xmma < xmpma) && (flma < flpma))

This would be correct format. 

 
Farrukh Aleem:
((xmma < xmpma) && (flma < flpma))

This would be correct format. 

Clearer but not better or more correct.
 
Alain Verleyen:
Clearer but not better or more correct.

Just what I was typing out.

Only a matter of style, nothing more.

 
Farrukh Aleem:
((xmma < xmpma) && (flma < flpma))

This would be correct format. 

Thank you both for the responses.  Farrukh, I have assumed that due to operator precedence the additional parenthesis were not needed.  What you have removes all ambiguity for sure, but I have not had this issue before and have many examples without the extra () that "appear" to be just fine in testing and live results.

 

I'll look at the values to make sure they are not zero and that they are as expected. I don't get the trade when I backtest across the same time window... am truly puzzled.  Low volatility.

Thanks,

 
Nathan:

 I have assumed that due to operator precedence the additional parenthesis were not needed. 

Correct. For reference, here are the precedence rules
Precedence Rules - Operations and Expressions - Language Basics - MQL4 Reference
Precedence Rules - Operations and Expressions - Language Basics - MQL4 Reference
  • docs.mql4.com
Precedence Rules - Operations and Expressions - Language Basics - MQL4 Reference
 
Nathan: Had an EA trade in clear violation of its rules and I can't see anything obvious in the code.
Print out your variables, and find out why.
 
whroeder1:
Print out your variables, and find out why.

Thank you.  I will do this, and I may have to also log the variables and their values with each trade going forward.  I can't reproduce the trade on the back test, but it traded on three different live accounts and two demo's (different brokers) so there is something very real in the code to either fix or plan for.

 
whroeder1:
Print out your variables, and find out why.

One final question here. 

Do you recommend testing the value of all or most variables? 

Should I not be assuming that there will always be a correct value returned for an iMA call if I'm looking at reasonable ranges?  Should I be testing each to be sure it is not zero or some other obviously wrong value?

Thanks,

 

First of all i would never use && like that.

if(xmma < xmpma)
{
  if(flma < flpma)
   {
    // Here you go...
   }
}

I have read stories of Million Dollar mistakes happening due to && and &.

And it took them quite some time to figure out the problem too.

Second, YES you always need to verify your data.

Data Availability

Presence of data on HCC format or even in the prepared for using HC format does not always denote the absolute availability if these data to be shown in a chart or used in mql4 programs.

When accessing to price data or indicator values from a mql4 program it should be remembered that their availability in a certain moment of time or starting from a certain moment of time is not guaranteed.

If the data is not available, what is the output of your indicatior call?

Assuming that everyhing is ok at all times can be a very bad idea.
Reason: