Logo
OFFLINEPIXEL
/ pinescript-strategies / pine-script-dema

$ Pinescript Double EMA (DEMA)

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

500+ Clients Helped
100% Satisfaction
Live Trading Ready
⚠️
Trading financial markets carries risk. All content (PineScript code, indicators, strategies) on this website is for educational purposes only. Past performance is not indicative of future results. By using any code or information from this site, you agree that you are solely responsible for your trading decisions. The author disclaims all liability for any losses incurred. To gain from experts experiences, You can always try our Invite Only Scripts
01

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 Pinescript, 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.

02

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`
03

Basic DEMA Implementation in Pinescript

pine-script@terminal
//@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)
$ ✓ Compiled successfully
04

Key Advantage

Key Advantage

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

05

Practical DEMA Strategies

06

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

pine-script@terminal
//@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)
$ ✓ Compiled successfully
07

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

pine-script@terminal
//@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)
$ ✓ Compiled successfully
08

3. DEMA for Dynamic Support and Resistance

pine-script@terminal
//@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)
$ ✓ Compiled successfully
09

Optimizing DEMA Performance

To get the most from the Double Exponential Moving Average in Pinescript:

  • 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.
10

Fast but Not Perfect

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.

11

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.
12

Conclusion

Conclusion

The Double Exponential Moving Average (DEMA) is a significant advancement in technical analysis indicators available in Pinescript 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.

Enhance Your Trading

Get a high-performance Pinescript analysis tool for actionable market insights, designed for traders on the move.

This strategy runs in live mode on TradingView, helping you identify potential opportunities.

Get Pinescript Strategy