◆ New — Strategy Validator, Code Fixer & Prop Compliance Checker are live. Try one free →
Home / Strategies / SMA Crossover Strategy
strategy

SMA Crossover Strategy

The SMA crossover strategy is one of the most widely taught moving-average methods in technical analysis. It uses two simple moving averages of different lengths and watches for the faster one crossing above or below the slower one to mark possible shifts in trend direction. It is an educational, rule-based framework for studying trend behavior, not a prediction of where price will go, and it does not guarantee any outcome.

How it works

A simple moving average (SMA) adds up the closing prices over a set number of bars and divides by that number, giving a smoothed line that lags the raw price. The strategy plots two of them: a "fast" SMA over a shorter lookback (for example 50 bars) and a "slow" SMA over a longer lookback (for example 200 bars). The core idea is that when the fast average climbs above the slow average, recent prices are running higher than the longer-term average, which traders read as a possible upward trend forming. When the fast average drops below the slow average, the opposite is read as a possible downward trend. The bullish cross is often called a "golden cross" and the bearish cross a "death cross." Because both lines are built from past closes, the signal is descriptive of a move that has already started, so the strategy is reactive by design and tends to be late at turning points. Traders use it to define a clear, repeatable rule set for studying trend behavior rather than to forecast price.

Entry rules

Exit rules

Risk notes

Pine v6 example

//@version=6
strategy("SMA Crossover Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, process_orders_on_close=true)

// Inputs
fastLen = input.int(50, "Fast SMA length", minval=1)
slowLen = input.int(200, "Slow SMA length", minval=1)

// Simple moving averages on closing price
fastSma = ta.sma(close, fastLen)
slowSma = ta.sma(close, slowLen)

// Crossover conditions
longSignal  = ta.crossover(fastSma, slowSma)
shortSignal = ta.crossunder(fastSma, slowSma)

// Strategy logic: long/flat study example.
// Enter long on the golden cross, close on the death cross.
// (A reversing variant would add strategy.entry on shortSignal; omitted here for clarity.)
if longSignal
    strategy.entry("Long", strategy.long)
if shortSignal
    strategy.close("Long")

// Plots for visual study
plot(fastSma, "Fast SMA", color=color.aqua)
plot(slowSma, "Slow SMA", color=color.orange)

Pitfalls

FAQ

What is the difference between an SMA crossover and an EMA crossover?

Both compare a fast and a slow moving average, but a simple moving average (SMA) weights every bar in its lookback equally, while an exponential moving average (EMA) gives more weight to recent bars. As a result EMA crossovers tend to react a little faster and SMA crossovers are smoother and slower. Neither is inherently better; they are just different trade-offs between responsiveness and noise, and both are lagging by nature.

What lengths should I use, like 50/200 or 9/21?

The 50/200 pair is the classic 'golden cross / death cross' setting often discussed for longer-term trend study, while shorter pairs such as 9/21 react faster and signal more often. There is no universally correct setting. The right lengths depend on the market, timeframe, and how often you want signals, and tuning them to fit one historical chart does not mean they will behave the same way later.

Does the SMA crossover repaint?

The averages themselves are calculated from closed prices, so a signal taken on a confirmed (closed) bar will not change afterward. The repainting-style trap is acting on the still-forming current bar, where the averages can move and a cross can appear or disappear before the bar closes. Treating signals as final only on closed bars avoids this. ForexCodes' Strategy Validator specifically checks for repainting, look-ahead bias, and intent mismatches like this when it converts a strategy to validated Pine Script v6 — try it free.

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