PipLedger
Today's forex newsNFP, CPI, FOMC and other high-impact eventsEconomic calendarCalendar →

MACD strategy with the 200 EMA: rules, a worked example and a free Pine Script

The MACD is one of the most widely used indicators, and pairing it with a 200 EMA is a popular way to trade it: follow the long-term trend, and use a MACD cross below the zero line to time the pullback. This guide explains the three parts of the MACD, the exact rules, a worked USDJPY example with the yen pip maths, and a free TradingView script.

By M. A. Horaira. Updated 10 October 2026. 9 minute read.

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 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

Target 157.018Entry 156.066Stop 155.431MACD crosses up1.5R0Cross below zeroMACD (12, 26, 9)

200 EMA (price chart) and signal line (MACD panel) MACD line

Illustrative chart drawn for this guide to show the rules. It isn't a real day's prices. USDJPY, 1-hour candles, with the MACD below.

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.

On yen pairs the profit is earned in yen, so its dollar value depends on the USDJPY rate when the trade closes.
USDJPY long
Entry156.066
Stop loss (10-candle low)155.431, 63.5 pips away
Target (1.5R)157.018, 95.2 pips away
Pip value1 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 hitabout $98
Profit at the targetabout $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

Common mistakes

  1. Taking every MACD cross. Without the trend filter and the zero line, the cross is noisy.
  2. Reading the histogram as a separate signal. The histogram changing sign is the same event as the cross.
  3. Acting before the candle closes. The lines can cross and uncross during a candle.
  4. 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.

  1. Open the chart. On TradingView, open USDJPY on the 1-hour chart.
  2. 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".
  3. 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.
  4. 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.
  5. 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.

Quick answers

What is the MACD and 200 EMA strategy?

It trades in the direction of the 200 EMA and uses a MACD cross to time the entry: buy when the MACD line crosses above its signal line below the zero line while the price is above the 200 EMA, and the reverse for shorts.

What are the standard MACD settings?

12, 26 and 9: a 12-period and a 26-period EMA for the MACD line, and a 9-period EMA of that line for the signal line. Most traders, and this script, use these settings.

Why use the 200 EMA with the MACD?

The 200 EMA shows the long-term trend. Taking only MACD signals in that direction cuts many of the false crosses that happen against the trend.

Which timeframe is best for the MACD strategy?

It's used on everything from the 15-minute to the daily chart. The 1-hour and 4-hour charts are common for forex. No timeframe is best in general; backtest the one you'll trade.

What is the MACD histogram?

The MACD line minus the signal line. When it crosses zero, the MACD line has crossed the signal line. Its height shows how far apart the two lines are.

Does the MACD strategy work for gold?

The rules apply to any chart, and the script runs on XAUUSD unchanged. Whether they work on gold over the dates you test is something only a backtest can show.

More guides

All 75 guides →

हिंदी में पढ़ें: Lot size kaise nikale?, Prop firm challenge kaise pass kare?, Gold trading kaise kare?, Forex trading kya hai, और India में यह legal है या नहीं?

Trading psychology

The habits that decide whether a good strategy makes money.

All 14 trading psychology guides

8 popular prop firms

Well-known firms for forex, gold and index traders that were still operating in 2026. Compare their rules before you buy a challenge.

If a link in this list is a referral link, PipLedger may earn a commission if you sign up, at no extra cost to you. This list is not a recommendation. Programs, prices and rules change often, so check each firm's own site, including its margin and drawdown rules, before you buy a challenge.