How to check when RSI cross some level for the first time after it came from level 50?

 

Hi everyone,

i want to check when RSI cross some level (let say 20) for the first time after it came down from level 50,

the second cross to same level i dont want it just the first one, and this is my attempt:

bool FirstTouchLevel20()
{
   int Lvl20TouchCount = 0;
   for(int i = 1; i <= 100; i++)
   {
      double rsi = iRSI(Symbol(), Period(), 14, 0, i);
      if(rsi <= 20)
      {
         Lvl20TouchCount++;
      }
      if(rsi >= 50.0)
      {
         break;
      }
   }
   
   return (Lvl20TouchCount <= 1);
}
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
  • www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
 

the solution was so simple.

bool FirstTouchLevel20()
{
   int Lvl20TouchCount = 0;
   for(int i = 1; i <= 100; i++)
   {
      double rsi = iRSI(Symbol(), Period(), 14, 0, i);
      if(rsi <= 20)
      {
         Lvl20TouchCount++;
      }
      if(rsi >= 50.0)
      {
         break;
      }
   }
   
   return (Lvl20TouchCount == 1);
}
 
Omran Alturk #:

the solution was so simple.

Yeah... but is not doing what you asked

 
Daniel Cioca #:

Yeah... but is not doing what you asked

No it does, i checked it, but i will be appreciate if you could give another solution because i think it is cranky.

 
Omran Alturk #:

No it does, i checked it, but i will be appreciate if you could give another solution because i think it is cranky.

You Said to count only when it comes down from 50 to 20… this means if now is 51 and it comes down to 19 you count one … if is going up to 21 and come down again to 19 you count again… it shouldn’t … you said to count only when it comes from above 50… 
 
Exactly. So do it, exactly that, but forward in time.
bool FirstTouchLevel20()
{
   enum State{eInit, eEnabled, eFirst, eFirstDone, eSecond };
   State s=eInit;
   for(int i = 100; i > 0; --i)
   {
      double rsi = iRSI(Symbol(), Period(), 14, 0, i);
      if(rsi >= 50)        s=Enabled;                       // Reset, above 50.
      if(s == eInit)       continue;                        // No above 50 yet.

      if(rsi > 20){        if(s==eFirst) s=eFirstDone;      // Touch complete.
                           continue; }                      // Not touching yet.

      if(s == eEnabled)   s=eFirst;                         // Second touch w/o reset.
      else                s=eSecond;
   }
   return s==isFirst || s==eFirstDone; // Saw 50, saw 20, not above 50, no second touch.
} 
 

Or maybe like this?

void FirstTouch()
  {
    int static flagabove50   =0;
    bool       firsttouch    =false;
     
      double rsi=iRSI(_Symbol,_Period,6,PRICE_CLOSE,1);

     if(rsi>=50)  flagabove50=1;
     if(rsi<20 && flagabove50==1)
           {
            
            firsttouch=true;
            flagabove50=0;
            
           }
      Print("rsi",rsi);
      Print("flag=",flagabove50);
      Print("firsttouch",firsttouch);
        

     }
 
Thank you guys, that was very helpful.
Reason: