The MACD trend strategy uses the Moving Average Convergence Divergence indicator to describe the direction and momentum of a trend, typically by watching how the MACD line crosses its signal line and where both sit relative to the zero line. Traders use it to frame whether momentum is currently leaning bullish or bearish and to structure entries in the direction of that lean. It is an educational framework for reading momentum, not a forecasting tool, and like any approach it can produce losing trades. Nothing here is advice or a prediction, and trading carries a real risk of loss.
MACD is built from two exponential moving averages (EMAs) of price. The MACD line is the difference between a fast EMA (commonly 12 periods) and a slow EMA (commonly 26 periods). A signal line (commonly a 9-period EMA of the MACD line) is plotted on top, and the gap between the two is shown as a histogram.
The idea behind a trend reading is simple in plain English: when the fast EMA pulls away above the slow EMA, short-term momentum is stronger than longer-term momentum, so the MACD line rises and crosses above its signal line. When the fast EMA falls below the slow EMA, the MACD line drops below its signal line. Many traders also watch the zero line: MACD above zero is often read as the broader momentum leaning up, and below zero as leaning down.
A common "trend" variant combines both signals — it only acts on a bullish signal-line crossover when MACD is also above zero (and the bearish case below zero). The aim is to take signals that agree with the wider momentum picture rather than every crossover. None of this predicts price; it is a structured way to describe momentum that has already happened, and it lags because it is built from moving averages.
If you want to turn a rules set like this into validated Pine Script v6 — with checks for look-ahead bias, repainting, and mismatches between your stated intent and what the code actually does — the ForexCodes Strategy Validator is built for exactly that. Try it free.
//@version=6
// MACD Trend Strategy — educational example only. Not financial advice.
// Illustrates a zero-line-filtered MACD signal-line crossover. Backtests are illustrative; past results do not indicate future results.
strategy("MACD Trend Strategy (Educational)", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// --- Inputs ---
fastLen = input.int(12, "Fast EMA Length", minval=1)
slowLen = input.int(26, "Slow EMA Length", minval=1)
signalLen = input.int(9, "Signal EMA Length", minval=1)
useZeroFilter = input.bool(true, "Require zero-line agreement")
// --- MACD calculation (built-in, v6 ta.* namespace) ---
[macdLine, signalLine, histLine] = ta.macd(close, fastLen, slowLen, signalLen)
// --- Conditions ---
crossUp = ta.crossover(macdLine, signalLine)
crossDown = ta.crossunder(macdLine, signalLine)
aboveZero = useZeroFilter ? macdLine > 0 : true
belowZero = useZeroFilter ? macdLine < 0 : true
longEntry = crossUp and aboveZero
shortEntry = crossDown and belowZero
// --- Orders ---
// Under default settings, signals are evaluated on the confirmed bar close and
// market orders fill at the next bar's open (no intrabar/look-ahead execution).
// Entries are filtered by the zero line; exits use the plain opposite crossover
// so a long can close even if the new short signal is filtered out.
if longEntry
strategy.entry("Long", strategy.long)
if shortEntry
strategy.entry("Short", strategy.short)
if crossDown
strategy.close("Long")
if crossUp
strategy.close("Short")
// --- Plots ---
plot(macdLine, "MACD", color=color.blue)
plot(signalLine, "Signal", color=color.orange)
plot(histLine, "Hist", color=(histLine >= 0 ? color.teal : color.red), style=plot.style_columns)
hline(0, "Zero", color=color.gray)The default 12/26/9 is the most common convention and a reasonable starting point for learning how the indicator behaves. There is no universally correct setting — longer lengths react more slowly and produce fewer signals, shorter lengths react faster and produce more (including more false ones). Whatever you choose, avoid tuning the numbers to make a past chart look perfect, since that tends not to carry forward.
Many traders find that adding a filter — such as only taking signals in the direction of the zero line or a higher-timeframe moving average — reduces the number of counter-trend signals that get chopped up in ranging markets. It does not guarantee better outcomes; it changes the trade-off between signal frequency and how many signals agree with the wider momentum. Test any filter on your own market and accept that results vary.
We can't and won't claim that. MACD is an educational tool for reading momentum, not a predictor, and any strategy built on it can produce losing trades. Whether it suits you depends on your market, risk management, costs, and discipline. Treat backtests as illustrative only — past performance does not indicate future results, and trading involves real risk of loss.
The most common errors are silent ones: code that repaints, peeks at future bars (look-ahead bias), or simply does something different from the rules you wrote in plain English. The ForexCodes Strategy Validator converts a plain-English or AI-generated strategy into validated Pine Script v6 and flags those issues before you rely on a backtest. 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.