Second Indicator reads buffer from first indicator, but the data is wrong.

 

Hi. I have been having an issue with a couple indicators I am working on. The second indicator is to read a buffer from the first indicator.

The first indicator has a buffer created to hold the data.

The second indicator creates an array to receive a copy of the buffer from the first indicator.

I have simplifed the code to the bare minimum, and the problem still exists and can be reproduced.

The second indicator gets a series of zeros on most timeframes, and a bunch of random data on other timelines. But, it is being sent this data, 123.123, in each


I gutted the two indicators, and made a very simple version of the attempt to have one indicator read a buffer from another custom indicator.

Here is the full code of each indicator.


Main Indicator:

//A_indicator_Main_Window.mq5


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 0


double         TrendData[];

int ThisBarTrade = 0;  

int OnInit()
  {
   SetIndexBuffer(0,TrendData,INDICATOR_DATA);
   return(INIT_SUCCEEDED);
}

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(prev_calculated==0){
      for(int j=0;j<14;j++){
         TrendData[j] = 123.123;
         Alert("TrendData[", j, "] = ", TrendData[ThisBarTrade]);
      }
   } else {
   
      if( Bars(_Symbol,_Period) != ThisBarTrade) {
         for(int j=0;j<14;j++){
            TrendData[j] = 123.123;
            Alert("TrendData[", j, "] = ", TrendData[ThisBarTrade]);
         }

       }
   }
   return(rates_total);
}




Second Indicator, which attempts to read a buffer from the first indicator

//B_Indicator_Data_Window.mq5

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 0

double TrendDataBuffer[];

int ThisBarTrade = 0;  
int TrendDataHandle = 0;

int OnInit(){
     ArrayResize(TrendDataBuffer,14);
     ArrayInitialize(TrendDataBuffer,EMPTY_VALUE);
     
     TrendDataHandle = iCustom(NULL,NULL,"A_indicator_Main_Window.ex5",5);
     if(TrendDataHandle == -1){
         Alert("A_Indicator_Main_Window.ex5 could not be accessed. TrendDataHandle = ",TrendDataHandle,"  error = ",GetLastError());
     }
   return(0);
}
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[]){
   
   int CopyBufferSize = 0;
   if(prev_calculated==0){
      if( Bars(_Symbol,_Period) != ThisBarTrade) {
    
         ThisBarTrade = Bars(_Symbol,_Period);
         CopyBufferSize =  CopyBuffer(TrendDataHandle,0,0,14,TrendDataBuffer);     
         Alert("CopyBufferSize is ", CopyBufferSize);
         if(CopyBufferSize > 0){  
            for( int i =0 ; i < CopyBufferSize; i++){
                  Alert("TrendDataBuffer[", i, "] = ", TrendDataBuffer[i]);
            }
         }
            
     }
  }
  return(rates_total);
}



On some timeframes, the copied buffer is populated with all zeros. On other timeframes, the copied buffer is full of random data.

I must be overlooking something quite simple here. But, in reviewing good code that I know works, and comparing to this code, I cannot find the differences.

 
When you post code please use the SRC button! Please edit your post.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
 
whroeder1:
When you post code please use the SRC button! Please edit your post.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
Done. I wasn't aware of that. It looks so much easier to read.
 
gary0318:

Hi. I have been having an issue with a couple indicators I am working on. The second indicator is to read a buffer from the first indicator.

The first indicator has a buffer created to hold the data.

The second indicator creates an array to receive a copy of the buffer from the first indicator.

I have simplifed the code to the bare minimum, and the problem still exists and can be reproduced.

The second indicator gets a series of zeros on most timeframes, and a bunch of random data on other timelines. But, it is being sent this data, 123.123, in each


I gutted the two indicators, and made a very simple version of the attempt to have one indicator read a buffer from another custom indicator.

Here is the full code of each indicator.


Main Indicator:




Second Indicator, which attempts to read a buffer from the first indicator




On some timeframes, the copied buffer is populated with all zeros. On other timeframes, the copied buffer is full of random data.

I must be overlooking something quite simple here. But, in reviewing good code that I know works, and comparing to this code, I cannot find the differences.

You are assigning values to first elements of the buffer. Copy buffer will retrieve values from the last elements of the buffer - see how it does it : https://www.mql5.com/en/docs/series/copybuffer . So, you are retrieving values from last elements of the buffer (which are not assigned any values by you if the chart has more than 27 bars on chart)

Assign values to the last 14 elements of the buffer and it should work

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:

You are assigning values to first elements of the buffer. Copy buffer will retrieve values from the last elements of the buffer - see how it does it : https://www.mql5.com/en/docs/series/copybuffer . So, you are retrieving values from last elements of the buffer (which are not assigned any values by you if the chart has more than 27 bars on chart)

Assign values to the last 14 elements of the buffer and it should work

Thanks, so much. That did it for me.
Reason: