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:
- EMA1: Calculate a standard Exponential Moving Average of the `source` (typically `close`) over a specified `length`.
- EMA2: Calculate an EMA of `EMA1` (the first EMA) over the *same* `length`.
- 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)
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.
- Uptrend: DEMA is rising (current DEMA > previous DEMA).
- Downtrend: DEMA is falling (current DEMA < previous DEMA).
//@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:
- Parameter Tuning: The `length` parameter is key. Experiment with different lengths (e.g., 9, 20, 50) to find what works best for your chosen asset and timeframe. Shorter lengths make it more reactive but can increase noise, while longer lengths provide more smoothing but with a slight increase in lag.
- Multi-Timeframe Analysis: Use DEMA on higher timeframes to confirm the overall trend and on lower timeframes for precise entries and exits. This helps to filter out noise on shorter timeframes.
- Combine with Other Indicators: DEMA is excellent for trend identification, but it's not a standalone indicator. Pair it with volume indicators, oscillators (like RSI for overbought/oversold conditions, or MACD for momentum confirmation), or price action analysis for stronger signals.
- Avoid Choppy Markets: While DEMA reduces lag, it can still produce false signals in prolonged sideways or non-trending markets. Consider using a trend strength indicator (e.g., ADX) to confirm a clear trend before relying heavily on DEMA signals.
Common DEMA Pitfalls
- Whipsaws in Consolidation: Despite its advanced smoothing, DEMA can still generate whipsaws in extremely flat or volatile, non-trending markets, leading to false signals.
- Requires Confirmation: While DEMA provides early signals, relying solely on its crossovers without additional confirmation from other indicators or price action can lead to premature entries or exits.
- Over-Optimization: Tuning DEMA parameters too precisely to historical data can result in curve-fitting, where the strategy performs well on past data but poorly in live trading.
- Not an Overbought/Oversold Indicator: DEMA is a trend-following tool and does not inherently provide information about overbought or oversold market conditions, unlike oscillators.
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.