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

Re: [amibroker] linear regression



PureBytes Links

Trading Reference Links

If you are looking for a home-grown version I'll paste mine below.  If 
anyone finds faults or has a better version, I'll be happy to hear about 
it.  This version is based on 'The New Technical Trader' by Chande & Kroll, 
p.21, but note that the book has a typo: in the calculations at the bottom 
of the page 'slope' should read q1/q2, NOT q1/q3.  Thanks to Tomasz for 
setting me straight re zero-based counting for the periods, and for not 
taking offence when I thought there was an error in the AB built-in version!

HHP
===========================

At 02:01 PM 11/09/2003, you wrote:
>Does anyone know how to calculate the 21 day linear regression of closing 
>prices?
>
>Thanks,
>
>
>Dan
==========================

/*     *** Linear Regression Calcs  ***

Check: Grid lines - Middle.
Check: Show dates.
Check: Logarithmic.

//===============================

***  NOTE: This algorithm uses zero-based numbering for the x-series (Period#).
Therefore on the Nth. day: x = N-1.  (For a 10-period look-back window, N runs
from 1 to 10, while x runs from 0 to 9).

This formula matches the values of the AmiBroker built-in functions
LinRegIntercept, LinRegSlope, AND LinearReg.  It adds the R-squared function.
*/
//===================================

N = Param("Number of Periods?",21,1,300,1);//No. of days in window.
N1 = N-1;//Provision for zero-based numbering of x-values.  See *** NOTE above.
x = 0;  //Initialize Day#, X-axis value.  See *** NOTE above.
y = Close;  //Y-axis value.
Sx = 0;  //Initialize running sum of Day#.
Sy = 0;  //Initialize running sum of Close.
Sx2 = 0;  //Initialize running sum of Day# squared.
Sy2 = 0;  //Initialize running sum of Close squared.
Sxy = 0;  //Initialize running sum of Day# * Close.

//==========================

do{

Sx = x + Ref(Sx,-1);  //Running sum of Day#.
Sy = y + Ref(Sy,-1);  //Running sum of Close.
Sx2 = x*x + Ref(Sx2,-1);  //Running sum of Day# squared.
Sy2 = y*y + Ref(Sy2,-1);  //Running sum of Close squared.
Sxy = x*y + Ref(Sxy,-1);  //Running sum of Day# * Close.

x++;
}while (x <= N1);//See *** NOTE above.

//======================

Q1 = (Sxy - ((Sx * Sy) / N));
Q2 = (Sx2 - ((Sx * Sx) / N));
Q3 = (Sy2 - ((Sy * Sy) / N));

slope = Q1 / Q2;
intercept = Sy/N - slope * Sx/N;
R_squared = (Q1 * Q1) / (Q2 * Q3);
MyLinReg = intercept + slope * N1;//See *** NOTE above.

//========================
SEno = Param("No. of Std. Errors",2,0.2,5,0.1);
USDBand = MyLinReg + StdErr(C,N)*SEno;
LSDBand = MyLinReg - StdErr(C,N)*SEno;

//============================

BarCol = IIf(C>=O,colorDarkOliveGreen,colorCustom16);
PlotOHLC(O,H,L,C,"",BarCol,styleCandle|styleNoLabel); GraphXSpace = 2;
Plot(MyLinReg,"MyLinReg",colorGreen,styleDots|styleNoLabel);
Plot(USDBand,"",colorGreen,styleLine|styleNoLabel);
Plot(LSDBand,"",colorGreen,styleLine|styleNoLabel);

//===============================

//  HEADING for Price Charts.
C1 = EncodeColor(colorBlack);
C2 = EncodeColor(colorDarkGrey);
C3 = WriteIf(C==Ref(C,-1), C2,
            WriteIf(C>Ref(C,-1), EncodeColor(colorGreen),
                 EncodeColor(colorRed)));
ChSgn = WriteIf(C>Ref(C,-1),"+",WriteIf(C==Ref(C,-1),"  "," "));
Ch = WriteVal(ROC(C,1),1.2) + "%";
Title = FullName() +"   [" + Name() + "]      " +
C1 + "    O:  " + C2 + WriteVal(O,1.2) +
C1 + "    H:  " + C2 + WriteVal(H,1.2) +
C1 + "    L:  " + C2 + WriteVal(L,1.2) +
C1 + "    C:  " + C2 + WriteVal(C,1.2) +
C1 + "    (" + C3 +ChSgn + Ch + C1 + ")       " +
C2 + WriteVal(DateTime(),formatDateTime) + EncodeColor(colorBrown) + "\n" +
" Linear Regression  (LinReg,  len: " + N + ", wid: " + SEno + ")";

//===============================

//Plot(LinRegSlope(C,N),"AB LinRegSlope",colorRed,styleLine);
//Plot(slope,"My Slope",colorGreen,styleLine);

//Plot(LinRegIntercept(C,N),"AB LinRegIntercept",colorRed,styleLine);
//Plot(intercept,"My Intercept",colorGreen,styleLine);

//Plot(LinearReg(C,N),"AB LinearReg",colorRed,styleLine);

//Plot(R_squared,"My R-squared",colorRed,styleLine);







------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Remanufactured Ink Cartridges & Refill Kits at MyInks.com for: HP $8-20. Epson $3-9, Canon $5-15, Lexmark $4-17. Free s/h over $50 (US & Canada).
http://www.c1tracking.com/l.asp?cid=6351
http://us.click.yahoo.com/0zJuRD/6CvGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

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