//------------------------------------------------------------------
#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 4
#property indicator_color1 LimeGreen
#property indicator_color2 Orange
#property indicator_color3 DimGray
#property indicator_color4 PaleVioletRed
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 3
#property indicator_width4 3
#property indicator_level1 0

//
//
//
//
//

extern string TimeFrame     = "Current time frame";
extern bool   BetterFormula = false;
extern int    SignalPeriod  = 3;
extern int    SignalMethod  = MODE_SMA;
extern bool   verticalLinesVisible   = false;
extern string verticalLinesID        = "HaDeltaLines";
extern color  verticalLinesUpColor   = LimeGreen;
extern color  verticalLinesDownColor = Orange;
extern int    verticalLinesStyle     = STYLE_DOT;
extern int    verticalLinesWidth     = 0;

double haUp[];
double haDn[];
double bhaOpn[];
double bhaClo[];
double haDelta[];
double haSignal[];
double cross[];

string indicatorFileName;
bool   returnBars;
int    timeFrame;

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

int init()
{
   IndicatorDigits(6);
   IndicatorBuffers(7);
   SetIndexBuffer(0, haUp); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1, haDn); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2, haDelta);
   SetIndexBuffer(3, haSignal);
   SetIndexBuffer(4, bhaOpn);
   SetIndexBuffer(5, bhaClo);
   SetIndexBuffer(6, cross);

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
   IndicatorShortName(timeFrameToString(timeFrame)+" haDelta ("+SignalPeriod+")");
   return(0);
}
int deinit()
{
   string lookFor       = verticalLinesID+":";
   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();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { haUp[0] = MathMin(limit+1,Bars-1); return(0); }
          
   //
   //
   //
   //
   //
   
   if (timeFrame == Period())
   {
      for (int 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;
         haUp[i]    = EMPTY_VALUE;
         haDn[i]    = EMPTY_VALUE;
           if (haDelta[i]>0) haUp[i] = haDelta[i];
           if (haDelta[i]<0) haDn[i] = haDelta[i];
      }
      for (i=limit; i >= 0; i--)
      {
         haSignal[i] = iMAOnArray(haDelta,0,SignalPeriod,0,SignalMethod,i);
         cross[i] = cross[i+1];
            if (haDelta[i]>haSignal[i]) cross[i] =  1;
            if (haDelta[i]<haSignal[i]) cross[i] = -1;
            if (verticalLinesVisible && cross[i] != cross[i+1])
               if (cross[i] == 1)
                     drawLine(i,verticalLinesUpColor );
               else  drawLine(i,verticalLinesDownColor);
      }               
      return(0);
   }
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         haDelta[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",BetterFormula,SignalPeriod,SignalMethod,verticalLinesVisible,verticalLinesID,verticalLinesUpColor,verticalLinesDownColor,verticalLinesStyle,verticalLinesWidth,2,y);
         haSignal[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",BetterFormula,SignalPeriod,SignalMethod,verticalLinesVisible,verticalLinesID,verticalLinesUpColor,verticalLinesDownColor,verticalLinesStyle,verticalLinesWidth,3,y);
         haUp[i]     = EMPTY_VALUE;
         haDn[i]     = EMPTY_VALUE;
           if (haDelta[i]>0) haUp[i] = haDelta[i];
           if (haDelta[i]<0) haDn[i] = haDelta[i];
   }
   return(0);
}

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

void drawLine(int i,color theColor)
{
   string name = verticalLinesID+":"+Time[i];
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
         ObjectSet(name,OBJPROP_COLOR,theColor);
         ObjectSet(name,OBJPROP_STYLE,verticalLinesStyle);
         ObjectSet(name,OBJPROP_WIDTH,verticalLinesWidth);
         ObjectSet(name,OBJPROP_BACK,true);
}

//
//
//
//
//

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);
}