

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 SeaGreen
#property indicator_color2 Red
#property indicator_color3 CadetBlue
#property indicator_color4 Purple
#property indicator_color5 Red
#property indicator_color6 RoyalBlue
#property indicator_color7 Plum
#property indicator_color8 PaleGreen

//---- input parameters
extern int      CCIPeriod=20;
extern int      N_reg_v=21;   
extern int      N_reg_l=84;   

//---- buffers
double cci_l[];
double cci_s[];
double reg_v[];
double reg_l[];
double reg_diff_short[];
double reg_diff_long[];
double cci_s_soft[];
double cci_l_soft[];


//  double cci[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,4);
   SetIndexBuffer(0,cci_l);
   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,4);
   SetIndexBuffer(1,cci_s);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,reg_v);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,reg_l);
   SetIndexStyle(4,DRAW_LINE,EMPTY,2);
   SetIndexBuffer(4,reg_diff_short);
   SetIndexStyle(5,DRAW_LINE,EMPTY,2);
   SetIndexBuffer(5,reg_diff_long);
   SetIndexStyle(6,DRAW_HISTOGRAM,EMPTY,4);
   SetIndexBuffer(6,cci_s_soft);
   SetIndexStyle(7,DRAW_HISTOGRAM,EMPTY,4);
   SetIndexBuffer(7,cci_l_soft);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i = 0;
 
   i = Bars-CCIPeriod-1;
/*   while ( i >= 0 )
   {
      //cci[i] = iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i); 
      if (iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i)>=0)    {cci_l[i]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i);   cci_s[i]=0;}
      if (iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i)<0)     {cci_s[i]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,i);   cci_l[i]=0;}
      i--;
   }
  */ 
   
// =============================================================
// CALCOLO REGRESSIONE LINEARE VELOCE (DEFAULT 21)
  
//   int      barre=100;
   double   sumx,sumy,sumxy,sumx2,sumy2,yint,slope,r;

    
   for (int k=Bars-CCIPeriod-N_reg_v-1; k>=0; k--)
      {
   	sumx = 0;
		sumy = 0;
		sumxy = 0;
		sumx2 = 0;
		sumy2 = 0;
		for (int n = 0; n <= N_reg_v-1; n++)
		 { 
		 sumx = sumx + n;                                                     //sum(X)
		 sumy = sumy + iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n);                                            //sum(Y)
		 sumxy = sumxy + n * iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n);                                      //sum(X*Y)
		 sumx2 = sumx2 + n * n;                                               //sum(X^2)
		 sumy2 = sumy2 + iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n) * iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n);                             //sum(Y^2)
		 }
		
		slope = (N_reg_v * sumxy - sumx * sumy) / (N_reg_v * sumx2 - sumx * sumx);
		yint = (sumy - slope * sumx) / N_reg_v; // Y Intercept
		r = (N_reg_v * sumxy - sumx * sumy) / MathSqrt((N_reg_v * sumx2 - sumx * sumx) * (N_reg_v * sumy2 - sumy * sumy)); // correlation coefficient
		reg_v[k] = slope*1+yint;
		}
   
// =============================================================
// CALCOLO REGRESSIONE LINEARE LENTA (DEFAULT 84)
  
//   int      barre=100;
  // double   sumx,sumy,sumxy,sumx2,sumy2,yint,slope,r;

    
   for ( k=Bars-CCIPeriod-N_reg_l-1; k>=0; k--)
      {
   	sumx = 0;
		sumy = 0;
		sumxy = 0;
		sumx2 = 0;
		sumy2 = 0;
		for ( n = 0; n <= N_reg_l-1; n++)
		 { 
		 sumx = sumx + n;                                                     //sum(X)
		 sumy = sumy + iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n);                                            //sum(Y)
		 sumxy = sumxy + n * iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n);                                      //sum(X*Y)
		 sumx2 = sumx2 + n * n;                                               //sum(X^2)
		 sumy2 = sumy2 + iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n) * iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k+n);                             //sum(Y^2)
		 }
		
		slope = (N_reg_l * sumxy - sumx * sumy) / (N_reg_l * sumx2 - sumx * sumx);
		yint = (sumy - slope * sumx) / N_reg_l; // Y Intercept
		r = (N_reg_l * sumxy - sumx * sumy) / MathSqrt((N_reg_l * sumx2 - sumx * sumx) * (N_reg_l * sumy2 - sumy * sumy)); // correlation coefficient
		reg_l[k] = slope*1+yint;
		}
   for (k=Bars-CCIPeriod-N_reg_l-1; k>=0; k--)
      {
      if (reg_l[k]-reg_v[k]>0) 
         {
         reg_diff_short[k]=reg_l[k]-reg_v[k];
         if (reg_l[k+1]-reg_v[k+1]<=0)    reg_diff_long[k]=reg_l[k]-reg_v[k];
         else reg_diff_long[k]=EMPTY_VALUE;
         }
      else
         {
         reg_diff_long[k]=reg_l[k]-reg_v[k];
         if (reg_l[k+1]-reg_v[k+1]>=0)    reg_diff_short[k]=reg_l[k]-reg_v[k];
         else reg_diff_short[k]=EMPTY_VALUE;
         }
      }

// ===========================================================
// DEFINIZIONE COLORE CANDELE CCI

   for (k=Bars-CCIPeriod-N_reg_l-1; k>=0; k--)
      {
      if (reg_v[k]<=reg_v[k+1] && reg_v[k]<0)
         {
         if (iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k)<reg_v[k])
            {
            cci_s[k]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            cci_l[k]=EMPTY_VALUE; 
            cci_s_soft[k]=EMPTY_VALUE;
            cci_l_soft[k]=EMPTY_VALUE;
            }
         else   
            {
            cci_s[k]=EMPTY_VALUE;
            cci_l[k]=EMPTY_VALUE; 
            cci_s_soft[k]=reg_v[k]; //iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            cci_l_soft[k]=EMPTY_VALUE;
            }
         }   
      if (reg_v[k]<=reg_v[k+1] && reg_v[k]>0)      
         {
          if (iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k)>=0)
            {
            cci_s[k]=EMPTY_VALUE;
            cci_l[k]=EMPTY_VALUE; 
            cci_s_soft[k]=reg_v[k]; //iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            cci_l_soft[k]=EMPTY_VALUE;
            }
          else
            {
            cci_s_soft[k]=reg_v[k];
            cci_s[k]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            cci_l[k]=EMPTY_VALUE; 
             //EMPTY_VALUE;
            cci_l_soft[k]=EMPTY_VALUE;
            }   
         }
      if (reg_v[k]>reg_v[k+1] && reg_v[k]>=0)
         {
         if (iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k)>reg_v[k])
            {
            cci_s[k]=EMPTY_VALUE;
            cci_l[k]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            cci_s_soft[k]=EMPTY_VALUE;
            cci_l_soft[k]=reg_v[k]; //EMPTY_VALUE;
            }   
         else
            {
            cci_s[k]=EMPTY_VALUE;
            cci_l[k]=EMPTY_VALUE;
            cci_s_soft[k]=EMPTY_VALUE;
            cci_l_soft[k]=reg_v[k]; //iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            }   
         }
      if (reg_v[k]>reg_v[k+1] && reg_v[k]<0)
         {
         if (iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k)<=0)
            {
            cci_s[k]=EMPTY_VALUE;
            cci_l[k]=EMPTY_VALUE;
            cci_s_soft[k]=EMPTY_VALUE;
            cci_l_soft[k]=reg_v[k]; //iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            }   
         else
            {
            cci_s[k]=EMPTY_VALUE;
            cci_l[k]=iCCI(NULL,0,CCIPeriod,PRICE_TYPICAL,k);
            cci_s_soft[k]=EMPTY_VALUE;
            cci_l_soft[k]=reg_v[k]; //EMPTY_VALUE;
            } 
         }     
      }   
      
   
   
   return(0);
}
//+------------------------------------------------------------------+

