Loading…

Butterworth Filter For Price Smoothing

A moving average is a crude low-pass filter - it attenuates high-frequency noise unevenly and never fully removes anything, it just weights it less. A Butterworth filter is designed specifically to be maximally flat in the frequencies it passes through and then fall off sharply beyond its cutoff. This header uses John Ehlers' well-known recursive translation of the analogue Butterworth design into a form driven purely by a period input - a handful of trigonometric coefficients computed once, then a short recursion per bar. No filter-design toolkit or root-finding is needed at runtime. Three filters are included. The 2-pole low-pass gives moderate smoothing with moderate lag - a reasonable default replacement for a short moving average. The 3-pole low-pass trades more lag for a noticeably sharper cutoff and smoother output, useful when the extra half-bar or so of delay is worth it for a cleaner line. Both are shown side by side in the demo over the same series so the trade-off is visible in the printed numbers rather than asserted. The third filter, a 2-pole high-pass, is the complement of the low-pass: it removes the slow trend and leaves whatever oscillates faster than the cutoff. This is the standard first step before a dominant-cycle or sine-wave-style study, which needs the cyclical component isolated from the trend it rides on - the demo builds a series with a trend and a shorter cycle layered on top specifically to show the high-pass output oscillating around zero once the trend is gone. All three filters are recursive: each output depends on the last two or three inputs and the filter's own last two or three outputs. That means there genuinely is no "not enough history yet" state once past the first few bars, unlike a windowed average - the first few outputs are seeded directly (from price for the low-pass filters, from zero for the high-pass, since its meaningful output is a deviation from trend). This is documented per function rather than hidden behind a sentinel value. Notes and limitations - These are fixed-design filters driven by a period, not a general-purpose filter designer - there is no way to request a different filter order or ripple characteristic than the three shapes provided. - Being recursive (IIR) filters, output at bar i depends on the filter's own output at i-1 and i-2 (or i-3), not just on price - two runs starting from different seed points will not agree exactly for the first several bars after the seed, even on identical price data from that point on. This matters when comparing a fresh run against a long-running live filter. - The high-pass filter's early bars are seeded at zero rather than at price and take a few bars to settle into a meaningful deviation-from-trend reading - do not treat the first two or three outputs as real. - None of the three filters look ahead. Every output depends only on the current and earlier bars, so none of them repaint on a closed chart.

Script · MT5 · v1.0 · Free

About Butterworth Filter For Price Smoothing

Butterworth Filter For Price Smoothing is a script for MetaTrader 5. It has 2 settings, such as Period and Bar count. MQL source code included.

Files in the download: ButterworthFilter.mq5.mqh, ButterworthFilter.txt, ButterworthFilterDemo.ex5, ButterworthFilterDemo.mq5.

Settings

SettingDefaultWhat it does
Period15.0Cutoff period for every filter in the comparison
Bar count50How many synthetic bars to generate

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Moving Average · HFT · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria