//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
// original idea fr this indicator by Dan Valcu
//------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 DimGray
#property indicator_color2 PaleVioletRed
#property indicator_width1 1
#property indicator_width2 2
#property indicator_level1 0

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern bool   BetterFormula   = false;
extern int    SmoothPeriod    = 3;
extern int    SmoothMethod    = MODE_SMA;
extern int    SignalPeriod    = 3;
extern int    SignalMethod    = MODE_SMA;
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";

double bhaOpn[];
double bhaClo[];
double haDelta[];
double haSmooth[];
double haSignal[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0, haSmooth);
   SetIndexBuffer(1, haSignal);
   SetIndexBuffer(2, haDelta);
   SetIndexBuffer(3, bhaOpn);
   SetIndexBuffer(4, bhaClo);
   SetIndexBuffer(5, trend);
    
    //
    //
    //
    //
    //
   
    indicatorFileName = WindowExpertName();
    returnBars        = TimeFrame == "returnBars";     if (returnBars)     return(0);
    timeFrame         = stringToTimeFrame(TimeFrame);
   
    //
    //
    //
    //
    //
    
    IndicatorShortName(timeFrameToString(timeFrame)+" haDelta smoothed");
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { haSmooth[0] = limit+1; return(0); }
            if (timeFrame!=Period())
            {
               limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
               for (int i=limit; i>=0; i--)
               {
                   int y = iBarShift(NULL,timeFrame,Time[i]);               
                      haSmooth[i] = iCustom(NULL,timeFrame,indicatorFileName,"",BetterFormula,SmoothPeriod,SmoothMethod,SignalPeriod,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,0,y);
                      haSignal[i] = iCustom(NULL,timeFrame,indicatorFileName,"",BetterFormula,SmoothPeriod,SmoothMethod,SignalPeriod,SignalMethod,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,1,y);             
               }
               return(0);
            }
   //
   //
   //
   //
   //
   
   for (i=limit; i >= 0; i--)
   {
      double prOpen  = Open[i];
      double prClose = Close[i];
      double prLow   = Low[i];
      double prHigh  = High[i];

         if (BetterFormula) {
               if (prHigh!=prLow)
                     double haClose = (prOpen+prClose)/2.0+(((prClose-prOpen)/(prHigh-prLow))*MathAbs((prClose-prOpen)/2.0));
                     else   haClose = (prOpen+prClose)/2.0; }
          else              haClose = (prOpen+prHigh+prLow+prClose)/4;
                     double haOpen  = (bhaOpn[i+1]+bhaClo[i+1])/2.0;

         //
         //
         //
         //
         //
         
         bhaOpn[i]  = haOpen;
         bhaClo[i]  = haClose;
         haDelta[i] = haClose-haOpen;
   }
   for (i=limit; i >= 0; i--) haSmooth[i] = iMAOnArray(haDelta ,0,SmoothPeriod,0,SmoothMethod,i);
   for (i=limit; i >= 0; i--) 
   {
      haSignal[i] = iMAOnArray(haSmooth,0,SignalPeriod,0,SignalMethod,i);
         trend[i] = trend[i+1];
         if (haSmooth[i]>haSignal[i]) trend[i] = 1;
         if (haSmooth[i]<haSignal[i]) trend[i] =-1;
    }
      
   //
   //
   //
   //
   //
      
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; 
      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert("crossed up");
      else  doAlert("crossed down");       
   }       
   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 tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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 =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," haDelta smoothed ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," haDelta smoothed "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}