//+------------------------------------------------------------------+
//|                                                  BB Macd nrp.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1  DimGray
#property indicator_color2  DimGray
#property indicator_color3  DimGray
#property indicator_color4  DeepSkyBlue
#property indicator_color5  PaleVioletRed
#property indicator_color6  PaleVioletRed
#property indicator_width4  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_style3  STYLE_DOT

//
//
//
//
//

#import "dynamicZone.dll"
   double dzBuyP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precision);
   double dzSellP(double& sourceArray[],double probabiltyValue, int lookBack, int bars, int i, double precision);
#import

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern string ForSymbol          = "";
extern double FastCycles         = 0.5;
extern int    FastFilter         = 1.0;
extern double SlowCycles         = 1.0;
extern int    SlowFilter         = 1.0;
extern int    DzLookBackBars         = 35;
extern double DzStartBuyProbability  = 0.90;
extern double DzStartSellProbability = 0.90;
extern bool   drawDots           = False;
extern bool   arrowsVisible      = false;
extern bool   arrowsShowBreakOut = true;
extern bool   arrowsShowRetrace  = true;
extern string arrowsIdentifier   = "pa macd arrows";
extern color  arrowsUpColor      = DeepSkyBlue;
extern color  arrowsDnColor      = Red;
extern bool   alertsOn           = false;
extern bool   alertsOnCurrent    = true;
extern bool   alertsMessage      = true;
extern bool   alertsSound        = false;
extern bool   alertsEmail        = false;
extern bool   Interpolate        = true;

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double bbMacd[];
double buffer4[];
double buffer5[];
double trendSlope[];
double trendValue[];

//
//
//
//
//

int    timeFrame;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0, buffer1);
   SetIndexBuffer(1, buffer2);
   SetIndexBuffer(2, buffer3);
   SetIndexBuffer(3, bbMacd);
   SetIndexBuffer(4, buffer4);
   SetIndexBuffer(5, buffer5);
   SetIndexBuffer(6, trendSlope);
   SetIndexBuffer(7, trendValue);
      if (drawDots) {
            SetIndexStyle(3, DRAW_ARROW); SetIndexArrow(3, 159);
            SetIndexStyle(4, DRAW_ARROW); SetIndexArrow(4, 159);
            SetIndexStyle(5, DRAW_NONE);
         }
      else
         {
            SetIndexStyle(3, DRAW_LINE);
            SetIndexStyle(4, DRAW_LINE);
            SetIndexStyle(5, DRAW_LINE);
         }

      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
         
   IndicatorDigits(5);
   if (ForSymbol=="") ForSymbol = Symbol();
   IndicatorShortName(ForSymbol+" "+timeFrameToString(timeFrame)+" PA Macd (" + DoubleToStr(FastCycles,2) + "," + DoubleToStr(SlowCycles,2) + "," + DzLookBackBars+")");
      SetIndexLabel(0, "Upperband");
      SetIndexLabel(1, "Lowerband");  
      SetIndexLabel(2, "Macd");
      SetIndexLabel(3, NULL);
      SetIndexLabel(4, NULL);
   return(0);
}

int deinit()
{
   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);
   }
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double work[][2];

//
//
//
//
//

int start()
{
   int i,r,limit,counted_bars = IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
       limit = MathMin(Bars - counted_bars,Bars-1);
       if (returnBars) { buffer1[0] = limit+1; return(0); }


   //
   //
   //
   //
   //

   if (calculateValue || (ForSymbol == Symbol() && timeFrame == Period()))
   {
      if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);
      if (!drawDots) if (trendSlope[limit]==-1) CleanPoint(limit,buffer4,buffer5);
      
      //
      //
      //
      //
      //
      
      for(i = limit,r=Bars-i-1; i >= 0 ; i--,r++)
      {
         double price  = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i);
         double alphaf = 2.0 / (1.0 + iHilbertPhase(price,FastFilter,FastCycles,i,0));
         double alphas = 2.0 / (1.0 + iHilbertPhase(price,SlowFilter,SlowCycles,i,1));
   
            if (r<=1)
               {
                  work[r][0] = price;
                  work[r][1] = price;
                  continue;
               }
            
         work[r][0] = work[r-1][0]+alphaf*(price-work[r-1][0]);
         work[r][1] = work[r-1][1]+alphas*(price-work[r-1][1]);
         bbMacd[i]  = work[r][0]-work[r][1];
         
         //
         //
         //
         //
         //
         
         if (DzStartBuyProbability >0) buffer1[i] = dzBuyP (bbMacd, DzStartBuyProbability,  DzLookBackBars, Bars, i, 0.0001);
         if (DzStartSellProbability>0) buffer2[i] = dzSellP(bbMacd, DzStartSellProbability, DzLookBackBars, Bars, i, 0.0001);
                                       buffer3[i] = dzSellP(bbMacd, 0.5                   , DzLookBackBars, Bars, i, 0.0001);
         
         buffer4[i] = EMPTY_VALUE;
         buffer5[i] = EMPTY_VALUE;
               
         //
         //
         //
         //
         //
               
         trendSlope[i] = trendSlope[i+1];
            if (bbMacd[i]>bbMacd[i+1]) trendSlope[i] =  1;
            if (bbMacd[i]<bbMacd[i+1]) trendSlope[i] = -1;
            if (trendSlope[i]==-1)
               if (drawDots)     buffer4[i] = bbMacd[i];
               else  PlotPoint(i,buffer4,buffer5,bbMacd);
         trendValue[i] = trendValue[i+1];
            if (bbMacd[i]>buffer1[i])                         trendValue[i] =  1;
            if (bbMacd[i]<buffer2[i])                         trendValue[i] = -1;
            if (bbMacd[i]<buffer1[i] && bbMacd[i]>buffer2[i]) trendValue[i] =  0;
         manageArrow(i);
      }
      manageAlerts();
      return(0);
   }      

   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(ForSymbol,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   if (!drawDots) if (trendSlope[limit]==-1) CleanPoint(limit,buffer4,buffer5);
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(ForSymbol,timeFrame,Time[i]);
         buffer1[i]    = iCustom(ForSymbol,timeFrame,indicatorFileName,"calculateValue","",FastCycles,FastFilter,SlowCycles,SlowFilter,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,0,y);
         buffer2[i]    = iCustom(ForSymbol,timeFrame,indicatorFileName,"calculateValue","",FastCycles,FastFilter,SlowCycles,SlowFilter,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,1,y);
         buffer3[i]    = iCustom(ForSymbol,timeFrame,indicatorFileName,"calculateValue","",FastCycles,FastFilter,SlowCycles,SlowFilter,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,2,y);
         bbMacd[i]     = iCustom(ForSymbol,timeFrame,indicatorFileName,"calculateValue","",FastCycles,FastFilter,SlowCycles,SlowFilter,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,3,y);
         trendSlope[i] = iCustom(ForSymbol,timeFrame,indicatorFileName,"calculateValue","",FastCycles,FastFilter,SlowCycles,SlowFilter,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,6,y);
         trendValue[i] = iCustom(ForSymbol,timeFrame,indicatorFileName,"calculateValue","",FastCycles,FastFilter,SlowCycles,SlowFilter,DzLookBackBars,DzStartBuyProbability,DzStartSellProbability,7,y);
         buffer4[i]    = EMPTY_VALUE;
         buffer5[i]    = EMPTY_VALUE;
            
               if (drawDots && trendSlope[i]==-1) buffer4[i] = bbMacd[i];
               manageArrow(i);
            
         //
         //
         //
         //
         //
      
         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++)
            {
               bbMacd[i+k]  = bbMacd[i]  + (bbMacd[i+n] -bbMacd[i])*k/n;
               buffer1[i+k] = buffer1[i] + (buffer1[i+n]-buffer1[i])*k/n;
               buffer2[i+k] = buffer2[i] + (buffer2[i+n]-buffer2[i])*k/n;
               if (buffer4[i+k] != EMPTY_VALUE) buffer4[i+k] = bbMacd[i+k];
            }               
   }
   if (!drawDots) for (i=limit;i>=0;i--) if (trendSlope[i]==-1) PlotPoint(i,buffer4,buffer5,bbMacd);
   manageAlerts();   
   return(0);
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double workHil[][18];
#define _price      0
#define _smooth     1
#define _detrender  2
#define _period     3
#define _instPeriod 4
#define _phase      5
#define _deltaPhase 6
#define _Q1         7
#define _I1         8

#define Pi 3.14159265358979323846264338327950288

//
//
//
//
//

double iHilbertPhase(double price, double filter, double cyclesToReach, int i, int s=0)
{
   if (ArrayRange(workHil,0)!=Bars) ArrayResize(workHil,Bars);
   int r = Bars-i-1; s = s*9;
      
   //
   //
   //
   //
   //
      
      workHil[r][s+_price]      = price;
      workHil[r][s+_smooth]     = (4.0*workHil[r][s+_price]+3.0*workHil[r-1][s+_price]+2.0*workHil[r-2][s+_price]+workHil[r-3][s+_price])/10.0;
      workHil[r][s+_detrender]  = calcComp(r,_smooth,s);
      workHil[r][s+_Q1]         = 0.15*calcComp(r,_detrender,s)  +0.85*workHil[r-1][s+_Q1];
      workHil[r][s+_I1]         = 0.15*workHil[r-3][s+_detrender]+0.85*workHil[r-1][s+_I1];
      workHil[r][s+_phase]      = workHil[r-1][s+_phase];
      workHil[r][s+_instPeriod] = workHil[r-1][s+_instPeriod];

      //
      //
      //
      //
      //
           
         if (MathAbs(workHil[r][s+_I1])>0)
                     workHil[r][s+_phase] = 180.0/Pi*MathArctan(MathAbs(workHil[r][s+_Q1]/workHil[r][s+_I1]));
           
         if (workHil[r][s+_I1]<0 && workHil[r][s+_Q1]>0) workHil[r][s+_phase] = 180.0-workHil[r][s+_phase];
         if (workHil[r][s+_I1]<0 && workHil[r][s+_Q1]<0) workHil[r][s+_phase] = 180.0+workHil[r][s+_phase];
         if (workHil[r][s+_I1]>0 && workHil[r][s+_Q1]<0) workHil[r][s+_phase] = 360.0-workHil[r][s+_phase];

      //
      //
      //
      //
      //
                        
      workHil[r][s+_deltaPhase] = workHil[r-1][s+_phase]-workHil[r][s+_phase];

         if (workHil[r-1][s+_phase]<90.0 && workHil[r][s+_phase]>270.0)
             workHil[r][s+_deltaPhase] = 360.0+workHil[r-1][s+_phase]-workHil[r][s+_phase];
             workHil[r][s+_deltaPhase] = MathMax(MathMin(workHil[r][s+_deltaPhase],60),7);
      
            //
            //
            //
            //
            //
                  
            double alpha    = 2.0/(1.0+MathMax(filter,1));
            double phaseSum = 0; for (int k=0; phaseSum<cyclesToReach*360.0 && (r-k)>0; k++) phaseSum += workHil[r-k][s+_deltaPhase];
         
               if (k>0) workHil[r][s+_instPeriod]= k;
                  workHil[r][s+_period] = workHil[r-1][s+_period]+alpha*(workHil[r][s+_instPeriod]-workHil[r-1][s+_period]);
   return (workHil[r][s+_period]);
}

//
//
//
//
//

double calcComp(int r, int from, int s)
{
   return((0.0962*workHil[r  ][s+from] + 
           0.5769*workHil[r-2][s+from] - 
           0.5769*workHil[r-4][s+from] - 
           0.0962*workHil[r-6][s+from]) * (0.075*workHil[r-1][s+_period] + 0.54));
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (!calculateValue && arrowsVisible && ForSymbol==Symbol())
   {
         deleteArrow(Time[i]);
         if (trendValue[i]!=trendValue[i+1])
         {
            if (arrowsShowBreakOut && trendValue[i] == 1)                        drawArrow(i,arrowsUpColor,241,false);
            if (arrowsShowBreakOut && trendValue[i] ==-1)                        drawArrow(i,arrowsDnColor,242,true);
            if (arrowsShowRetrace  && trendValue[i] == 0 && trendValue[i+1]== 1) drawArrow(i,arrowsDnColor,242,true);
            if (arrowsShowRetrace  && trendValue[i] == 0 && trendValue[i+1]==-1) drawArrow(i,arrowsUpColor,241,false);
         }
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = 3.0*iATR(NULL,0,20,i)/4.0;   
   
      //
      //
      //
      //
      //
      
      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]+gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i] -gap);
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trendValue[whichBar] != trendValue[whichBar+1])
      {
         if (trendValue[whichBar] == 1)                               doAlert(whichBar,"up");
         if (trendValue[whichBar] ==-1)                               doAlert(whichBar,"down");
         if (trendValue[whichBar] == 0 && trendValue[whichBar+1]== 1) doAlert(whichBar,"back from up into zone");
         if (trendValue[whichBar] ==-0 && trendValue[whichBar+1]==-1) doAlert(whichBar,"back from down into zone");
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," PA macd broke bands ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"PA macd "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i]   = 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;
      }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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 char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}