CopyBuffer error for other time frame rather than chart time frame. Any solutions ?

 

This is MQL5 language questions.

I guess this have been asked so many times by different coders.

But I try once more if MQL5.com can fix this in the near future.



The concept of handle and CopyBuffer works if you want to copy the indicator array for the same time frame as your chart.

However, if you are trying to copy indicator array for different time frame to your chart, then it fails. 


For example, if you want to copy indicator array for Daily time frame and if your chart is Hourly time frame, then CopyBuffer always fails. It works if you copy indicator buffer of hourly and you are using hourly chart.


Could we please fix this errors ? or any work around solution ?

Hopefully I don't have to post the same questions over and over again.


//This is called in OnInit function ================================

    handle_CCI      = iCCI     (Symbol(), YourTimeFrame, IndicatorPeriod, PRICE_TYPICAL);

   

//This is called at OnCalcualte Function =============================

   double temp[];
   ArrayInitialize(temp, 0.0);
   
   //s1 always comes back as -1, temp array never get filled with CCI value. 
   int s1 = CopyBuffer(handle_CCI, 0, 0, 10, temp); 
 

It returns -1 because the data hasn't been loaded yet. To prevent this from happening use BarsCalculated().

int handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   handle=iCCI(_Symbol,PERIOD_D1,14,PRICE_TYPICAL);
//---
   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[])
  {
//---
   if(BarsCalculated(handle)<10)
      return(0);
//---
   double Buffer[];
   printf("Total copied = %d",CopyBuffer(handle,0,0,10,Buffer));
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Ernst Van Der Merwe:

It returns -1 because the data hasn't been loaded yet. To prevent this from happening use BarsCalculated().

How great you are.

Very good suggestion.

Thanks so much.