MACD Crossover EA Template for MetaTrader 5
The MACD (Moving Average Convergence Divergence) Crossover is one of the most trusted momentum strategies in trading. This free EA template uses the standard 12/26/9 MACD with signal line crossover detection, ATR-based risk management, and an optional higher-timeframe trend filter. Best for EURUSD, GBPUSD, and XAUUSD on H1 to H4 timeframes. Build it in AlgoStudio without coding and export a production-ready MQL5 Expert Advisor in minutes.
What Is a MACD Crossover Strategy?
The MACD indicator measures the relationship between two exponential moving averages (by default, the 12 EMA and 26 EMA). The difference between these two EMAs forms the MACD line. A 9-period EMA of the MACD line forms the signal line. When the MACD line crosses above the signal line, it indicates that short-term momentum is turning bullish. When it crosses below, momentum is turning bearish.
This is a momentum-based strategy — it detects shifts in the speed and direction of price movement. Unlike pure trend-following strategies that focus on direction alone, the MACD also captures acceleration and deceleration of trends. A MACD crossover often signals a trend change before a simple moving average crossover does, because it measures the convergence and divergence of the underlying EMAs.
Gerald Appel developed the MACD in the late 1970s, and it remains one of the most widely used indicators in trading. Its popularity is justified by its simplicity and effectiveness: the 12/26/9 default settings have been tested across decades of market data on virtually every tradeable instrument.
How This EA Template Works
The template uses signal line crossover detection — it enters when the MACD line crosses the signal line. This is the most common and most reliable MACD entry method, providing early momentum signals without the lag of waiting for a zero-line cross.
The optional higher-timeframe trend filter (200 EMA on H4) ensures you only take MACD signals in the direction of the larger trend. This is especially valuable because the MACD generates frequent crossovers during consolidation periods, and the trend filter helps you avoid most of these false signals.
Default Parameters
These defaults use the universally recognized 12/26/9 MACD on H1. All parameters are exported as input variables so you can optimize them in the MT5 Strategy Tester.
| Parameter | Value | Type |
|---|---|---|
| Fast EMA Period | 12 | Indicator |
| Slow EMA Period | 26 | Indicator |
| Signal Period | 9 | Indicator |
| Stop Loss | 1.5x ATR(14) | ATR-based |
| Take Profit | 2:1 R:R | Risk-reward |
| HTF Trend Filter | 200 EMA on H4 (optional) | Filter |
| Position Sizing | 1% risk per trade | Risk |
How to Build This EA Without Coding
1. Create a new project in AlgoStudio
Sign up for free (no credit card required) and click “New Project”. Name your project “MACD Crossover Strategy” and open the visual builder canvas.
2. Add the MACD indicator block
Drag a MACD indicator block onto the canvas and keep the standard 12/26/9 settings. Connect it to Buy and Sell condition nodes, then add Stop Loss and Take Profit nodes. The MACD uses signal line crossover detection — buy when the MACD line crosses above the signal line, sell when it crosses below.
3. Configure risk management and optional trend filter
Set the stop loss to 1.5x ATR(14), take profit to 2:1 risk-reward, and position sizing to 1% risk per trade. Enable the higher-timeframe trend filter (200 EMA on H4) to trade only in the direction of the larger trend. This prevents most false signals during sideways markets.
4. Export, backtest, and optimize
Click Export to generate a .mq5 file. Load it into MetaTrader 5 and backtest on EURUSD H1 with at least 2 years of historical data. Optimize the MACD periods cautiously — the standard 12/26/9 is hard to beat. Demo trade for 1-3 months before going live.
Optimization Tips
Use the HTF trend filter
Adding a 200 EMA on H4 as a trend filter significantly reduces false signals. Only take buy signals when price is above the EMA and sell signals when below. This prevents most whipsaw losses during consolidation periods and keeps you trading in the direction of the dominant trend.
Consider wider stops for volatile instruments
The default 1.5x ATR stop loss works well for forex pairs. For XAUUSD (gold) and other volatile instruments, increase to 2.0x ATR to avoid being stopped out by normal volatility. The take profit should scale proportionally — if you use 2.0x ATR for the stop, use a 2:1 risk-reward ratio for a 4.0x ATR take profit.
Resist the urge to over-optimize MACD periods
The 12/26/9 MACD has been the standard for over 40 years. If your backtester says 11/23/7 is significantly better, you're likely overfitting. Small changes to MACD periods (like 10/26/9 or 12/28/9) may have merit, but dramatic departures from the standard almost always indicate curve-fitting. A strategy that only works with one specific parameter set is not robust.