
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DeepSkyBlue
#property indicator_color2  Red
#property indicator_color3  Red
#property indicator_color4  Gold
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_level1 80
#property indicator_level2 20
#property indicator_level3 0

//
//
//
//
//

extern ENUM_TIMEFRAMES TimeFrame   = PERIOD_CURRENT;
extern int             HMALength   = 10;
extern int             KPeriod     = 14;
extern int             DPeriod     = 5;
extern int             Slowing     = 5;
extern ENUM_MA_METHOD  Method      = 0;
extern ENUM_STO_PRICE  Price       = STO_LOWHIGH;
extern bool            Interpolate = true;

//
//
//
//
//

double hma[];
double hmada[];
double hmadb[];
double trend[];
double SignalBuffer[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
      SetIndexBuffer(0,hma);         SetIndexLabel(0,"Stoch Main");
      SetIndexBuffer(1,hmada);      
      SetIndexBuffer(2,hmadb);
      SetIndexBuffer(3,SignalBuffer); SetIndexLabel(3,"Stoch Signal");
      SetIndexBuffer(4,trend); 

      //
      //
      //
      //
      //
   
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame==-99;
      TimeFrame         = MathMax(TimeFrame,_Period);
      
   //
   //
   //
   //
   //
         
   IndicatorShortName(timeFrameToString(TimeFrame)+" zero lag HMA Stochastic("+HMALength+","+KPeriod+","+DPeriod+","+Slowing+")");         
   return(0);
}
int deinit() { 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) { trend[0] = limit+1; return(0); }
         
   //
   //
   //
   //
   //

   if (TimeFrame==Period())
   {
      if (trend[limit] == -1) CleanPoint(limit,hmada,hmadb);
      for(int i=limit; i>=0; i--) {
         double hma1 = iHull(iStochastic(NULL,0,KPeriod,DPeriod,Slowing,Method,Price,MODE_MAIN,i),HMALength,i,0);
         double hma2 = iHull(hma1                                                                ,HMALength,i,1);
         hma[i]  = 2.0*hma1-hma2;
        
         hmada[i] = EMPTY_VALUE;
         hmadb[i] = EMPTY_VALUE;
         trend[i] = trend[i+1];
            if (hma[i] > hma[i+1]) trend[i] =   1;
            if (hma[i] < hma[i+1]) trend[i] =  -1;
            if (trend[i] == -1) PlotPoint(i,hmada,hmadb,hma);
         }
      for (int cnt = 0; cnt < limit; cnt++) SignalBuffer[cnt] = iMAOnArray(hma,0,DPeriod,0,Method,cnt);
      return(0);
   }      

   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/_Period));
   if (trend[limit] == -1) CleanPoint(limit,hmada,hmadb);
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
         trend[i]        = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,HMALength,KPeriod,DPeriod,Slowing,Method,Price,4,y);
         hma[i]          = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,HMALength,KPeriod,DPeriod,Slowing,Method,Price,0,y);
         SignalBuffer[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,HMALength,KPeriod,DPeriod,Slowing,Method,Price,3,y);
         hmada[i]        = EMPTY_VALUE;
         hmadb[i]        = EMPTY_VALUE;
         

      //
      //
      //
      //
      //
       
      if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;

      //
      //
      //
      //
      //

      datetime time = iTime(NULL,TimeFrame,y);
         for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
         for(int k = 1; k < n; k++)
         {
            hma[i+k] = hma[i]  +(hma[i+n]-hma[i])*k/n;
            SignalBuffer[i+k] = SignalBuffer[i]  +(SignalBuffer[i+n]-SignalBuffer[i])*k/n;
         }            
   }
   for(i=limit; i>=0; i--) if (trend[i]==-1) PlotPoint(i,hmada,hmadb,hma);
   
   //
   //
   //
   //
   //
   
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

#define hullInstances 2
double workHull[][hullInstances*2];
double iHull(double price, double period, int r, int instanceNo=0)
{
   if (period<=1) return(price); r = Bars-r-1;
   if (ArrayRange(workHull,0)!= Bars) ArrayResize(workHull,Bars);

   //
   //
   //
   //
   //

      int HmaPeriod  = (int)MathMax(period,2);
      int HalfPeriod = (int)MathFloor(HmaPeriod/2);
      int HullPeriod = (int)MathFloor(MathSqrt(HmaPeriod));
      double thma,hmw,weight; instanceNo *= 2;

         workHull[r][instanceNo] = price;

         //
         //
         //
         //
         //
               
         hmw = HalfPeriod; thma = hmw*price; 
            for(int k=1; k<HalfPeriod && (r-k)>=0; k++)
            {
               weight = HalfPeriod-k;
               hmw   += weight;
               thma  += weight*workHull[r-k][instanceNo];  
            }             
            workHull[r][instanceNo+1] = 2.0*thma/hmw;

         hmw = HmaPeriod; thma = hmw*price; 
            for(k=1; k<period && (r-k)>=0; k++)
            {
               weight = HmaPeriod-k;
               hmw   += weight;
               thma  += weight*workHull[r-k][instanceNo];
            }             
            workHull[r][instanceNo+1] -= thma/hmw;

         //
         //
         //
         //
         //
         
         hmw = HullPeriod; thma = hmw*workHull[r][instanceNo+1];
            for(k=1; k<HullPeriod && (r-k)>=0; k++)
            {
               weight = HullPeriod-k;
               hmw   += weight;
               thma  += weight*workHull[r-k][1+instanceNo];  
            }
   return(thma/hmw);
}


//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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("");
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
         if (first[i+2] == EMPTY_VALUE) {
                first[i]   = from[i];
                first[i+1] = from[i+1];
                second[i]  = EMPTY_VALUE;
            }
         else {
                second[i]   =  from[i];
                second[i+1] =  from[i+1];
                first[i]    = EMPTY_VALUE;
            }
      }
   else
      {
         first[i]  = from[i];
         second[i] = EMPTY_VALUE;
      }
}