How To Trade With Triple Exponential Moving Average (TEMA)?

In this post, I want to discuss in detail how to trade with the Triple Exponential Moving Average (TEMA). It is a good thing to know the different technical indicators that are available in the toolkit. Did you read the post on Fuzzy Candlestick Patterns MQL5 Indicator? I tried to use MQL5 fuzzy logic library in coding candlestick patterns. In this post, I will also code TEMA in MQL4 and see whether it can help us in improving our trading accuracy. Moving averages are a standard tool for a professional trader. Traders use both Simple Moving Averages (SMA) as well as Exponential Moving Averages (EMA). In a simple moving average we give equal weight to every price bar while in the exponential moving average we give more weight to the recent price bars and gradually less weight to not recent price bars. In trading, it you want to develop an edge, you will need to learn how to develop your own custom indicators. This is precisely what Patrick G. Mulloy did in 1994 when he published an article in the famous technical trading magazine, Technical Analysis of Stocks and Commodities. He wanted to develop a moving average that did not lag price. He called it Triple Exponential Moving Average. So what is this  Triple Exponential Moving Average you might be wondering? Let’s start by finding this out first.

Triple Exponential Moving Average Indicator for MT4

As you must have known by now, moving averages can smooth the price but they always lag behind the price. Problem of is lag is there in every technical indicator. Trading requires you to enter in the trend as early as possible so that you reap the maximum profit. Traders call it staying ahead of the crowd. Staying ahead of the crowd is an edge that you need. But when you use technical indicators they are lagging and have almost zero predictive power.So if you are interested in using moving averages or for that matter other indicators in trading, your signal will always be late and there are chances when you enter the market the move is almost over. This is what happens when you try to wait for the fast moving average to cross above the slow moving average before you enter into a buy signal. This problem of lag is more severe in case of simple moving averages as compared to exponential moving averages. But exponential moving averages also lag considerable and cannot follow sudden price changes. This is what most professional traders do. They don’t trade the moving average cross. They use moving averages as dynamic support and resistance levels where price has a very high chance of bouncing. You can read this post in which I explain how you can use EMA 21 and EMA 55 as dynamic support and resistance.

Coming back to Triple Exponential Moving Average, bad news! Triple Exponential Moving Average indicator is not available by default in MT4. It is available in MT5 but it is not available in MT4. Don’t worry. I have coded a TEMA indicator. You can copy the TEMA MQL4 code and use it! Go through the MQL4 code below. Coding your own indicators and EA is a thing that you should learn. Days of manual trading is over. You should learn algorithmic trading. The first step in learning algorithmic trading is to learn a programming language. MQL4 and MQL5 are two programming languages that simple for you to learn easily. Once you start coding your indicators and EA in MQL4/MQL5, you can learn machine learning and deep learning and start using them in your indicators and EA.  Did you read the post on how to double your account in 1 trade? In the code below, you will see that TEMA is not a moving average rather it is based on a formula that first calculates an exponential moving average. We calculate the exponential moving average of the exponential moving average which is in essence double exponential moving average and subtract it from the exponential moving average.  Then we also calculate the exponential moving average of the double exponential moving average and subtract that too from the exponential moving average.

//+------------------------------------------------------------------+
//|                                                         TEMA.mq4 |
//|                                                     Ahmad Hassam |
//|                                       https://www.doubledoji.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "https://www.doubledoji.com/"
#property strict


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property  indicator_width1  2
//---- input parameters
input int       periodEMA=14; //Smoothing Period
//---choose which price to use
input ENUM_APPLIED_PRICE priceTEMA = PRICE_CLOSE; //Applied Price 


//---- buffers
double bufferTEMA[];
double EMA[];
double doubleEMA[];
double tripleEMA[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
//----Initialize the indicator buffer
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,bufferTEMA);
//---Initialize the secondary buffers   
   SetIndexBuffer(1,EMA);
   SetIndexBuffer(2,doubleEMA);
   SetIndexBuffer(3,tripleEMA);

   IndicatorShortName("TEMA("+(string)periodEMA+")");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i=0;
   int kk1=0;
   int kk2=0;
   int kk3=0;
   int counted_bars=IndicatorCounted();
//----
   if (counted_bars==0)
      {
      kk1=Bars-1;
      kk2=kk1-periodEMA;
      kk3=kk2-periodEMA;
      }
   if (counted_bars>0)
      {
      kk1=Bars-counted_bars-1;
      kk2=kk1;
      kk3=kk2;
      }
/* 
   calculate the moving average
   double  iMA(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // averaging method
   int          applied_price,    // applied price
   int          shift             // shift
   );
   
 Calculates the Moving Average indicator on data, stored in array, and returns its value.

double  iMAOnArray(
   double       array[],          // array with data
   int          total,            // number of elements
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // MA averaging method
   int          shift             // shift
   );
*/
// calculate the exponential moving average EMA
   for (i=kk1;i>=0;i--) EMA[i]=iMA(NULL,0,periodEMA,0,MODE_EMA,priceTEMA,i);  //A
// calculate the EMA of EMA
   for (i=kk2;i>=0;i--) doubleEMA[i]=iMAOnArray(EMA,0,periodEMA,0,MODE_EMA,i);  //B
//calculate the EMA of EMA of EMA
   for (i=kk3;i>=0;i--) tripleEMA[i]=iMAOnArray(doubleEMA,0,periodEMA,0,MODE_EMA,i);  //C
// calulate the TEMA
   for (i=kk3;i>=0;i--) bufferTEMA[i]=3*EMA[i]-3*doubleEMA[i]+tripleEMA[i];       //D
//----
   return(0);
  }
//+------------------------------------------------------------------+

In the above code, Triple Exponential Moving Average gets calculated in lines A, B, C and D above. Let’s discuss each line now. In line A, we calculate the exponential moving average for each bar in the start and then after that only for each new bar. This we do by initializing kk1 variable with Bars-1 when counted_bars is zero and when it is not zero we change kk1 to Bars – counted_bars -1. We save each value in an array EMA. In code line B, we calculate the EMA of EMA for the smoothing period periodEMA which in our case is 14 but you can change it to any integer value like 21 and 55 and whatever you like. We save this in an array doubleEMA. In code line C, we calculate the EMA of the doubleEMA meaning we calculate the EMA of EMA of EMA. Hence the name Triple Exponential Moving Average. In code line C, we subtract doubleEMA from EMA and add tripleEMA to it. I have made the TEMA formula bold in the above code. Did you read the post how a GBPUSD swing trade made 2,000 pips in 10 days? You can see the TEMA indicator that I have coded above applied to chart below!

TEMA Indicator

So you can see a Triple Exponential Moving Average is not exactly a moving average. In the above screenshot, you can see the red line which is the TEMA. TEMA is following the price pretty closely. In the above chart, red line is almost in the middle of most candles. But did you observe one thing? TEMA may not be a good support and resistance indicator. Why? This is what we have done. We have adjusted an EMA by subtracting doubleEMA and adding tripleEMA so that it moves with the price without any lag. Open your MT4.You can copy the above code into MetaQuotes Language Editor. Click on New in MetaEditor. A Wizard will open select custom indicator and click Next. Choose name of your custom indicator TEMA. Click Next and then Finish. Delete the template code in the new TEMA file. Copy the above code and paste it in that file. Click compile and you are all set to check TEMA on your chart. As said above, TEMA is already available on MT5. You if you are trading on MT5, you can use default TEMA that is available in the list of indicators. Did you check our Million Dollar Trading Challenge?

TEMA Bollinger Bands

Important question is how to use TEMA in your trading strategy. I have coded a TEMA Bollinger Bands indicators. Bollingers Bands are popular with many traders. It plots price standard deviation against simple moving average which is usually 20 SMA. In the code below I have replaced the SMA with TEMA by using the icustom function that calls the above TEMA code that I have provided above.

//+------------------------------------------------------------------+
//|                                                      BB_TEMA.mq4 |
//|                                                     Ahmad Hassam |
//|                                      https://www.doubledoji.com/ |
//+------------------------------------------------------------------+
#property copyright "Ahmad Hassam"
#property link      "https://www.doubledoji.com/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 DeepSkyBlue
#property indicator_color3 DeepSkyBlue

// External parameters
//choose the number of bars to use
input int BandsPeriod = 21; //Smoothing Period
input int BandsShift = 0;   //Shift
//---choose which averaging method to use
input ENUM_MA_METHOD BandsMethod = MODE_EMA;  //Smoothing Methods
//---choose which price to use
input ENUM_APPLIED_PRICE priceBB = PRICE_CLOSE; //Applied Price

//---choose the number of standard deviations to use
input int Deviations = 1; // No Of Standard Deviations

//---- buffers
double TEMA[];
double upperBand[];
double lowerBand[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,TEMA);
SetIndexLabel(0,"EMA");
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,upperBand);
SetIndexLabel(1,"UpperBand");
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,lowerBand);
SetIndexLabel(2,"LowerBand");
//----
return(0);
}

//+------------------------------------------------------------------+
//| Custom Bollinger Bands Indicator                              |
//+------------------------------------------------------------------+

// start() function
   int start()
  {
  int Count=0;
  int limit=0;
  int counted_bars=IndicatorCounted();
  int CalculateBars = Bars - counted_bars;
  double StdDev;

/* 
   Calculates the specified custom indicator and returns its value.

   double  iCustom(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   string       name,             // path/name of the custom indicator compiled program
   ...                            // custom indicator input parameters (if necessary)
   int          mode,             // line index
   int          shift             // shift
   );
   
   Calculate the standard deviation 
    
   double  iStdDev(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // MA averaging method
   int          applied_price,    // applied price
   int          shift             // shift
   );
*/
// NULL means current chart symbol
// 0 means current chart timeframe
if (counted_bars==0)
      {
      limit=Bars-1;
      
      }
   if (counted_bars>0)
      {
      limit=CalculateBars-1;
      
      }
      
      ArrayResize(TEMA, Bars);
      ArraySetAsSeries(TEMA, true);
  

for(Count = limit; Count >= 0; Count--)
{
         TEMA[Count] = iCustom(NULL,0,"TEMA",BandsPeriod,BandsShift,BandsMethod,priceBB,0,Count); //A
         StdDev = iStdDev(NULL,0,BandsPeriod,BandsShift,BandsMethod,priceBB,Count);               //B
         upperBand[Count] = TEMA[Count] + (StdDev * Deviations);                                  //C
         lowerBand[Count] = TEMA[Count] - (StdDev * Deviations);                                  //D
         
}
//----
   return(0);
  }
//+------------------------------------------------------------------+

Above I have given you the MQL4 code for TEMA Bollinger Bands. In the above code I call the TEMA indicator using icustom function in line A. In line B, we use the iStdDev function to calculate the standard deviation of price. In C I add the StdDev value to TEMA and in D I subtract StdDev value from TEMA. Below is the screenshot of TEMA Bollinger Bands with 1 standard deviation. Did you check our Million Dollar Trading Challenge II?

TEMA Indicator

You can see in the above screenshot, red TEMA and the blue standard deviation lines. You can see that price is always within 1 standard deviation from TEMA. In the above chart, price is within the 1 standard deviation always. In the screenshot below, I show the standard Bollinger Bands. The standard Bollinger Bands with 2 standard deviations.

BB Indicator

You can compare TEMA Bollinger Bands with the standard Bollinger Bands. You can easily see, TEMA Bollinger Bands contain price within 1 standard deviation while the Standard Bollinger Bands contain price with 2 standard deviations. Did you watch the documentary on the day in the life of a million dollar trader? As I have said above, simple moving averages and the exponential moving averages can be used as dynamic support and resistance lines. TEMA lacks this property. It cannot be used for support and resistance. So how do we use it in a trading strategy?