"If" question

 

Lets consider 4 moving averages:

double EMA1 =iMA(Symbol(),0,1,0,MODE_EMA,PRICE_CLOSE,1);
double EMA2 =iMA(Symbol(),0,2,0,MODE_EMA,PRICE_CLOSE,1);
double EMA3 =iMA(Symbol(),0,3,0,MODE_EMA,PRICE_CLOSE,1);
double EMA4 =iMA(Symbol(),0,4,0,MODE_EMA,PRICE_CLOSE,1);

Now if i want to test a buy or sell condition we use the BUY and SELL booleans, the question is:


Is this equivalent

if(EMA1<EMA2 && EMA2<EMA3 && EMA3<EMA4)
SELL=true;

if(EMA1>EMA2 && EMA2>EMA3 && EMA3>EMA4)
BUY=true;

to this?

if(EMA1<EMA2<EMA3<EMA4)
SELL=true;

if(EMA1>EMA2>EMA3>EMA4)
BUY=true;


If they are equivalent then the other question is how many variables can i link together in the same operation with the >, <, <=, >=, ||, && operators?

If not then why not?

 
Proximus:

Lets consider 4 moving averages:

Now if i want to test a buy or sell condition we use the BUY and SELL booleans, the question is:


Is this equivalent

to this?

No, it's not equivalent . . . read this and the following posts: https://www.mql5.com/en/forum/141790
 

The answer is no

From what I have experienced

if(EMA1<EMA2<EMA3<EMA4)
SELL=true;

is equivalent to

if(EMA1<EMA2 || EMA1<EMA3 || EMA1<EMA4)
SELL=true;

I'm not certain enough to actually code in this way though.

 
GumRai:

The answer is no

From what I have experienced

is equivalent to

Nope . . .

if(EMA1<EMA2<EMA3<EMA4)
SELL=true;

is the same as

if( ( (EMA1<EMA2) < EMA3 ) < EMA4)
SELL=true;

and the problem with this is that EMA1, EMA2, EMA3 and EMA4 are all doubles, but the expression (EMA1 < EMA2) results in a bool, so (EMA1 < EMA2) < EMA3 is testing a bool against a double and is not what was ever intended.

 
RaptorUK:

Nope . . .

is the same as

if( ( (EMA1<EMA2) < EMA3 ) < EMA4)
SELL=true;

and the problem with this is that EMA1, EMA2, EMA3 and EMA4 are all doubles, but the expression (EMA1 < EMA2) results in a bool, so (EMA1 < EMA2) < EMA3 is testing a bool against a double and is not what was ever intended.

Hi RaptorUK,

sorry, but I don't quite understand your answer

I think that there may be a typo in there because you have 2 open brackets and 3 close brackets

 
GumRai:

Hi RaptorUK,

sorry, but I don't quite understand your answer

I think that there may be a typo in there because you have 2 open brackets and 3 close brackets

The brackets look OK to me . . . here they are coloured and spaced out . . .

if (   (   (  EMA1 < EMA2 )  < EMA3  )  < EMA4  ) 
SELL=true;

. . . the < happens left to right so (EMA1 < EMA2) happens first and a bool is returned, next will be bool < EMA3

 
RaptorUK:

The brackets look OK to me . . . here they are coloured and spaced out . . .

. . . the < happens left to right so (EMA1 < EMA2) happens first and a bool is returned, next will be bool < EMA3

I apologise RaptorUK, I must have had a cross-eyed moment :(
 
GumRai:
I apologise RaptorUK, I must have had a cross-eyed moment :(

No need, I have such moments all the time
 

RaptorUK,

I know it is off topic, but how do you manage to highlight with different background colours?

Whenever I have tried to highlight in SRC, the whole code disappears!

 
GumRai:

RaptorUK,

I know it is off topic, but how do you manage to highlight with different background colours?

Whenever I have tried to highlight in SRC, the whole code disappears!

You highlight after you have added the SRC not in the SRC window
 
RaptorUK:
You highlight after you have added the SRC not in the SRC window


Thaks RaptorUK, that has been bugging me!
Reason: