//+------------------------------------------------------------------+
//|                                              PolyVelocity_v1.mq4 |
//|                                Copyright © 2007, TrendLaboratory |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory"
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Yellow
#property indicator_width1  2
#property indicator_color2  LightBlue
#property indicator_width2  1
#property indicator_level1  0
//---- external variables
extern int Price     =   0;
extern int PreSmooth =   1;
extern int Length    =  14;
extern int Signal    =   5;
extern int MAMode    =   1;
//---- indicator buffers
double PolyVel[];
double SigVel[];
//---- global variables
double data[];

//+---------------------------------------------------------------------------+
//| Custom indicator initialization function                                  |
//+---------------------------------------------------------------------------+
int init()
{
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,PolyVel);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,SigVel);
   
//---- name for DataWindow and indicator subwindow label
   string short_name="PolyVelocity ("+Price+","+Length+","+Signal+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"PolyVelocity");
   SetIndexLabel(1,"Signal");
   
   SetIndexDrawBegin(0,2*Length);
   SetIndexDrawBegin(1,2*Length);
   
   ArrayResize(data, Length);

}
//+---------------------------------------------------------------------------+
//| Custom indicator deinitialization function                                |
//+---------------------------------------------------------------------------+
int deinit()
{

   return(0);
}

double _PolyVel(double price[],int shift)
{
   int    i, N;
   double AvgX, AvgY, AvgXX, X2, SXX, SXY, SX, SY, tmp;
   double SYY, SXX2, SX2X2, SYX2, A, B, C, MaxLength = 100;
   double Y[100];

   N = Length;
  
   if(N <= 1) double Vel = 0;
   else
   {   
   SXX = 0; SXY = 0; SYY = 0; SXX2 = 0; SX2X2 = 0; SYX2 = 0; SY = 0; SX = 0;
      for(i = 1; i <= N; i++)
      {
      Y[i] = price[i-1];
      X2 = i * i;
      SX += i;
      SY += Y[i]; 
      SXX  += i * i;
      SXY  += i * Y[i];
      SYY  += Y[i] * Y[i];
      SXX2  += i * X2;
      SX2X2 += X2 * X2;
      SYX2  += Y[i] * X2;
      }
   
   AvgX = 0.5*(N+1); 
   AvgY = SY/N;
   AvgXX = SXX/N;
   
   double B22 = SXX - AvgX*SX; //N*(N+1)*(N-1)/12.0;
   double B23 = SXX2 - AvgX*SXX;
   double B24 = SXY - AvgX*SY;
   double B33 = SX2X2 - AvgXX*SXX;
   double B34 = SYX2 - AvgXX*SY;
       
   tmp = B22*B33 - B23*B23;
   
   if (tmp != 0) C = ( B22*B34 - B23*B24 )/tmp;
   if (B22 != 0) B = ( B24 - B23*C ) / B22;
   
   A = AvgY - B * AvgX - C * AvgXX;

   int bar = shift+1; 
   Vel = B + 2.0 * C * bar;
   }
    
   return(Vel);
}
//+------------------------------------------------------------------+
//| PolyVelocity_v1                                                  |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1; 
   if ( counted_bars < 1 ) 
   for(i=1;i<Length;i++) {PolyVel[Bars-i]=0; SigVel[Bars-i]=0;}    
     
         
   for(shift=limit;shift>=0;shift--) 
   {	
      for(i=0;i<=Length-1;i++) 
      data[i] = iMA(NULL,0,PreSmooth,0,3,Price,i+shift);
            
   PolyVel[shift] = -_PolyVel(data,0);
   }
   
   for(shift=limit;shift>=0;shift--) 
   SigVel[shift] = iMAOnArray(PolyVel,0,Signal,0,MAMode,shift);	

   return(0);
}
//+---------------------------------------------------------------------------+