I recently got interested in some of John Ehlers’ filters after reading Richard’s hit pieces on exponential moving averages. In traveling the intarwebz, I found an excellent site by Dave Newberg that had many of the Ehler indicators coded in Easy Language for Tradestation. I started porting them over to Thinkscript to see how they would work.

The first is called the Two-Pole Butterworth filter. It has roots in an analog circuit, but has been adapted to be a digital signal filter. The methodology is found here for the mathematically adventurous: wikipedia article. The basic idea is to try to get smoothing with as little lag as possible.

Here’s a shot of how the two pole Butterworth filter does vs. an exponential moving average of the same period:

Much smoother, better response, and a closer match to recent market prices. All the responsiveness of a much faster EMA, with the smoothing of a much slower EMA. This is going on my charts instead of EMA’s from now on.

You can get the “Two-Pole Butterworth Filter” Thinkscript code from my Google site, under “Released Thinkscript Studies“.

Here’s the John Ehlers book that the code is from:

Cybernetic Analysis for Stocks and Futures: Cutting-Edge DSP Technology to Improve Your Trading (Wiley Trading)

### Like this:

Like Loading...

*Related*

Tags: butterworth, digital_filter, ehlers, moving_averages, Thinkscript

This entry was posted on September 25, 2009 at 5:20 pm and is filed under Thinkscript. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

April 29, 2010 at 7:44 pm

I was comparing your code to Dave Newberg’s code.

for variable b1, he has:

b1=2*a1*Cosine(1.414*180/Period);

you have:

def b1 = 2 * a1 * Cos(1.414 * 3.14159 / Period);

Should that be 180 instead of pi?

April 29, 2010 at 7:55 pm

It depends on if the Cosine function is operating in radians or degrees. As I understand it, TOS’s function argument is in radians, so it should be pi. Of course, cos(pi) in radians is equivalent to cos (180) in degrees. They both equal -1.

April 29, 2010 at 7:57 pm

But I should say that I only translated that code. I didn’t derive the formulas…

April 29, 2010 at 9:06 pm

Thanks for your quick reply. Wow, that was subtle! You really know the details of Thinkscript! After testing the study with the value of 180 instead of pi, it stops filtering for large periods, so it looks like your implementation is the correct one. Thanks again for all of your coding work. You have an excellent site.

July 6, 2010 at 12:12 am

Thanks for this study (as well as various others on the site). I have no idea how it works; all I know is that it works — and well. Like you, I find this much more useful than the EMA for certain applications. Keep up the good work!

January 29, 2012 at 2:31 pm

Excellent 2 Pole Butterworth study. Got it to work with no prior scripting experience! But could I trouble to suggest what I should change in the TOS moving average crossover study script to incorporate this as my second moving average (have been using 2 and 13 EMA until now — any other suggestions?). Most grateful…