◆ New — Strategy Validator, Code Fixer & Prop Compliance Checker are live. Try one free →
Home / Strategies / EMA Crossover Strategy
Trend-following / moving-average

EMA Crossover Strategy

The EMA crossover strategy uses two exponential moving averages — a faster one and a slower one — and treats the moment they cross as a signal that short-term momentum has shifted relative to the longer-term trend. Traders commonly use it as a simple, rules-based way to define trend direction and frame entries and exits. It is an educational framework only: it is not predictive, signals can be late or false in choppy markets, and trading involves the risk of loss. If you want to turn this idea into validated Pine Script v6 and check it for look-ahead bias and repainting before you trust it, run it through the ForexCodes Strategy Validator (try it free).

How it works

An exponential moving average (EMA) is an average of recent prices that weights the most recent bars more heavily than older ones, so it reacts faster than a simple moving average of the same length. In this strategy you plot two EMAs with different lookback lengths — for example a 9-period "fast" EMA and a 21-period "slow" EMA. When the fast EMA crosses above the slow EMA, it is read as short-term price strength building relative to the longer-term average (a "bullish" cross). When the fast EMA crosses below the slow EMA, it is read as short-term weakness relative to the trend (a "bearish" cross). Note that the popular terms "golden cross" and "death cross" are conventionally reserved for the slower 50/200 moving-average cross; a 9/21 cross is the same mechanical idea on a faster pair, not literally a golden/death cross. The crossover itself is just a mechanical condition on two lagging lines; it describes what price has recently done, not what it will do next. Because both EMAs are derived from past closes, they smooth out noise but also lag turning points, which is the central trade-off of the approach. Traders typically pick the two lengths to match their timeframe and how much smoothing versus responsiveness they want, then decide separately how to manage position size, stops, and exits — the crossover alone does not handle risk.

Entry rules

Exit rules

Risk notes

Pine v6 example

//@version=6
strategy("EMA Crossover (educational)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, calc_on_every_tick=false, process_orders_on_close=true)

// Inputs
fastLen = input.int(9, "Fast EMA length", minval=1)
slowLen = input.int(21, "Slow EMA length", minval=1)
useTrendFilter = input.bool(false, "Only trade with 200 EMA trend")

// Indicators (ta.* namespace). On the live, unclosed bar these values
// update intrabar; orders are filled on bar close via process_orders_on_close.
fastEma  = ta.ema(close, fastLen)
slowEma  = ta.ema(close, slowLen)
trendEma = ta.ema(close, 200)

// Crossover conditions
bullCross = ta.crossover(fastEma, slowEma)
bearCross = ta.crossunder(fastEma, slowEma)

// Optional longer-trend alignment
longOk  = not useTrendFilter or close > trendEma
shortOk = not useTrendFilter or close < trendEma

// Entries. Sending the opposite-direction entry reverses an open position
// (closes the prior trade and flips), which is the exit-on-opposite-signal rule.
if bullCross and longOk
    strategy.entry("Long", strategy.long)
if bearCross and shortOk
    strategy.entry("Short", strategy.short)

// Plot the EMAs for context
plot(fastEma,  "Fast EMA",  color.new(color.aqua, 0))
plot(slowEma,  "Slow EMA",  color.new(color.orange, 0))
plot(useTrendFilter ? trendEma : na, "Trend EMA", color.new(color.gray, 0))

// Educational tool only. Not financial advice. Past results do not indicate
// future performance; trading involves risk of loss. Validate this script for
// look-ahead bias and repainting with the ForexCodes Strategy Validator.

Pitfalls

FAQ

What EMA lengths should I use for a crossover?

There is no universally correct pair — 9/21 and 12/26 are common starting points, and 50/200 is popular for slower, higher-timeframe trend reads. Shorter lengths react faster but whipsaw more; longer lengths are smoother but later. Choose lengths that fit your timeframe and test them out-of-sample rather than tuning them to past data. None of these settings is predictive.

Is an EMA crossover better than an SMA crossover?

Neither is 'better' in general; they trade off responsiveness against smoothness. EMAs weight recent prices more heavily, so they turn sooner and can catch shifts earlier but also produce more false crosses in noise. SMAs are steadier and lag more. The right choice depends on how much smoothing you want and how you manage risk around the signals.

Does the EMA crossover strategy work on all markets and timeframes?

The mechanics compute on any market or timeframe, but behaviour varies a lot: trending instruments tend to give cleaner crosses while ranging ones produce frequent whipsaw. There is no setting that performs consistently everywhere, and past behaviour on a chart does not indicate future results. Validate the exact rules on the specific instrument and timeframe you intend to trade — the ForexCodes Strategy Validator can convert your rules to Pine v6 and flag repainting or look-ahead bias before you do.

Educational & software only — not financial advice, not a recommendation to trade. Backtests are illustrative; past performance does not predict future results. Trading involves substantial risk of loss.

Catch the bug that compiles.Run auditGet Pro