Pine Script Triple EMA (TEMA)

Master this ultra-responsive and virtually lag-free moving average in TradingView's Pine Script for razor-sharp trend identification and powerful reversal signals.

Subscribe now @ $99/month

What is the Triple Exponential Moving Average (TEMA)?

The Triple Exponential Moving Average (TEMA), introduced by Patrick Mulloy, is an advanced moving average that builds upon the concept of DEMA to reduce lag even further. It's designed to provide an even faster and smoother response to price changes than traditional EMAs and even DEMAs, aiming to get closer to the current price without sacrificing too much smoothing.

In Pine Script, TEMA is a powerful tool for traders who demand highly responsive trend-following indicators for quick market analysis and agile trade execution, especially in fast-moving markets.

Components and Calculation

The calculation of TEMA involves three EMAs of the same specified `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. EMA3: Calculate an EMA of `EMA2` (the second EMA) over the *same* `length`.
  4. TEMA Formula: `TEMA = (3 * EMA1) - (3 * EMA2) + EMA3`

This formula cleverly combines the three EMAs to effectively remove even more lag than DEMA, providing an extremely smooth and responsive moving average. It attempts to project the current trend more accurately by minimizing the distorting effect of lag.

Basic TEMA Implementation in Pine Script

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

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

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

// Calculate TEMA using the built-in function
temaValue = ta.tema(close, length)

// Plot the TEMA line
plot(temaValue, title="TEMA", color=color.blue, linewidth=2)
Ultra-Responsive: TEMA is celebrated for its ability to reduce lag significantly, making it one of the most responsive moving averages available.

Practical TEMA Strategies

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

TEMA's superior responsiveness makes it an excellent indicator for quickly identifying and staying with the prevailing trend direction. Coloring the TEMA based on its slope is a highly effective visual strategy.

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

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

// Calculate TEMA
temaValue = ta.tema(close, length)

// Determine TEMA color based on its direction
temaColor = temaValue > temaValue[1] ? color.green : color.red

// Plot the TEMA line with dynamic color
plot(temaValue, title="TEMA", color=temaColor, linewidth=2)

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

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

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

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

Crossovers involving TEMA generate extremely fast signals due to its minimal lag. This can be price crossing TEMA, or two TEMAs of different lengths crossing each other for a more refined signal.

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

// Inputs for TEMA lengths
fastTemaLength = input.int(10, title="Fast TEMA Length", minval=1)
slowTemaLength = input.int(30, title="Slow TEMA Length", minval=1)

// Calculate TEMAs
fastTema = ta.tema(close, fastTemaLength)
slowTema = ta.tema(close, slowTemaLength)

// Plot the TEMAs
plot(fastTema, title="Fast TEMA", color=color.blue, linewidth=2)
plot(slowTema, title="Slow TEMA", color=color.orange, linewidth=2)

// Crossover conditions (Fast TEMA crossing Slow TEMA)
longCondition = ta.crossover(fastTema, slowTema)
shortCondition = ta.crossunder(fastTema, slowTema)

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

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

3. TEMA for Dynamic Support and Resistance

TEMA's ability to hug price closely makes it an excellent candidate for identifying dynamic support in uptrends and dynamic resistance in downtrends. Interactions between price and the TEMA line can offer insights into trend strength and potential areas of interest.

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

length = input.int(20, title="TEMA Length", minval=1)
temaValue = ta.tema(close, length)

plot(temaValue, title="TEMA", color=color.blue, linewidth=2)

// Highlight potential support/resistance interactions (conceptual - adjust thresholds)
// These conditions check for price being very close to TEMA, implying a touch or test
isSupportTouch = close > temaValue * 0.995 and close < temaValue * 1.005 and temaValue[1] < close[1] // Price touches TEMA from below or just above
isResistanceTouch = close < temaValue * 1.005 and close > temaValue * 0.995 and temaValue[1] > close[1] // Price touches TEMA from above or just below

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

Optimizing TEMA Performance

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

Speed vs. Noise: TEMA is designed for speed. This means it might still produce more signals than a heavily smoothed average. Always confirm signals to avoid overtrading.

Common TEMA Pitfalls

Conclusion

The Triple Exponential Moving Average (TEMA) represents a significant leap forward in reducing lag in moving averages within Pine Script for TradingView. Its sophisticated calculation provides an exceptionally responsive and smooth line, making it ideal for traders who prioritize timely trend identification and dynamic signal generation. By understanding TEMA's unique characteristics, intelligently tuning its parameters, and integrating it strategically with other analytical tools, you can leverage its power to gain a sharper perspective on market movements and enhance your trading decisions.

Enhance Your Trading

Get a high-performance Pine Script 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.

Subscribe now @ $99/month