The median is the value separating the higher half of a data sample, a population, or a probability distribution, from the lower half. For a data set, it may be thought of as the “middle” value. For example, in the data set {1, 3, 3, 6, 7, 8, 9}, the median is 6, the fourth largest, and also the fourth smallest, number in the sample. For a continuous probability distribution, the median is the value such that a number is equally likely to fall above or below it. (wikipedia)
The first part of the code is the median calculation of a Close price (function created by @Despair), then this median price is averaged with a non linear time series Simple Moving Average function that smooth the information and so improve the reading of values.
Idea come from this topic in the Spanish forum: nueva media móvil con precio mediano
//PRC_MA of last X PriceMedian | indicator
//01.05.2018
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge
// --- settings
length=5
priceMAperiod=20
// --- end of settings
// median average
//median calc
once Median=0
if barindex>length then
FOR X = 0 TO length-1
M = close[X] //this example takes the median of the last X values
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
count=0
sum=0
lastmedian=0
for i = 0 to barindex do
if median[i]<>lastmedian then
lastmedian=median[i]
count=count+1
sum=sum+median[i]
if count=priceMAperiod+1 then
sum=sum-last
last=median[i]
break
//
endif
endif
next
if last<>last[1] then
avg = sum/priceMAperiod
endif
return avg