Too many OR in a IF

 

Hello Everyone,

I am writing a long IF with many OR. My way of writing right now is that:

IF ( condition A1&&condition A2 || condition B1&&condition B2.......)

{sell[i]=1}

Is there any way to separate condition A set and condition B set into 2 IF like:

IF (condition A1&&condition A2) {sell[i]=1}

IF (condition B1&&condition B2) {sell[i]=1}

This way, I can manage the flow of condition better.

Any help is very much appreciated.

Thank you,

SCFX

 

Have you considered Functions()?

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

 

I think you can just use brackets right?

IF ( (condition A1 && condition A2) || (condition B1 && condition B2) ) {sell[i]=1}

That works, if it's what you mean

 
scfx:

Hello Everyone,

I am writing a long IF with many OR. My way of writing right now is that:

IF ( condition A1&&condition A2 || condition B1&&condition B2.......)

{sell[i]=1}

Is there any way to separate condition A set and condition B set into 2 IF like:

IF (condition A1&&condition A2) {sell[i]=1}

IF (condition B1&&condition B2) {sell[i]=1}

This way, I can manage the flow of condition better.

Any help is very much appreciated.

Thank you,

SCFX

You can also declare bool variable :

bool cond1 = condition A1&&condition A2;
bool cond2 = condition B1&&condition B2;
IF (cond1 || cond2 .......) {sell[i]=1}

In all case, it's highly recommended to use brackets (...) when combining AND, OR in the same statement.

 

I use an array S[] to collect each SELL condition separately:

if( condition1 && condition2 ) S[1]=1;
if( condition3 && condition4 ) S[2]=1;
if( condition5 && condition6 ) S[3]=1;
if( condition7 && condition8 ) S[4]=1;
(...)
if( S[1]&&S[3] ) SELL=1; 
if( S[2]&&S[3] ) SELL=1;
if( S[1]||S[3]||S[4] ) SELL=1;
(...)
and then I combine them the way I need.

This way you write less and increase code readability.

 
  1. if( condition1 && condition2 ) S[1]=1;
    You do this but forget to initially set var=0 and the code is broken. Local variables (SELL) will default to zero, but the array will not be reset after the first call. Use bools properly.
    // is S[1] .. S[4] and SELL zero here?
    if( condition1 && condition2 ) S[1]=1;
    if( condition3 && condition4 ) S[2]=1;
    if( condition5 && condition6 ) S[3]=1;
    if( condition7 && condition8 ) S[4]=1;
    (...)
    if( S[1]&&S[3] ) SELL=1; 
    if( S[2]&&S[3] ) SELL=1;
    if( S[1]||S[3]||S[4] ) SELL=1;
    (...)
    bool s1=( condition1 && condition2 ),
         s2=( condition3 && condition4 ),
         s3=( condition5 && condition6 ),
         s4=( condition7 && condition8 );
    (...)
    bool SELL = ( S1&&S3 )
             || ( S2&&S3 )
             || ( S1||S3||S4 );

  2. Use self documenting variable names and you'll eliminate many errors when using bools. When you read the code and speak it, if it doesn't make sense it's probably wrong.
    bool  isFastAboveSlow = maFastCur > maSlowCur,
         wasFastAboveSlow = maFastPre > maSlowPre,
         isMAcross = isFastAboveSlow != wasFastAboveSlow;

Reason: