DAKParticipant
Average
Hi everyone,
I am working on an indicator trying to return the Median function instead of the classic Average one.
For example the Median value of the 5 latest price records such as : Median (Price, 5)
I haven’t seen any function of this type in ProRealTime or on the site’s topics so I tried to code it but can’t manage to get a correct result.
Any help would be greatly appreciated. Thanks !
Please confirm this definition for a Median:
Median : which is the middle number of a group of numbers; that is, half the numbers have values that are greater than the median, and half the numbers have values that are less than the median. For example, the median of 2, 3, 3, 5, 7, and 10 is 4.
DAKParticipant
Average
Yes Nicolas I confirm this definition.
Pretty hard to code…
Yes pretty hard without Arrays. Should be possible with a nested loop (first rough idea that come in mind).. let me think of it.
I solved this once with 2 nested for-loops.
@Despair
Would save me time if you could share your code snippet with us. Thank you in advance.
I will of course do so but I can’t find it so far. :-S I will search again…
Found it 🙂
once Median=0
if barindex>length then
FOR X = 0 TO length-1
M = close[X] //this example takes the median of the last 5 closes
SmallPart = 0
LargePart = 0
FOR Y = 0 TO length-1
IF close[Y] < M THEN
SmallPart = SmallPart + 1
ELSIF close[Y] > M THEN
LargePart = LargePart + 1
ENDIF
IF LargePart = SmallPart AND Y = length-1 THEN
Median = M
BREAK
ENDIF
NEXT
NEXT
endif
Return Median as "Median"
DAKParticipant
Average
It works very well thank you guys for quick and helpful response !
Hi Nicolas,
Have you worked out the Median function using arrays?
Tnx in advance.
Actually I don’t think arrays are of much help in this case, because CLOSE is an array by itself (close[0], close[1], etc…). In any case you will have to always scan LENGTH bars (or elements) to get the MEDIAN.
What is the data set is not CLOSE?
Whatever tha data set, it is historicized (thus becoming an array).
Be it MedianPrice, TypicalPrice, xClose (Heikin-Ashi close) or any expression assigned to a variable.