MACD Trend Following Strategy

Build a trend-following strategy using MACD (Moving Average Convergence Divergence). This tutorial teaches you to ride strong market trends by identifying momentum shifts and staying in winning positions longer than mean reversion strategies.

What You'll Build

A trend-following strategy that enters when MACD crosses above signal line (bullish) and exits when it crosses below (bearish). Profits from sustained directional moves.

Time Required: 20-25 minutes

Strategy Overview

Strategy Type

Trend Following - captures sustained price movements by riding momentum.

Indicator

MACD - measures trend direction and momentum through moving average convergence/divergence.

Entry Logic

Buy when MACD line crosses above signal line, indicating bullish momentum shift.

Exit Logic

Sell when MACD line crosses below signal line, indicating momentum weakening.

Understanding MACD

MACD Components

MACD Line: Difference between 12-period EMA and 26-period EMA. Represents short-term momentum.
Signal Line: 9-period EMA of MACD line. Smoothed average used for crossover signals.
Histogram: Difference between MACD and signal line. Visual representation of distance between lines.

Trading Signals: MACD > Signal = Bullish (buy), MACD < Signal = Bearish (sell). The crossovers indicate momentum shifts.

Step 1: Understand MACD in MangoLabs

Single Feature, Multiple Columns. MACD is a single feature with canonical name macd:fast=12:slow=26:signal=9. It exposes three sub-columns you can reference in a Data Source node:

  • macd — the MACD line (EMA12 − EMA26)
  • macd_signal — the signal line (9-period EMA of MACD)
  • macd_hist — the histogram (MACD − Signal)

To detect a crossover (MACD crossing above Signal), add both macd and macd_signal as columns, plus their :lag=1 variants for the previous bar.

Step 2: Build Strategy Logic

2

Design MACD Crossover Strategy

  1. Create new strategy: "MACD Trend Following"
  2. Build entry condition (bullish crossover — MACD crosses above Signal):
    Buy Signal:
    • Add a Data Source node; add columns macd, macd_signal, macd:lag=1, macd_signal:lag=1
    • Add Condition node: macd > macd_signal (current bar: MACD above Signal)
    • Add another Condition node: macd:lag=1 < macd_signal:lag=1 (previous bar: MACD was below Signal)
    • Add Logic (AND) node: both conditions must be true → crossover confirmed
    • Add an Action node set to BUY; connect the AND output to it
  3. Build exit condition (bearish crossover — MACD crosses below Signal):
    Sell Signal:
    • Reuse the same Data Source node
    • Add Condition node: macd < macd_signal
    • Add another Condition node: macd:lag=1 > macd_signal:lag=1
    • Add Logic (AND) node combining both
    • Add an Action node set to SELL; connect the AND output to it
  4. Save the strategy (validation runs automatically as you build)

Step 3: Backtest

Backtest Configuration

  • Symbol: BTCUSDT, ETHUSDT (trending assets work best)
  • Timeframe: 4h or 1d (MACD works better on higher timeframes)
  • Period: 1 year for robust results
  • Position Size: 100% (trend following benefits from full exposure)

Why higher timeframes? MACD is less noisy on 4h/daily candles. Trend following needs time to develop and capture large moves. Lower timeframes (1h, 15m) generate too many false signals.

Expected Results

Target Metrics for MACD Strategy

Total Return (1yr)

30-80%

Sharpe Ratio

0.8-1.5

Max Drawdown

20-35%

Win Rate

40-50%

Trades (1yr)

15-40

Avg Win/Loss Ratio

1.5-3.0

MACD strategies typically have lower win rates than mean reversion but larger winners (better risk/reward ratio). Drawdowns are higher due to riding trends through pullbacks.

Strategy Enhancements

Add Trend Filter

Only take MACD signals in direction of 200-day moving average. Reduces whipsaws in sideways markets.

Zero-Line Filter

Only buy when MACD crosses above signal AND MACD is above 0. Ensures trend is truly bullish.

Histogram Confirmation

Require MACD histogram to be increasing for 2-3 bars after crossover to confirm momentum.

Trailing Stop

Instead of exiting on crossunder, use a trailing stop (e.g., 10% from peak) to capture more of the trend.

MACD vs RSI: When to Use Each

Use MACD When:

  • Markets are trending (clear uptrends or downtrends)
  • You want to capture large, sustained moves
  • Trading on higher timeframes (4h, daily)
  • You can tolerate lower win rates for bigger winners
  • Willing to hold positions for days/weeks

Use RSI When:

  • Markets are range-bound (sideways movement)
  • You want quicker entries and exits
  • Trading on lower timeframes (1h, 15m)
  • You prefer higher win rates with smaller gains
  • Holding positions for hours/days

Completion Checklist

  • ✓ Configured a Data Source node with macd, macd_signal, and their :lag=1 variants
  • ✓ Built crossover logic using Condition + Logic (AND) nodes
  • ✓ Backtested on 4h/daily timeframe with 1+ year data
  • ✓ Achieved acceptable metrics (Sharpe >0.8, reasonable drawdown)
  • ✓ Deployed to paper trading
  • ✓ Understand trend following requires patience - fewer trades, bigger wins

What's Next?