need help on CopyBuffer

 

When I calc other timeframe indicator with CopyBuffer function, there is always error 4806.

below is the example code, Can anybody help, thanks.


#property copyright   "kendobrat"
#property link        "http://www.mql5.com"
#property description "Double Relative Strength Index"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 50
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_color2  clrLime
#property indicator_levelcolor clrMagenta

input int InpPeriodRSI=14;

double    ExtRSI_1[];
double    ExtRSI_2[];

int       nHandle_1;
int       nHandle_2;

int       ExtPeriodRSI;

void OnInit()
  {

   if(InpPeriodRSI<1)
     {
      ExtPeriodRSI=14;
      Print("Incorrect value for input variable InpPeriodRSI =",InpPeriodRSI,
            "Indicator will use value =",ExtPeriodRSI,"for calculations.");
     }
   else ExtPeriodRSI=InpPeriodRSI;

   SetIndexBuffer(0,ExtRSI_1,INDICATOR_DATA);
   SetIndexBuffer(1,ExtRSI_2,INDICATOR_DATA);

   IndicatorSetInteger(INDICATOR_DIGITS,2);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodRSI);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtPeriodRSI);
   IndicatorSetString(INDICATOR_SHORTNAME,"DOUBLE RSI("+string(ExtPeriodRSI)+")");

   nHandle_1 = iRSI(Symbol(),0,ExtPeriodRSI,PRICE_TYPICAL);
   nHandle_2 = iRSI(Symbol(),PERIOD_D1,ExtPeriodRSI,PRICE_TYPICAL);
   
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   if (CopyBuffer(nHandle_1,0,0,rates_total - ExtPeriodRSI - 1,ExtRSI_1)<0) printf("error code 1 #" + GetLastError());
   if (CopyBuffer(nHandle_2,0,0,rates_total - ExtPeriodRSI - 1,ExtRSI_2)<0) printf("error code 2 #" + GetLastError());
   return(rates_total);
  }




always happens error code 2 #4806

 
kendobrat:

When I calc other timeframe indicator with CopyBuffer function, there is always error 4806.

below is the example code, Can anybody help, thanks.





always happens error code 2 #4806

and handle is fine,   I already checked with 

if (nHandle_2 == INVALID_HANDLE) {printf("handle error 2");}
 

Use BarsCalculated() to check if data has been loaded. Also keep in mind that PERIOD_D1 has a different rates_total count than the chart timeframe. 

   if (CopyBuffer(nHandle_1,0,0,rates_total - ExtPeriodRSI - 1,ExtRSI_1)<0) printf("error code 1 #" + GetLastError());
   int ratesTotal=Bars(_Symbol,PERIOD_D1);
   if(BarsCalculated(nHandle_2)<ratesTotal)  return(0);
   if (CopyBuffer(nHandle_2,0,0,ratesTotal - ExtPeriodRSI - 1,ExtRSI_2)<0) printf("error code 2 #" + GetLastError());
   return(rates_total);
 
Ernst Van Der Merwe:

Use BarsCalculated() to check if data has been loaded. Also keep in mind that PERIOD_D1 has a different rates_total count than the chart timeframe. 

changed codes like this

   int ratesTotal=Bars(_Symbol,PERIOD_D1);
   if (CopyBuffer(nHandle_1,0,0,rates_total - ExtPeriodRSI - 1,ExtRSI_1)<0) printf("error code 1 #" + GetLastError());
   if (CopyBuffer(nHandle_2,0,0,ratesTotal - ExtPeriodRSI - 1,ExtRSI_2)<0) printf("error code 2 #" + GetLastError());
   if(BarsCalculated(nHandle_2)<ratesTotal)  printf("calculated bars " + BarsCalculated(nHandle_2) + "   ratestotal " + ratesTotal);

and message shows     

calculated bars -1      ratestotal 2487


data has not been loaded , what should I do?

thanks.

 
kendobrat:

changed codes like this

and message shows     

calculated bars -1      ratestotal 2487


data has not been loaded , what should I do?

thanks.

If data hasn't been loaded, return(0); and wait for the next tick.

   if (CopyBuffer(nHandle_1,0,0,rates_total - ExtPeriodRSI - 1,ExtRSI_1)<0) printf("error code 1 #" + GetLastError());
   int ratesTotal=Bars(_Symbol,PERIOD_D1);
   if(BarsCalculated(nHandle_2)<ratesTotal)  return(0);
   if (CopyBuffer(nHandle_2,0,0,ratesTotal - ExtPeriodRSI - 1,ExtRSI_2)<0) printf("error code 2 #" + GetLastError());
   printf("calculated bars %d  ratestotal %d",BarsCalculated(nHandle_2),ratesTotal);
   return(rates_total);
 
Ernst Van Der Merwe:

If data hasn't been loaded, return(0); and wait for the next tick.

thanks ,  problem solved.

Reason: