iCCI array out of range

 

Hello!

I'm trying to do an CCI indicator but show the error array out of range.

Follow my code and please help me!!

//--- plot candles
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   1 

#property indicator_label1  "candles"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrRed,clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- buffers do Indicador 
double         bufferIcci[];
double         bufferOpenCandle[]; 
double         bufferLowCandle[]; 
double         bufferHighCandle[]; 
double         bufferCloseCandle[]; 
double         bufferCandleColor[]; 
//--- handle
int handleICCI;

double closeCandle, openCandle;
//+------------------------------------------------------------------+ 
//| Função de inicialização do indicador customizado                 | 
//+------------------------------------------------------------------+ 
int OnInit() 
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,bufferOpenCandle,INDICATOR_DATA);      
   SetIndexBuffer(1,bufferLowCandle,INDICATOR_DATA);   
   SetIndexBuffer(2,bufferHighCandle,INDICATOR_DATA);      
   SetIndexBuffer(3,bufferCloseCandle,INDICATOR_DATA);    
   SetIndexBuffer(4,bufferCandleColor,INDICATOR_COLOR_INDEX); 
   SetIndexBuffer(5,bufferIcci,INDICATOR_CALCULATIONS);   
       
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,0);
   PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,0);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
                     
   handleICCI = iCCI(Symbol(), PERIOD_CURRENT, 45, PRICE_TYPICAL);
   
//---  
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| Função de iteração do indicador customizado                      | 
//+------------------------------------------------------------------+ 
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<45)
      return(0); 

   int calculated=BarsCalculated(handleICCI);
   if(calculated<rates_total)
     {
      Print("Not all data of handleICCI is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
   if(IsStopped()) return(0); //Checking for stop flag
   if(CopyBuffer(handleICCI,0,0,rates_total,bufferIcci)<=0)
   {
      Print("Getting fast CCI is failed! Error",GetLastError());
      return(0);   
   }
   
   int start;
   if(prev_calculated==0) 
      start=0;
   else start=prev_calculated-1;
        
   for(int i=0; i<rates_total-3 && !IsStopped(); i++)
   {  
      bufferLowCandle[i] = low[i];
      bufferHighCandle[i] = high[i];
      bufferCloseCandle[i] = close[i];
      bufferOpenCandle[i] = open[i];   
      if(i==2) Print(low[i]+" "+close[i]);

      
      if (bufferIcci[i] >= 0 && bufferIcci[i+1] < 0) 
      {
         bufferCandleColor[i] = 1;
      }
      if (bufferIcci[i] <= 0 && bufferIcci[i+1] > 0) 
      {
         bufferCandleColor[i] = 0;
      }
      if (bufferIcci[i] >= 0) 
      {
         bufferCandleColor[i] = 1;
      } 
      else if(bufferIcci[i] <= 0)
      {
         bufferCandleColor[i] = 0;      
      }      
   }
//--- valor retorno de prev_calculated para a próxima chamada 
   return(rates_total); 
  } 
 
Eduardo Oliveira :

Hello!

I'm trying to do an CCI indicator but show the error array out of range .

Follow my code and please help me!!

You have inserted the code correctly - thanks for that. Please (for the future) do one more additional action: attach (using the button Attach file) Your code. Why you need to do this: it's so easy to check - I click on the attached file and the browser downloads this file right away. All I have to do is click on the downloaded file and the file will open in MetaEditor.

 

Corrected code - you need to be more careful with indexes.

***
   if(!FillArrayFromBuffer(BufferCCI,handle_iCCI,values_to_copy))
      return(0);
//--- main loop
   int limit=prev_calculated-1;
   if(prev_calculated==0)
      limit=1;
   for(int i=limit; i<rates_total; i++)
     {
      OpenBuffer[i]=open[i];
      HighBuffer[i]=high[i];
      LowBuffer[i]=low[i];
      CloseBuffer[i]=close[i];
      CandlesColors[i]=0.0;
      //---
      if(BufferCCI[i-1]<0.0 && BufferCCI[i]>=0.0)
        {
         CandlesColors[i]=1.0;
        }
      if(BufferCCI[i-1]>0.0 && BufferCCI[i]<=0.0)
        {
         CandlesColors[i]=0.0;
        }
      if(BufferCCI[i]>=0.0)
        {
         CandlesColors[i]=1.0;
        }
      else
        {
         CandlesColors[i]=0.0;
        }
     }
//--- memorize the number of values in the Commodity Channel Index indicator
   bars_calculated=calculated;
//--- return the prev_calculated value for the next call
   return(rates_total);
  }
***
Files:
 

I added the CCI indicator to the chart:

CCI Color Candles

 
Vladimir Karputov:

You have inserted the code correctly - thanks for that. Please (for the future) do one more additional action: attach (using the button ) Your code. Why you need to do this: it's so easy to check - I click on the attached file and the browser downloads this file right away. All I have to do is click on the downloaded file and the file will open in MetaEditor.

Sorry about it, it's my first time to do here...

 
Vladimir Karputov:

Corrected code - you need to be more careful with indexes.

Ok, I'll see where I write indexes and I'll rewrite


Thank you!

Reason: