Download MetaTrader 5
To add comments, please log in or register
Develop trading robots to order. Make profit with Freelance service!
Massimo
478
Massimo 2016.04.10 17: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.

Alain Verleyen
Moderator
28648
Alain Verleyen 2016.04.10 17: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)
   && ...
   )
  {
    ...
  }
Massimo
478
Massimo 2016.04.10 17: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.

Carl Schreiber
5957
Carl Schreiber 2016.04.10 17: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) ) ...

Alain Verleyen
Moderator
28648
Alain Verleyen 2016.04.10 18: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.
whroeder1
13640
whroeder1 2016.04.10 19: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){ ... }
Massimo
478
Massimo 2016.04.10 20:48  
Thank you all.
I understood.
Greetings, Massimo.
/
To add comments, please log in or register