static arrayofdoubles Spud2SBuffer; // This line of code must stay here.
static arrayofdoubles Spud2MaBuffer; // This line of code must stay here.
static arrayofdoubles Spud2MaxBuffer; // This line of code must stay here.
double Lreg(int,int);

double Lreg(int st0, int st1)
{ 
  double    Sx=0,Sxx=0,Sxy=0,Sy;
  double    Beta,Alfa,x,y,c,value,rv;
  int       i,n;

  rv = 0.0;
  n = st1 - st0 + 1 ;                       //int n=m_pos[1]-m_pos[0]+1;
  Sy  =Spud2SBuffer[st0];
  Sx  = 0.0;                                // double sumx=0.0;
  Sxy = 0.0;                                // double sumxy=0.0;
  Sxx = 0.0;                                // double sumx2=0.0;
  for (i=1;i<n;i++)                         //for(i=1; i<n; i++)
  {  
     x   = i;   
     y   = Spud2SBuffer[st0+i];
     Sx  = Sx + x;                          //sumx+  = i;
     Sy  = Sy + y;                          //sumy+  = value
     Sxx = Sxx + (x* x);                    //sumx2+ = i*i
     Sxy = Sxy + (x* y);                    //sumxy+ = value*i;
  }
  c    = Sxx*n - Sx * Sx;                   //c=sumx2*n-sumx*sumx;
  if (c==0.0) return 0;                     //if(c==0.0) return;
  Beta = (n*Sxy-Sx*Sy)/c;                   //b=(sumxy*n-sumx*sumy)/c;
  Alfa = (Sy-Beta*Sx)/n;                    //a=(sumy-sumx*b)/n;
  rv = Alfa;       
   return(rv);
}


char* Spud2 (char* period,char* curtf)
{
    static char periodd[1000];
    strncpy(periodd, period, 1000);
    int intperiod=atoi(periodd);
	static char curtff[1000];
    strncpy(curtff, curtf, 1000);
    int intcurtf=atoi(curtff);
    
    int       Per      = 15;
    int       Pk       = 5;
    int       Pd       = 3;
    int       Ps       = 3;
    int       MaxBar   = intperiod;
    int awal=1;
    
    // Put your indicator C++ code here
//        if (Per<5 || MathMod(Per,5)>0 || Bars<500) // ||
       // Period()>Per) 
  //     return;
       //Print (DoubleToString((Period()-5)*60));
   
   int       counted_bars,i,j,k,l,m,limit;               // Number bars want to be calculated
   double    sumlo,sumhi,loes0,hies0,clos1,factor;                    // different beetwen Asking periode & M5             
   double  h0,h1;          
   limit = intperiod;          
   if (awal)
   {
      limit = MaxBar;
      awal = 0;
   }              
   factor = Per/5;
   for (i=limit-1;i>=0;i--)                          // All bar loop
   {  //Print(" ******************** i : ",i);
      sumlo = 0.0;sumhi=0.0;
      h0 = itimeb(intcurtf,i)-(Per * (Ps-1)*60);
      for (j=Ps-1;j>=0;j--)                          // "slowing" loop
      {  
         m    = ibarshift(intcurtf,h0,intperiod);
         clos1=  iclose(intcurtf,m); 
         //Print("time to seek h0 ",TimeToStr(h0,TIME_MINUTES),
         //      " ",TimeToStr(iTime(NULL,0,m),TIME_MINUTES),
         //      " Close 1 : ",clos1
         //      );
         // Collecting Highest + Lowest in pK , M5 chart               
         h1   =  itimeb(intcurtf,m)+0; //(Period()-5)*60;
         k    =  ibarshift(intcurtf,h1,intperiod);
         //Print("in M5 h1 ",TimeToStr(h1,TIME_MINUTES),
         //      " Close  : ",iClose(NULL,PERIOD_M5,k)
         //      );
         
         hies0=  ihigh(intcurtf,ihighest(intcurtf,MODE_HIGH,Pk*factor,k));
         loes0=  ilow (intcurtf,ilowest (intcurtf,MODE_LOW ,Pk*factor,k));
         //Print(" Pk*factor ",Pk*factor,
         //      " -> Pk Hi : ",hies0," lo : ",loes0);
         
         sumlo+= clos1-loes0;
         sumhi+= hies0-loes0;         
            h0+= Per * 60 ;

      }
      if (sumhi<=0)  Spud2SBuffer[i]= 100.0;
      else
      {
         Spud2SBuffer[i] = sumlo/sumhi*100;
      }
      if (limit-i>Pd) Spud2MaBuffer[i] = Lreg(i,i+Pd-1);//
      imaonarray(Spud2SBuffer,0,Pd,i,MODE_SMA,0);
      
      Spud2MaxBuffer[i] =100;
   }
return 0;
}
