PureBytes Links
Trading Reference Links
|
HB
----- Original Message -----
From: HB
To: amibroker@xxxxxxxxxxxxxxx
Sent: Wednesday, July 24, 2002 1:36 AM
Subject: Re: [amibroker] Parallel lines for regression channels ?
Ok, I've been trying to build the 40-day linear regression line (startingfrom today, going back 40 days) and I can't even seem to do that.
First, some basic math:
linear line: y = mx + b where m is the slope, b is the intercept, and x is the bar #
LRline = Slope * (bar #) + Intercept
Note: Any time I write "Summation", it is the sum of the variable for i = 1 to N, where N is the period
Slope = [ N*(Summation(X*Y)) - Summation(X) * Summation(Y) ] / [N * Summation (X^2) - (Summation(X))^2]
OR
Slope = [Summation ((X-MeanX)*(Y-MeanY))] / [Summation((X-MeanX)^2)]
Intercept = [Summation(Y) - Slope * Summation(X)] / N
OR
Intercept = MeanY - Slope * MeanX
Based on that, here's the AFL I've implemented so far. Someone please correct what I'm doing wrong !
// Raff Regression Channels (at least the beginning of it :)
periods = 40;
DaysBack = 0;
// Compute the number of bars in datafile, this section was developed by Frank Snay
RABars = 0; //initialize
TotalBars = Cum(1); //how many bars in database
FinalBar = LastValue(TotalBars);//number value of last bar
EndDay = FinalBar - DaysBack;//for other than 0 DaysBack
StartDay = EndDay - periods+1;//starting point for line
Master1 = IIf(TotalBars >= StartDay AND TotalBars <= EndDay,1,0);//defined period
RABars = IIf(Master1,Ref(RABars,-1)+1,0); // daily counter in defined period
RABarKntr = IIf(Master1,Sum(RABars,periods),0); //Sum of daily counts
// Calculate LRLine formula components
SumX = Sum (RABars, periods);
SumY = Sum(C, periods);
SumXY = Sum (C * RABars, periods) ;
SumXX = Sum (RABars * RABars, periods);
n = periods;
Slope = (n * SumXY - SumX * SumY) / (n * SumXX - SumX * SumX );
Intercept = (SumY - Slope * SumX) / n;
/*
// Other calculation method which yields a different result ?!
SumX = Sum (RABars, periods);
SumY = Sum(C, periods);
SumXY = Sum (C * RABars, periods) ;
SumXX = Sum (RABars * RABars, periods);
MeanX = SumX/periods;
MeanY = SumY/periods;
Slope = Sum((RABars-MeanX)*(C-MeanY), periods);
Intercept = MeanY - slope * MeanX;
*/
// Linear Regression Line
LRLine = Slope * RABars + Intercept;
LRLine = IIf(RABarKntr >= 1, LRLine,-1e10);
// Graph the output
MaxGraph=2;
Graph0 = Close; Graph0Style = 1; Graph0Color =1;
Graph1 =LRLine; Graph1Style = 1;
//END
----- Original Message -----
From: hmab1
To: amibroker@xxxxxxxxxxxxxxx
Sent: Tuesday, July 23, 2002 7:07 PM
Subject: [amibroker] Parallel lines for regression channels ?
Hello,
Is it possible in AFL to contruct parallel lines on either side of a
linear regression line ?
I'm trying to build a linear regression channels indicator.
The central line would be the linear regression line. The upper and
lower channel lines are placed equidistant from the center line and
parallel to it. The distance between the central line and the upper
line would be equal to greatest distance between the central line and
the highest value. The distance between the central line and the
lower line would be equal to greatest distance between the central
line and the lowest value.
I've been racking my brain, reading through the various AFL
functions, and searching this group !! Is this even possible ?
Inputs would be the start date, end date, and variable array (e.g.
Close).
Thanks,
HB
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
Yahoo! Groups Sponsor
ADVERTISEMENT
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
------=_NextPart_001_01EC_01C232B5.4E7F4C30
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2716.2200" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>To see what the channels are supposed to look like,
see the attached spreadsheet.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>HB</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
HB
</DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=amibroker@xxxxxxxxxx
href="mailto:amibroker@xxxxxxxxxxxxxxx">amibroker@xxxxxxxxxxxxxxx</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Wednesday, July 24, 2002 1:36
AM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> Re: [amibroker] Parallel lines
for regression channels ?</DIV>
<DIV><BR></DIV>
<DIV><FONT face=Arial size=2>Ok, I've been trying to build the 40-daylinear
regression line (starting from today, going back 40 days) and I can't even
seem to do that.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>First, some basic math:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>linear line: y = mx + b where m is the
slope, b is the intercept, and x is the bar #</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>LRline = Slope * (bar #) + Intercept</FONT></DIV>
<DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Note: Any time I write "Summation", it is the sum
of the variable for i = 1 to N, where N is the period</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Slope = [ N*(Summation(X*Y)) - Summation(X) *
Summation(Y) ] / [N * Summation (X^2) - (Summation(X))^2]</FONT></DIV>
<DIV><FONT face=Arial size=2>OR</FONT></DIV>
<DIV><FONT face=Arial size=2>Slope = [Summation ((X-MeanX)*(Y-MeanY))] /
[Summation((X-MeanX)^2)]</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Intercept = [Summation(Y) - Slope * Summation(X)]
/ N</FONT></DIV>
<DIV><FONT face=Arial size=2>OR</FONT></DIV>
<DIV><FONT face=Arial size=2>Intercept = MeanY - Slope * MeanX</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV></DIV>
<DIV><FONT face=Arial size=2>Based on that, here's the AFL I've implemented so
far. Someone please correct what I'm doing wrong !</FONT></DIV>
<DIV><FONT face=Arial><FONT size=2><FONT face=Arial
size=2></FONT></FONT></FONT> </DIV><FONT face=Arial size=2><FONT size=1>
<DIV><FONT color=#008000 size=2>// Raff Regression Channels (at leastthe
beginning </FONT></FONT><FONT face="Courier New" size=1><FONT color=#008000
size=2>of it :)</FONT></DIV></FONT><FONT color=#008000 size=1>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV></FONT></FONT><FONT face=Arial size=2><FONT size=1></FONT><FONT
size=2>periods = <FONT color=#ff00ff>40</FONT>;</FONT><FONT size=1></DIV>
<DIV></FONT><FONT size=2>DaysBack = <FONT color=#ff00ff>0</FONT>;</FONT><FONT
size=1></DIV></FONT><FONT color=#008000 size=1>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2>// Compute the number of bars in datafile,
this section was developed by Frank Snay</FONT></DIV></FONT><FONT size=1>
<DIV></FONT><FONT size=2>RABars = <FONT color=#ff00ff>0</FONT>; </FONT><FONT
color=#008000 size=1><FONT size=2>//initialize</FONT></DIV></FONT><FONT
size=1>
<DIV></FONT><FONT size=2>TotalBars = <FONT color=#0000ff>Cum</FONT>(<FONT
color=#ff00ff>1</FONT>); </FONT><FONT color=#008000 size=1><FONT size=2>//how
many bars in database</FONT></DIV></FONT><FONT size=1>
<DIV></FONT><FONT size=2>FinalBar = <FONT
color=#0000ff>LastValue</FONT>(TotalBars);</FONT><FONT color=#008000
size=1><FONT size=2>//number value of last bar</FONT></DIV></FONT><FONT
size=1>
<DIV></FONT><FONT size=2>EndDay = FinalBar - DaysBack;</FONT><FONT
color=#008000 size=1><FONT size=2>//for other than 0
DaysBack</FONT></DIV></FONT><FONT size=1>
<DIV></FONT><FONT size=2>StartDay = EndDay - periods+<FONT
color=#ff00ff>1</FONT>;</FONT><FONT color=#008000 size=1><FONT
size=2>//starting point for line</FONT></DIV></FONT><FONT size=1>
<DIV></FONT><FONT size=2>Master1 = <FONT color=#0000ff>IIf</FONT>(TotalBars
>= StartDay <B>AND</B> TotalBars <= EndDay,<FONT
color=#ff00ff>1</FONT>,<FONT color=#ff00ff>0</FONT>);</FONT><FONT
color=#008000 size=1><FONT size=2>//defined period</FONT></DIV></FONT><FONT
size=1>
<DIV></FONT><FONT size=2>RABars = <FONT color=#0000ff>IIf</FONT>(Master1,<FONT
color=#0000ff>Ref</FONT>(RABars,-<FONT color=#ff00ff>1</FONT>)+<FONT
color=#ff00ff>1</FONT>,<FONT color=#ff00ff>0</FONT>); </FONT><FONT
color=#008000 size=1><FONT size=2>// daily counter in defined
period</FONT></DIV></FONT><FONT size=1>
<DIV></FONT><FONT size=2>RABarKntr = <FONT
color=#0000ff>IIf</FONT>(Master1,<FONT
color=#0000ff>Sum</FONT>(RABars,periods),<FONT color=#ff00ff>0</FONT>);
</FONT><FONT color=#008000 size=1><FONT size=2>//Sum of daily
counts</FONT></DIV></FONT><FONT size=1></FONT><FONT color=#008000 size=1>
<DIV><FONT size=2><FONT color=#000000><FONT color=#000000
size=2></FONT></FONT></FONT> </DIV>
<DIV><FONT color=#000000 size=2>// Calculate LRLine formula
components</FONT></DIV><FONT color=#000000 size=2><FONT size=1>
<DIV></FONT><FONT size=2>SumX = <FONT color=#0000ff>Sum</FONT> (RABars,
periods);</FONT><FONT size=1></DIV>
<DIV></FONT><FONT size=2>SumY = <FONT color=#0000ff>Sum</FONT>(<B>C</B>,
periods);</FONT><FONT size=1></DIV>
<DIV></FONT><FONT size=2>SumXY = <FONT color=#0000ff>Sum</FONT> (<B>C</B> *
RABars, periods) ;</FONT><FONT size=1></DIV>
<DIV></FONT><FONT size=2>SumXX = <FONT color=#0000ff>Sum</FONT> (RABars *
RABars, periods);</FONT><FONT size=1></DIV></FONT></FONT>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2>n = periods;</FONT></DIV>
<DIV>
<DIV><FONT size=2></FONT> </DIV></DIV></FONT><FONT color=#008000size=1>
<DIV><FONT color=#000000 size=2>Slope = (n * SumXY - SumX * SumY) /(n * SumXX
- SumX * SumX );</FONT></DIV>
<DIV><FONT color=#000000 size=2>Intercept = (SumY - Slope * SumX) /
n;</FONT></DIV></FONT><FONT size=1></FONT><FONT color=#008000 size=1>
<DIV><FONT color=#000000 size=2></FONT> </DIV><FONT color=#000000
size=2><FONT size=1>
<DIV><FONT color=#008000 size=2>/*</FONT></DIV>
<DIV><FONT color=#008000 size=2>// Other calculation method which yields a
different result ?!</FONT></DIV>
<DIV><FONT color=#008000 size=2>SumX = Sum (RABars, periods);</FONT></DIV>
<DIV><FONT color=#008000 size=2>SumY = Sum(C, periods);</FONT></DIV>
<DIV><FONT color=#008000 size=2>SumXY = Sum (C * RABars, periods)
;</FONT></DIV>
<DIV><FONT color=#008000 size=2>SumXX = Sum (RABars * RABars,
periods);</FONT></DIV>
<DIV><FONT color=#008000 size=2>MeanX = SumX/periods;</FONT></DIV>
<DIV><FONT color=#008000 size=2>MeanY = SumY/periods;</FONT></DIV>
<DIV><FONT color=#008000 size=2></FONT> </DIV>
<DIV><FONT color=#008000 size=2>Slope = Sum((RABars-MeanX)*(C-MeanY),
periods);</FONT></DIV>
<DIV><FONT color=#008000 size=2>Intercept = MeanY - slope *
MeanX;</FONT></DIV>
<DIV><FONT color=#008000 size=2>*/</FONT></DIV></FONT></FONT>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2>// Linear Regression
Line</FONT></DIV></FONT><FONT size=1>
<DIV><FONT size=2>LRLine = Slope * RABars + Intercept; </FONT></DIV>
<DIV></FONT><FONT size=2>LRLine = <FONT color=#0000ff>IIf</FONT>(RABarKntr
>= <FONT color=#ff00ff>1</FONT>, LRLine,-<FONT
color=#ff00ff>1e10</FONT>);</FONT><FONT size=1></DIV></FONT><FONT
color=#008000 size=1>
<DIV><FONT color=#000000 size=2></FONT> </DIV>
<DIV><FONT color=#000000 size=2>// Graph the output</FONT></DIV></FONT><FONT
size=1>
<DIV></FONT><FONT size=2>MaxGraph=2;</FONT><FONT size=1></DIV></FONT><FONT
color=#008000 size=1>
<DIV><FONT color=#000000 size=2>Graph0 = Close; Graph0Style = 1; Graph0Color
=1;</FONT></DIV></FONT><FONT size=1>
<DIV></FONT><FONT size=2>Graph1 =LRLine; Graph1Style = <FONT
color=#ff00ff>1</FONT>;</FONT></DIV></FONT>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>//END</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=hbahlool0542@xxxx
href="mailto:hbahlool0542@xxxx">hmab1</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=amibroker@xxxxxxxxxxxx
href="mailto:amibroker@xxxxxxxxxxxxxxx">amibroker@xxxxxxxxxxxxxxx</A></DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Tuesday, July 23, 2002 7:07
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> [amibroker] Parallel lines for
regression channels ?</DIV>
<DIV><BR></DIV><TT><BR>Hello,<BR><BR>Is it possible in AFL to contruct
parallel lines on either side of a <BR>linear regression line ?<BR><BR>I'm
trying to build a linear regression channels indicator.<BR><BR>The central
line would be the linear regression line. The upper and <BR>lower channel
lines are placed equidistant from the center line and <BR>parallel to it.
The distance between the central line and the upper <BR>line would be equal
to greatest distance between the central line and <BR>the highest
value. The distance between the central line and the <BR>lower line
would be equal to greatest distance between the central <BR>line and the
lowest value.<BR><BR>I've been racking my brain, reading through the various
AFL <BR>functions, and searching this group !! Is this even possible
?<BR><BR>Inputs would be the start date, end date, and variable array (e.g.
<BR>Close).<BR><BR>Thanks,<BR>HB<BR><BR><BR></TT><BR><BR><TT>Your use of
Yahoo! Groups is subject to the <A
href="http://docs.yahoo.com/info/terms/">Yahoo! Terms of Service</A>.</TT>
<BR></BLOCKQUOTE><BR><BR><TT>Your
use of Yahoo! Groups is subject to the <A
href="http://docs.yahoo.com/info/terms/">Yahoo! Terms of Service</A>.</TT>
<BR></BLOCKQUOTE></BODY></HTML>
------=_NextPart_001_01EC_01C232B5.4E7F4C30--
Attachment:
xls00009.xls
Attachment:
Description: "Description: MS-Excel spreadsheet"
|