//+------------------------------------------------------------------+
//|                                    smHeikinAshi-Simulator_v5.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011.11.22, SwingMan"
//#property link      "http://www.ross-trading.de/"

/*
Source: RT-MovingAverage Colored2.mq4
*/
string indicatorName="smHeikinAshi-Simulator_v4";
/*+------------------------------------------------------------------+
2010.11.29  -v2
2011.11.07  -v3 -
2011.11.21  -v4 - Draw Bands
2012.02.03  -v5 - Draw 2 Bands
//+-----------------------------------------------------------------*/

#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 LimeGreen //DodgerBlue
#property indicator_color2 Magenta
#property indicator_color3 LimeGreen
#property indicator_color4 Magenta
#property indicator_color5 Chocolate //Brown    //-- Bands
#property indicator_color6 Chocolate //Brown
#property indicator_color7 Chocolate //Brown    //-- Bands
#property indicator_color8 Chocolate //Brown

#property indicator_width1 1 //--lines
#property indicator_width2 1
#property indicator_width3 0 //-- arrows
#property indicator_width4 0
#property indicator_width5 1 //-- bands
#property indicator_width6 1
#property indicator_width7 1 //-- bands
#property indicator_width8 1

#property indicator_style7 STYLE_DOT
#property indicator_style8 STYLE_DOT

//---- input parameters 
//+------------------------------------------------------------------+
//extern int MaMetod2 =3;
//extern int MaPeriod2=2;
//+------------------------------------------------------------------+
extern int period = 55;
int method = MODE_LWMA;                        
int price  = PRICE_TYPICAL;
//--
extern string ____AverageTrueRange = "-----------------------------------------";
extern bool Draw_Bands = true;
extern double ATR_BandsFactor   = 2.0;
extern double ATR_Bands2Factor  = 4.0;
//--
int ATR_Period = 34;
int ATR_SmoothPeriod = 100;
//+------------------------------------------------------------------+
       
//---- buffers 
double Uptrend[], Dntrend[];
double Uptrend2[], Dntrend2[];
double ExtMapBuffer[];
double BandUP[],BandDN[];
double BandUP2[],BandDN2[];

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init()
{
int iArrow=119;
//   IndicatorBuffers(7);

   SetIndexBuffer(0, Uptrend); SetIndexStyle(0,DRAW_LINE); SetIndexLabel(0,NULL);
   SetIndexBuffer(1, Dntrend); SetIndexStyle(1,DRAW_LINE); SetIndexLabel(1,NULL);
   SetIndexBuffer(2, Uptrend2); SetIndexStyle(2,DRAW_ARROW); SetIndexLabel(2,"HeikinAvg-Up");
   SetIndexBuffer(3, Dntrend2); SetIndexStyle(3,DRAW_ARROW); SetIndexLabel(3,"HeikinAvg-Dn");   
   SetIndexBuffer(4, BandUP); SetIndexStyle(4,DRAW_LINE); SetIndexLabel(4,"Band UP");
   SetIndexBuffer(5, BandDN); SetIndexStyle(5,DRAW_LINE); SetIndexLabel(5,"Band DN");   
   SetIndexBuffer(6, BandUP2); SetIndexStyle(6,DRAW_LINE); SetIndexLabel(6,"Band2 UP");
   SetIndexBuffer(7, BandDN2); SetIndexStyle(7,DRAW_LINE); SetIndexLabel(7,"Band2 DN");   
   
   SetIndexArrow(2,iArrow);
   SetIndexArrow(3,iArrow);

   //SetIndexBuffer(6, ExtMapBuffer);   
   ArraySetAsSeries(ExtMapBuffer,true);
   
//----
   IndicatorShortName(indicatorName);
   return(0);
}
//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit()
{ 
   //Comment("");
   return(0);
} 

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start()
{
   ArrayResize(ExtMapBuffer,Bars);
   
   int counted_bars=IndicatorCounted();
   if(counted_bars < 0) return(-1);
   int x=0;
   //int p=MathSqrt(period);
   int e=Bars - counted_bars + period + 1;
   if(e > Bars) e=Bars;
//----
   double trend[];
   ArrayResize(trend, e);
   ArraySetAsSeries(trend, true);


   for(x=e-period; x>=0; x--)
   {
      ExtMapBuffer[x]=iMA(Symbol(),Period(), period, 0, method,price,x);
      double dATR= Get_ATR(Period(), x);
      if (Draw_Bands)
      {
         BandUP[x]= ExtMapBuffer[x] + dATR*ATR_BandsFactor; 
         BandDN[x]= ExtMapBuffer[x] - dATR*ATR_BandsFactor;
         BandUP2[x]= ExtMapBuffer[x] + dATR*ATR_Bands2Factor; 
         BandDN2[x]= ExtMapBuffer[x] - dATR*ATR_Bands2Factor;
      }
   }

   for(x=e-period; x>=0; x--)
   {
      trend[x]=trend[x+1];
      
      if (ExtMapBuffer[x]>= ExtMapBuffer[x+1]) trend[x] =1;
      if (ExtMapBuffer[x]<  ExtMapBuffer[x+1]) trend[x] =-1;
      
      if (trend[x]>0)
      { 
         Uptrend[x]=ExtMapBuffer[x];
         if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
         Dntrend[x]=EMPTY_VALUE;
         Uptrend2[x]=ExtMapBuffer[x];
         Dntrend2[x]=EMPTY_VALUE;
      }
      else
      if (trend[x]<0)
      {
         Dntrend[x]=ExtMapBuffer[x];
         if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
         Uptrend[x]=EMPTY_VALUE;
         Uptrend2[x]=EMPTY_VALUE;
         Dntrend2[x]=ExtMapBuffer[x];
      }
   }
   return(0);
}
//+------------------------------------------------------------------+ 

//___________________________________________________________________                     
double Get_ATR(int iTimeFrame, int iBar)
{
double sumATR=0;
double nValues=0;
   for (int i=0; i<ATR_SmoothPeriod; i++)
   {
      double dATR= iATR(Symbol(),iTimeFrame,ATR_Period,iBar+i);
      if (dATR != 0)
      {
         nValues=nValues+1;
         sumATR= sumATR+dATR;
      }
   }
   if (nValues!=0)
      dATR= sumATR / nValues;
   return(dATR);
}

