//+------------------------------------------------------------------+
//|                                                    OHLC - HA.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Lime
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3


//----
extern color color1 = Red;
extern color color2 = Lime;
extern color color3 = Red;
extern color color4 = Lime;
extern bool  SoundOn = True;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double pointz;
double haOpen, haHigh, haLow, haClose;

//----
int ExtCountedBars=0;
datetime LastAlert;

//|------------------------------------------------------------------|
int init()  {
//|------------------------------------------------------------------|
  pointz = Point; 
  if ((Digits == 5) || (Digits == 3)) pointz = pointz*10;
  
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, color4);
   SetIndexBuffer(3, ExtMapBuffer4);

  SetIndexDrawBegin(0,10);
  SetIndexDrawBegin(1,10);
  SetIndexDrawBegin(2,10);
  SetIndexDrawBegin(3,10);

  SetIndexBuffer(0,ExtMapBuffer1);
  SetIndexBuffer(1,ExtMapBuffer2);
  SetIndexBuffer(2,ExtMapBuffer3);
  SetIndexBuffer(3,ExtMapBuffer4);

  plot_obj();
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  plot_obj();
  return(0);
}

//+------------------------------------------------------------------+
int plot_obj()  {
//+------------------------------------------------------------------+
  if(Bars<=300) return(0);
  ExtCountedBars=IndicatorCounted();
  if (ExtCountedBars<0) return(-1);
  if (ExtCountedBars>0) ExtCountedBars--;
  int pos=MathMax(Bars-ExtCountedBars-1,300);

  while(pos>=0)  {
    haOpen=(haOpen+haClose)/2;
    haClose=(Open[pos]+High[pos]+Low[pos]+Close[pos])/4;
    haHigh=MathMax(High[pos], MathMax(haOpen, haClose));
    haLow=MathMin(Low[pos], MathMin(haOpen, haClose));
    double top = MathMax(Open[pos],Close[pos]);
    double bot = MathMin(Open[pos],Close[pos]);
    if (Open[pos]==Close[pos]) top = top + 0.1 * pointz;  //Handles doji's

    if (haOpen<haClose)  {                                // Green candle
      ExtMapBuffer1[pos]=Low[pos];
      ExtMapBuffer2[pos]=High[pos];
      ExtMapBuffer3[pos]=bot;
      ExtMapBuffer4[pos]=top;
    } else {                                              // Red candle
      ExtMapBuffer1[pos]=High[pos];
      ExtMapBuffer2[pos]=Low[pos];
      ExtMapBuffer3[pos]=top;
      ExtMapBuffer4[pos]=bot;
    } 
 	   pos--;
     }

//---- Sound Alert added by Tater_Tha_Great
  double Stoch_Main = iStochastic(NULL,0,10,3,3,MODE_SMA,1,MODE_MAIN,1);
  double Stoch_Sig = iStochastic(NULL,0,10,3,3,MODE_SMA,1,MODE_SIGNAL,1); 
   if(SoundOn) {
     if(Time[0]>LastAlert) {
       if(ExtMapBuffer3[1]>ExtMapBuffer4[1] && ExtMapBuffer3[2]<=ExtMapBuffer4[2] && Stoch_Main < Stoch_Sig) {
         LastAlert = Time[0];
         Alert("Heiken Ashi Alert (", Symbol(), ", ", Period(), ")--Possible SELL ");
         PlaySound("Alert2.wav");
       }
       else if(ExtMapBuffer3[1]<ExtMapBuffer4[1] && ExtMapBuffer3[2]>=ExtMapBuffer4[2] && Stoch_Main > Stoch_Sig) {
         LastAlert = Time[0];
         Alert("Heiken Ashi Alert (", Symbol(), ", ", Period(), ")--Possible BUY ");
         PlaySound("Alert2.wav");
       }       
     }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+