Error Array out of range in an custom indicator

 

Dear Members 

I have failed to identify the cause 'array out of range' error in my custom indicator code as below:

I understand that custom indicators handle the array sizing on it own, and hence did not attempted to set the size.

  input int                                                     ATRPeriod = 13;         // Averaging Period for ATR

  double BufferPATR[];                                                                          // ATR AS PERCENTAGE
  double BufferTR[];                                                                                    // TRUE RANGE

int OnInit() {

                string name = StringFormat("iATR as %%Based Oscillator(%i) ",ATRPeriod);
    IndicatorSetString(INDICATOR_SHORTNAME,name);
    IndicatorSetInteger(INDICATOR_DIGITS,5);
    IndicatorSetInteger(INDICATOR_HEIGHT,150);

    SetIndexBuffer(0,BufferPATR,INDICATOR_DATA);
    SetIndexBuffer(1,BufferTR,INDICATOR_CALCULATIONS);

    PlotIndexSetString(0,PLOT_LABEL,"ATR%("+(string)ATRPeriod+")");
    PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
    PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
    PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrLightSeaGreen);
    PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);
    PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
  //---
   return(INIT_SUCCEEDED);

} // End of OnInit()
//+----------------------------------------------------------------------------------------------------------+
//| 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 <= ATRPeriod)
                        return(0.00);

                int i,start;

                // Preliminary Calculation
                if(prev_calculated == 0) {

                        BufferTR[0]    = 0.00;		... causing ArrayOut of range error
                        BufferPATR[0] = 0.00;

                        // Fill array of True Range values for each period
                        for(i = 1; i < rates_total && !IsStopped(); i++) {
                                BufferTR[i] = (MathMax(high[i],close[i-1]) - MathMin(low[i],close[i-1])) / close[i-1] * 100;
                        }

                        // First ATR Period Values of indicator is not calculated
                        double firstValue = 0.00;
                        for(i =1; i <= ATRPeriod; i++) {
                                BufferPATR[i] = 0.00;
                                firstValue += BufferPATR[i];
                        }

                        // Calculate the first value of the indicator
                        firstValue /= ATRPeriod;
                        BufferPATR[ATRPeriod] = firstValue;
                        start = ATRPeriod + 1;
                }
                else
                        start = prev_calculated - 1;

                // Main loop of Calculation
                for(i = start; i < rates_total && !IsStopped(); i ++) {
                        BufferTR[i] = (MathMax(high[i],close[i-1]) - MathMin(low[i],close[i-1])) / close[i-1] * 100;
                        BufferPATR[i] = BufferPATR[i-1] + (BufferTR[i] - BufferTR[i-ATRPeriod]) / ATRPeriod;
                }

    return(rates_total);

} // End of method OnCalculate()
 
Check your "indicator buffer" is set to 2 or greater
 
Thank-god Avwerosuoghene Odukudu #:
Check your "indicator buffer" is set to 2 or greater

@Thank-god Avwerosuoghene Odukudu 

yes it is working now. What is silly mistake I made :)

Thanks a lot man.

 
Thank-god Avwerosuoghene Odukudu #:
Check your "indicator buffer" is set to 2 or greater

I have the same problem.
would you please to explain more?
I can't understand this: " Check your "indicator buffer" is set to 2 or greater".

this is the code:

*** Attention: I just try to sketched Tenkansen (H1)

<Improperly formatted code removed by moderator>
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. double    TenkanBuffer_HTF_Temp[];
    
    double    KijunBuffer_HTF_Temp[];
    
    double    SpanABuffer_HTF_Temp[];
    
    double    SpanBBuffer_HTF_Temp[];
    
    double    TenkanBuffer_HTF[];
    
    double    KijunBuffer_HTF[];
    
    double    SpanABuffer_HTF[];
    
    double    SpanBBuffer_HTF[];

    These are not buffers. They are plain arrays.

  3.   handle_Ichi_HTF_1 = iIchimoku(_Symbol, Period_HTF_1, InpTenkan, InpKijun, InpSenkouB);
    
       if(CopyBuffer(handle_Ichi_HTF_1, 0, 0, 2, TenkanBuffer_HTF_Temp)<0)

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

 
  1. Now you have changed your original post, resulting in the above reply meaningless.

  2. Always post all relevant code (using Code button) or attach the source file.

    How many buffers did you declare you have. You have not posted all relevant code. You were previously asked in #1 and failed to answer.