Creating an upper and lower band on an array

 

Hi. I am trying to replicate the TTI ATR Extreme indicator from a book by Kirk Northington.

The code first produce an indicator called ATREx, which is then smooth using a 3-period SMA. This new smoothed indicator is called ATREx_Smooth.

After that, it creates an upper and lower band which is one 200-period standard deviation from a 40-period EMA of the ATREx.

Somehow, I think there is a mistake in the code as it is not showing what I expect it to show. Any experts here can comment?

//+------------------------------------------------------------------+

//|                                          TTI_TCW_ATR_Extreme.mq4 |

//|                                                           chweet |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "chweet"

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property indicator_separate_window

#property indicator_buffers 1

#property indicator_buffers 2

#property indicator_color2 White

#property indicator_buffers 3

#property indicator_color3 Red

#property indicator_buffers 4

#property indicator_color4 Orange


double ATREx_200[], ATREx[], ATREx_Smooth[], ATREx_Upper[], ATREx_Lower[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ATREx);

   SetIndexBuffer(1,ATREx_Upper);

   SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,2);

   SetIndexBuffer(2,ATREx_Lower);

   SetIndexStyle (2,DRAW_LINE,STYLE_SOLID,2);

   SetIndexBuffer(3,ATREx_Smooth);

   SetIndexStyle (3,DRAW_LINE,STYLE_SOLID,2);


//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   int i, j, Counted_bars;

   double Moving_Average_Close, Moving_Average_Low, Moving_Average_High, ATR;

   

   Counted_bars=IndicatorCounted();

   i=Bars-Counted_bars-1;

   

   while(i>0)

      {

           Moving_Average_Close=iMA(NULL,0,40,0,MODE_EMA,PRICE_CLOSE,i);

           Moving_Average_Low=iMA(NULL,0,40,0,MODE_EMA,PRICE_LOW,i);

           Moving_Average_High=iMA(NULL,0,40,0,MODE_EMA,PRICE_HIGH,i);

           ATR=iATR(NULL,0,14,i);

                              

           if(close[i]<Moving_Average_Close)

           {

             ATREx[i]=(((low[i]-Moving_Average_Low)/close[i])*100)*((ATR/close[i])*100);

           }

           else

             ATREx[i]=(((high[i]-Moving_Average_High)/close[i])*100)*((ATR/close[i])*100);

          i--;

       }

       

    j=Bars-Counted_bars-1; 

    

    while (j>0)

       {                    

           ATREx_Smooth[j]=iMAOnArray(ATREx,0,3,0,MODE_SMA,j);

           ATREx_Upper[j]=iMAOnArray(ATREx,0,40,0,MODE_EMA,j)+iStdDevOnArray(ATREx,0,200,0,MODE_EMA,j);

           ATREx_Lower[j]=iMAOnArray(ATREx,0,40,0,MODE_EMA,j)-iStdDevOnArray(ATREx,0,200,0,MODE_EMA,j);

           j--;

            

       }

         

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions