Median filter
Author: thodder
Creation Date: 11/17/2010 10:46 AM
profile picture

thodder

#1
While coding some indicators, I've found the need for a Median filter (similar to TradeStation). I thought the following code would be a nice addition to the Community.Components library..

CODE:
Please log in to see this code.

profile picture

Eugene

#2
Hi Tim,

Thanks. By the way, is the int bar parameter really required?
profile picture

thodder

#3
Yes. To copy the values from the series to the temporary array, I do the following:

aTmp[i] = ds[bar - i];

TradeStation seems to know what bar you are on whereas WLP needs that passed as a parameter.
profile picture

Eugene

#4
Oops sorry, I overlooked it! Nevermind.
profile picture

thodder

#5
No problem. It's helpful to have someone else reviewing my code to make sure I didn't make any stupid mistakes.

I stumbled onto the need for the Median filter while porting some of John Ehlers algorithms to WLP for my own use.

AdaptiveLaguerre Bug with its Median filter??

I noticed that indicator AdaptiveLaguerre in the Community.Indicators library had implemented a median filter inline, but the code puzzled me. Now I believe there is an bug in the code. The following code segment in AdaptiveLaguerre could return zero even if zero is not in the source data...

CODE:
Please log in to see this code.


To show what I mean, I've extracted the code and put it into a strategy for a controlled test...

CODE:
Please log in to see this code.


Notice that tmp does not have zero in any of the source data; however, the final result for alpha is zero.

As long as AdaptiveLaguerre is used with a DataSeries that can not contain negative values, the issue in the code above should not affect the calculations. Using AdaptiveLaguerre with prices should be fine.



Since there was already one indicator in Community.Indicators that needed this functionality, I assumed there could probably be more in the future that might need a Median filter.
profile picture

Eugene

#6
Thanks again, Median will see the light in Community.Components 2010.12.
profile picture

Eugene

#7
fyi: Community.Indicators 2010.12 will also utilize a Median function coded previously like this: New trend indicator: MSR by David Varadi
profile picture

thodder

#8
Eugene, I didn't even notice that this has already been implemented in the standard indicators...

http://www2.wealth-lab.com/WL5WIKI/Median.ashx

I'd recommend removing my addition to Community.Components based on this new find. They appear to be the same.
profile picture

Eugene

#9
Well, we reinvented the wheel... Happens sometimes. Thanks for the heads-up. :) Sure, no need in adding this to Community.Components.