//+------------------------------------------------------------------+
//|                                                                  |
//|                            _TRO_nma-laden_History                |
//|                                                                  |
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 

// this updn was posted here: http://www.forex-tsd.com/general-discussion/7123-daily-average-indicator-2.html#post212976

#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Lime    
#property indicator_color2 DarkGreen         
#property indicator_color3 Yellow      
#property indicator_color4 Maroon    
#property indicator_color5 Red        
   

//---- parameters
 
extern string myPair    = "";

extern int  NMAPeriod  = 40;
extern int  NMAPrice   = PRICE_CLOSE;
extern int  TEMAPeriod =  1;
extern bool UseLog     = true;


//---- buffers

   double     ExtBuffer0[];   
   double     ExtBuffer1[];
   double     ExtBuffer2[];
   double     ExtBuffer3[]; 
   double     ExtBuffer4[];   

   int myPeriod = 0 ;
 
//+------------------------------------------------------------------+

double nma[] ;
double prices[];
double momentum[];
double emas[][3];
double alpha , xnma ;


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

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_MN1: tfs="MN"; break;
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
   }
   return(tfs);
} 

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

int init()
{
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
 
   
   SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 4, indicator_color1);
   SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 4, indicator_color2);
   SetIndexStyle(2,DRAW_HISTOGRAM, EMPTY, 4, indicator_color3);
   SetIndexStyle(3,DRAW_HISTOGRAM, EMPTY, 4, indicator_color4);
   SetIndexStyle(4,DRAW_HISTOGRAM, EMPTY, 4, indicator_color5);
 
   
   

if (myPair == "") myPair = Symbol();
   
if( myPeriod == 0) myPeriod = Period() ;    


string tPeriod = TimeFrameToString(myPeriod) ;

IndicatorShortName("TRO_nma-laden "+myPair+"("+tPeriod+")" );
    
   
      TEMAPeriod = MathMax(TEMAPeriod,1);
      NMAPeriod  = MathMax(NMAPeriod ,1);
           alpha = 2.0 /(1.0 + TEMAPeriod);
    
return(0);
}

//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
  {
   double coef = MathSqrt(2)-1;
   int    counted_bars=IndicatorCounted();
   int    i,k,r,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;
            if (ArrayRange(emas,0) != Bars)
            {
               ArrayResize(nma,Bars);
               ArrayResize(emas,Bars);
               ArrayResize(prices,Bars);
               ArrayResize(momentum,Bars);
            }               

   //
   //
   //
   //
   //
               
   for(i=limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      double rawPrice  = iMA(NULL,0,1,0,MODE_SMA,NMAPrice,i);
      double rawPrice1 = iMA(NULL,0,1,0,MODE_SMA,NMAPrice,i+1); // added     
      
      double calPrice = iTema(rawPrice,i);
      
         prices[r] = calPrice;
         if (UseLog && (calPrice>0))
             prices[r] = MathLog(calPrice);
   
         //
         //
         //
         //
         //
                      
         momentum[r] = prices[r]-prices[r-1];
      
            double momFirst = momentum[r]+momentum[r-1]*coef;
            double momRatio = 0.00;
      
                  for (k = 2; k<NMAPeriod; k++) momRatio += momentum[r-k]*(MathSqrt(k+1)-MathSqrt(k));
      
            double sumMomen = 0.00;

                  for (k = 0; k<NMAPeriod; k++) sumMomen += MathAbs(momentum[r-k]);
         
            double ratio = 0.00;

                  if (sumMomen != 0) ratio = MathAbs(momFirst+momRatio)/sumMomen;
      
         //
         //
         //
         //
         //
      xnma   = nma[i+1]+ratio*(rawPrice-nma[i+1]);  
      nma[i] = xnma ;
                 
//      nma[i] = nma[i+1]+ratio*(rawPrice-nma[i+1]);
 

/*
Comment( "rawPrice=", DoubleToStr(rawPrice, Digits), "\n",
         "xnma=", DoubleToStr(xnma, Digits), "\n",
         " nma[1] ", DoubleToStr(nma[1], Digits)  ) ; 
*/

while(true)
{
      
      
  if (rawPrice > xnma && rawPrice1 < nma[i+1] ) { PlotHist(4,0,0,0,0,i) ; break; }
  if (rawPrice > xnma )                         { PlotHist(0,4,0,0,0,i) ; break; }
  if (rawPrice < xnma && rawPrice1 > nma[i+1] ) { PlotHist(0,0,0,0,4,i) ; break; }
  if (rawPrice < xnma  )                        { PlotHist(0,0,0,4,0,i) ; break; }  
                                                    PlotHist(0,0,4,0,0,i) ; break; 

} // while 

} // for




return(0);

} // start

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

double iTema(double price,int shift)
{
   int i = Bars-shift-1;
   if (i < 1)
      {
         emas[i][0] = price;
         emas[i][1] = price;
         emas[i][2] = price;
      }
   else
      {
         emas[i][0] = emas[i-1][0]+alpha*(price     -emas[i-1][0]);
         emas[i][1] = emas[i-1][1]+alpha*(emas[i][0]-emas[i-1][1]);
         emas[i][2] = emas[i-1][2]+alpha*(emas[i][1]-emas[i-1][2]);
      }
   return(3*emas[i][0] - 3*emas[i][1] + emas[i][2]);
}
//+------------------------------------------------------------------+
      
void PlotHist(int a,int b,int c,int d,int e, int bars)
{

ExtBuffer0[bars]=a;    
ExtBuffer1[bars]=b;
ExtBuffer2[bars]=c;
ExtBuffer3[bars]=d;
ExtBuffer4[bars]=e;
 
}

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