Why CopyBuffer fails with multiple indicators (error 4806)?

 

I just created a simple indicator with two moving averages, but CopyBuffer for the second indicator keep failing, so the terminal shows only the first moving average line instead of both.

The code is pretty simple textbook example:

// Configuration.
#property copyright "<COPYRIGHT>"
#property link "<LINK>"
#property version "1.0"
#property indicator_chart_window

#property indicator_buffers 2
#property indicator_plots 2
double buffer_1[];
double buffer_2[];

#property indicator_label1  "Moving Average (6)" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrWhite 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1

#property indicator_label2  "Moving Average (18)" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrWhite
#property indicator_style2  STYLE_DASH 
#property indicator_width2  1

int handle_1;
int handle_2;


int OnInit()
{       
        handle_1 = iMA(
                Symbol(),               // Symbol
                Period(),               // Timeframe
                6,                      // Period
                0,                      // Shift
                MODE_EMA,               // Method
                PRICE_CLOSE     	// Price
        );

        handle_2 = iMA(
                Symbol(),               // Symbol
                Period(),               // Timeframe
                18,                     // Period
                0,                      // Shift
                MODE_EMA,               // Method
                PRICE_CLOSE     	// Price
        );

        SetIndexBuffer(0, buffer_1, INDICATOR_DATA);
        SetIndexBuffer(1, buffer_2, INDICATOR_DATA);

        return (INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
        IndicatorRelease(handle_1);
        IndicatorRelease(handle_2);
}

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[])
{
        ResetLastError();
        int copiedAmount_1 = CopyBuffer(
                handle_1,               // Handle
                0,                      // Index
                0,                      // Start
                rates_total,    	// Amount
                buffer_1                // Target
        );
        PrintFormat("Copied (%i) amount to buffer_1 from handle_1 (%i). GetLastError: (%d).", copiedAmount_1, handle_1, GetLastError());

        ResetLastError();
        int copiedAmount_2 = CopyBuffer(
                handle_2,               // Handle
                1,                      // Index
                0,                      // Start
                rates_total,    	// Amount
                buffer_2                // Target
        );
        PrintFormat("Copied (%i) amount to buffer_2 from handle_2 (%i) GetLastError: (%d).", copiedAmount_2, handle_2, GetLastError());
        
        return(rates_total);
}

It always shows:

2019.08.05 11:49:50.958 TCE (EURUSD,H1) Copied (100009) amount to buffer_1 from handle_1 (10). GetLastError: (0).
2019.08.05 11:49:50.958 TCE (EURUSD,H1) Copied (-1) amount to buffer_2 from handle_2 (11) GetLastError: (4806).

Please help, I'm trial and erroring for long hours now.

 

Second parameter is not index but buffer number. There is no buffer 1 for iMA

Check here : https://www.mql5.com/en/docs/series/copybuffer

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
Counting of elements of copied data (indicator buffer with the index buffer_num) from the starting position is performed from the present to the past, i.e., starting position of 0 means the current bar (indicator value for the current bar). When copying the yet unknown amount of data, it is recommended to use a dynamic array as a buffer[]...
 
Mladen Rakic:

Second parameter is not index but buffer number. There is no buffer 1 for iMA

Check here : https://www.mql5.com/en/docs/series/copybuffer

Thanks for getting back!

What you mean "There is no buffer 1 for iMA"?

I thought I bound a buffer via SetIndexBuffer(1, buffer_2, INDICATOR_DATA); (to index 1).


Or is it something that the iMA documentation says here - https://www.mql5.com/en/docs/indicators/ima - "... It has only one buffer..."?

How should I maintain multiple buffers of moving averages then?

I'm planning to make juicy calculations using those buffers, but also would like to display each.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                              https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
 
Geri_: What you mean "There is no buffer 1 for iMA"?

I thought I bound a buffer via SetIndexBuffer(1, buffer_2, INDICATOR_DATA); (to index 1).

Or is it something that the iMA documentation says here - https://www.mql5.com/en/docs/indicators/ima - "... It has only one buffer..."?

Not your buffer_2. iMA has no buffer two — you already linked to the documentation — only one buffer. You are trying to copy iMA to your buffer_2.

 

Thanks, I thought the buffer index parameter of CopyBuffer referenced the target buffer index (the one I set with  SetIndexBuffer).

Now I can see that it rather references the source - iMA - buffer index. Which has only one, so it should be  0 at both call.

 

hello coders ;

sorry my english;

i have run time ERROR 4007 ;i think error come line141

i don't understand why handle3 get this ERROR in copybuffer() function

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20
#property indicator_level2 50
#property indicator_level3 80
#property indicator_buffers 9
#property indicator_plots 1
#property indicator_color1 clrAliceBlue


//---- input parameters
input int FastMA=12;
input int SlowMA=24;
input int Crosses=50;
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price
input ENUM_APPLIED_PRICE InpAppliedPrice2=PRICE_TYPICAL; // Applied price2

//---- buffers
double MA[];
double MCD1[];
double MCD2[];
double MAfast[],MAslow[];
double Cross[];
double max_min[];
double PointDeviation[];
double PeriodTimeAVG[];

//---- var
double smconst,ST,max,min;
int ShiftFirstCross;
int ShiftCrossesCross;
int k,handle1,handle2,handle3,handle4;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {


   SetIndexBuffer(0, MA,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
   PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
   SetIndexBuffer(1, MCD1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(1, MCD2,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, MAfast,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, MAslow,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5, Cross,INDICATOR_CALCULATIONS);
   SetIndexBuffer(6, max_min,INDICATOR_CALCULATIONS);
   SetIndexBuffer(7, PointDeviation,INDICATOR_CALCULATIONS);
   SetIndexBuffer(8, PeriodTimeAVG,INDICATOR_CALCULATIONS);
   IndicatorSetString(INDICATOR_SHORTNAME,"cycle");
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,0.0);
   handle1=iMA(NULL,PERIOD_CURRENT,FastMA,0,MODE_SMA,PRICE_CLOSE);
   handle2=iMA(NULL,PERIOD_CURRENT,SlowMA,0,MODE_SMA,PRICE_CLOSE);
   handle3=iMA(NULL,PERIOD_CURRENT,FastMA,0,MODE_EMA,PRICE_TYPICAL);
   handle4=iMA(NULL,PERIOD_CURRENT,SlowMA,0,MODE_EMA,PRICE_TYPICAL);



   ShiftFirstCross=0;
   ShiftCrossesCross=0;
   k=0;
   max=0.;
   min=1000000.;
  }
//+------------------------------------------------------------------+
//| Schaff Trend Cycle                                               |
//+------------------------------------------------------------------+
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 counted_bars=prev_calculated-1;
   int i,j,limit,NumberCross,BarsCross;
   double prev,MinMACD,MaxMACD,delta,Sum_max_min;

   if(rates_total<=SlowMA)
      return(-1);


   if(counted_bars>0)
      counted_bars--;
   limit=rates_total-counted_bars;
   if(limit>rates_total-SlowMA-1)
      limit=rates_total-SlowMA-1;
//+------------------------------------------------------------------+
  
   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++;
     }
//+------------------------------------------------------------------+
//--- get handle1 buffer
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(handle1,0,0,to_copy,MAfast)<=0)
     {
      Print("Getting handle1 is failed! Error",GetLastError());
      return(0);
     }
//--- get handle2 buffer
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(handle2,0,0,to_copy,MAslow)<=0)
     {
      Print("Getting handle2 is failed! Error",GetLastError());
      return(0);
     }
//--- get handle3 buffererror
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(handle3,0,0,to_copy,MCD1)<=0)
     {
      Print("Getting handle3 is failed! Error",GetLastError());
      return(0);
     }
     
//--- get handle4 buffererror
   if(IsStopped())
      return(0); //Checking for stop flag
   if(CopyBuffer(handle4,0,0,to_copy,MCD2)<=0)
     {
      Print("Getting handle4 is failed! Error",GetLastError());
      return(0);
     }

   ArraySetAsSeries(MAfast,true);
   ArraySetAsSeries(MAslow,true);
   ArraySetAsSeries(MCD1,true);
   ArraySetAsSeries(MCD2,true);
//+------------------------------------------------------------------+
//--- not all data may be calculated
   int calculated=BarsCalculated(handle1);
   if(calculated<rates_total)
     {
      Print("Not all data of handle1 is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(handle2);
   if(calculated<rates_total)
     {
      Print("Not all data of handle2 is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(handle3);
   if(calculated<rates_total)
     {
      Print("Not all data of handle3 is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(handle4);
   if(calculated<rates_total)
     {
      Print("Not all data of handle4 is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   for(i=limit; i>0; i--)
     {
      Cross[i]=0.;
      if(MAfast[i]>=MAslow[i] && MAfast[i+1]<MAslow[i+1])
        {

         if(ShiftFirstCross==0)
            ShiftFirstCross=i;

         if(ShiftCrossesCross==0)
           {
            k++;

            if(k==Crosses+1)
               ShiftCrossesCross=i;
           }

         Cross[i]=1.;

         max_min[i]=max-min;

         max=0.;
         min=1000000.;
        }
      else
         if(MAfast[i]<=MAslow[i] && MAfast[i+1]>MAslow[i+1])
           {

            if(ShiftFirstCross==0)
               ShiftFirstCross=i;

            if(ShiftCrossesCross==0)
              {
               k++;

               if(k==Crosses+1)
                  ShiftCrossesCross=i;
              }

            Cross[i]=-1.;

            max_min[i]=max-min;

            max=0.;
            min=1000000.;
           }
         else
           {
            if(max<high[i])
               max=high[i];
            if(min>low[i])
               min=low[i];
           }
     }
   if(limit>ShiftCrossesCross)
      limit=ShiftCrossesCross;
   for(i=limit; i>0; i--)
     {

      j=i;
      while(Cross[j]==0.)
         j++;

      NumberCross=0;
      BarsCross=0;
      Sum_max_min=0.;
      while(NumberCross<Crosses)
        {

         if(Cross[j]!=0.)
           {
            NumberCross++;
            Sum_max_min=Sum_max_min+max_min[j];
           }
         j++;
         BarsCross++;
        }


      PeriodTimeAVG[i]=BarsCross/Crosses;
      PointDeviation[i]=NormalizeDouble(Sum_max_min/Crosses/2./_Point,0);
     }
//+------------------------------------------------------------------+    
   for(i=limit; i>=0; i--)
     {
      MinMACD=(MCD1[i]-MCD2[i]);
      MaxMACD=(MCD1[i]-MCD2[i]);
      for(j=i+1; j<i+PeriodTimeAVG[i+1]; j++)
        {
         if((MCD1[j]-MCD2[j])<MinMACD)
            MinMACD=(MCD1[j]-MCD2[j]);
         if((MCD1[j]-MCD2[j])>MaxMACD)
            MaxMACD=(MCD1[j]-MCD2[j]);
        }

      delta=MaxMACD-MinMACD;
      if(delta==0.)
         ST=50.;
      else
        {
         ST=((MCD1[i]-MCD2[i])-MinMACD)/delta*100;
        }

      prev=MA[i+1];
      MA[i]=(2./(1.+PeriodTimeAVG[i+1]/2.))*(ST-prev)+prev;
     }

   return(rates_total);
  }

 

Error:

   SetIndexBuffer(1, MCD1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(1, MCD2,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, MAfast,INDICATOR_CALCULATIONS);


Correct code

   SetIndexBuffer(1, MCD1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2, MCD2,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, MAfast,INDICATOR_CALCULATIONS);
 
Vladimir Karputov:

Error:


Correct code

oooohhhhhh......i knew i mistake somewhere...thank you vladimir thank you !
 
William Roeder #:

Not your buffer_2. iMA has no buffer two — you already linked to the documentation — only one buffer. You are trying to copy iMA to your buffer_2.

Is it possible to have 10 handles in arrray[10] by for cycle?

         for(uchar x = 0; x < ArraySize(pairs); x++)

           {

           switch(f)

              {

               case 4:

                  h[x] = iCustom(pairsT[x], PERIOD_CURRENT, "indicator_1", 1);

                  break;

               case 5:

                  h[x] = iCustom(pairsT[x], PERIOD_CURRENT, "Valu indicator_2", r1);

                  break;

               case 6:

                  h[x] = iCustom(pairsT[x], PERIOD_CURRENT, "Valu indicator_3", r1);

                  break;

               case 7:

                  h[x] = iCustom(pairsT[x], PERIOD_CURRENT, "Examples\\indicator_4", 2);

etc.....


Reason: