//+------------------------------------------------------------------+
//|                                                      QQE new.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers    5
#property indicator_color1     Green
#property indicator_color2     Gold
#property indicator_color3     Red
#property indicator_color4     DimGray
#property indicator_color5     Lime
#property indicator_width1     2
#property indicator_width2     2
#property indicator_width3     2
#property indicator_width4     2
#property indicator_width5     2
#property indicator_levelcolor Gold
#property indicator_level1     0

//
//
//
//
//

extern int    SF             = 5;
extern int    MomentumLength = 20;
extern int    Price          = PRICE_CLOSE;
extern double WP             = 3.236;

//
//
//
//
//

extern bool alertsOn              = true;
extern bool alertsSignalLineCross = true;
extern bool alertsOnCurrent       = true;
extern bool alertsMessage         = true;
extern bool alertsSound           = true;
extern bool alertsEmail           = false;

//
//
//
//
//

double buffer1[];
double buffer2[];
double Trend[];
double HistoU[];
double HistoM[];
double HistoD[];
double work[][6];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{

   IndicatorBuffers(6);
   SetIndexBuffer(0,HistoU); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexLabel(0, "QQE");
   SetIndexBuffer(1,HistoM); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexLabel(1, "QQE trend");
   SetIndexBuffer(2,HistoD); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,buffer1);SetIndexDrawBegin(3,MomentumLength);
   SetIndexBuffer(4,Trend);
   SetIndexBuffer(5,buffer2); 
   
   
   MomentumLength = MathMax(MomentumLength,1);
 
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iEma 0
#define iEmm 1
#define iQqe 2
#define iPcm 3
#define tQqe 4
#define tQqs 5

//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,r,limit;
   
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
           limit = MathMin(Bars-counted_bars,Bars-1);
           if (ArrayRange(work,0) != Bars) ArrayResize(work,Bars); 

   //
   //
   //
   //
   //

   double alpha1 = 2.0/(SF+1.0);
   double alpha2 = 2.0/(MomentumLength*2.0);
      
   for (i=limit, r=Bars-i-1; i>=0; i--,r++)
   {  
   
      buffer2[i] = iMA(NULL,0,1,0,MODE_SMA,Price,i);
      
      double sumMom = 0;
      double sumWgh = 0;
      for (int k=0; (i+k)<Bars && k<MomentumLength; k++)
      {
         double weight = MathSqrt(k+1);
               sumMom += (buffer2[i]-buffer2[i+k+1])/weight;
               sumWgh += weight;
      }
      if (sumWgh != 0)         
            buffer1[i] = sumMom/sumWgh; 
      else  buffer1[i] = 0;
      
      work[r][iPcm] = work[r-1][iPcm] + alpha1* (buffer1[i]                            - work[r-1][iPcm]);
      work[r][iEma] = work[r-1][iEma] + alpha2*(MathAbs(work[r-1][iPcm]-work[r][iPcm]) - work[r-1][iEma]);
      work[r][iEmm] = work[r-1][iEmm] + alpha2*(work[r][iEma] - work[r-1][iEmm]);

      //
      //
      //
      //
      //

         double pcm0 = work[r  ][iPcm];
         double pcm1 = work[r-1][iPcm];
         double dar  = work[r  ][iEmm]*WP;
         double tr   = work[r-1][iQqe];
         double dv   = tr;
   
            if (pcm0 < tr) { tr = pcm0 + dar; if ((pcm1 < dv) && (tr > dv)) tr = dv; }
            if (pcm0 > tr) { tr = pcm0 - dar; if ((pcm1 > dv) && (tr < dv)) tr = dv; }
         
      //
      //
      //
      //
      //
         
         work[r][iQqe] = tr;
         work[r][tQqe] = work[r-1][tQqe];
         work[r][tQqs] = work[r-1][tQqs];
         buffer1[i]    = work[r][iPcm];
         Trend[i]      = tr;
         HistoU[i]     =  EMPTY_VALUE;
         HistoM[i]     =  EMPTY_VALUE;
         HistoD[i]     =  EMPTY_VALUE;
   
         if (buffer1[i] > 0)                                       HistoU[i] = buffer1[i];
         if (buffer1[i] < 0)                                       HistoD[i] = buffer1[i];
         if (HistoU[i] == EMPTY_VALUE && HistoD[i] == EMPTY_VALUE) HistoM[i] = buffer1[i];

      //
      //
      //
      //
      //
               
         if (buffer1[i] > 0)        work[r][tQqe] =  1;
         if (buffer1[i] < 0)        work[r][tQqe] = -1;
         if (buffer1[i] > Trend[i]) work[r][tQqs] =  1;
         if (buffer1[i] < Trend[i]) work[r][tQqs] = -1;
   }    
   
   //
   //
   //
   //
   //
   
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = Bars-1;
      else     whichBar = Bars-2;

      //
      //
      //
      //
      //
         
      if (alertsSignalLineCross)
      {
         if (work[whichBar][tQqs] != work[whichBar-1][tQqs])
         {
            if (work[whichBar][tQqs] ==  1) doAlert(" qqe line crossed signal line UP");
            if (work[whichBar][tQqs] == -1) doAlert(" qqe line crossed signal line DOWN");
         }         
      }
      else
      {
         if (work[whichBar][tQqe] != work[whichBar-1][tQqe])
         {
            if (work[whichBar][tQqe] ==  1) doAlert(" qqe line crossed 0 line UP");
            if (work[whichBar][tQqe] == -1) doAlert(" qqe line crossed 0 line 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)," QQE - ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"QQE"),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}


