#property copyright "TRENDPOWER"


#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 C'100,130,240';
#property indicator_color2 C'247,0,0';
#property indicator_color3 C'20,97,255';
#property indicator_color4 C'242,100,120';
#property indicator_width1 8
#property indicator_width2 8
#property indicator_width3 8
#property indicator_width4 8
#property indicator_minimum 0
#property indicator_maximum 2

//
//
//
//
//

extern int    MaPeriod        =  2;
extern int    MaMetod         =  2;
extern int    Step            =  10;
extern bool   BetterFormula   = true;
extern bool   jurik           = true;
extern int    Length          =  2;
extern double Phase           =  0;
extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = false;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;
//
//
//
//
//

double histUp1[];
double histDn1[];
double histUp2[];
double histDn2[];
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double trend[][2];

//
//
//
//
//


//+------------------------------------------------------------------+
//|                                                                  |
//|------------------------------------------------------------------|
//
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0, histUp1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1, histDn1); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2, histUp2); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3, histDn2); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4, ExtMapBuffer1);
   SetIndexBuffer(5, ExtMapBuffer2);
   SetIndexBuffer(6, ExtMapBuffer3);
   SetIndexBuffer(7, ExtMapBuffer4);
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haClose, haLow, haHigh;
   int    counted_bars=IndicatorCounted();
   int    pointModifier;
   int    i,r,limit;

   //
   //
   //
   //
   //
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);
         if (Digits==3 || Digits==5)
               pointModifier = 10;
         else  pointModifier = 1;               
          
   //
   //
   //
   //
   //
   
   for(i=limit,r=Bars-i-1; i >= 0; i--,r++)
   {
      if (jurik)
         {
            maOpen  = iSmooth(Open[i], Length,Phase,i, 0);
            maClose = iSmooth(Close[i],Length,Phase,i,10);
            maLow   = iSmooth(Low[i],  Length,Phase,i,20);
            maHigh  = iSmooth(High[i], Length,Phase,i,30);
         }
      else
         {
            maOpen  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_OPEN ,i);
            maClose = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_CLOSE,i);
            maLow   = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_LOW  ,i);
            maHigh  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_HIGH ,i);
         }
   
      //
      //
      //
      //
      //
        
         if (BetterFormula) {
               if (maHigh!=maLow)
                     haClose = (maOpen+maClose)/2+(((maClose-maOpen)/(maHigh-maLow))*MathAbs((maClose-maOpen)/2));
               else  haClose = (maOpen+maClose)/2; }
         else        haClose = (maOpen+maHigh+maLow+maClose)/4;
                     haOpen   = (ExtMapBuffer3[i+1]+ExtMapBuffer4[i+1])/2;
                     haHigh   = MathMax(maHigh, MathMax(haOpen,haClose));
                     haLow    = MathMin(maOpen,  MathMin(haOpen,haClose));

         if (haOpen<haClose) { ExtMapBuffer1[i]=haLow;  ExtMapBuffer2[i]=haHigh; } 
         else                { ExtMapBuffer1[i]=haHigh; ExtMapBuffer2[i]=haLow;  } 
                               ExtMapBuffer3[i]=haOpen;
                               ExtMapBuffer4[i]=haClose;
      
      //
      //
      //
      //
      //

      if (Step>0)
      {
         if( MathAbs(ExtMapBuffer1[i]-ExtMapBuffer1[i+1]) < Step*pointModifier*Point ) ExtMapBuffer1[i]=ExtMapBuffer1[i+1];
         if( MathAbs(ExtMapBuffer2[i]-ExtMapBuffer2[i+1]) < Step*pointModifier*Point ) ExtMapBuffer2[i]=ExtMapBuffer2[i+1];
         if( MathAbs(ExtMapBuffer3[i]-ExtMapBuffer3[i+1]) < Step*pointModifier*Point ) ExtMapBuffer3[i]=ExtMapBuffer3[i+1];
         if( MathAbs(ExtMapBuffer4[i]-ExtMapBuffer4[i+1]) < Step*pointModifier*Point ) ExtMapBuffer4[i]=ExtMapBuffer4[i+1];
      }            
      trend[r][0] = trend[r-1][0];
      trend[r][1] = trend[r-1][1];
      if (ExtMapBuffer1[i]>ExtMapBuffer2[i]) trend[r][0] = -1;
      if (ExtMapBuffer1[i]<ExtMapBuffer2[i]) trend[r][0] =  1;
      if (ExtMapBuffer3[i]>ExtMapBuffer4[i]) trend[r][1] = -1;
      if (ExtMapBuffer3[i]<ExtMapBuffer4[i]) trend[r][1] =  1;
      
      //
      //
      //
      //
      //
      
      histUp1[i] = EMPTY_VALUE;
      histDn1[i] = EMPTY_VALUE;
      histUp2[i] = EMPTY_VALUE;
      histDn2[i] = EMPTY_VALUE;
         if (trend[r][1] ==  1) histUp2[i]=1;
         if (trend[r][1] == -1) histDn2[i]=1;
         if (trend[r][0] ==  1) histUp1[i]=2;
         if (trend[r][0] == -1) histDn1[i]=2;
   }  
   
   //
   //
   //
   //
   //
   
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      whichBar = Bars-whichBar-1;
      
      //
      //
      //
      //
      //
         
      if (trend[whichBar][0] != trend[whichBar-1][0])
      if (trend[whichBar][0] == 1)
            doAlert("up");
      else  doAlert("down");       
   }

   //
   //
   //
   //
          
   return(0);
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double wrk[][40];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax+s];
      double del2   = price - wrk[r-1][bsmin+s];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
            if (wrk[r][avolty+s] > 0)
               double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}



void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," heiken ashi jurik smoothed trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," heiken ashi jurik smoothed "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

