Hello Guys and Gals. New user here and stuck allready.
For a volatility filter I am trying to find the median distance from a median price. In other words the 50th percentile of the distances to the 50th percentile of the past X Bars.
For example (Period 5 Bars) if the past 5 Bars of a certain Stock had closing prices
of 22, 25, 26, 26, 29 the Median would be the middle or 26. Then i would like to
know the Median of the differences (positive values only) to that median(26).
22-26=4, 25-26=1, 26-26=0, 26-26=0, 29-26=3. Results 4,1,0,0,3.
List sorted would be 0,0,1,3,4 the Median deviation in this case would be again the
middle Value which would be 1.
I am a former Multicharts (Power Language) user which currently is not realy helping
me in my thinking.
I tried to code a Function to do the above calculation. It will give me the correct Value
for the LAST bar only, but not for the rest of the Bars that came before.
Here is the function
function MAD( array, period )
{
result = Null;
for( i = BarCount - Period; i < BarCount; i++ )
{
rawprice[i] = array[i];
}
rawprice = Sort(rawprice);
for( i = BarCount - Period; i < BarCount; i++ )
{
rawmad[i] = abs( array[i] - rawprice[ (BarCount-1-Period) + ceil(.5*Period) ] ); // substracting the seperate closing values to the median calculated in the loop above to find the differences
}
rawmad = Sort(rawmad);
result = rawmad[ (BarCount-1-Period) + ceil(.5*Period) ];
return result;
}
Now i am sure i made some terrible mistakes here and i am not sure if i even need
to do looping in Amibroker to achieve what i would like to do, and i most likely have not understooth how arrays are calculated in AmiBroker.
Could someone be so kind and point me the right direction ?
@bysoaa - if you copy someone's other code changing just one line you should KEEP REFERENCES given in original formula so original author is credited for work.