//+------------------------------------------------------------------+
//|                                                       haDiff.mq4 |
//|                               Copyright � 2014, Gehtsoft USA LLC |
//|                                            http://fxcodebase.com |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2014, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Green
#property indicator_color2  Red

extern int    Length = 3;
extern int    Method = 0;  // 0 - SMA
                           // 1 - EMA
                           // 2 - SMMA
                           // 3 - LWMA
extern bool   alertsOn         = false;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = false;
extern bool   alertsEmail      = false;
extern bool   alertsNotify     = false;
extern string soundFile        = "alert2.wav";
extern bool   ShowArrows       = false;
extern string arrowsIdentifier = "haDiff Arrows1";
extern double arrowsUpperGap   = 1.0;
extern double arrowsLowerGap   = 1.0;
extern color  arrowsUpColor    = LimeGreen;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 241;
extern int    arrowsDnCode     = 242;

//
//
//
//
//

double Difference[];
double Signal[];
double O[];
double C[];
double trend[];

int init()
{
 IndicatorBuffers(5);
 SetIndexBuffer(0,Difference);
 SetIndexBuffer(1,Signal);
 SetIndexBuffer(2,O);
 SetIndexBuffer(3,C);
 SetIndexBuffer(4,trend);
 IndicatorShortName("HA Difference");
return(0);
}

int deinit() 
{  
   deleteArrows(); 
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);
   
   //
   //
   //
   //
   //
   
   for(int i = limit; i>=0; i--)
 	{
 	   if (High[i] != Low[i])
        O[i]=(Open[i+1]+Close[i+1])*0.50;
        C[i]=(Open[i]+High[i]+Low[i]+Close[i])*0.25;
        Difference[i] = C[i]- O[i];

   } 
 
   for(i = limit; i>=0; i--)
   {
     Signal[i]= iMAOnArray(Difference,0,Length,0,Method,i);
     trend[i] = trend[i+1];
     if (Difference[i]>Signal[i]) trend[i] = 1;
     if (Difference[i]<Signal[i]) trend[i] =-1;
     
     //
     //
     //
     //
     //
     
     if (ShowArrows)
     {
       deleteArrow(Time[i]);
       if (trend[i] != trend[i+1])
       {
         if (trend[i] == 1)  drawArrow(i,arrowsUpColor,arrowsUpCode,false);
         if (trend[i] ==-1)  drawArrow(i,arrowsDnColor,arrowsDnCode, true);
       }
     }
   }
   
   //
   //
   //
   //
   //
      
   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);
}

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

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()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," haDiff ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," haDiff "),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);
}

//
//
//
//
//

void deleteArrows()
{
   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);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}