Double condition ( "OR" in MQL4)

Back to topics list To post a new topic, please log in or register
avatar
63
omissamf 2016.04.10 19:02
 
Hello to all. I need help to find out if the code I wrote is right or not.
I wrote this code to mean that the condition is valid if I cross the RSI at level 50.
.....

 if(iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i) < 50
 && iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i+1) > 50
 &&............


I would like to add an additional condition which is valid if the RSI cross the level 50 "OR" if the RSI is below the 10 level.

I wrote this code but I would like to know from the experts if it is correct:

.....

 if((iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i) < 50 + iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i+1) > 50 || iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i) < 10)
 &&............



Thank you all, Massimo.

avatar
Moderator
3206
angevoyageur 2016.04.10 19:20 #
 
omissamf:
Hello to all. I need help to find out if the code I wrote is right or not.
I wrote this code to mean that the condition is valid if I cross the RSI at level 50.


I would like to add an additional condition which is valid if the RSI cross the level 50 "OR" if the RSI is below the 10 level.

I wrote this code but I would like to know from the experts if it is correct:



Thank you all, Massimo.

It's not correct. You need to use && and parentheses :

double current    = iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i);
double previous   = iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i+1);

if(((current<50 && previous>50) || current<10)
   && ...
   )
  {
    ...
  }

 

avatar
63
omissamf 2016.04.10 19:33 #
 
Thanks, angevoyageur.

But I could also write:


.....

 if((iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i) < 50 && iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i+1) > 50 || iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i) < 10)
 &&............


or is it really wrong?

Massimo.

avatar
2017
gooly 2016.04.10 19:57 #
 

Let it become your habit if you mix && and || always use parentheses!

in your case you open 2 '(' and don't close them but - what do you want (?):

a)  iRSI(1) && ( iRSI(2) || iRSI(3) ) && iRSI(4) ...   or

b)  (iRSI(1) && iRSI(2) ) || ( iRSI(3) && iRSI(4) ) ...

avatar
Moderator
3206
angevoyageur 2016.04.10 20:00 #
 
omissamf:
Thanks, angevoyageur.

But I could also write:



or is it really wrong?

Massimo.

It's not the same. Please read the documentation about precedence rules.
avatar
12985
WHRoeder 2016.04.10 21:28 #
 
gooly: Let it become your habit if you mix && and || always use parentheses!
  1. Always use parentheses when mixing ands and ors.
  2. Or make your code readable.
    double current    = iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i);
    double previous   = iRSI(NULL, PERIOD_CURRENT, 2, PRICE_CLOSE, i+1);
    
    bool wasAbove = previous > 50;
    bool  isAbove =  current > 50;
    bool hasCrossed = isAbove != wasAbove;
    
    if(hasCrossed){ ... }
avatar
63
omissamf 2016.04.10 22:48 #
 
Thank you all.
I understood.
Greetings, Massimo.
Back to topics list  

To add comments, please log in or register