Reading multiple indicator buffers

 

Hi


I'm facing an challenge to read multiple indicator buffers in my EA. CopyBuffer copies first inidicator but for rest, always returns -1. Here is test indicator code:

#property indicator_chart_window
#property indicator_buffers 3

double _b1[], _b2[], _b3[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   if(!SetIndexBuffer(0,_b1,INDICATOR_DATA)) return(INIT_FAILED);
   if(!SetIndexBuffer(1,_b2,INDICATOR_DATA)) return(INIT_FAILED);
   if(!SetIndexBuffer(2,_b3,INDICATOR_DATA)) return(INIT_FAILED);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
      
   for(int i=prev_calculated; i < rates_total; i++) {
      _b1[i] = 13.5;
      _b2[i] = 11.9;
      _b3[i] = 198.3;
   }
      
//--- return value of prev_calculated for next call
   return(rates_total);
  }

And here test EA code


int g_HPF3_handle;
double _b1[], _b2[], _b3[];


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  g_HPF3_handle = iCustom(Symbol(), PERIOD_CURRENT, "4buffertest");
  if (g_HPF3_handle == INVALID_HANDLE) {
      Print("Unable to intialize indicator. EA is terminating");
      return(INIT_FAILED);
  } else {
      SetIndexBuffer(0,_b1,INDICATOR_DATA);
      SetIndexBuffer(0,_b2,INDICATOR_DATA);
      SetIndexBuffer(0,_b3,INDICATOR_DATA);
      return(INIT_SUCCEEDED);
  }
//---
   
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  IndicatorRelease(g_HPF3_handle); 
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   int c1, c2, c3;
      
   ResetLastError();
   c1 = CopyBuffer(g_HPF3_handle,0,0,1,_b1);
   c2 = CopyBuffer(g_HPF3_handle,1,0,1,_b2);
   c3 = CopyBuffer(g_HPF3_handle,2,0,1,_b3);
   
   Print("B1" + IntegerToString(c1) + " B2" + IntegerToString(c2) + " B3" + IntegerToString(c3));
    
   
  }
//+------------------------------------------------------------------+
 
The SetIndexBuffer is not required on EA side.