The rules at a glance
- Indicators: MACD (12, 26, 9) and a 200-period EMA
- Long: the price is above the 200 EMA and the MACD line crosses above its signal line while both are below zero (a pullback in an uptrend)
- Short: the price is below the 200 EMA and the MACD line crosses below its signal line while above zero
- Stop loss: the lowest low of the last 10 candles for a long (highest high for a short), or 1.5 × ATR
- Target: 1.5 times the risk (1.5R); one trade at a time
How the MACD works
The Moving Average Convergence Divergence indicator was created by Gerald Appel in the late 1970s (Thomas Aspray added the histogram in 1986). It has three parts:
- The MACD line: the 12-period EMA minus the 26-period EMA. Above zero, the shorter-term average is above the longer one, so momentum is up.
- The signal line: a 9-period EMA of the MACD line, a slower version of it.
- The histogram: the MACD line minus the signal line. It grows as momentum builds and shrinks as it fades.
The most common signal is the MACD line crossing its signal line. On its own, that cross gives a lot of false signals in sideways markets. The 200 EMA filter and the zero line are what turn it into a more selective strategy.
Why the cross has to be below zero
When the price is above the 200 EMA, the long-term trend is up. A MACD that dips below zero in that trend means a pullback: short-term momentum has turned down for a while. When the MACD then crosses back above its signal line, still below zero, momentum is turning up again from a pullback, which is the moment trend traders want. A cross up above zero in the same trend tends to come later in the move, with less room left. The script lets you drop the zero-line rule to compare.
The rules, step by step
1. Check the trend
Longs only when the price closes above the 200 EMA; shorts only below it.
2. Wait for the cross below zero
The MACD line crosses above the signal line, at a candle close, while the MACD line is below zero.
3. Place the stop at the swing
The stop goes at the lowest low of the last 10 candles, the bottom of the pullback. The script's other choice is 1.5 × ATR.
4. Target 1.5 times the risk
This strategy uses a 1.5R target rather than 2R, because the pullback stop can be fairly wide. Test 2R as well.
Worked example: USDJPY, 1-hour chart
200 EMA (price chart) and signal line (MACD panel) MACD line
USDJPY has been climbing for about two weeks and is well above the 200 EMA (about 154.43). A pullback drags the MACD line below zero. Then, with the MACD still at -0.075, it crosses back above its signal line, and the candle closes at 156.066. The lowest low of the last 10 candles is 155.431.
| USDJPY long | |
|---|---|
| Entry | 156.066 |
| Stop loss (10-candle low) | 155.431, 63.5 pips away |
| Target (1.5R) | 157.018, 95.2 pips away |
| Pip value | 1 pip on 1 lot is ¥1,000, which is $6.41 at 156.07 |
| Account and risk | $10,000, risking 1% = $100 |
| Lot size | $100 ÷ (63.5 pips × $6.41) = 0.246, rounded down to 0.24 lots |
| Loss if the stop is hit | about $98 |
| Profit at the target | about $146 |
The pullback ends and the trend resumes; the target is reached 12 hours after the entry. The pip value calculator works out yen pip values for any account currency.
For a filter that measures how strong a trend is rather than which way it's going, see the ADX trend following strategy.
Risk management
- Expect a lower win rate in ranges. When the price chops around the 200 EMA, the filter flips back and forth and the crosses lose their meaning.
- Size from the swing. Pullback depths vary, so the stop distance does too. Keep the risk fixed.
- Watch the yen. USDJPY can move sharply on Bank of Japan decisions or talk of intervention. Check the economic calendar.
- Mind the swap. On the 1-hour chart, trades can last a day or two. The swap calculator shows the overnight cost.
Common mistakes
- Taking every MACD cross. Without the trend filter and the zero line, the cross is noisy.
- Reading the histogram as a separate signal. The histogram changing sign is the same event as the cross.
- Acting before the candle closes. The lines can cross and uncross during a candle.
- Using forex pip values on yen pairs. A yen pip is 0.01, worth about $6.40 a lot at these prices, not $10.
Backtest it yourself on TradingView
This page doesn't quote a win rate or a profit figure, and you should be wary of anyone who does without showing the test. Results change with the market, the dates, the data feed and the costs you put in. The honest way is to run the test yourself, and the free script below does the work in a couple of minutes.
- Open the chart. On TradingView, open USDJPY on the 1-hour chart.
- Add the script. Open the Pine Editor from the Pine icon in the panel on the right of the chart (older layouts have it as a tab under the chart), delete the code that's already there, paste this strategy's code and click "Add to chart".
- Put in your costs. Open the strategy's settings and go to Properties. Enter your broker's commission and a little slippage. A test without costs flatters every strategy, and short-term ones most of all.
- Read the Strategy Tester. The panel under the chart shows the net profit, the maximum drawdown, the profit factor, the number of trades and a list of every trade. Click a few trades and check on the chart that each one follows the rules. Trade sizes are shown in units rather than lots: 100,000 units is one lot of a forex pair, and 100 ounces is one lot of gold.
- Change one setting at a time. Compare the zero-line rule on and off first, then the swing stop with the ATR stop, and 1.5R with 2R. Try the same settings on EURUSD, XAUUSD and the 4-hour chart.
As a rough guide, don't trust a result with fewer than 100 trades, a profit factor under 1 once costs are in, or a drawdown you couldn't sit through with real money. Settings tuned to look good on one stretch of history often fail on the next, so check the same settings on dates you didn't tune them on, then demo trade the strategy for a few weeks. How much history you can test depends on your TradingView plan: the free plan loads 5,000 candles, which is a few months of 15-minute candles but years of daily ones.
The script sizes each trade from the stop loss and your risk percentage, starts with $10,000 and assumes 1:100 leverage (a 1% margin setting), so the tester doesn't flag margin calls on normal forex trade sizes. Change these under Properties to match your account. By default TradingView fills orders at the open of the candle after the signal, so the fill can differ a little from the signal candle's close.
Download the free Pine Script and PDF
The script is written in Pine Script version 6 for this guide and follows the rules above. It can't read the economic calendar, so skipping news days is up to you. It's free to use, change and share. The PDF is a one-page cheat sheet with the rules, a worked example, the lot size maths and a checklist, to keep next to your chart.
Show the code (Pine Script v6, 54 lines)
//@version=6
// MACD and 200 EMA strategy by PipLedger (https://pipledgerfx.com/macd-strategy)
// Buys a MACD cross up below the zero line while the price is above the 200 EMA (a pullback in an uptrend), and the reverse for shorts.
// Free to use and change. For education and backtesting only; not financial advice.
strategy("PipLedger: MACD + 200 EMA", shorttitle="MACD+EMA", overlay=true, initial_capital=10000, pyramiding=0, default_qty_type=strategy.fixed, default_qty_value=1, margin_long=1, margin_short=1, currency=currency.USD)
// ---------- Inputs
fastLen = input.int(12, "MACD fast length", minval=1)
slowLen = input.int(26, "MACD slow length", minval=2)
sigLen = input.int(9, "MACD signal length", minval=1)
trendLen = input.int(200, "Trend EMA", minval=10)
needZero = input.bool(true, "Cross must happen below the zero line (above it for shorts)")
stopType = input.string("Swing", "Stop loss", options=["Swing", "ATR"])
swingBars = input.int(10, "Swing lookback (bars)", minval=2)
atrMult = input.float(1.5, "ATR multiple (for the ATR stop)", minval=0.1, step=0.1)
rr = input.float(1.5, "Target (R multiple)", minval=0.5, step=0.25)
riskPct = input.float(1.0, "Risk per trade (% of equity)", minval=0.1, maxval=5.0, step=0.1)
// ---------- Helpers
fx = strategy.convert_to_account(1.0) // one unit of the chart's quote currency in account currency (1 on USD-quoted charts)
calcQty(float entry, float stop) =>
float dist = math.abs(entry - stop)
dist > 0 and not na(fx) ? strategy.equity * riskPct / 100 / (dist * syminfo.pointvalue * fx) : 0.0
[macdLine, sigLine, hist] = ta.macd(close, fastLen, slowLen, sigLen)
emaT = ta.ema(close, trendLen)
atr = ta.atr(14)
lowSw = ta.lowest(low, swingBars)
highSw = ta.highest(high, swingBars)
upX = ta.crossover(macdLine, sigLine)
dnX = ta.crossunder(macdLine, sigLine)
flat = strategy.position_size == 0
longSig = flat and upX and close > emaT and (not needZero or macdLine < 0)
shortSig = flat and dnX and close < emaT and (not needZero or macdLine > 0)
if longSig
float sl = stopType == "Swing" ? lowSw : close - atr * atrMult
float q = calcQty(close, sl)
if q > 0 and sl < close
strategy.entry("Long", strategy.long, qty=q)
strategy.exit("Long exit", "Long", stop=sl, limit=close + (close - sl) * rr)
if shortSig
float sl = stopType == "Swing" ? highSw : close + atr * atrMult
float q = calcQty(close, sl)
if q > 0 and sl > close
strategy.entry("Short", strategy.short, qty=q)
strategy.exit("Short exit", "Short", stop=sl, limit=close - (sl - close) * rr)
// ---------- Chart (add TradingView's built-in MACD in its own pane to see the lines)
plot(emaT, "200 EMA", color=color.new(color.orange, 0), linewidth=2)
plotshape(longSig, "MACD cross up", shape.triangleup, location.belowbar, color.teal, size=size.small)
plotshape(shortSig, "MACD cross down", shape.triangledown, location.abovebar, color.red, size=size.small)Trading this from India
Residents can trade EURUSD, GBPUSD and USDJPY futures and options on NSE and BSE through a SEBI-registered broker. The contracts are quoted the same way as the spot pair, one lot is 1,000 units of the base currency, and they're cash-settled in rupees. They trade only from 9 a.m. to 7:30 p.m. IST, volumes are thin and not every broker offers them, so check the hours and the spread first. Trading forex or CFDs with offshore online platforms isn't allowed for residents under FEMA, even if the app is easy to download. Details are in is forex trading legal in India.
Related strategies: 9 and 21 EMA crossover, RSI 60/40, Bollinger Bands squeeze, VWAP pullback, XAUUSD scalping, ADX trend following, 20 and 50 EMA pullback. All 34 strategies are compared on one page in forex trading strategies.