Detrending Facade
Detrending a price or indicator series sounds like a solved problem, but the three common approaches remove trend in genuinely different ways and each leaves a different kind of artefact behind. First-differencing removes any trend shape whatsoever but is a high-pass filter in disguise, so it amplifies short-term noise. Linear regression only removes a straight-line trend and leaves any real curvature sitting in the residual, which then looks like a spurious slow cycle to anything downstream. A moving-average filter can track a curving trend, but its window length trades off how much curvature it follows against how much genuine signal it filters out along with the trend. Picking the wrong one for the series at hand is a quiet, common source of a misleading backtest signal. This header does not try to pick the "best" method - there isn't one, it depends on the series. It puts all three behind one DetrendSeries() call selected by an enum, so switching methods to see which one actually flattens a given series is a one-line change instead of three separately hand-rolled formulas. Notes and limitations DETREND_DIFFERENCE writes a placeholder 0.0 into index 0 (there is no prior sample to difference against); statistics computed over the result should skip index 0. DETREND_MA_FILTER uses a shrinking window near the start of the array rather than leaving early residuals undefined, so the first (period-1) residuals are computed from fewer samples than the requested window and are noisier than the fully warmed-up region - discard them if that matters for the caller's purpose. DETREND_LINEAR fits a single straight line across the whole array; it does not detect or handle a structural break partway through the series. None of the three methods look at real time gaps (weekends, missing bars) - they operate purely on array index order, so a series with irregular spacing should be resampled to even spacing first if that matters.
Script · MT5 · v1.0 · Free
About Detrending Facade
Detrending Facade is a script for MetaTrader 5. It has 2 settings, such as Sample count and Ma period. MQL source code included.
Files in the download: DetrendingFacade.mq5.mqh, DetrendingFacade.txt, DetrendingFacadeDemo.ex5, DetrendingFacadeDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample count | 60 | length of the synthetic test series |
| Ma period | 8 | moving-average period for DETREND_MA_FILTER |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trend · MT5 Script · MQL5 Source Code