//+------------------------------------------------------------------+
//|                                            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 "Copyright © 2008,Forex-TSD.com"
#property link      "http://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 Aqua
#property indicator_color6 Magenta
#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 string DummyString      = "Leave Alone String";
extern int    RelativeTFindex  = 0;  //Index of TF relative to the chart TF.  1 = 1TF higher, 2 = 2TF higher, etc.
extern int    MaPeriod         = 3;
extern int    MaMetod          = 2;
extern int    Step             = 1;
extern bool   BetterFormula    = false;
extern bool   T3Average        = true;
extern double T3Hot            = 0.618;
extern bool   T3Original       = false;
extern bool   ShowArrows       = false;

extern string _                = "Alerts settings";
extern bool   alertsOn         = false;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = false;
extern bool   alertsEmail      = false;

//
//
//
//
//

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double UpArrow[];
double DnArrow[];

//
//
//
//
//

double emas[][24];
double alpha;
double c1;
double c2;
double c3;
double c4;

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;

int periods[] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200};
int currentIndex;
string TimeFrame;
int timeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//|------------------------------------------------------------------|
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,ExtMapBuffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,ExtMapBuffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,ExtMapBuffer3); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,ExtMapBuffer4); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4,UpArrow); 
   SetIndexBuffer(5,DnArrow); 
   
   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);
      
   //
   //
   //
   //
   //
   
   int periodsCount = ArraySize(periods);
   for (int i = 0; i < periodsCount; i++) 
   {
       if (Period() == periods[i]) 
       {
           currentIndex = i;
           break;
       }
   }

   timeFrame = periods[currentIndex + RelativeTFindex];
   
   TimeFrame = timeFrameToString(timeFrame);
      
   indicatorFileName = WindowExpertName();
   calculateValue    = (DummyString=="calculateValue"); if (calculateValue) return(0);
   returnBars        = (DummyString=="returnBars");     if (returnBars)     return(0);
      
   //
   //
   //
   //
   //

   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haClose, haLow, haHigh;
   int    counted_bars=IndicatorCounted();
   int    pointModifier;
   int    i,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 (returnBars)  { ExtMapBuffer1[0] = limit+1; return(0); }
         
         if (Digits==3 || Digits==5)
               pointModifier = 10;
         else  pointModifier = 1;   
   
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {          
          
   //
   //
   //
   //
   //
   
     for(int pos=limit; pos >= 0; pos--)
     {
        if (T3Average)
           {
              maOpen  = iT3(Open[pos] ,pos, 0);
              maClose = iT3(Close[pos],pos, 6);
              maLow   = iT3(Low[pos]  ,pos,12);
              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)
                     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[pos+1]+ExtMapBuffer4[pos+1])/2;
                     haHigh   = MathMax(maHigh, MathMax(haOpen,haClose));
                     haLow    = MathMin(maLow,  MathMin(haOpen,haClose));

           if (haOpen<haClose) { ExtMapBuffer1[pos]=haLow;  ExtMapBuffer2[pos]=haHigh; } 
           else                { ExtMapBuffer1[pos]=haHigh; ExtMapBuffer2[pos]=haLow;  } 
                               ExtMapBuffer3[pos]=haOpen;
                               ExtMapBuffer4[pos]=haClose;
      
        //
        //
        //
        //
        //

        if (Step>0)
        {
           if( MathAbs(ExtMapBuffer1[pos]-ExtMapBuffer1[pos+1]) < Step*pointModifier*Point ) ExtMapBuffer1[pos]=ExtMapBuffer1[pos+1];
           if( MathAbs(ExtMapBuffer2[pos]-ExtMapBuffer2[pos+1]) < Step*pointModifier*Point ) ExtMapBuffer2[pos]=ExtMapBuffer2[pos+1];
           if( MathAbs(ExtMapBuffer3[pos]-ExtMapBuffer3[pos+1]) < Step*pointModifier*Point ) ExtMapBuffer3[pos]=ExtMapBuffer3[pos+1];
           if( MathAbs(ExtMapBuffer4[pos]-ExtMapBuffer4[pos+1]) < Step*pointModifier*Point ) ExtMapBuffer4[pos]=ExtMapBuffer4[pos+1];
        } 
              
        UpArrow[pos] = EMPTY_VALUE;
        DnArrow[pos] = EMPTY_VALUE;
        
        if (ExtMapBuffer3[pos]<ExtMapBuffer4[pos] && ExtMapBuffer4[pos+1]<ExtMapBuffer3[pos+1]) UpArrow[pos]=ExtMapBuffer4[pos+1]-(Ask-Bid);
        if (ExtMapBuffer3[pos]>ExtMapBuffer4[pos] && ExtMapBuffer4[pos+1]>ExtMapBuffer3[pos+1]) DnArrow[pos]=ExtMapBuffer4[pos+1]+(Ask-Bid);
     }
    
   //
   //
   //
   //
   //
         
   manageAlerts();
   return(0);
   }
    
    //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   
   for(pos = limit; pos >= 0; pos--)
   {
   
     int y = iBarShift(NULL,timeFrame,Time[pos]);
  
       ExtMapBuffer1[pos] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RelativeTFindex, MaPeriod,MaMetod,Step,BetterFormula,T3Average,T3Hot,T3Original,0,y);
       ExtMapBuffer2[pos] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RelativeTFindex, MaPeriod,MaMetod,Step,BetterFormula,T3Average,T3Hot,T3Original,1,y);
       ExtMapBuffer3[pos] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RelativeTFindex, MaPeriod,MaMetod,Step,BetterFormula,T3Average,T3Hot,T3Original,2,y);
       ExtMapBuffer4[pos] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RelativeTFindex, MaPeriod,MaMetod,Step,BetterFormula,T3Average,T3Hot,T3Original,3,y);
       UpArrow[pos]       = EMPTY_VALUE;
       DnArrow[pos]       = EMPTY_VALUE;
        
      if (ExtMapBuffer3[pos]<ExtMapBuffer4[pos] && ExtMapBuffer4[pos+1]<ExtMapBuffer3[pos+1]) UpArrow[pos]=ExtMapBuffer4[pos+1]-(Ask-Bid);
      if (ExtMapBuffer3[pos]>ExtMapBuffer4[pos] && ExtMapBuffer4[pos+1]>ExtMapBuffer3[pos+1]) DnArrow[pos]=ExtMapBuffer4[pos+1]+(Ask-Bid);
   }
    
      manageAlerts();
   return(0);
 }

//
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      
      if (UpArrow[whichBar]!= EMPTY_VALUE) doAlert(whichBar,"HAma buy");   
      if (DnArrow[whichBar]!= EMPTY_VALUE) doAlert(whichBar,"HAma sell"); 
      
      }
   }
//
//
//
//
//

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()," ",timeFrameToString(TimeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Heiken Ashi ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Heiken Ashi "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

//
//
//
//
//


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]);
}

//
//
//
//
//


