Pine Script Double EMA (DEMA)

Master this ultra-responsive and low-lagging moving average in TradingView's Pine Script for early trend identification and powerful reversal signals.

Posted: Expertise: Intermediate/Advanced

What is the Double Exponential Moving Average (DEMA)?

The Double Exponential Moving Average (DEMA), introduced by Patrick Mulloy, is an advanced moving average designed to reduce lag significantly compared to traditional EMAs, while still maintaining a good level of smoothing. It attempts to eliminate the inherent lag of moving averages by subtracting a smoothed EMA from a single EMA. The goal is to create a more responsive trend-following indicator that provides earlier signals without excessive whipsaws.

In Pine Script, DEMA is a powerful tool for traders seeking a moving average that reacts quickly to price changes, making it ideal for identifying early trend shifts and dynamic support/resistance levels.

Components and Calculation

The calculation of DEMA involves two EMAs of the same length:

  1. EMA1: Calculate a standard Exponential Moving Average of the `source` (typically `close`) over a specified `length`.
  2. EMA2: Calculate an EMA of `EMA1` (the first EMA) over the *same* `length`.
  3. DEMA Formula: `DEMA = (2 * EMA1) - EMA2`

By subtracting the "lag" component of the second EMA from twice the first EMA, DEMA aims to accelerate the moving average's response to price changes, making it appear to move ahead of traditional EMAs.

Basic DEMA Implementation in Pine Script

Pine Script v5 provides a convenient built-in function `ta.dema()` that simplifies its implementation.

//@version=5
indicator("My Double EMA Indicator", overlay=true)

// Input for DEMA length
length = input.int(20, title="DEMA Length", minval=1)

// Calculate DEMA using the built-in function
demaValue = ta.dema(close, length)

// Plot the DEMA line
plot(demaValue, title="DEMA", color=color.blue, linewidth=2)

Key Advantage: DEMA is specifically designed to reduce the lag inherent in traditional moving averages, providing more timely signals for trend changes.

Practical DEMA Strategies

1. DEMA as a Trend Direction Filter (Color Change)

DEMA's responsiveness makes it an excellent indicator for quickly identifying the prevailing trend direction. Coloring the DEMA based on its slope is a clear visual strategy.

//@version=5
strategy("DEMA Trend Color Strategy", overlay=true)

// Input for DEMA length
length = input.int(20, title="DEMA Length", minval=1)

// Calculate DEMA
demaValue = ta.dema(close, length)

// Determine DEMA color based on its direction
demaColor = demaValue > demaValue[1] ? color.green : color.red

// Plot the DEMA line with dynamic color
plot(demaValue, title="DEMA", color=demaColor, linewidth=2)

// Example entry logic: buy when DEMA turns green, sell when DEMA turns red
longCondition = demaValue > demaValue[1] and demaValue[1] <= demaValue[2] // DEMA turns up
shortCondition = demaValue < demaValue[1] and demaValue[1] >= demaValue[2] // DEMA turns down

if (longCondition)
    strategy.entry("Long", strategy.long)

if (shortCondition)
    strategy.entry("Short", strategy.short)

2. DEMA Crossover Strategy (with Price or another DEMA)

Crossovers involving DEMA can generate powerful and early trading signals due to its reduced lag. This can be price crossing DEMA, or two DEMAs of different lengths crossing each other.

//@version=5
strategy("DEMA Crossover Strategy", overlay=true)

// Inputs for DEMA lengths
fastDemaLength = input.int(10, title="Fast DEMA Length", minval=1)
slowDemaLength = input.int(30, title="Slow DEMA Length", minval=1)

// Calculate DEMAs
fastDema = ta.dema(close, fastDemaLength)
slowDema = ta.dema(close, slowDemaLength)

// Plot the DEMAs
plot(fastDema, title="Fast DEMA", color=color.new(color.blue, 0), linewidth=2)
plot(slowDema, title="Slow DEMA", color=color.new(color.orange, 0), linewidth=2)

// Crossover conditions (Fast DEMA crossing Slow DEMA)
longCondition = ta.crossover(fastDema, slowDema)
shortCondition = ta.crossunder(fastDema, slowDema)

// Strategy entries/exits
if (longCondition)
    strategy.entry("Long", strategy.long)

if (shortCondition)
    strategy.entry("Short", strategy.short)

3. DEMA for Dynamic Support and Resistance

Due to its responsiveness, DEMA can often serve as dynamic support in an uptrend and dynamic resistance in a downtrend. Price interacting with the DEMA line can provide clues about trend strength and potential reversals.

//@version=5
indicator("DEMA Support/Resistance", overlay=true)

length = input.int(20, title="DEMA Length", minval=1)
demaValue = ta.dema(close, length)

plot(demaValue, title="DEMA", color=color.new(color.blue, 0), linewidth=2)

// Highlight potential support/resistance interactions (conceptual - adjust thresholds)
isSupportTouch = close > demaValue * 0.995 and close < demaValue * 1.005 and demaValue[1] < close[1] // Price touches DEMA from below or just above
isResistanceTouch = close < demaValue * 1.005 and close > demaValue * 0.995 and demaValue[1] > close[1] // Price touches DEMA from above or just below

plotshape(isSupportTouch, title="Potential Support", location=location.belowbar, color=color.new(color.green, 0), style=shape.circle, size=size.tiny)
plotshape(isResistanceTouch, title="Potential Resistance", location=location.abovebar, color=color.new(color.red, 0), style=shape.circle, size=size.tiny)

Optimizing DEMA Performance

To get the most from the Double Exponential Moving Average in Pine Script:

Fast but Not Perfect: DEMA is very responsive, but no moving average is entirely lag-free. Always be aware of the trade-off between responsiveness and false signals, especially in volatile, non-trending conditions.

Common DEMA Pitfalls

Conclusion

The Double Exponential Moving Average (DEMA) is a significant advancement in technical analysis indicators available in Pine Script for TradingView. Its innovative calculation method effectively minimizes lag, providing traders with a highly responsive and smoother line for identifying trend direction and potential reversals. By understanding its construction, thoughtfully tuning its parameters, and integrating it as part of a comprehensive trading strategy, you can leverage DEMA to gain a clearer and more timely perspective on market movements and enhance your trading decisions.