This is a Linear Reg fit using the equations from "Standard Math Tables" 
published by CRC.  The Y axis uses the array 'Prc', the X axis uses the array 
'X'.  You can put any values you want into those two arrays.

To get it to work you need to 'Insert' it into a Chart Window
    then click on the last bar. 
The answers come out in the Interpretation window. 
This was coded for comfort - not for speed.
Watch out for line wraps


//Linear Regression Fit  with Standard Error function;
// Ed Hoopes 
// April 2008
// Rev A -   Started with QFit


//  *******************************  Linear Regression Fit   
****************************;

Pers     =  Param("Periods", 20, 5, 200, 1 );
LookBack =  Param("Look Back", 0, 0, 500, 1 );

SetBarsRequired( 100 );

//Prc      =  (Open + High + Low + Close ) / 4 ;
Prc      =  Close;
 
//Initialize variables;
        SUMX  = 0.00;  SUMY   = 0.00; 
        SUMXY = 0.00;  SUMX2  = 0.00;    
//      X[0]  = 0.00;

for (i = 0; i < BarCount; i++ )
{
                X[i] = 0;
}

for (j = BarCount - 1 - Pers; j < BarCount; j++ )
{
                X[j] = X[j-1] + 1;
}

//Calculate SUMS;

        SumX   =  Sum(X,   Pers );
        SumY   =  Sum(Prc, Pers );
        SumX2  =  Sum(X*X, Pers );
        SumXY  =  Sum(X*Prc, Pers );


// Calculate the Slope;
SlopeNumerator     = Pers * SumXY - SumX * SumY ;
SlopeDenominator   = Pers * SumX2 - SumX * SumX ;
Slope              = SlopeNumerator / SlopeDenominator ; 

// Calculate the Intercept;
InterceptTerm1     = SumY / Pers ;
InterceptTerm2     = Slope * SumX / Pers;
Intercept          = InterceptTerm1 - InterceptTerm2;

// Calculate the Standard Error;

        SumSE          =  Sum((Prc - (intercept + Slope * X))^2, Pers );
        DenomSE        =  Pers - 2;
        StErr          =  sqrt(SumSE / DenomSE );

printf("Slope       =  " + Slope     + "\n" );
printf("Intercept =  "   + Intercept + "\n" );
printf("StdErr      =  " + StErr     + "\n" );

Plot(Slope, "Prc", colorYellow, styleLine + styleThick );




--- In [email protected], "Mike" <sfclimb...@...> wrote:
>
> I haven't looked into either myself, but the two most common answers to 
> alternative array manipulations are:
> 
> Osaka DLL http://www.amibroker.org/3rdparty/
> R Plugin http://www.amibroker.com/members/
> 
> Of course, you could always just write your own function:
> 
> http://www.amibroker.com/guide/a_userfunctions.html
> 
> Mike
> 
> --- In [email protected], "brucet30" <brucet30@> wrote:
> >
> > I am looking for some AFL code that would perform a linear regression on 
> > two arrays (x, y) and return the intercept and slope for this data set. THe 
> > only funcitons I have been able to find so far in the AFL library and 
> > elsewhere assume the x-axis is time and the y-axis is price of some other 
> > indicator series. I need something more general, that could work with any 
> > (X,y) data set. Any help on this would be very much appreciated.
> > Thanks
> >
>


Reply via email to