Please help me. Why array out of range when I try to use iCustom for smaller timeframe.

 
Can anyone tell me why it print array out of range(number71,55)?Thx a lot(Want to calculate the average high minus low f or 20 days, the custom indicator in iCustom is the code itself)
#property indicator_chart_window

#property indicator_buffers 4
#property indicator_plots 4

double DayHigh[];
double DayLow[];
double DayAmplitudeBuffer[];
double AverageDayAmplitudeBuffer[];

int DayAmplitudeHandle;
double DayAmplitudeCurrentBuffer[];
double AverageDayAmplitudeCurrentBuffer[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,DayHigh,INDICATOR_DATA);
   SetIndexBuffer(1,DayLow,INDICATOR_DATA);
   SetIndexBuffer(2,DayAmplitudeBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,AverageDayAmplitudeBuffer,INDICATOR_DATA);

   DayAmplitudeHandle=iCustom(_Symbol,PERIOD_D1,"自訂\\12");

   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[])
  {
   CopyHigh(Symbol(),PERIOD_D1,TimeCurrent(),rates_total,DayHigh);
   CopyLow(Symbol(),PERIOD_D1,TimeCurrent(),rates_total,DayLow);
   
   for(int i=0; i<rates_total; i++)
     {
      DayAmplitudeBuffer[i]=StringToDouble(IntegerToString(StringToInteger(DoubleToString((DayHigh[i]-DayLow[i])/Point()))));
      AverageDayAmplitudeBuffer[i]=CalculateAverageDayAmplitude(i);
     }
   CopyBuffer(DayAmplitudeHandle,2,0,rates_total,DayAmplitudeCurrentBuffer);
   CopyBuffer(DayAmplitudeHandle,3,0,rates_total,AverageDayAmplitudeCurrentBuffer);

   datetime Time[];
   CopyTime(_Symbol,PERIOD_D1,0,rates_total,Time);

   int pos=0;
   for(int i=0; i<rates_total; i++)
     {
      if(time[i]>Time[pos]) //i for smaller timeframe, pos for PERIOD_D1, when time i bigger than time pos, pos will plus1 to make effect like mutitimeframe indicator
        {
         pos++;
        }
      DayAmplitudeBuffer[i]=DayAmplitudeCurrentBuffer[pos];
      AverageDayAmplitudeBuffer[i]=AverageDayAmplitudeCurrentBuffer[pos];
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
double CalculateAverageDayAmplitude(int StartBar)
  {
   double DayAmplitudeSum=0;
   for(int pos=StartBar-20; pos<StartBar; pos++)
     {
      if(StartBar-20<0)
        {
         return(0);
        }
      DayAmplitudeSum=DayAmplitudeSum+DayAmplitudeBuffer[pos];
     }
   return(DayAmplitudeSum/20);
  }
//+------------------------------------------------------------------+

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
Good morning
In general, "out of ranges" are quite simple to resolve.

Either the array is not created, or you are trying to position yourself on an index that is outside the current size.

You can therefore look very easily with an ArrayPrint to see if it is created and if the index exists.

Otherwise do a print to see the value which gives the index.

Good luck
 
Gerard Willia G J B M Dinh Sy #:
Good morning
In general, "out of ranges" are quite simple to resolve.

Either the array is not created, or you are trying to position yourself on an index that is outside the current size.

You can therefore look very easily with an ArrayPrint to see if it is created and if the index exists.

Otherwise do a print to see the value which gives the index.

Good luck

Thanks for your help, but still have question. I tried arrayprint and print the array[rates_total-1], it seems the handle works. But when I change timeframe it will print array out of range first then print the value. I tried arraysize but still can't find what's the problem, if it's able can you help me to find out the problem, thx!

 
I don't know.
You need to do tests in the code
All the print commands, Comment(), breakpoints to try to follow what's happening as best as possible.
Sorry I couldn't do more
 
Gerard Willia G J B M Dinh Sy #:
I don't know.
You need to do tests in the code
All the print commands, Comment(), breakpoints to try to follow what's happening as best as possible.
Sorry I couldn't do more

That's Ok. Thanks a lot for your help. :) 

 
Gerard Willia G J B M Dinh Sy #:
I don't know.
You need to do tests in the code
All the print commands, Comment(), breakpoints to try to follow what's happening as best as possible.
Sorry I couldn't do more
It seems that the problem is Time[] array, because after Copytime I use arraysize to print, no matter how i change timeframe it will run array out of range first and arraysize fixed to be period day, not sure if the reason is this. Do you have better way to put array into multitimeframe? Thanks.
 
kenny0779 # :
It seems that the problem is Time[] array, because after Copytime I use arraysize to print, no matter how i change timeframe it will run array out of range first and arraysize fixed to be period day, not sure if the reason is this. Do you have better way to put array into multitimeframe? Thanks.

Good morning
If you are referring to this time[]

 int OnCalculate ( const int rates_total,
                 const int prev_calculated,
                 const datetime &time[],

You can't do anything about it.
It's a system constant

 
Gerard Willia G J B M Dinh Sy #:

Good morning
If you are referring to this time[]

You can't do anything about it.
It's a system constant

Oh!, I mean the Time array I customed, but actually I'm not sure whether the problem is CopyBuffer or the Time[] array I customed now.

 
kenny0779 # :
I'm not sure whether the problem is CopyBuffer or the Time[] array I customed now.

You need to do error management to remove all doubt.
Copyffer returns the number of elements copied.

 
kenny0779:
Can anyone tell me why it print array out of range(number71,55)?Thx a lot(Want to calculate the average high minus low f or 20 days, the custom indicator in iCustom is the code itself)
Why not just run the program in debug mode, then you can catch the error and see the array dimension and index.
 
Nikolai Semko #:
Why not just run the program in debug mode, then you can catch the error and see the array dimension and index.

Because I still can't find the problem. :(

Reason: