Problem to build a function RSI (CCI)

rodrigosm
363

Hi everybody,

I was trying to build an indicator that calculates the RSI from a CCI indicator.

The main problem is that I don’t know why this indicator doesn’t calculates in real time properly. When I open in real time, only the past data is ok, every new tick that is received change dramatically the indicator (in time = 0). If I close the graphic and open it again, or if I remove the indicator and add it again, both will show corrects results for the past, where before had an error.

Anyone knows why it’s happening?

#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Lime

//---- Indicator Parameters ----//
extern int Periodo_RSI = 4;
extern int Periodo_CCI = 20;
extern int TimeFrame =0;

double CCI[];
double RSI[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

      IndicatorBuffers(2); 

      SetIndexStyle(0, DRAW_HISTOGRAM);
      SetIndexBuffer(0,RSI);
      
      SetIndexStyle(1, DRAW_LINE);
      SetIndexBuffer(1,CCI);
      

      
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int  counted_bars=IndicatorCounted();
   int  limit;
   int total= ArraySize(CCI);
   ArraySetAsSeries(CCI,true) ;   


//---- Data ckecking ----//

   if (counted_bars==0)
      {
      limit=Bars-1;
      }
   if (counted_bars>0)
      {
      limit=Bars-counted_bars;
      }
      

     

   for(int i=limit;i>=0;i--) // Count down for one pass
    { 

      CCI[i]= iCCI(Symbol(),
                    TimeFrame,
                    Periodo_CCI,
                    PRICE_CLOSE,
                    i);
      
      RSI[i]= iRSIOnArray(CCI,
                          total,
                          Periodo_RSI,
                          i)-50;
      
                       
    }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Thanks