Why I can not get the corrrect indicators value in EA testing ?

 

Why I can not get the corrrect indicators value in EA testing ?

I tested the following simple indicator test_1.mq4

//+------------------------------------------------------------------+
//|                                                       test_1.mq4 |
//|                                                             test |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "test"
#property link      "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- buffers
double ExtMapBuffer1[];
double temp[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);   
   ArraySetAsSeries(temp,true);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
     int limit;
     int counted_bars=IndicatorCounted(); 
     if(counted_bars<0) return(-1); 
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
//----
      ArrayResize(temp,Bars);
     
      for(int i=0;i<limit;i++)     
        temp[i]= iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,i);  
      
      for(i=0;i<limit;i++)
       ExtMapBuffer1[i]= iMAOnArray(temp,Bars,13,0,MODE_EMA,i);// 
       
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

I attached the indicator to chart for testing. it showed as below after testing a while.

 

I paused tesint, and attached the indicator again for comparing.

it showed the different line,why ?

 

If I changed temp[] to indicatorbuffer,then the probem will be resolved.

 

Changed temp[] to indicatorbuffer:

//+------------------------------------------------------------------+
//|                                                       test_2.mq4 |
//|                                                             test |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "test"
#property link      "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- buffers
double ExtMapBuffer1[];
double temp[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(2);  // 
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);  
   SetIndexBuffer(1,temp); // 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
     int limit;
     int counted_bars=IndicatorCounted(); 
     if(counted_bars<0) return(-1); 
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
//----
     
      for(int i=0;i<limit;i++)     
        temp[i]= iMA(NULL,0,30,0,MODE_EMA,PRICE_CLOSE,i);  
      
      for(i=0;i<limit;i++)
       ExtMapBuffer1[i]= iMAOnArray(temp,Bars,13,0,MODE_EMA,i);// 
       
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

Tested again....

 

paused testing,and loaed indicator again for comparing, it showed the same thing.

 

if i use buffers more than 8, how to code correctly ?

 
      ArrayResize(temp,Bars);
bool    ResizeBuffer(double& buffer[], int size){
    if (ArraySize(buffer) != size){
        ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
        if (ArrayResize(buffer, size) <= 0){
            trading.disabled    = "ArrayResize [1] failed: "+GetLastError()
                                + " Trading disabled.";
            Alert(trading.disabled);    Print(trading.disabled);
            return(false);  }
        ArraySetAsSeries(buffer, true);
    }
    return(true);
}
 
Read this is should help you: https://www.mql5.com/en/articles/1456
 
RaptorUK:
Read this is should help you: https://www.mql5.com/en/articles/1456

Thank you.
Reason: