//+------------------------------------------------------------------+
//|                                               Heiken Ashi Ma.mq4 |
//+------------------------------------------------------------------+
//|                                                      mod by Raff |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  clrRed
#property indicator_color2  clrGreen
#property indicator_color3  clrRed
#property indicator_color4  clrGreen
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  2
#property indicator_width4  2
#property strict

//
//
//

input ENUM_MA_METHOD     MaMetod                = 1;                // Ma method
input int                MaPeriod               = 30;               // Ma period
input bool               alertsOn               = false;            // Turn alerts on?
input bool               alertsOnCurrent        = false;            // Alerts on still opened bar?
input bool               alertsMessage          = true;             // Alerts should display message?
input bool               alertsSound            = false;            // Alerts should play a sound?
input bool               alertsNotify           = false;            // Alerts should send a notification?
input bool               alertsEmail            = false;            // Alerts should send an email?
input string             soundFile              = "alert2.wav";     // Sound file

double Buffer1[],Buffer2[],Buffer3[],Buffer4[],valw[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|

int OnInit()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0, Buffer1,INDICATOR_DATA); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1, Buffer2,INDICATOR_DATA); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2, Buffer3,INDICATOR_DATA); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3, Buffer4,INDICATOR_DATA); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4, valw);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason) { }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int  OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1;
   
   //
   //
   //
   
   for (; i>=0 && !_StopFlag; i--)
   {
      double maOpen  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_OPEN, i);
      double maClose = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_CLOSE,i);
      double maLow   = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_LOW,  i);
      double maHigh  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_HIGH, i);

      double haOpen  = (i<rates_total-1) ? (Buffer3[i+1] + Buffer4[i+1])*0.5 : maOpen;
      double haClose = (maOpen+maHigh+maLow+maClose)*0.25;
      double haHigh  = fmax(maHigh,fmax(haOpen, haClose));
      double haLow   = fmin(maOpen,fmin(haOpen, haClose));
      
      if (haOpen<haClose) { Buffer1[i] = haLow;  Buffer2[i] = haHigh; } 
      else                { Buffer1[i] = haHigh; Buffer2[i] = haLow;  } 
                            Buffer3[i] = haOpen;
                            Buffer4[i] = haClose;
      valw[i]  = (i<rates_total-1) ? (Buffer3[i]<Buffer4[i]) ? 1 : (Buffer3[i]>Buffer4[i]) ? -1 : valw[i+1] : 0;
  }
  if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
      if (valw[whichBar] != valw[whichBar+1])
      if (valw[whichBar] == 1)
            doAlert(" up ");
      else  doAlert(" down ");       
   }       
return(rates_total);
}

//
//
//

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 = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" Heiken Ashi kuskus "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsNotify)  SendNotification(message);
          if (alertsEmail)   SendMail(_Symbol+" Heiken Ashi kuskus ",message);
          if (alertsSound)   PlaySound(soundFile);
       
   }
}

//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

