Zero Phase Forward Backward Filter
A causal filter - the only kind usable on a live chart, since it can only see the current bar and earlier ones - always introduces phase lag: its smoothed turning points arrive after the price turning points that caused them. Forward-backward filtering is the classic offline signal-processing technique for removing that lag entirely: run the same filter forward through the series, then run it a second time backward through the already -filtered result. Each pass's lag points in the opposite time direction, so the two cancel, leaving an output whose turning points line up with price turning points exactly - zero net phase shift. That cancellation is only meaningful, and only possible at all, on a fixed array that is already fully known: the backward pass, by definition, needs to have already seen the end of the series before it can compute bar 0's final output. On a live chart there is no "end of the series" - the next tick has not printed yet. This library is therefore a research tool for studying a finished chart export, a backtest dataset, or historical data pulled for analysis - never a live indicator or an EA signal source. The warning is stated in the file header, repeated at the top of every function doc-comment that produces zero-phase output, and repeated again here. Two base filters are provided, both run forward-then-backward: ZeroPhaseEmaFilter (exponential smoothing) and ZeroPhaseSmaFilter (a plain N-bar simple moving average). The demo builds a synthetic series with a sharp step change and prints an ordinary one-pass causal EMA next to both zero-phase variants over the same data, so the lag difference - and the fact that it comes from looking ahead, not from a better filter design - is visible directly in the printed numbers rather than merely asserted. Notes and limitations - USES FUTURE DATA BY DESIGN. Repeated here a final time: do not attach any output of this library to a live chart expecting a usable trading signal. Every value at bar i is computed using bars after i in time. - Output changes retroactively as new bars are appended to the array - a value read at bar i today will not match the same bar's value if the array is re-run after more history is appended, because the backward pass sees a different "future" each time. This is the direct consequence of the lookahead property, not a bug. - The EMA and SMA base filters here are simple by design, matching the library's small effort budget - no attempt is made to offer a Butterworth or other higher-order base filter for the zero-phase treatment. - No live account, order, or symbol-info call anywhere in this library - pure array arithmetic. The risk this library carries is entirely the lookahead-misuse risk documented above, not a trading-execution risk.
Script · MT4 · v1.0 · Free
About Zero Phase Forward Backward Filter
Zero Phase Forward Backward Filter is a script for MetaTrader 4. It has 2 settings, such as Alpha and Sma period. MQL source code included.
Files in the download: ZeroPhaseFilter.mqh, ZeroPhaseFilter.txt, ZeroPhaseFilterDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Alpha | 0.30 | EMA smoothing constant used by both passes |
| Sma period | 5 | SMA period for the SMA-based zero-phase variant |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Buy/Sell Arrows · MT4 Script · MQL4 Source Code