//+------------------------------------------------------------------+
//| HeikenAshiOscillator.mq4
//| Copyright http://www.pointzero-indicator.com
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  LimeGreen
#property indicator_color2  Red
#property indicator_width1  3
#property indicator_width2  3
#property indicator_minimum 0
#property indicator_maximum 1


//-------------------------------
// External variables
//-------------------------------
//
//

extern string TimeFrame           = "Current time frame";

extern string note                = "turn on Alert = true; turn off = false";
extern bool   alertsOn            = true;
extern bool   alertsOnCurrent     = false;
extern bool   alertsMessage       = true;
extern bool   alertsSound         = true;
extern bool   alertsNotify        = false;
extern bool   alertsEmail         = false;

//-------------------------------
// Buffers
//-------------------------------

double FextMapBuffer1[];
double FextMapBuffer2[];
double FextMapBuffer4[];
double FextMapBuffer5[];
double trend1[];

//
//
//
//
//

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,FextMapBuffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,FextMapBuffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,FextMapBuffer4);  
   SetIndexBuffer(3,FextMapBuffer5);    
   SetIndexBuffer(4,trend1);   
   
      //
      //
      //
      //
      //

         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
         
      //
      //
      //
      //
      //  
   
   IndicatorShortName(timeFrameToString(timeFrame)+  "  Heiken Ashi Oscillator");
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
  
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
   int pos,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { FextMapBuffer1[0] = limit+1; return(0); }
   
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame==Period())
   {     
     for(pos=limit; pos >= 0; pos--)
     {  
       double open  = iCustom(NULL,0,"Heiken Ashi",2,pos);
       double close = iCustom(NULL,0,"Heiken Ashi",3,pos);
      
       FextMapBuffer4[pos] = (open + close) / 2;
       FextMapBuffer5[pos] = FextMapBuffer4[pos] - FextMapBuffer4[pos+1];

       FextMapBuffer1[pos] = EMPTY_VALUE;
       FextMapBuffer2[pos] = EMPTY_VALUE;
       trend1[pos] = trend1[pos+1];
       if(FextMapBuffer5[pos]>0) trend1[pos] = 1;
       if(FextMapBuffer5[pos]<0) trend1[pos] =-1;
       if(trend1[pos] == 1) FextMapBuffer1[pos] = 1;
       if(trend1[pos] ==-1) FextMapBuffer2[pos] = 1;
     }
   
   manageAlerts();
   return(0);
   }
   
   //
   //
   //
   //
   //
  
  limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
  for (pos=limit;pos>=0; pos--)
  {
     int y = iBarShift(NULL,timeFrame,Time[pos]);
        trend1[pos]         = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",3,y);
        FextMapBuffer1[pos] = EMPTY_VALUE;
        FextMapBuffer2[pos] = EMPTY_VALUE;
        if(trend1[pos] == 1) FextMapBuffer1[pos] = 1;
        if(trend1[pos] ==-1) FextMapBuffer2[pos] = 1;
  }
  manageAlerts();
return(0);
}

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

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);
}

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

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trend1[whichBar] != trend1[whichBar+1])
      {
         if (trend1[whichBar] == 1) doAlert(whichBar,"up");
         if (trend1[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()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Heiken Ashi Oscillator ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," Heiken Ashi Oscillator " +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Heiken Ashi Oscillator "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}