DEMA
DEMA in ProBuilder returns the Double Exponential Moving Average, a reduced-lag moving average built from two EMAs. Syntax, formula, examples, gotchas.
Syntax
DEMA[N](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
N | integer | 20 | Number of periods for both EMA passes. Lower values hug price tightly, higher values smooth more. |
price | price source | close | Series the average is computed on, such as close, open, high, low, or a custom variable. |
Formula
EMA1 = ExponentialAverage[N](price)
EMA2 = ExponentialAverage[N](EMA1)
DEMA = 2 * EMA1 - EMA2How it works
Every moving average lags price, and smoothing an EMA a second time lags even more. The DEMA construction exploits that: the difference EMA1 - EMA2 approximates the current lag, and adding it back on top of EMA1 (which is what 2 * EMA1 - EMA2 does) pushes the line forward toward the most recent prices.
The result is not an average of averages. Despite the name, the DEMA responds faster than a single EMA of the same period, not slower. In trending markets it turns earlier at swing points, which is the reason it is used in crossover systems where signal delay is costly.
The trade-off is stability. Because the lag compensation is an extrapolation, the DEMA can overshoot price after sharp moves and generate whipsaw crossings in choppy conditions. Confirmation from a slower average or a trend strength filter is the usual remedy.
Examples
Example 1, DEMA versus EMA lag comparison (Indicator)
// Same period, two smoothing methods, plotted on price
fastLine = DEMA[20](close)
slowLine = ExponentialAverage[20](close)
RETURN fastLine COLOURED(200, 60, 60) AS "DEMA 20", slowLine COLOURED(60, 60, 200) AS "EMA 20"Overlays a 20-period DEMA and a 20-period EMA. The DEMA visibly turns earlier at swing highs and lows, showing the lag reduction.
Example 2, Counting bars below the average (Indicator)
// 10-period DEMA of the opening price
i1 = DEMA[10](open)
a = 0
// Count how many consecutive past opens sit below the current DEMA value
WHILE Open[a + 1] < i1 DO
a = a + 1
WEND
RETURN aWalks backwards through the bars and counts how many consecutive previous opens are below the current DEMA, a rough measure of how stretched price is against the average.
Example 3, Double DEMA crossover system (ProOrder)
// Fast and slow DEMA crossover with position reversal
fastDema = DEMA[10](close)
slowDema = DEMA[40](close)
IF fastDema CROSSES OVER slowDema THEN
BUY 1 CONTRACT AT MARKET
ELSIF fastDema CROSSES UNDER slowDema THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIFA classic dual moving average system using DEMA on both legs. The reduced lag moves entries closer to the actual turn than the same system built on simple averages.
Interpretation
DEMA is read like any moving average, only faster:
- Price above a rising DEMA. Uptrend reading. Pullbacks toward the line are potential continuation zones.
- Price below a falling DEMA. Downtrend reading, symmetric to the above.
- Crossovers. A fast DEMA crossing a slow one signals a momentum shift earlier than the equivalent SMA or EMA crossover, with correspondingly more false starts in ranges.
Common errors and gotchas
- Assuming DEMA is slower than EMA. The name suggests double smoothing, but the formula subtracts the second EMA. A DEMA reacts faster than an EMA of the same period, so porting period settings from EMA systems changes behaviour.
- Overshoot after gaps and spikes. The
2 * EMA1 - EMA2extrapolation can move the line beyond price after abrupt moves. Conditions likeclose CROSSES OVER demamay fire more often than expected right after volatility events. - Wrong bracket usage. The period goes in square brackets and the price in parentheses:
DEMA[10](open).DEMA(10, open)raises a syntax error. - Backwards loops must terminate. When scanning past bars with
WHILE, as in Example 2, make sure the loop condition eventually fails on available history, otherwise the indicator hits the platform loop limit on long charts.
Related instructions
ExponentialAverage, the single EMA that DEMA is built from.TEMA, triple exponential variant with even less lag and more overshoot.ZLEMA, zero-lag EMA, a different approach to lag compensation.HullAverage, weighted-average based low-lag alternative.Average, simple moving average baseline.WeightedAverage, linearly weighted average.WilderAverage, slow smoothing used inside RSI and ADX.TimeSeriesAverage, linear-regression based average.
