Loading…

Single And Double Exponential Smoothing

Exponential smoothing weights every past observation, but the weight decays geometrically with age instead of cutting off sharply at a window edge - there is no moment where an old bar "falls off the back" the way it does in a moving average. One constant, alpha, controls how fast the weight decays: close to 1 tracks price almost exactly, close to 0 smooths heavily and reacts slowly to a change. Single exponential smoothing tracks a level only, and by construction always lags behind a real trend - it is perpetually chasing a moving target, because nothing in the model expects the series to keep moving. Double exponential smoothing (Holt's method) fixes this by adding a second smoothed estimate that tracks the trend itself. The level update then blends the new observation against the PREVIOUS level projected forward by the trend, not against the bare previous level - that projection is what actually removes most of the lag rather than just running two smoothing passes. The trend update applies the same geometric-decay idea to the change in level, with its own constant, beta. Because double smoothing carries an explicit trend estimate, it can also forecast: level + trend * steps is a genuine short-horizon projection, which single smoothing has no way to produce since it has no trend term to extrapolate with. The demo runs both forms over the same steady uptrend and shows single smoothing sitting visibly behind the raw price while double smoothing's level tracks much closer to it, then prints a five-bar forecast from the double-smoothing state. Both methods are provided as a stateful step function - ExpSmoothUpdate and DoubleExpSmoothUpdate, the way a live EA should call this header, once per new bar, holding one persistent state struct for the life of the chart - and as a batch array function for backtesting or building a smoothed line over history in one call. Notes and limitations - Both alpha and beta must be strictly greater than 0 and no greater than 1. Zero would freeze the corresponding state forever (a level or trend that never updates), which is refused rather than silently accepted. - Neither method has a concept of seasonality. A series with a strong repeating cycle (e.g. an intraday volume pattern) needs a triple (Holt-Winters) form this header does not provide - double smoothing will treat cyclical swings as trend noise. - The forecast from DoubleExpSmoothForecast is a straight-line projection of the current trend estimate. It says nothing about how long that trend is likely to continue, and confidence in it should shrink quickly with the number of steps ahead - this is a smoothing library, not a predictive model with error bounds. - Both step functions self-initialise from the first value passed to them if the state was never explicitly reset with the Init function. This is a convenience for quick scripts; a long-running EA should still call Init explicitly so the starting point is deliberate, not whatever tick happened to arrive first.

Script · MT4 · v1.0 · Free

About Single And Double Exponential Smoothing

Single And Double Exponential Smoothing is a script for MetaTrader 4. It has 4 settings, such as Alpha, Beta and Bar count. MQL source code included.

Files in the download: ExpSmoothing.mqh, ExpSmoothing.txt, ExpSmoothingDemo.mq4.

Settings

SettingDefaultWhat it does
Alpha0.3Level smoothing constant, 0 < alpha <= 1
Beta0.2Trend smoothing constant (double smoothing only)
Bar count30How many synthetic bars to generate
Forecast bars5How many bars ahead to forecast

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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 · Trend · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria