Error when using RSI and string value

 

Hi,


I'm developing a code(on MQL4) that when RSI is on vaue 72, he store on a string like "position 1". And i want to open a order when rsi has an different value but also string has position 1.

For eg. IF RSI is 72, x = "position 1".

if rsi is 69 and x = "position 1", open order.

But not getting with success because i'm thinking that when rsi is out of condition of string, the string will have a new value.


Here is the code:


 double rsi = iRSI(_Symbol,_Period,14,PRICE_CLOSE,0);//Open RSI
  
  int total;
  string r="pos0";
  
  if(rsi>=72){

   r = "pos1";
  }
  

   if(r=="pos1" && rsi==69){
   
       int ord1 = OrderSend(_Symbol,OP_BUY,0.2,Ask,3,0,0,NULL,0,0,Green); //Open buy order
   }
   
   }
 
Thalles Heringer Heringer:

But not getting with success because i'm thinking that when rsi is out of condition of string, the string will have a new value.

Strange way to do it, why not use a bool instead of a string?

Any way, you are declaring the variable locally so it will be lost every tick.

string r="pos0";

Declare it as a static variable and remember to reset it if/when necessary

static string r="pos0";

RSI is unlikely to ever equal 69

if(r=="pos1" && rsi==69)

Maybe

if(r=="pos1" && rsi<=69)

I will move this to the MQL4/MT4 section

 
   if(r=="pos1" && rsi==69){
  1. r equals "pos1" only if rsi >= 72. Therefor your test will never be true. If r was static (or global) then you would be testing if it went above 72 and then back down below 69.

  2. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum

  3. Don't use strings when you mean a bool or an enum.
 
Keith Watford:

Strange way to do it, why not use a bool instead of a string?

Any way, you are declaring the variable locally so it will be lost every tick.

Declare it as a static variable and remember to reset it if/when necessary

RSI is unlikely to ever equal 69

Maybe

I will move this to the MQL4/MT4 section


Thanks for the Reply Keith. I think that i can't use bool because will have more 2 positions, will have pos0 (between 30 and 70), pos1 (above 70), pos2(above 75), pos3(above 80), pos 4(when rsi below 69 and pos is diferent from pos1 or pos0).


I runned this code and worked Keith!!!!!


Really thank you for your tip!

Reason: