StratCraft
Backtrader Indicator Guide

Backtrader EMA Crossover Trend Following Strategy

Trend-following indicator that gives more weight to recent prices, reacting faster than SMA. Crossovers between fast and slow EMAs generate trading signals.

bt.indicators.EMATrend Following Strategies

Trend-following indicator that gives more weight to recent prices, reacting faster than SMA. Crossovers between fast and slow EMAs generate trading signals.

EMA crossovers are the classic trend-following entry signal. When a fast EMA (e.g., 12-period) crosses above a slow EMA (e.g., 26-period), it signals the start of an uptrend. Trend-following strategies hold positions until the EMAs cross back, capturing the full trend move.

Learn more about Trend Following Strategies →
Pythonbacktrader
import backtrader as bt

class EMATrendFollowing(bt.Strategy):
    params = (('fast', 12), ('slow', 26))

    def __init__(self):
        self.ema_fast = bt.indicators.EMA(self.data.close, period=self.p.fast)
        self.ema_slow = bt.indicators.EMA(self.data.close, period=self.p.slow)
        self.crossover = bt.indicators.CrossOver(self.ema_fast, self.ema_slow)

    def next(self):
        if self.crossover > 0:  # Fast EMA crosses above slow EMA
            self.buy()
        elif self.crossover < 0:  # Fast EMA crosses below slow EMA
            self.sell()
ParameterDefaultDescription
period12EMA lookback period