New Indicator To Determine Statistical Median, Statistical Median IS NOT Median Price = (H + L)/2

 
Hello out there!

I want to build a new indicator in MQL4 that gives me the statistical MEDIAN as the output result (describing the central tendency of a range of price data similar to the Moving Average function).

In order to do so, I need to implement the following steps. Below you find a coded example of the task to be implemented:
def getMedian(numericValues):
theValues = sorted(numericValues)
if len(theValues) % 2 == 1:
return theValues[(len(theValues)+1)/2-1]
else:
lower = theValues[len(theValues)/2-1]
upper = theValues[len(theValues)/2]
return (float(lower + upper)) / 2


1) I have to sort the numeric values of an array in ascending order.
2) I have to determine and store the length of the array.
3) Depending on the number of values in the array (odd or even) I need to return a calculated value of certain rank positions within the sorted array.

So now my questions:
How can I easily sort an array of arguments = input variables, say the Close Price for a certain number of time periods, within MQL4 scripting language?
How can I determine the length of the array, i.e. the number of values included?
How to explicitly address certain positions within the sorted array? e.g. Median = (Array[7] + Array[8])/2

Appreciate your answers, experiences and ideas. Thx.
 
fxmatt:
How can I easily sort an array of arguments = input variables, say the Close Price for a certain number of time periods, within MQL4 scripting language?

https://docs.mql4.com/array/ArraySort

How can I determine the length of the array, i.e. the number of values included?

https://docs.mql4.com/array/ArraySize

How to explicitly address certain positions within the sorted array? e.g. Median = (Array[7] + Array[8])/2

Exactly like your example; just use braces... Don't forget that the array starts from index 0.


If u haven't done so, I recommend you read about Arrays in the Book -> https://book.mql4.com/variables/arrays ; and also take a look at array functions in the docs -> https://docs.mql4.com/array.

 
Thanks Gordon! Great help...appreciate it.
 
fxmatt:
Thanks Gordon! Great help...appreciate it.


I have already achieved some progress on dealing with the arrays in MQL4, but I still couldn't figure out how to sort the predefined time series, like Close[] for example. The standard obviously doesn't allow this. So I tried to define and initialize a user-defined array with the corresponding values for a chosen number of bars in the past. See below extract of the init function:

int Quant_Bars = 8;

double num_array[];

//--------------------------------------------------------------------

int init()

{ // Assigning values

for (int i=0; i<Quant_Bars; i++)

{

num_array[i] = Close[i];

}

return; // Exit init()

}

//-------------------------------------------------------------------

Unfortunately the num_array values won't get initialized properly, they always show the value 0.
Is anybody able to give me a hint on what is going wrong here?

 
Your problem is that u haven't declared the size of the array. Note that you can't declare it's size with a variable, it must be a constant:
int Quant_Bars = 8;

//declaring array size with a variable -> THIS WILL NOT COMPILE!
double num_array[Quant_Bars]; 

//declaring array size with a constant -> this is ok
double num_array[8];     //or use a predefined constant with value 8 (using #define)

//an alternative way: changing array size later in the code 
double num_array[];
ArrayResize(num_array,8);
There are also specialized functions for copying arrays under different circumstances (which will work much faster in real-time). Check out: ArrayCopy(), ArrayCopyRates(), ArrayCopySeries().

Next time:
 
You probably want to limit the size of the array, who cares what the close was 5 years ago
fix the size of the array
copy the close[i] to the array[i] for i=1..fixed size
sort array...
Reason: