Academy/Strategy Building/Trend-Following Strategies
Strategy BuildingLesson 3

Trend-Following Strategies

Learn how to build strategies that profit from uptrends and downtrends. "The trend is your friend."

12 minute read
4 key takeaways

Trend-Following: Riding the Wave

The trend is your friend—until it ends. Trend-following strategies buy in uptrends and sell in downtrends. They're the oldest algorithm and still work because markets genuinely trend.

Why Trends Work

New information doesn't instantly distribute. Institutions move slowly, retail investors lag further. Momentum compounds as more participants jump on the trend. This creates exploitable persistence.

The SMA Crossover: The Classic

BUY when SMA_fast > SMA_slow, SELL when SMA_fast < SMA_slow

Golden cross = bullish, death cross = bearish

python
df['SMA_20'] = df['Close'].rolling(20).mean()
df['SMA_50'] = df['Close'].rolling(50).mean()

# Generate signals
df['Position'] = 0
df.loc[df['SMA_20'] > df['SMA_50'], 'Position'] = 1  # Long
df.loc[df['SMA_20'] < df['SMA_50'], 'Position'] = -1  # Neutral

# Entry/exit points
df['Signal'] = df['Position'].diff()  # 2=entry, -2=exit, 0=hold

Donchian Channel Breakout

Buy when price breaks above the N-day high (new 20-day high = breakout to upside). Sell when price breaks below N-day low.

High = Max(Close, last N days), Low = Min(Close, last N days)

Buy on breakout above high, sell on breakdown below low

ATR Trailing Stop

Once in a trend, use ATR to place a trailing stop that adapts to volatility.

Stop = Current Price - (N × ATR)

Typically N=1.5 to 3

In high volatility, ATR is large so stop is further (avoiding whipsaws). In low volatility, stop is closer (protecting gains). Adaptive to market conditions.

The Trend Filter: Avoid Counter-Trend Trades

Many losing trades happen when you trade against the longer-term trend. Solution: only trade in the direction of the major trend.

python
# Trend filter: Only trade in direction of 200-day SMA
df['SMA_200'] = df['Close'].rolling(200).mean()
df['In_Uptrend'] = df['Close'] > df['SMA_200']

# Only go long when in uptrend
df['Valid_Signal'] = (df['Signal'] == 1) & (df['In_Uptrend'])

When Trend-Following Fails

  • Range-bound markets with no trend (sideways trading)
  • False breakouts that immediately reverse
  • Severe trends with massive drawdowns (2008, 2020)
  • Sudden trend reversals (whipsaws)

Whipsaws Are Part of the Game

You'll get stopped out before the real move starts. This is normal. Trend-following has poor entry timing but excellent long-term risk/reward.

Calm = Profits for Trend Followers

Trend-following strategies profit most in steady, continuous trends. Choppy, volatile markets = lots of small losses. Use VIX as a filter: trade less when VIX > 30.

Key Takeaways
  • Trends exist because information diffuses slowly through markets
  • Trend-following has positive skew: many small losses, few large wins
  • Moving average crosses are simple but effective trend signals
  • Filter trades using a longer-term trend indicator to avoid counter-trend trades