Two Pole Butterworth Filter for Think or Swim

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)

Tags: , , , ,

6 Responses to “Two Pole Butterworth Filter for Think or Swim”

  1. Frank Zardoz Says:

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

    for variable b1, he has:

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

    Should that be 180 instead of pi?

  2. Prospectus Says:

    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.

  3. Prospectus Says:

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

  4. Frank Zardoz Says:

    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.

  5. J Swanson Says:

    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!

  6. Thomas Taylor Says:

    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…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: