//+------------------------------------------------------------------+
//|ha sw .mq4 
//| 
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  IndianRed
#property indicator_color2  MediumSeaGreen
#property indicator_color3  IndianRed
#property indicator_color4  MediumSeaGreen
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  3
#property indicator_width4  3

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame        = PERIOD_CURRENT;
extern int             Shift            = 0;
extern bool            alertsOn         = true;
extern bool            alertsOnCurrent  = false;
extern bool            alertsMessage    = true;
extern bool            alertsSound      = false;
extern bool            alertsNotify     = false;
extern bool            alertsEmail      = false;
extern string          soundFile        = "alert2.wav";
extern bool            ShowArrows       = false;
extern string          arrowsIdentifier = "ha Arrows";
extern double          arrowsUpperGap   = 0.5;
extern double          arrowsLowerGap   = 0.5;
extern color           arrowsUpColor    = LimeGreen;
extern color           arrowsDnColor    = Red;
extern color           arrowsUpCode     = 241;
extern color           arrowsDnCode     = 242;
//
//
//
//
//

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double trend[];
string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,Buffer1); SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(1,Buffer2); SetIndexStyle(1,DRAW_SECTION);
   SetIndexBuffer(2,Buffer3); SetIndexStyle(2,DRAW_SECTION);
   SetIndexBuffer(3,Buffer4); SetIndexStyle(3,DRAW_SECTION);
   SetIndexBuffer(4,trend);
   
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = MathMax(TimeFrame,_Period);
      SetIndexShift(0,Shift * TimeFrame/Period()); 
      SetIndexShift(1,Shift * TimeFrame/Period());
      SetIndexShift(2,Shift * TimeFrame/Period());
      SetIndexShift(3,Shift * TimeFrame/Period());  
   IndicatorShortName(timeFrameToString(TimeFrame)+" Heiken Ashi sw");
return(0);
}
int deinit()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
   return(0);
}

//
//
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

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)  { Buffer1[0] = limit+1; return(0); }
   
   //
   //
   //
   //
   //
   
   if (TimeFrame == Period())
   {
     for (pos=limit; pos >= 0; pos--)
     {
          
         double haClose = (Open[pos]+High[pos]+Low[pos]+Close[pos])/4;
         double haOpen  = (Buffer3[pos+1]+Buffer4[pos+1])/2;
         double haHigh  = MathMax(High[pos],MathMax(haOpen,haClose));
         double haLow   = MathMin(Low[pos], MathMin(haOpen,haClose));
          
         if (haOpen<haClose) { Buffer1[pos]=haLow;  Buffer2[pos]=haHigh; } 
         else                { Buffer1[pos]=haHigh; Buffer2[pos]=haLow;  } 
                               Buffer3[pos]=haOpen;
                               Buffer4[pos]=haClose;
          
                trend[pos] = trend[pos+1];
                if (Buffer3[pos] < Buffer4[pos]) trend[pos] =  1;
                if (Buffer3[pos] > Buffer4[pos]) trend[pos] = -1;  
         
                //
                //
                //
                //
                //
     
                if (ShowArrows)
                {
                  ObjectDelete(arrowsIdentifier+":"+Time[pos]);
                  if (trend[pos]!=trend[pos+1])
                  {
                    if (trend[pos] == 1)  drawArrow(pos,arrowsUpColor,arrowsUpCode,false);
                    if (trend[pos] ==-1)  drawArrow(pos,arrowsDnColor,arrowsDnCode, true);
                  }
               }
      } 
      
      //
      //
      //
      //
      //
      
      if (alertsOn)
      {
         if (alertsOnCurrent)
              int whichBar = 0;
         else     whichBar = 1;
         if (trend[whichBar] != trend[whichBar+1])
         {
            if (trend[whichBar] == 1)   doAlert(whichBar,"crossed up");
            if (trend[whichBar] ==-1)   doAlert(whichBar,"crossed down");
         }         
      }
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   for(pos=limit; pos >= 0; pos--)  
   {
      int y = iBarShift(NULL,TimeFrame,Time[pos]);
         Buffer1[pos] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Shift,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,0,y);
         Buffer2[pos] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Shift,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,1,y);
         Buffer3[pos] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Shift,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,2,y);
         Buffer4[pos] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,Shift,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,3,y);
   }
return(0);         
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Heiken Ashi ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " Heiken Ashi "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}


//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}