use of MathMean function

 

Hello all,

I need to calculate the average value of some values inside a vector. Let me explain better:

#1 CopyBuffer(funz_RSI,0,0,20,value_RSI)

With the above declaration I have the vector value_RSI composed of 20 values.

I want to calculate the average value with the first 10 value of value_RSI, and then the average value with the last 10 value of value_RSI

So I do:

#2 lastRSI_avg=MathMean(value_RSI,0,10);
#3 preRSI_avg=MathMean(value_RSI,11,20);

The problem is that it returns an error.

#4 lastRSI_avg=MathMean(value_RSI);
#5 preRSI_avg=MathMean(value_RSI);

Without any indication of parameters it works, but it does the average of all the values.


I found there is this definition:

https://www.mql5.com/en/docs/standardlibrary/mathematics/stat/array_stat/mathmean

but I've not understood how it works.

Could someone explain me how to do?


Thanks

Documentation on MQL5: Standard Library / Mathematics / Statistics / Statistical Characteristics / MathMean
Documentation on MQL5: Standard Library / Mathematics / Statistics / Statistical Characteristics / MathMean
  • www.mql5.com
Standard Library / Mathematics / Statistics / Statistical Characteristics / MathMean - Reference on algorithmic/automated trading language for MetaTrader 5
 

Here is the apparent discrepancy between the help and the standard library.

MathMean (MQL5 help):

Calculates the mean (first moment) of array elements. Analog of themean()in R.

double  MathMean(
   const double&  array[],               // array with data
   const int      start=0,               // initial index 
   const int      count=WHOLE_ARRAY      // the number of elements

   );

Parameters

array

[in]  Array with data for calculation of the mean.

start=0

[in]  Initial index for calculation.

count=WHOLE_ARRAY

[in]  The number of elements for calculation.

Return Value

The mean of array elements. In case of error it returnsNaN(not a number).


But the standard library (\MQL5\include\Math\Stat\Math.mqh):

//+------------------------------------------------------------------+
//| Computes the mean value of the values in array[]                 |
//+------------------------------------------------------------------+
double MathMean(const double &array[])
  {
   int size=ArraySize(array);
//--- check data range
   if(size<1)
      return(QNaN); // need at least 1 observation
//--- calculate mean
   double mean=0.0;
   for(int i=0; i<size; i++)
      mean+=array[i];
   mean=mean/size;
//--- return mean
   return(mean);
  }

As you can see in the standard library there are no parameters


   const int      start=0,               // initial index 
   const int      count=WHOLE_ARRAY      // the number of elements



 
Report on ServiceDesk.
 
Vladimir Karputov:
Report on ServiceDesk.

So?

I have not understood how to solve my issue. Thanks

 
managertop40 :

So?

I have not understood how to solve my issue. Thanks


I filed a report. We are waiting for a response from the Service Desk.
 

Ok, great!!


Thanks for your help!


P.S. : sorry, I've not caught that

 

Ok, report the issue!


But if I want to solve it, practically how can I do?


I tried something like this:

double aux[];
for(int i=15;i<20;i++)
     {
      int h=i-15;
      aux[h]=vector_to_avg[i];
     }
double median_vector=MathMedian(aux);

but it doesn't work! It makes crash MetaEditor. Why?


How could I solve?


Thanks

 

HI,

Do you know when the ticket will be resolved ?


Thanks for the help,

 
Try to use "SimpleMa" instead.

#include <MovingAverages.mqh>

SimpleMa(value_RSI,0,10);

Cheers!
 
Siyan Saheem #: Try to use "SimpleMa" instead.
  1. Do you really expect that they are still waiting for a solution after five years?
  2. Or just write your own.
    Not tested, not compiled, just typed.
    template<typename T> T mean(const T& arr[], int iBeg=0, int length=14){
       return sum_array(arr, iBeg, iBeg+length) / length;
    }
    template<typename T> T sum_array(const T& arr[], int iBeg, int iEnd){
       T sum=arr[iBeg]; while(++iBeg < iEnd) sum += arr[iBeg];  return sum;
    }
    Not tested, not compiled, just typed.
 
William Roeder #:
  1. Do you really expect that they are still waiting for a solution after five years?
  2. Or just write your own.
    Not tested, not compiled, just typed. Not tested, not compiled, just typed.

For MathMean to work you need to include:

#include <Math\Stat\Math.mqh>