Smoothing ATR. HELP!?

 

Hello,

 

I have been trying to smooth ATR like i did applying the EMA 128 on the ATR 480[1st indicator window]. Here is my code for the ATR indicator on the last window[2nd indicator window].

They should have the same results. What am i doing wrong?

Thank You in advance

 

 

 

//+------------------------------------------------------------------+
//|                      Copyright ɠ2005, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

// This indicator has been modified to the ATR based on the so-called smoothed-average method instead of the simple-moving average
// See: https://forum.mql4.com/38874

#property copyright "Copyright ɠ2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=480;
extern int MAPeriod=128;
//---- buffers
double AtrBuffer[];
double TempBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 1 additional buffer used for counting.
   IndicatorBuffers(2);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AtrBuffer);
   SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="ATR("+AtrPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,AtrPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
//----
   if(Bars<=AtrPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double high=High[i];
      double low =Low[i];
      if(i==Bars-1) TempBuffer[i]=high-low;
      else
        {
         double prevclose=Close[i+1];
         TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);  // this is the original true-range formula for the MQL4 ATR indicator
         
        }
      i--;
     }
//----
   if(counted_bars>0) counted_bars--;

   int limit=Bars-counted_bars;
   
   for(i=1; i<limit; i++)
       AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,MAPeriod,0,MODE_EMA,i);  // this is the smoothed average


   return(0);
  }
//+------------------------------------------------------------------+

  

 
investguy:

Hello,

 

I have been trying to smooth ATR like i did applying the EMA 128 on the ATR 480[1st indicator window]. Here is my code for the ATR indicator on the last window[2nd indicator window].

They should have the same results. What am i doing wrong?

Your TempBuffer is not ATR(480)  it is ATR(1)  . . .  I think,  Technical Indicators aren't my thing.
 
RaptorUK: Your TempBuffer is not ATR(480)  it is ATR(1)
  1. Exactly.
    ATR(1)
    TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
    Try
    TempBuffer[i]= iATR(NULL,0, AtrPeriod, i);

  2. // this is the original true-range formula for the MQL4 ATR indicator
    Also remember the Average in ATR is SMA in mql4. The original formula was EMA.
 
Thank You. That answered my questions. 
Reason: