Trying to find the first retracement, what am I doing wrong?

 
I am trying to code a simple indicator which finds the first retracement.

For eg, if 50 ema goes above 100 EMA and RSI level crosses above 30, it should show arrow. Only the first RSI signal aka first retracement. It should ignore other ones. What am i doing wrong?



//+------------------------------------------------------------------+
//|                                             FirstRetracement.mq4 |
//|                              Copyright 2019,          Max Victor |
//|                                            https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Max Victor"
#property link      "https://www.mql5.com/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot buyArrow
#property indicator_label1  "buyArrow"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- plot sellArrow
#property indicator_label2  "sellArrow"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  3
//--- input parameters
input int      sMA=50;
input int      bMA=100;
input int   rSI=8;
input double   upperLevel=70.0;
input double   lowerLevel=30.0;
//--- indicator buffers
double         buyArrowBuffer[];
double         sellArrowBuffer[];
//double         buffer1[], buffer2[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(4);
   SetIndexBuffer(0,buyArrowBuffer);
   SetIndexBuffer(1,sellArrowBuffer);
   SetIndexBuffer(2,buffer1);
   SetIndexBuffer(3,buffer2);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,233);
   PlotIndexSetInteger(1,PLOT_ARROW,234);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
    if(rates_total<2*sMA-2|| rates_total<2*bMA-2|| rates_total<2*rSI-2)
      return(0);
      
      int limit;
   if(prev_calculated==0)
      limit=0;
   else limit=prev_calculated-1;
   
   
   bool rsibool = 0;
  //rsi 0 means nothing
  //rsi 1 means trendup and rsibuy met;
  //rsi 2 means trenddown and rssell met;
  for(int i=limit;i<rates_total && !IsStopped();i++){
 
 double sma1 = iMA(NULL,0,sMA,0,MODE_EMA,PRICE_CLOSE,i);
 double bma1 = iMA(NULL,0,bMA,0,MODE_EMA,PRICE_CLOSE,i);
 double rsi1 = iRSI(NULL,0,rSI,PRICE_CLOSE,i);
 double rsi2 = iRSI(NULL,0,rSI,PRICE_CLOSE,i+1);
 
 
   
    if(sma1 > bma1 && rsi1 > lowerLevel && rsi2 < lowerLevel && rsibool !=1)
      {
       buyArrowBuffer[i] = iLow(NULL,0,i);
       rsibool = 1;
   //    break;
      }
      
      if(sma1 < bma1 && rsi1 < upperLevel && rsi2 > upperLevel && rsibool !=2)
      {
       sellArrowBuffer[i] = iHigh(NULL,0,i);
       rsibool = 2;
//break;
      }
   
  }
  //loop ends
  

      

      
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.

 
Keith Watford:
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.

I am sorry, I didn't notice it.  I will be careful from now on. Thank you.
 
  1.     if(rates_total<2*sMA-2|| rates_total<2*bMA-2|| rates_total<2*rSI-2)
          return(0);
          
          int limit;
       if(prev_calculated==0)
          limit=0;
       else limit=prev_calculated-1;
    Your look back is the maximum of sMA, bMA and rSI+1. Limit=0 and limit=prev_calculated is bogus.
              How to do your lookbacks correctly.

  2. You want only the first one, but your test is for both at the same time. When the MAs cross, set your boolean to false. Only when the boolean is false do you test RSI. When you find the retracement, mark it and set your boolean to true.

  3. You need to remember the results. Your boolean must be static.

  4. You are looking at short above long. Look for a cross.

    double aPrev = …, aCurr = …,
           bPrev = …, bCurr = …;
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
           isCross = isUp != wasUp;

  5.    bool rsibool = 0;
    
           rsibool = 1;
           rsibool = 2;
    
    True is non-zero. Why are you using ints? There is no difference between the two assignments.

 
William Roeder:
  1. Your look back is the maximum of sMA, bMA and rSI+1. Limit=0 and limit=prev_calculated is bogus.
              How to do your lookbacks correctly.

  2. You want only the first one, but your test is for both at the same time. When the MAs cross, set your boolean to false. Only when the boolean is false do you test RSI. When you find the retracement, mark it and set your boolean to true.

  3. You need to remember the results. Your boolean must be static.

  4. You are looking at short above long. Look for a cross.

  5. True is non-zero. Why are you using ints? There is no difference between the two assignments.

Thank you so much, William!
I am a complete newbie. You don't know how much valuable  and help I have received from your comment. I will do it again and increase my understanding.

Thank you so much again :)