Function to Calculate the Average value of N close prices

 
Hello everyone, I have a simple problem (not for a beginner like me. LOL).

I want to create a function that calculate the average of any of the  OHLC values for a N period, and be called within the "OnCalculate" function . 

The Function i programmed is the follow :

void Average(const double &buffer1[], const double &buffer2[],int N_candle, double &aver)
{
    double sum=0.0;
    for(int i=0;i<N_candle;i++)
    {
        sum=sum+(buffer1[i]+buffer2[i])/(2*N_candle);
    }
    aver=sum/N_candle;
}


Inside the int OnCalculate function i call it using the code:

   Average(high[i],low[i],12,aver);
   InferiorBuffer[i] = MediaBuffer[i] -  (Inferior/300)*aver  ;
   SuperiorBuffer [i] = MediaBuffer[i] +  (Superior/300)

 The MT5 compiler pop up the erros:



'high' - parameter conversion not allowed

'high' - array required

'low' - array required



Where is my mistake?

Thank you all!

 
Pedro Henrique Vieira:
Hello everyone, I have a simple problem (not for a beginner like me. LOL).

I want to create a function that calculate the average of any of the  OHLC values for a N period, and be called within the "OnCalculate" function . 

The Function i programmed is the follow :


Inside the int OnCalculate function i call it using the code:

 The MT5 compiler pop up the erros:



'high' - parameter conversion not allowed

'high' - array required

'low' - array required



Where is my mistake?

Thank you all!

Here is the problem:

Average(high[i],low[i],12,aver);

The input is an array, not a single double value, so you need to write it like:

Average(high,low,12,aver);
 
Ehsan Tarakemeh:

Here is the problem:

The input is an array, not a single double value, so you need to write it like:

Wow! So simple.

Thanks a lot!!!

 
  1.         sum=sum+(buffer1[i]+buffer2[i])/(2*N_candle);
        }
        aver=sum/N_candle;
    Sum your two buffers and divide by 2n outside the loop. Currently, you are dividing by 3n.
  2. Also, do you ever set aver to zero? Do it inside the function.
Reason: