Category
Technical
Difficulty
Beginner
Used in
Strategy designMT5 operation

Exponential Moving Average

A moving average that weights recent prices more heavily than older ones, so it turns sooner than a simple average. EA trend filters and crossover entries are built on it more often than on any other indicator.

also: EMA, exponential MA

Updated · Reviewed

In plain English

An average of recent prices that pays more attention to what just happened than to what happened a while ago. It turns faster than a plain average, which makes it more useful in a trend and more talkative in a range.

Why it matters

The EMA is the default building block of EA trend logic: it supplies the direction that indicators like ADX deliberately do not. It is also the most over-used and most over-optimised component in retail EAs, because a crossover rule is easy to write and its two periods are easy to tune until any history looks profitable.

  • It gives a strategy a sign. Trend strength and volatility are directionless; a price-versus-EMA or fast-versus-slow comparison is where up and down enter the rules.
  • It reacts faster than a simple average, which matters at the turns — where most of a trend strategy's result is decided.
  • It is a lagging measure by construction. Every moving average describes what has already happened, so a crossover confirms rather than predicts.
  • Two periods is two parameters, and a grid search over them will always find a pair that looks excellent on any given window. That is the classic overfitting trap in EA design.

In MetaTrader 5

Where it appears in MT5

  • Insert → Indicators → Trend → Moving Average, then set Method to Exponential
  • The MA properties dialog also sets the applied price — Close is the usual choice and changes the values materially
  • Charts show it in the main window, on the price scale, unlike ATR or ADX
  • Strategy Tester → Inputs — the fast and slow periods are the two inputs most likely to be over-optimised

How EAs use it

  • Create handles in OnInit — iMA(_Symbol, PERIOD_CURRENT, 21, 0, MODE_EMA, PRICE_CLOSE) — one per period, kept for the EA's life.
  • As a direction filter: only take longs while price or the fast EMA is above the slow one, and shorts below.
  • As a crossover entry: the fast EMA crossing the slow one on a completed bar, which requires reading both bars 1 and 2 to detect the cross rather than the state.
  • As a trailing reference: exit when price closes on the other side of a chosen EMA, which is a trailing rule that adapts to the trend's own pace.
  • Higher-timeframe EMAs are read with the same call and a different period argument, which is how an M15 EA takes its direction from H4.

Typical settings

Setting Typical value Note
Fast / slow pair, short term 8 / 21 Reacts quickly; produces many crossings, most of which are not trends.
Fast / slow pair, medium term 50 / 200 The widely watched pair. Few signals, and the ones it gives arrive late.
Single trend filter EMA 200 on the entry timeframe Used as a directional gate rather than as an entry.
Applied price Close Typical and stable. Median or typical price smooths further and changes every backtest result.
Signal bar the last completed bar Reading the forming bar produces crossings that appear and disappear intrabar.

Common operational problems

  • Detecting the state (fast above slow) instead of the event (fast crossed slow), which re-enters on every bar for the length of the trend.
  • Reading index 0 so the crossover flickers with each tick, giving backtest results that live trading cannot reproduce.
  • Handles created in OnTick, leaking until iMA returns INVALID_HANDLE.
  • CopyBuffer at startup before the slow period has enough history, which returns zeros that compare as a crossover.
  • Optimising both periods on one window and shipping the peak, which is the most common way an EA looks excellent in testing and fails immediately afterwards.

Related MT5 functions

iMA(symbol, timeframe, period, shift, MODE_EMA, applied_price)
Creates the handle; MODE_EMA is what makes it exponential rather than simple.
CopyBuffer(handle, 0, start, count, array)
Reads the average out of its single buffer.
ArraySetAsSeries(array, true)
Makes index 0 the current bar, which is the convention most crossover code assumes.
iTime(symbol, timeframe, 0)
Used to act once per bar rather than once per tick.

Example

The difference between reading a state and reading an event, which is the single most common EMA bug in EA code.

Bar 2 — fast vs slow
fast below slow
Bar 1 — fast vs slow
fast above slow
The pair of readings is what makes this a crossing rather than a condition.
State test result
fires every bar
For as long as the trend lasts, adding a position each time.
Event test result
fires once

Both versions look profitable in a strong trend during testing. Only one of them is the strategy that was intended.

Calculation if (fast[2] <= slow[2] && fast[1] > slow[1]) → cross up

Result One entry per crossing instead of one per bar

How it is used

An EMA rule is easy to write and easy to fit. Everything worth checking is about whether the parameters survive being moved.

Range What it means
Result collapses when a period moves by 10% Fitted to the test window. A robust setting has neighbours that also work.
A broad range of period pairs works similarly The effect is likely structural rather than fitted, which is what a plateau in an optimisation surface means.
Crossover with no regime filter Signals fire in ranges too, where they mostly fail. This is what ADX or a volatility filter is usually added to fix.
Read on the forming bar Not reproducible. The same market produces different entries depending on tick timing.
  • Prefer a plateau to a peak when choosing periods. The best single result on a backtest is the least likely to repeat.
  • Pair a crossover with a regime filter. A trend rule without one takes its worst trades in the conditions it is worst at.
  • Detect crossings with two bars, never with one comparison, and act on completed bars only.
  • Remember that a moving average follows price. It cannot lead, so a strategy built on one is a participation strategy rather than a prediction.

The `/indicators` page for moving averages covers the calculation and the SMA comparison; this entry is about how the EMA is wired into EA logic.

Common mistakes

Confusing the state with the event

Testing whether the fast EMA is above the slow one is true for the entire trend. Testing whether it crossed requires the previous bar as well, and the difference between the two is one entry versus dozens.

Optimising both periods and shipping the best pair

Two free parameters on a smooth surface will always produce an impressive backtest. The value that matters is whether neighbouring pairs behave similarly, which is the difference between an effect and a fit.

Using a crossover with no filter for market conditions

Moving averages cross constantly in ranges, and nearly all of those crossings lose. The filter is not an optimisation of the strategy — it is what makes the strategy applicable.

Assuming EMA is strictly better than SMA

It is faster to react, which cuts both ways: earlier entries in real trends, more false signals in noise. Which is preferable depends on the strategy, and the difference is smaller than the choice of period.

In depth

What the published EAs use for direction instead

Fourteen EAs are published here with their full closed-trade records. Not one takes its direction from an exponential moving average.

Direction mechanismEAsWhat it is
Ichimoku Tenkan/Kijun cross5Kestrel Hover, Orrery, Peregrine, Tessera, Zerqon
Stacked simple moving averages1Lanternfish — SMA 25/50/75 on M30, entries on M5
An exponential moving average0
Something other than a cross8RSI extremes, envelope fades, candle patterns

Those five kept the crossing and dropped the tuning: Tenkan/Kijun periods come from the indicator’s definition, not a grid search. All five read a closed bar, and Orrery and Peregrine act only when the signal bar’s body beats a multiple of its recent average — the regime filter a bare crossover needs. Their profit factors run 1.27 to 1.46, the same band as the rest of the catalogue.

The Strategy Tester’s Inputs tab takes a Start, Step and Stop per period. Read the whole result set, not its top row: a plateau of neighbouring pairs is an effect, a lone peak is a fit. That is overfitting in practice, and why a filter such as ADX comes before another period is tuned.

Frequently asked questions

What EMA periods do MT5 EAs use?
The 8/21 pair for short-term systems and 50/200 for medium-term trend detection are the most common, with a single EMA 200 often used purely as a directional gate. The specific numbers matter less than whether the strategy still works when they are moved — a result that depends on one exact pair is fitted to the test window.
Is an EMA better than an SMA for EAs?
It reacts faster, which produces earlier entries in genuine trends and more false signals in noise. Neither is universally better, and the difference between the two is usually smaller than the difference between two period choices within either.
Why does my EMA crossover EA open so many positions?
Almost always because the code tests whether the fast EMA is above the slow one rather than whether it just crossed. The first condition stays true for the whole trend and fires on every bar; detecting the crossing requires comparing the previous bar as well.
Can an EMA predict a trend reversal?
No. It is an average of prices that have already occurred, so it necessarily follows. A crossover confirms that a change has happened rather than that one is coming, and that lag is the price of the smoothing.