Error when call 2 times CopyBuffer()

 

Hello ,

This custom indicator should draw 7 & 20 period SMA

#property indicator_chart_window
#property indicator_buffers 2 
#property indicator_plots   2
#property indicator_label1  "iMA1" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrWhite 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
#property indicator_label2  "iMA2" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//
input int ma_period1=7, ma_period2=20;
double  iMABuffer1[], iMABuffer2[]; 
int handle1, handle2, index1=0, index2=1; 
//
int OnInit(){
   SetIndexBuffer(index1, iMABuffer1, INDICATOR_DATA); 
   PlotIndexSetInteger(index1, PLOT_SHIFT, 0); 
   handle1 = iMA(NULL, 0, ma_period1, 0, MODE_SMA, PRICE_CLOSE);  
   if(handle1 == INVALID_HANDLE) return(INIT_FAILED); 
   
   SetIndexBuffer(index2, iMABuffer2, INDICATOR_DATA); 
   PlotIndexSetInteger(index2, PLOT_SHIFT, 0); 
   handle2 = iMA(NULL, 0, ma_period2, 0, MODE_SMA, PRICE_CLOSE);  
   if(handle2 == INVALID_HANDLE) return(INIT_FAILED); 
   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 x, y, errx, erry;
   ResetLastError();
   x = CopyBuffer(handle1, index1, 0, 25, iMABuffer1);
   errx = GetLastError();  
   ResetLastError(); 
   y = CopyBuffer(handle2, index2, 0, 25, iMABuffer2);
   erry = GetLastError();
   Comment(x + " " + errx + " " + y + " " + erry);
   if (x<0 || y<0) return(0);
   return(rates_total);
} 
//  
void OnDeinit(const int reason) { 
   if(handle1 != INVALID_HANDLE) IndicatorRelease(handle1); 
   if(handle2 != INVALID_HANDLE) IndicatorRelease(handle2); 
   Comment("");
}     

If  first call on CopyBuffer() works the second one return -1.

With error number 4806 (ERR_INDICATOR_DATA_NOT_FOUND : Requested data not found)

How to resolve it


 
   y = CopyBuffer(handle2, index2, 0, 25, iMABuffer2);

Wrong parameter.

 
Alain Verleyen #:

Wrong parameter.

Hello 

I set index2 = 1 and start_position = 0 

If it isn't correct what are they

Thanks for your reply

 
paul selvan #:

Hello 

I set index2 = 1 and start_position = 0 

If it isn't correct what are they

Thanks for your reply

Continue to think and check the documentation of CopyBuffer, you will understand.
 

By setting index1= index2 = 0  (same  indicator buffer number) it's ok