Navigation:Home > Content >

pa_wprsmooth_w_histo_ma.mq4

Time: 2012-08-11 | Download file:pa_wprsmooth_w_histo_ma.mq4

#property indicator_separate_window
#property indicator_buffers    6
#property indicator_color1     Peru
#property indicator_color2     Peru
#property indicator_color3     DeepSkyBlue
#property indicator_color4     Red
#property indicator_color5     PaleVioletRed
#property indicator_color6     LimeGreen
#property indicator_style1     STYLE_DOT
#property indicator_style2     STYLE_DOT
#property indicator_level1     0
#property indicator_levelcolor Peru



extern double filter            = 1.0;
extern double cycles            = 5.0;
extern double mafilter          = 1.0;
extern double macycles          = 1.0;
extern int    SmoothLength      = 5;
extern double SmoothPhase       = 0;
extern bool   ShowHistogram     = false;
extern bool   ShowUpDownLimit   = true;
extern double lowerlevel        = -40.0;
extern double upperlevel        = 40;
extern bool   arrowsVisible     = false;
extern bool   arrowsOnZeroCross = true;
extern double arrowsDistance    = 1.0;
extern string arrowsIdentifier  = "wprSmoothArrows";
extern color  arrowsUpColor     = DeepSkyBlue;
extern color  arrowsDnColor     = PaleVioletRed;




double wpr[];
double wprU[];
double wprD[];
double limU[];
double limD[];
double wprma[];
double state[];


int init()
  {
   IndicatorBuffers(7);
   SetIndexBuffer(0,limU); SetIndexLabel(0,NULL);
   SetIndexBuffer(1,limD); SetIndexLabel(1,NULL);
   SetIndexBuffer(4,wpr);
   SetIndexBuffer(5,wprma);
   SetIndexBuffer(6,state);
   
   if (ShowHistogram)
      {
         SetIndexBuffer(2,wprU); SetIndexStyle(2,DRAW_HISTOGRAM); SetIndexLabel(2,"smooth %R");
         SetIndexBuffer(3,wprD); SetIndexStyle(3,DRAW_HISTOGRAM); SetIndexLabel(3,"smooth %R");
      }
      else
      {
         SetIndexStyle(2,DRAW_NONE); SetIndexLabel(2,NULL);
         SetIndexStyle(3,DRAW_NONE); SetIndexLabel(3,NULL);
      }
    
  
   IndicatorShortName("smooth pa %R ("+DoubleToStr(cycles,2)+","+DoubleToStr(filter,2)+","+DoubleToStr(macycles,2)+","+DoubleToStr(mafilter,2)+")");
   return(0);
}

int deinit()
{
   if (arrowsVisible) deleteArrows();
   return(0);
}

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

int start()
{
  double calcSmoothLength = MathSqrt(SmoothLength); 
  int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
    limit = MathMin(Bars-counted_bars,Bars-1);
    
    
    
   for(i=limit; i>=0; i--)
      {
   
      double dClose   = Close[i];
      double dMaxHigh = High[Highest(NULL,0,MODE_HIGH,iHilbertPhase(iMA(NULL,0,1,0,MODE_SMA,PRICE_MEDIAN,i),filter,cycles,i,0), i)];
      double dMinLow  = Low[Lowest(NULL, 0, MODE_LOW, iHilbertPhase(iMA(NULL,0,1,0,MODE_SMA,PRICE_MEDIAN,i),filter,cycles,i,1), i)];         
              wpr[i]  = iDSmooth(50+(-100)*(dMaxHigh - dClose) / (dMaxHigh - dMinLow),calcSmoothLength,SmoothPhase,i);
              
      if (ShowHistogram)
      {
         wprD[i] = wprD[i+1];
         wprU[i] = wprU[i+1];
            if (wpr[i] > wpr[i+1]) { wprU[i] = wpr[i];  wprD[i] = EMPTY_VALUE; }
            if (wpr[i] < wpr[i+1]) { wprD[i] = wpr[i];  wprU[i] = EMPTY_VALUE; }
      }
              
     if (ShowUpDownLimit)
     {
        limD[i] = lowerlevel;
        limU[i] = upperlevel;
     }         
          
          
   }
   
   for(i=limit; i>=0; i--)
   {
      wprma[i] = iMAOnArray(wpr,0,iHilbertPhase(iMA(NULL,0,1,0,MODE_SMA,PRICE_MEDIAN,i),mafilter,macycles,i,2),0,MODE_SMA,i);
      state[1] = state[i+1];
         if (arrowsOnZeroCross)
         {
            if (wpr[i] > 0) state[i] =  1;
            if (wpr[i] < 0) state[i] = -1;
         }
         else
         {
            if (wpr[i] > wprma[i]) state[i] =  1;
            if (wpr[i] < wprma[i]) state[i] = -1;
         }
         manageArrow(i);
   }      
   
   
   
   
   return(0);
   
}

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

void manageArrow(int i)
{
   if (arrowsVisible)
   {
      deleteArrow(Time[i]);
      if (state[i]!=state[i+1])
      {
         if (state[i] == 1) drawArrow(i,arrowsUpColor,241,false);
         if (state[i] ==-1) drawArrow(i,arrowsDnColor,242,true);
      }
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = arrowsDistance*iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      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 deleteArrows()
{
   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);
   }
}
void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}


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

double wrk[][20];

#define bsmax  5
#define bsmin  6
#define volty  7
#define vsum   8
#define avolty 9

//
//
//
//
//

double iDSmooth(double price, double length, double phase, int i, int s=0)
{
   return (iSmooth(iSmooth(price,length,phase,i,s),length,phase,i,s+10));
}

//
//
//
//
//

double iSmooth(double price, double length, double phase, int i, int s=0)
{
   if (length <=1) return(price);
   if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);
   
   int r = Bars-i-1; 
      if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

   //
   //
   //
   //
   //
   
      double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);
      double pow1   = MathMax(len1-2.0,0.5);
      double del1   = price - wrk[r-1][bsmax+s];
      double del2   = price - wrk[r-1][bsmin+s];
      double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);
      int    forBar = MathMin(r,10);
	
         wrk[r][volty+s] = 0;
               if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1); 
               if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2); 
         wrk[r][vsum+s] =	wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;
         
         //
         //
         //
         //
         //
   
         wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);
            if (wrk[r][avolty+s] > 0)
               double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;   
	               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);
                  if (dVolty < 1)                      dVolty = 1.0;

      //
      //
      //
      //
      //
	        
   	double pow2 = MathPow(dVolty, pow1);
      double len2 = MathSqrt(0.5*(length-1))*len1;
      double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

         if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;
         if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;
	
   //
   //
   //
   //
   //
      
      double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;
      double beta  = 0.45*(length-1)/(0.45*(length-1)+2);
      double alpha = MathPow(beta,pow2);

         wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);
         wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];
         wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);
         wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];
         wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]); 

   //
   //
   //
   //
   //

   return(wrk[r][4+s]);
}

double workHil[][27];
#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