MACD Crossover Strategy
The MACD crossover strategy enters when the MACD line crosses the signal line, filtered by the histogram turning positive/negative. A 200 EMA trend filter ensures you only take longs in uptrends and shorts in downtrends.
Pine Script v6 — Complete Strategy Code
//@version=6//@version=6
strategy("MACD Crossover + Trend Filter", overlay=true,
default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// ── Inputs ──
int fastLen = input.int(defval=12, title="MACD Fast", minval=1, group="MACD")
int slowLen = input.int(defval=26, title="MACD Slow", minval=1, group="MACD")
int sigLen = input.int(defval=9, title="Signal", minval=1, group="MACD")
int trendLen = input.int(defval=200, title="Trend EMA", minval=1, group="Filter")
float slMult = input.float(defval=1.5, title="SL ATR Mult", minval=0.1, step=0.1, group="Risk")
float tpMult = input.float(defval=3.0, title="TP ATR Mult", minval=0.1, step=0.1, group="Risk")
// ── Calculations ──
[macdLine, signalLine, histLine] = ta.macd(close, fastLen, slowLen, sigLen)
float trendEma = ta.ema(close, trendLen)
float atr = ta.atr(14)
bool longCond = ta.crossover(macdLine, signalLine) and macdLine < 0 and close > trendEma
bool shortCond = ta.crossunder(macdLine, signalLine) and macdLine > 0 and close < trendEma
// ── Visuals ──
plot(trendEma, "Trend EMA", color.new(color.gray, 50))
plotshape(longCond, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(shortCond, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
// ── Orders ──
if longCond
strategy.entry("Long", strategy.long)
strategy.exit("XL", from_entry="Long", stop=close - atr * slMult, limit=close + atr * tpMult)
if shortCond
strategy.entry("Short", strategy.short)
strategy.exit("XS", from_entry="Short", stop=close + atr * slMult, limit=close - atr * tpMult)
// ── Alerts ──
alertcondition(longCond, title="MACD Long", message="MACD long signal — {{ticker}}")
alertcondition(shortCond, title="MACD Short", message="MACD short signal — {{ticker}}")Copy and paste into TradingView Pine Editor, or generate a custom variation with AI.
How it works
Strategy logic, step by step
- 1
MACD calculated with default 12/26/9 settings using ta.macd().
- 2
Long: MACD line crosses above signal line AND MACD line is below zero (catching early momentum).
- 3
Short: MACD line crosses below signal line AND MACD line is above zero.
- 4
Trend filter: 200 EMA — long only above, short only below.
- 5
ATR-based stop loss and take profit.
Parameters
Configurable inputs
| Parameter | Default |
|---|---|
| MACD Fast | 12 |
| MACD Slow | 26 |
| Signal | 9 |
| Trend EMA | 200 |
Ask the TradePilot copilot to backtest this and sweep the inputs to find the best combination for your asset and timeframe.
FAQ
MACD Crossover Strategy questions
Why enter when MACD line is below zero?
Crossing above signal while still below zero catches the early stage of a momentum reversal — before the market has already moved. Waiting for MACD above zero tends to produce late entries.
Can I remove the trend filter?
Yes — ask TradePilot to "remove the 200 EMA trend filter from the MACD strategy" and the inline editor will remove those conditions, making it trade in both directions without a bias filter.
More strategies
Other Pine Script strategies
EMA Crossover Strategy
Fast/slow EMA crossover with RSI filter — the most tested trend-following setup in retail trading.
Supertrend Strategy
ATR-based trend-following with dynamic support/resistance. Two parameters, clear signals, easy to optimize.
RSI Divergence Strategy
Enter on confirmed RSI divergence — price making new extremes while RSI fails to confirm.
Want a custom version of this strategy?
Describe your modifications in plain English — different indicators, different risk parameters, long-only, multi-timeframe filter — and TradePilot generates the full Pine Script v6 code in seconds.