You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Not sure I follow 100%... where len is even, division by 2 will always return an integer. When it's odd I need it to ignore the decimal and return just the integer. 5/2 = 2.5 as a double but 2 as an int. So len as int is best. Am I missing something?
Not sure I follow 100%... where len is even, division by 2 will always return an integer. When it's odd I need it to ignore the decimal and return just the integer. 5/2 = 2.5 as a double but 2 and an int. So len as int is best. Am I missing something?
The mean value of the two array values involves a division by 2, you need to make this a 2. (a double) instead of just 2 (an int) otherwise your mean value computation will fall into an integer typecast in the event that the array values themselves are integer values (even if the array itself is defined as a double).
mean = (Integer+Integer)/2 = (Integer+Integer)/Integer = Integer
mean = (Integer+Integer)/2. = (Integer+Integer)/Double = Double
You want your mean value, representing the median, to be a double and not typecast to an integer value.
Ah... I see what you mean. I read https://book.mql4.com/basics/expressions but understand from that that if median is declared as double (as it is in on the first line of the block), it will return a double even if doubles and intergers are mixed int the calculation...
double mean = (Integer+Integer)/2 = (Integer+Integer)/Integer = double
I might be about to have a "duh" moment but it hasn't slapped me in the face yet....your help in expediating the slap when it finally comes is gratefully received. So am I missing something?
V
(I'm off to watch opener on World Cup)
double mean = (Integer+Integer)/2 = (Integer+Integer)/Integer = double
Incorrect, the variable "mean" will be a double but the value it will be holding as computed by the integer/integer will be an integer value.
https://docs.mql4.com/basis/types/casting
Examples:
Well I honestly didn't know that. thanks for pointing it out. I can see it now though int/int=int then cast to a double so 1/2=0 then cast to 0.0... all makes sense.
I think I ought to have another look through all my other code!
Thanks again
V
[I've updated the function code in case anyone is using it]
Incorrect, the variable "mean" will be a double but the value it will be holding as computed by the integer/integer will be an integer value.
https://docs.mql4.com/basis/types/casting
Examples:
Hello
I use Time Series to create new order with SL and TP based on median bar size from, may be it could help:
To speed things up you can:
1) Add new values to already sorted array -> this way Copy() and Sort() get executed faster
2) Write a custom Sort() based on better/more suitable algorithm
Nothing else, median calculation requires a sorted array.
Also, as Viffer wrote, median is defined differently for odd and even size of array.
I have been programing a mean, because I find that the means indicator available for free are not exact in
their calculations. So , I have created some very different solution to the one you are showing here.
My mql4 is almost finish. It has no bugs, but still not calculating the exact value of the mean.
My program has four parts to it.
1. Fill the array
2. Sort the array
3. Find the mean if the period is odd number, or find the 2 center numbers and operate. (a+b)/2
4. Graph the array.
THIS IS MY FUNCION 1) FILL THE ARRAY mediana1[]
double LLENAR_ARRAY(double Array)
double a1= Close[x];
mediana1[x]=a1;