Why one if works and the other don't?

 

My EA is supposed to open a long trade when the RSI value is greater than 70, the k is 1 and there's one position open in the current currency pair. That part works, I tested it, but when we go to the part of doing the reverse, it's doesn't happen anything, in other words, when the RSI value is below 70, the k is 2 and there's one position open, my EA should open a short trade but it doesn't. The k works for identifying if the open trade is a short or a long one. There are no error or warnings. I've been searching for hours for the problem and I can't find it. The if's are a identical reversal of each other. Why one works and the other doesn't?

#include<Trade/trade.mqh>

CTrade trade;
CPositionInfo pos;

int myRSIDefinition;
int k;

int OnInit()
   {
      //defining the properties of the RSI
      myRSIDefinition = iRSI(_Symbol, _Period, 14, PRICE_CLOSE);      
      
      return(INIT_SUCCEEDED);
   }

void OnTick()
   {
  
      //creating an array for several prices
      double myRSIArray[];
      
      //sorting the price array from the current candle downwards
      ArraySetAsSeries(myRSIArray,true);
      
      //defined EA, current candle, 3 candles, store result
      CopyBuffer(myRSIDefinition, 0, 0, 3, myRSIArray);
      
      //calculating RSI for the current candle
      double myRSIValue= NormalizeDouble(myRSIArray[0],2);         
      
      //if the position of the current currency pair has a open buy order then it attributes 1 to k, if not, 2 to k
      if(PositionSelect(_Symbol)==true && pos.PositionType()==POSITION_TYPE_BUY){    
               k=1;
            }else{
               k=2;
            }      

      //if the value of the RSI is greater than 70, then the EA opens a long trade
      if(k==2 && PositionsTotal() == 1 && myRSIValue >=70)
         {
            double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits);
            double Balance= AccountInfoDouble(ACCOUNT_BALANCE);
            double Equity= AccountInfoDouble(ACCOUNT_EQUITY);
            
            if(Equity >= Balance){
               trade.Buy(0.05, NULL, Ask,0,(Ask+100 * _Point), NULL);
              
            }
  
         }
      
      //if the value of the RSI is below 70, then the EA opens a short trade
      if(k==1 && PositionsTotal() == 1 && myRSIValue <70){
          double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits);
          double Balance= AccountInfoDouble(ACCOUNT_BALANCE);
          double Equity= AccountInfoDouble(ACCOUNT_EQUITY);
            
          if(Equity >= Balance){
             trade.Sell(0.05, NULL, Bid,0,(Bid+100 * _Point), NULL);
              
          }
      } 
   }
   
 
      //if the value of the RSI is greater than 70, then the EA opens a long trade
      
      //if the value of the RSI is below 70, then the EA opens a short trade

Are you sure about this?

 
lippmaje:

Are you sure about this?

yea, Why? It's working now, but now I got another problem. My robot doesn't open the trade immediately when the RSI changes to below 70 or higher than 70. I added a do while, but still the same. What did I do wrong?
 

Didn't you mean 'below 30' ?

You have to consider 3 cases: 1=long, 2=short and 0=none. Your variable k will stick at 2 even if no position exists.

 
lippmaje:

Didn't you mean 'below 30' ?

You have to consider 3 cases: 1=long, 2=short and 0=none. Your variable k will stick at 2 even if no position exists.

I know that. That's exactly what I want. No, I didn't mean below 30. I know it seems, but this is just a project. If I have a sell trade and the RSI is below 70 it doesn't do nothing like I want, but when the RSI changes and goes higher than 70, my robot doesn't add a buy trade. Why doesn't he add it? Do I need to put a while to the EA always repeat the process or? I'm really confused.
 
I'm also confused by this design. What means above 70, are you looking for a cross from below or not? A while loop around the order logic will make the EA freeze.
 

lippmaje:
I'm also confused by this design. What means above 70, are you looking for a cross from below or not? A while loop around the order logic will make the EA freeze.  

70 is the RSI LEVEL I set. If a cross from below the RSI LEVEL happens I want it to add a buy trade, and if a cross from up happens I want it to add a sell trade. I added a while loop but it freezed like you said. So, what I'm doing wrong here?

 

To code a cross you need two values, actual RSI and previous one. The previous one could be the one from the last tick, or the one from the last bar(candle).

Then you check if prevRSI<70 and currRSI>=70. This is how a cross from below works. For a cross from above check it vice versa.

I suggest to search in the code base for a free RSI EA and to study how it works.

(Ignore the link to GetTickCount, it's auto-generated and not relevant here.)

 
lippmaje:

To code a cross you need two values, actual RSI and previous one. The previous one could be the one from the last tick, or the one from the last bar(candle).

Then you check if prevRSI<70 and currRSI>=70. This is how a cross from below works. For a cross from above check it vice versa.

I suggest to search in the code base for a free RSI EA and to study how it works.

(Ignore the link to GetTickCount, it's auto-generated and not relevant here.)

I downloaded one but it is so complex. In a simple way, how can I get the previous tick RSI?
 
15112020:
In a simple way, how can I get the previous tick RSI?

Keep the actual value in a global or static variable and copy it to prevRSI before reading in the new value.

double prevRSI = currRSI;
currRSI = myRSIArray[0];
 
lippmaje:

Keep the actual value in a global or static variable and copy it to prevRSI before reading in the new value.

So, like this? I inserted your code in line 25 and 26. Did I put them in the right spot?

#include<Trade/trade.mqh>

CTrade trade;
CPositionInfo pos;

int myRSIDefinition;
int k;

int OnInit()
   {
      //defining the properties of the RSI
      myRSIDefinition = iRSI(_Symbol, _Period, 14, PRICE_CLOSE);      
      
      return(INIT_SUCCEEDED);
   }

void OnTick()
   {
  
      //creating an array for several prices
      double myRSIArray[];
      
      //here they are
      double currRSI = myRSIArray[0];
      double prevRSI = currRSI;       

      //sorting the price array from the current candle downwards
      ArraySetAsSeries(myRSIArray,true);
      
      //defined EA, current candle, 3 candles, store result
      CopyBuffer(myRSIDefinition, 0, 0, 3, myRSIArray);
      
      //calculating RSI for the current candle
      double myRSIValue= NormalizeDouble(myRSIArray[0],2);         
      
      //if the position of the current currency pair has a open buy order then it attributes 1 to k, if not, 2 to k
      if(PositionSelect(_Symbol)==true && pos.PositionType()==POSITION_TYPE_BUY){    
               k=1;
            }else{
               k=2;
            }      

      //if the value of the RSI is greater than 70, then the EA opens a long trade
      if(k==2 && PositionsTotal() == 1 && myRSIValue >=70)
         {
            double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits);
            double Balance= AccountInfoDouble(ACCOUNT_BALANCE);
            double Equity= AccountInfoDouble(ACCOUNT_EQUITY);
            
            if(Equity >= Balance){
               trade.Buy(0.05, NULL, Ask,0,(Ask+100 * _Point), NULL);
              
            }
  
         }
      
      //if the value of the RSI is below 70, then the EA opens a short trade
      if(k==1 && PositionsTotal() == 1 && myRSIValue <70){
          double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID),_Digits);
          double Balance= AccountInfoDouble(ACCOUNT_BALANCE);
          double Equity= AccountInfoDouble(ACCOUNT_EQUITY);
            
          if(Equity >= Balance){
             trade.Sell(0.05, NULL, Bid,0,(Bid-100 * _Point), NULL);
              
          }
      } 
   }
   
Reason: