Master this ultra-responsive and low-lagging moving average in TradingView's Pinescript for early trend identification and powerful reversal signals.
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.
The calculation of DEMA involves two EMAs of the same length:
//@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)
DEMA is specifically designed to reduce the lag inherent in traditional moving averages, providing more timely signals for trend changes.
//@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)
//@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)
//@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)
To get the most from the Double Exponential Moving Average in Pinescript:
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.
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.
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