Continuous Array Out of Range - Custom Indicator

 

Hi I'm learning to code custom indicators, I am trying to do one for Ichimoku where the indicator must meet a checklist of 4 items:

1) TK Cross

2) Price relative to cloud

3) Favourable cloud (bullish/bearish)

4) Lag span above cloud

I have the following code with no errors however critical Array out of Range errors upon running:

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 2

#property indicator_type1 DRAW_ARROW
#property indicator_color1 Magenta
#property indicator_label1 "Buy Signal"

#property indicator_type2 DRAW_ARROW
#property indicator_color2 Lime
#property indicator_label2 "Sell Signal"

input int Tenkan =9;
input int Kijun =26;
input int Senkou_B =52;
input int Chikou_Offset =30;

double SellBuffer[], BuyBuffer[];
int ichihandle;


int OnInit()
  {
      ichihandle=iIchimoku(NULL,0,Tenkan,Kijun,Senkou_B);
      
      SetIndexBuffer(0,SellBuffer,INDICATOR_DATA);
      PlotIndexSetInteger(0,PLOT_ARROW,234);
      PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
      ArraySetAsSeries(SellBuffer,true);
      
      SetIndexBuffer(0,BuyBuffer,INDICATOR_DATA);
      PlotIndexSetInteger(0,PLOT_ARROW,233);
      PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
      ArraySetAsSeries(BuyBuffer,true);
      
      IndicatorSetInteger(INDICATOR_DIGITS,_Digits);


   return(INIT_SUCCEEDED);
  }



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[])
  {
   
   int first, bar;
   double TenkanArray[], KijunArray[], SenkouAArray[], SenkouBArray[], Chikou[];
   
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   
   if(prev_calculated==0)
      first=Senkou_B+1;
   else first=prev_calculated+1;
   
   CopyBuffer(ichihandle,0,0,Senkou_B+1,TenkanArray);
   ArraySetAsSeries(TenkanArray,true);
   
   CopyBuffer(ichihandle,1,0,Senkou_B+1,KijunArray);
   ArraySetAsSeries(KijunArray,true);
   
   CopyBuffer(ichihandle,2,0,Senkou_B+1,SenkouAArray);
   ArraySetAsSeries(SenkouAArray,true);
   
   CopyBuffer(ichihandle,3,0,Senkou_B+1,SenkouBArray);
   ArraySetAsSeries(SenkouBArray,true);
   
   CopyBuffer(ichihandle,4,0,Senkou_B+1,Chikou);
   ArraySetAsSeries(Chikou,true);
   
   for(bar = first; bar >=0; bar--)
      {
        
        
         bool bTKCross, sTKCross, bCloud, sCloud, LSAbove, LSBelow, priceAboveCloud, priceBelowCloud;
         
         if(TenkanArray[bar]>KijunArray[bar])
            {
               bTKCross = 1;
               sTKCross = 0;
            }
         else
            {
               bTKCross = 0;
               sTKCross = 1;
            }
            
         if(SenkouAArray[bar]>SenkouBArray[bar])
            {
               bCloud = 1;
               sCloud = 0;
            }
         else
            {
               bCloud = 0;
               sCloud = 1;
            }
         
         if(Chikou[Chikou_Offset]>high[Chikou_Offset])
            {
               LSAbove = 1;
               LSBelow = 0;
            }
         else
            {
               LSAbove = 0;
               LSBelow = 1;
            }
         
         if(bCloud=1 && low[bar]>SenkouAArray[bar])
            {
               priceAboveCloud = 1;
               priceBelowCloud = 0;
            }
         else
            {
               priceAboveCloud = 0;
               priceBelowCloud = 1;
            }
         
         if(bTKCross == 1 && bCloud == 1 && LSAbove == 1 && priceAboveCloud == 1)
            {
               BuyBuffer[bar]=low[bar]-(50*Point());
            }
         if(sTKCross == 1 && sCloud == 1 && LSBelow == 1 && priceBelowCloud == 1)
            {
               SellBuffer[bar]=high[bar]+(50*Point());
            }   
         
      }


   return(rates_total);
  }

If anyone could help me out I'd greatly appreciate it.

Thanks in advance,
Adam

 
Adam Woodcock:

Hi I'm learning to code custom indicators, I am trying to do one for Ichimoku where the indicator must meet a checklist of 4 items:

1) TK Cross

2) Price relative to cloud

3) Favourable cloud (bullish/bearish)

4) Lag span above cloud

I have the following code with no errors however critical Array out of Range errors upon running:

If anyone could help me out I'd greatly appreciate it.

Thanks in advance,
Adam

 if(prev_calculated==0)
      first=Senkou_B+1;
   else first=prev_calculated+1;
   
   CopyBuffer(ichihandle,0,0,Senkou_B+1,TenkanArray);
   ArraySetAsSeries(TenkanArray,true);
   
...
   for(bar = first; bar >=0; bar--)

Your are copying "Senkou_B+1" values, which are indexed from 0 tp Senkou_B, that's your range.

But your are using an index "bar" starting from limit which is initalized to Senkou_B1+1 or prev_calculated+1 => out of range.