Navigation´╝ÜHome > Content >

pa_nonlag_bbmacd.mq4

Time: 2017-04-19 | Download file:pa_nonlag_bbmacd.mq4

//+------------------------------------------------------------------+
//|                                                  BB Macd nrp.mq4 |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  Gold
#property indicator_color2  Gold
#property indicator_color3  Lime
#property indicator_color4  Red
#property indicator_color5  Red
#property indicator_style1  STYLE_DASH
#property indicator_style2  STYLE_DASH
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2
#property indicator_level1  0
#property indicator_levelcolor MediumOrchid


//
//
//
//
//
extern int    Length                 = 21;
extern bool   drawDots               = true;
extern int    price                  = PRICE_MEDIAN;
extern double filter                 = 1;
extern double cyclesFast             = 5.0;
extern double cyclesSlow             = 10.0;
extern int    devLength              = 20;
extern bool   UseClassicalDeviations = false; 
extern double StDv                   = 2.0;


//
//
//
//
//

double buffer1[];
double buffer2[];
double bbMacd[];
double buffer4[];
double buffer5[];
double buffer6[];
double emaFast[];
double emaSlow[];
double alphas[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0, buffer1);
   SetIndexBuffer(1, buffer2);
   SetIndexBuffer(2, bbMacd);
   SetIndexBuffer(3, buffer4);
   SetIndexBuffer(4, buffer5);
   SetIndexBuffer(5, buffer6);
   SetIndexBuffer(6, emaFast); 
   SetIndexBuffer(7, emaSlow); 
   
      if (drawDots) {
            SetIndexStyle(2, DRAW_ARROW); SetIndexArrow(2, 159);
            SetIndexStyle(3, DRAW_ARROW); SetIndexArrow(3, 159);
            SetIndexStyle(4, DRAW_NONE);
         }
      else
         {
            SetIndexStyle(2, DRAW_LINE);
            SetIndexStyle(3, DRAW_LINE);
            SetIndexStyle(4, DRAW_LINE);
         }

   //
   //
   //
   //
   //
     
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
    IndicatorShortName("pa nonlag bbmacd ("+DoubleToStr(cyclesFast,2)+","+DoubleToStr(cyclesSlow,2)+","+DoubleToStr(filter,2)+")");
    SetIndexLabel(0, "Upperband");
    SetIndexLabel(1, "Lowerband");  
    SetIndexLabel(2, "BB Macd");
    SetIndexLabel(3, NULL);
    SetIndexLabel(4, NULL);
    
   return(0);
}

int deinit(){return(0);}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int counted_bars = IndicatorCounted();
   int limit,i;


   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
       limit = Bars - counted_bars;

      if (!drawDots)
      if (bbMacd[limit]= 0 ; i--)
   {
         double prc = iNoLagMa(iMA(NULL,0,1,0,MODE_SMA,PRICE_MEDIAN,i),Length,alphas,0,i);
         if (i==Bars-1)
         {
            emaFast[i] = prc;
            emaSlow[i] = prc;
            continue;
         }

      //
      //
      //
      //
      //
      
      double alphaFast = 2.0/(1.0+iHilbertPhase(prc,filter,cyclesFast,i,0));
      double alphaSlow = 2.0/(1.0+iHilbertPhase(prc,filter,cyclesSlow,i,1));
  

         emaFast[i] = emaFast[i+1]+alphaFast*(prc-emaFast[i+1]);
         emaSlow[i] = emaSlow[i+1]+alphaSlow*(prc-emaSlow[i+1]);
         bbMacd[i]  = emaFast[i]-emaSlow[i];
         buffer6[i] = buffer6[i+1] + alpha*(bbMacd[i]-buffer6[i+1]);
        double sDev = iDeviationPlus(bbMacd, buffer6, devLength, i);
         buffer1[i] = buffer6[i] + (StDv * sDev);
         buffer2[i] = buffer6[i] - (StDv * sDev);
         buffer4[i] = EMPTY_VALUE;
         buffer5[i] = EMPTY_VALUE;
               
         //
         //
         //
         //
         //
               
         if (bbMacd[i]0)
   {
      int    len = values[forValue][_len];
      double sum = 0;
           for (k=0; k < len-1; k++) sum += alphasArray[k]*prices[r-k][forValue];      
           return( sum / values[forValue][_weight]);
   }
   else 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-workHil[r][s+_phase];
         if (workHil[r][s+_I1]<0 && workHil[r][s+_Q1]<0) workHil[r][s+_phase] = 180+workHil[r][s+_phase];
         if (workHil[r][s+_I1]>0 && workHil[r][s+_Q1]<0) workHil[r][s+_phase] = 360-workHil[r][s+_phase];

      //
      //
      //
      //
      //
                        
      workHil[r][s+_deltaPhase] = workHil[r-1][s+_phase]-workHil[r][s+_phase];

         if (workHil[r-1][s+_phase]<90 && workHil[r][s+_phase]>270)
             workHil[r][s+_deltaPhase] = 360+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; phaseSum0; 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));
}

Recommend