//+------------------------------------------------------------------+
//|                                            Heiken Ashi Ma T3.mq4 |
//+------------------------------------------------------------------+
//|                                                      mod by Raff |
//|                                               2009 mod by mladen |
//|  tools copied and pasted alerts from another HA indy so blame me |
//+------------------------------------------------------------------+
//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 Red
#property indicator_color2 RoyalBlue
#property indicator_color3 Red
#property indicator_color4 RoyalBlue
#property indicator_color5 Magenta
#property indicator_color6 Aqua
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 1
#property indicator_width6 1

//
//
//
//
//

extern int    MaPeriod        = 5;
extern int    MaMetod         = 2;
extern int    Step            = 1;
extern bool   BetterFormula   = true;
extern bool   T3Average       = true;
extern double T3Hot           = 1.00;
extern bool   T3Original      = false;

extern bool   ShowArrows      = false;
extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double UpArrow[];
double DnArrow[];
double trend[];

//
//
//
//
//

double emas[][24];
double alpha;
double c1;
double c2;
double c3;
double c4;

//+------------------------------------------------------------------+
//|                                                                  |
//|------------------------------------------------------------------|
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
   SetIndexBuffer(0, Buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1, Buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2, Buffer3); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3, Buffer4); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4, UpArrow);
   SetIndexBuffer(5, DnArrow);
   SetIndexBuffer(6, trend);
   
   if (ShowArrows)
   {
   SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(4,233);
   SetIndexStyle(5,DRAW_ARROW); SetIndexArrow(5,234);
   }
   else
   {
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   }
   
   double a  = T3Hot;
             c1 = -a*a*a;
             c2 =  3*(a*a+a*a*a);
             c3 = -3*(2*a*a+a+a*a*a);
             c4 = 1+3*a+a*a*a+3*a*a;

      MaPeriod = MathMax(1,MaPeriod);
      if (T3Original)
           alpha = 2.0/(1.0 + MaPeriod);
      else alpha = 2.0/(2.0 + (MaPeriod-1.0)/2.0);

  
   //
   //
   //
   //
   //

   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    pointModifier;
   int    pos,limit;

   //
   //
   //
   //
   //
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (ArrayRange(emas,0) != Bars) ArrayResize(emas,Bars);
         if (Digits==3 || Digits==5)
               pointModifier = 10;
         else  pointModifier = 1;               
          
   //
   //
   //
   //
   //
   
   for(pos=limit; pos >= 0; pos--)
   {
      if (T3Average)
         {
            double maOpen  = iT3(Open[pos] ,pos, 0);
            double maClose = iT3(Close[pos],pos, 6);
            double maLow   = iT3(Low[pos]  ,pos,12);
            double maHigh  = iT3(High[pos] ,pos,18);
         }
      else
         {
            maOpen  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_OPEN ,pos);
            maClose = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_CLOSE,pos);
            maLow   = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_LOW  ,pos);
            maHigh  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_HIGH ,pos);
         }
   
      //
      //
      //
      //
      //
        
         if (BetterFormula) {
               if (maHigh!=maLow)
                     double haClose  = (maOpen+maClose)/2+(((maClose-maOpen)/(maHigh-maLow))*MathAbs((maClose-maOpen)/2));
                     else  haClose   = (maOpen+maClose)/2; }
               else        haClose   = (maOpen+maHigh+maLow+maClose)/4;
                     double haOpen   = (Buffer3[pos+1]+Buffer4[pos+1])/2;
                     double haHigh   = MathMax(maHigh, MathMax(haOpen,haClose));
                     double haLow    = MathMin(maLow,  MathMin(haOpen,haClose));

         if (haOpen<haClose) { Buffer1[pos]=haLow;  Buffer2[pos]=haHigh; } 
         else                { Buffer1[pos]=haHigh; Buffer2[pos]=haLow;  } 
                               Buffer3[pos]=haOpen;
                               Buffer4[pos]=haClose;
         
         //
         //
         //
         //
         //
            
              trend[pos] = trend[pos+1];
              if (Buffer3[pos] < Buffer4[pos]) trend[pos] =  1;
              if (Buffer3[pos] > Buffer4[pos]) trend[pos] = -1;
      
         //
         //
         //
         //
         //

         if (Step>0)
         {
            if( MathAbs(Buffer1[pos]-Buffer1[pos+1]) < Step*pointModifier*Point ) Buffer1[pos]=Buffer1[pos+1];
            if( MathAbs(Buffer2[pos]-Buffer2[pos+1]) < Step*pointModifier*Point ) Buffer2[pos]=Buffer2[pos+1];
            if( MathAbs(Buffer3[pos]-Buffer3[pos+1]) < Step*pointModifier*Point ) Buffer3[pos]=Buffer3[pos+1];
            if( MathAbs(Buffer4[pos]-Buffer4[pos+1]) < Step*pointModifier*Point ) Buffer4[pos]=Buffer4[pos+1];
         }         
         if (ShowArrows)
         {
               UpArrow[pos] = EMPTY_VALUE; 
               DnArrow[pos] = EMPTY_VALUE;
               if (trend[pos]!=trend[pos+1])
               {
                  if (trend[pos]== 1) UpArrow[pos] = Low[pos]  - iATR(NULL,0,20,pos)/2.0;
                  if (trend[pos]==-1) DnArrow[pos] = High[pos] + iATR(NULL,0,20,pos)/2.0;
               }                  
         }
      }
      manageAlerts();
    return(0);
   }
      
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double iT3(double price,int shift,int buffer)
{
   int i = Bars-shift-1;
   if (i < 1)
      {
         emas[i][0+buffer] = price;
         emas[i][1+buffer] = price;
         emas[i][2+buffer] = price;
         emas[i][3+buffer] = price;
         emas[i][4+buffer] = price;
         emas[i][5+buffer] = price;
      }
   else
      {
         emas[i][0+buffer] = emas[i-1][0+buffer]+alpha*(price            -emas[i-1][0+buffer]);
         emas[i][1+buffer] = emas[i-1][1+buffer]+alpha*(emas[i][0+buffer]-emas[i-1][1+buffer]);
         emas[i][2+buffer] = emas[i-1][2+buffer]+alpha*(emas[i][1+buffer]-emas[i-1][2+buffer]);
         emas[i][3+buffer] = emas[i-1][3+buffer]+alpha*(emas[i][2+buffer]-emas[i-1][3+buffer]);
         emas[i][4+buffer] = emas[i-1][4+buffer]+alpha*(emas[i][3+buffer]-emas[i-1][4+buffer]);
         emas[i][5+buffer] = emas[i-1][5+buffer]+alpha*(emas[i][4+buffer]-emas[i-1][5+buffer]);
      }
   return(c1*emas[i][5+buffer] + c2*emas[i][4+buffer] + c3*emas[i][3+buffer] + c4*emas[i][2+buffer]);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,0,whichBar));
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(whichBar,"up");
         if (trend[whichBar] ==-1) doAlert(whichBar,"down");
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," HAMA trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"HAMA "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}