[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: FW: [amibroker] Curvilinear (Parabolic) second order regression



PureBytes Links

Trading Reference Links


Here is some *.afl code that plots a fit to a quadratic equation.

I converted it from a Fortran-90 subroutine I had.  The F-90 routine
used a 3x3 matrix  -  AB doesn't allow 2-d arrays that I can tell, so
I faked the matrix with a series of variables.

There are quite a few caveats about using this routine.

The algorithm calculates small differences between large numbers.  The
F-90 routine is entirely in double precision.  I don't know if AB
computes in this mode (Tomascz???)

In other least squares computations there are a few tweaks to the
calculations to prevent things from blowing up due to the appearance
of rounding errors in the difference calculations.  I don't know where
these should be inserted in these.

Consequently you get funny results occasionally.  If you get an odd
result, it can be 'repaired' by changing the number of bars used by
1-2 bars.  This usually changes the calculations enough to fix things.

I am hopeful that someone with a better understanding of math and/or
AB will fix the code I've shown to remove these problems.  PLEASE
re-post your corrections here.

Cheers,

Reef-Break


// Regression Fit to a Quadratic Equation */
// Ed Hoopes
// April 2005
// Rev A

// Number of lookback days;
NPNT    = Param("Pers", 50, 8, 200);

X       = Cum(1);        // The x cordinate array (# of Bars);
Y       = Close;         // The y co-ordinate array (Prices);    

// Calculate some terms;

SUMX    = LastValue(Sum(X,NPNT));
SUMY    = LastValue(Sum(Y,NPNT));
SUMXY   = LastValue(Sum(X*Y,NPNT));
SUMSQX  = LastValue(Sum(X*X,NPNT));
SUMX3   = LastValue(Sum(X*X*X,NPNT));
SUMX4   = LastValue(Sum(X*X*X*X,NPNT));
SUMX2Y  = LastValue(Sum(Y*X*X,NPNT));

// Calculate some more terms;
XPNT    = NPNT;
RECIP   = 1 / XPNT;
SAVEA   = SUMSQX  - ((SUMX * SUMX) / XPNT);
SAVEC   = SUMXY   - ((SUMX * SUMY) / XPNT);

XMEAN   = SUMX / XPNT;
YMEAN   = SUMY / XPNT;

// Fake a 3x3 matrix with variables - fake a Gauss Elimination;

COEF21  = SAVEC / SAVEA ;

TEMP    = (SUMX * SUMSQX) / XPNT ;
TEMP1   = SUMX3 - TEMP;

COEF32  = (((SUMX2Y - ((SUMY   * SUMSQX) / XPNT)) / TEMP1) -  COEF21) / 
			(((SUMX4  - ((SUMSQX * SUMSQX) / XPNT)) / TEMP1) - (TEMP1 / SAVEA));

COEF22  = (SAVEC - COEF32 * (SumX3 - TEMP)) / SAVEA;

COEF12  = YMEAN - (COEF22 * XMEAN) - (COEF32 * (SUMSQX * RECIP));

ATERM   = COEF12;  
BTERM   = COEF22;
CTERM   = COEF32;

/* Force the x value to be very negative so the graph does not appear
before the lookback period */

Domain =  IIf ( X > LastValue(X) - NPNT, 1 , -1e10);   
X = X * Domain;

// Quadratic Regression Line y = a + bx + cx^2  ;

Y = ATERM + BTERM * X + CTERM * X * X;

// Plot a Bar chart; 
Graph0      = Close;
Graph0Style = styleBar;
Graph0Color = colorLightGrey;

// Plot the Quadratic Fit */
Graph1      = Y;
Graph1Style = styleLine;
Graph1Color = colorYellow;





--- In amibroker@xxxxxxxxxxxxxxx, "MPLM (Intel EPSD) Razvan Andrei"
<randrei@xxxx> wrote:
> Hi,
> does anyone coded in afl the second order regression?
> 
> 
>                         Andrei Razvan
>                         Product Manager
>                               ASBISC Enterprises Ltd
>                         Limassol  - Cyprus
> 
>                         ra@xxxx tel:
>                               fax: +0744 303088
>                               +4021 2333844
> 
> 
> 
> 
> 
> 
> 
> 
> 
> Best regards,
> Razvan Andrei
> Product Manager
> ASBIS Enterprises Ltd.
> Please reply to: ra@xxxx
> mobile:+40744-303088;
> www.asbis.com
> 
> 
> 
> -----Mesaj original-----
> De la: MPLM (Intel EPSD) Razvan Andrei [mailto:randrei@x...]
> Trimis: 8 aprilie 2005 21:31
> Catre: amibroker@xxxxxxxxxxxxxxx
> Subiect: [amibroker] Curvilinear (Parabolic) second order regression
> 
> 
> Hi,
> does anyone coded for AB the second order regression? some examples
are in
> the attached pictures (with trading channels).
> 
> 
> Thanks,
> Razvan Andrei
> 
> 
> 
> 
> Please note that this group is for discussion between users only.
> 
> To get support from AmiBroker please send an e-mail directly to
> SUPPORT {at} amibroker.com
> 
> For other support material please check also:
> http://www.amibroker.com/support.html
> 
> 
> 
> 
> 
>
----------------------------------------------------------------------------
> ----
> Yahoo! Groups Links
> 
>   a.. To visit your group on the web, go to:
>   http://groups.yahoo.com/group/amibroker/
> 
>   b.. To unsubscribe from this group, send an email to:
>   amibroker-unsubscribe@xxxxxxxxxxxxxxx
> 
>   c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.





------------------------ Yahoo! Groups Sponsor --------------------~--> 
Has someone you know been affected by illness or disease?
Network for Good is THE place to support health awareness efforts!
http://us.click.yahoo.com/Rcy2bD/UOnJAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/