Navigation´╝ÜHome > Content >

correlations_-_OsMa_to_price.mq4

Time: 2010-11-22 | Download file:correlations_-_OsMa_to_price.mq4

//+------------------------------------------------------------------+
//|              correlations - OsMa to price.mq4 |
//|                          jaguar1637 in the same spirit of mladen |
//|                                                                  |
//| Pearson correlation of OsMa to price          |
//+------------------------------------------------------------------+
#property copyright "jaguar1637"
#property link      "http://beathespread.com"

#property indicator_separate_window
#property indicator_buffers  1
#property indicator_color1   Crimson
#property indicator_width1   2
#property indicator_maximum  1.1
#property indicator_minimum -1.1
#property indicator_level1   0
#property indicator_level2   0.5
#property indicator_level3   -0.5
#property indicator_levelcolor DimGray

extern int  CorrelationPeriod    =  8;
extern int  Price                = PRICE_CLOSE;
extern int  PriceSmoothPeriod    =  0;
extern int  PriceSmoothPhase     =  0;
extern int  PosCorrSmoothPeriod  =  0;
extern int  PosCorrSmoothPhase   =  0;

extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;

double corr[];
double OM[];
double price[];

int init()
{
   IndicatorBuffers(3);
   SetIndexBuffer(0,corr);
   SetIndexBuffer(1,OM);
   SetIndexBuffer(2,price);
   return(0);
}

int start()
{
   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--)
   { 
      OM[i] = iCustom(NULL,0,"OsMa",FastEMA,SlowEMA,SignalSMA,1,i);
      price[i] = iSmooth(iMA(NULL,0,1,0,MODE_SMA,Price,i),PriceSmoothPeriod,PriceSmoothPhase,i, 0);
      corr[i]  = iSmooth(iCorrelation(OM,price,CorrelationPeriod,i),PosCorrSmoothPeriod,PosCorrSmoothPhase,i,10);
   }
   return(0);
}


double iCorrelation(double& first[], double& second[], int length, int i)
{
   double sx  = 0;
   double sy  = 0;
   double sxy = 0;
   double sx2 = 0;
   double sy2 = 0;
            
   for (int k=0; k 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]);
}

Recommend