//+------------------------------------------------------------------+
//|                                         Heiken Ashi Smoothed.mq4 |
//|                                                                  |
//|                                                      mod by Raff |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com // alerts by http://ForexBaron.net "
#property link      "http://www.forex-tsd.com/"
//----
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 RoyalBlue
#property indicator_color3 Pink
#property indicator_color4 Blue
//---- parameters
extern ENUM_MA_METHOD MaMethod = MODE_SMMA;
extern int MaPeriod=6;
extern ENUM_MA_METHOD MaMethod2 = MODE_LWMA;
extern int MaPeriod2=2;

extern string ahi="******* ALERT SETTINGS:";
extern int    AlertCandle=1;
extern bool   PopupAlerts            = False;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFileLong          = "alert.wav";
extern string SoundFileShort         = "alert2.wav";
int lastDir=3;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
//----
int ExtCountedBars=0;

int           Chart_Scale,j,nn,Bar_Width;//,counted_bars,limit; 
bool          Deinitialized;
bool          Alert_Allowed;
static bool   allow = true;
static bool   disallow = false;    
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
  
  /////////////////////////////

 Deinitialized = false;   
  
  //Determine the current chart scale (chart scale number should be 0-5)
  Chart_Scale = ChartScaleGet();
  
  //Set bar widths             
        if(Chart_Scale == 0) {Bar_Width = 1;}
  else {if(Chart_Scale == 1) {Bar_Width = 1;}      
  else {if(Chart_Scale == 2) {Bar_Width = 2;}
  else {if(Chart_Scale == 3) {Bar_Width = 3;}
  else {if(Chart_Scale == 4) {Bar_Width = 6;}
  else {Bar_Width = 13;} }}}}

/////////////////////////////
//---- indicators
   IndicatorBuffers(8);
   SetIndexStyle(0,DRAW_NONE, 0, 1, Red);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_NONE, 0, 1, RoyalBlue);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, Bar_Width, Pink);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, Bar_Width, Blue);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0,5);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexBuffer(7,ExtMapBuffer8);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haHigh, haLow, haClose;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0)
     {
      maOpen=iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_OPEN,pos);
      maClose=iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_CLOSE,pos);
      maLow=iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_LOW,pos);
      maHigh=iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_HIGH,pos);
//----
      haOpen=(ExtMapBuffer5[pos+1]+ExtMapBuffer6[pos+1])/2;
      haClose=(maOpen+maHigh+maLow+maClose)/4;
      haHigh=MathMax(maHigh, MathMax(haOpen, haClose));
      haLow=MathMin(maLow, MathMin(haOpen, haClose));
      if (haOpen<haClose)
        {
         ExtMapBuffer7[pos]=haLow;
         ExtMapBuffer8[pos]=haHigh;
        }
      else
        {
         ExtMapBuffer7[pos]=haHigh;
         ExtMapBuffer8[pos]=haLow;
        }
      ExtMapBuffer5[pos]=haOpen;
      ExtMapBuffer6[pos]=haClose;
      pos--;
     }
   int i;
   for(i=0; i<Bars; i++) ExtMapBuffer1[i]=iMAOnArray(ExtMapBuffer7,Bars,MaPeriod2,0,MaMethod2,i);
   for(i=0; i<Bars; i++) ExtMapBuffer2[i]=iMAOnArray(ExtMapBuffer8,Bars,MaPeriod2,0,MaMethod2,i);
   for(i=0; i<Bars; i++) ExtMapBuffer3[i]=iMAOnArray(ExtMapBuffer5,Bars,MaPeriod2,0,MaMethod2,i);
   for(i=0; i<Bars; i++) ExtMapBuffer4[i]=iMAOnArray(ExtMapBuffer6,Bars,MaPeriod2,0,MaMethod2,i);
//---- alerts:

 string sDir="none";
 string oldsDir="none";
 if (ExtMapBuffer2[AlertCandle]>ExtMapBuffer1[AlertCandle] && ExtMapBuffer4[AlertCandle]>ExtMapBuffer3[AlertCandle]) {sDir="LONG";}//LONG
 if (ExtMapBuffer2[AlertCandle]<ExtMapBuffer1[AlertCandle] && ExtMapBuffer4[AlertCandle]<ExtMapBuffer3[AlertCandle]) {sDir="SHORT";}//SHORT
 
 if (ExtMapBuffer2[AlertCandle+1]>ExtMapBuffer1[AlertCandle+1] && ExtMapBuffer4[AlertCandle+1]>ExtMapBuffer3[AlertCandle+1]) {oldsDir="LONG";}//LONG
 if (ExtMapBuffer2[AlertCandle+1]<ExtMapBuffer1[AlertCandle+1] && ExtMapBuffer4[AlertCandle+1]<ExtMapBuffer3[AlertCandle+1]) {oldsDir="SHORT";}//SHORT

 if (sDir=="LONG" && oldsDir=="SHORT" && lastDir!=1) {lastDir=1; doAlerts("CandleDir changed to LONG",SoundFileLong);}
 if (sDir=="SHORT" && oldsDir=="LONG" && lastDir!=2) {lastDir=2; doAlerts("CandleDir changed to SHORT",SoundFileShort);}
//---
   return(0);
  }
//+------------------------------------------------------------------+


void doAlerts(string msg,string SoundFile) {
 static datetime lastAlert=0;
 
 if (lastAlert!=iTime(NULL,0,0)) {
  lastAlert=iTime(NULL,0,0);
        msg="Heiken Ashi Smoothed Alert on "+Symbol()+", period "+TFtoStr(Period())+": "+msg;
 string emailsubject="MT4 alert on acc. "+AccountNumber()+", "+WindowExpertName()+" - Alert on "+Symbol()+", period "+TFtoStr(Period());
  if (PopupAlerts) Alert(msg);
  if (EmailAlerts) SendMail(emailsubject,msg);
  if (PushNotificationAlerts) SendNotification(msg);
  if (SoundAlerts) PlaySound(SoundFile);
 }
}//void doAlerts(string msg,string SoundFile) {

string TFtoStr(int period) {
 switch(period) {
  case 1     : return("M1");  break;
  case 5     : return("M5");  break;
  case 15    : return("M15"); break;
  case 30    : return("M30"); break;
  case 60    : return("H1");  break;
  case 240   : return("H4");  break;
  case 1440  : return("D1");  break;
  case 10080 : return("W1");  break;
  case 43200 : return("MN1"); break;
  default    : return(DoubleToStr(period,0));
 }
 return("UNKNOWN");
}//string TFtoStr(int period) {
//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Set up to get the chart scale number                                         |
//+-------------------------------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)                                                    
  {
  Chart_Scale = ChartScaleGet();
  if(Alert_Allowed == allow)
    {
    init();
    Alert_Allowed = allow;
    }
  else
    {
    init();
    Alert_Allowed = disallow;
    }          
  }

//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Get the chart scale number                                                   |
//+-------------------------------------------------------------------------------------------+
int ChartScaleGet()
  {
  long result = -1;
  ChartGetInteger(0,CHART_SCALE,0,result);
  return((int)result);
  }
    
//+-------------------------------------------------------------------------------------------+
//|Custom indicator end                                                                       |
//+-------------------------------------------------------------------------------
