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

Re: [amibroker] Parallel lines for regression channels ?



PureBytes Links

Trading Reference Links


Ok, I've been trying to build the 40-day linear 
regression line (starting from today, going back 40 days) and I can't even seem 
to do that.
 
First, some basic math:
 
linear line: y = mx + b  wherem 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 !
<FONT face=Arial 
size=2> 
// Raff Regression Channels (at least the 
beginning <FONT color=#008000 
size=2>of it :)
 
<FONT 
size=2>periods = 40;
DaysBack = 0;<FONT 
size=1>
 
// Compute the number of bars in datafile, this 
section was developed by Frank Snay
RABars = 0; <FONT 
color=#008000 size=1>//initialize
TotalBars = Cum(<FONT 
color=#ff00ff>1); //how 
many bars in database
FinalBar = <FONT 
color=#0000ff>LastValue(TotalBars);<FONT color=#008000 
size=1>//number value of last bar
EndDay = FinalBar - DaysBack;<FONT color=#008000 
size=1>//for other than 0 DaysBack
StartDay = EndDay - periods+<FONT 
color=#ff00ff>1;//starting 
point for line
Master1 = IIf(TotalBars 
>= StartDay AND TotalBars <= EndDay,<FONT 
color=#ff00ff>1,0);<FONT color=#008000 
size=1>//defined period
RABars = IIf(Master1,<FONT 
color=#0000ff>Ref(RABars,-1)+<FONT 
color=#ff00ff>1,0); <FONT color=#008000 
size=1>// daily counter in defined period<FONT 
size=1>
RABarKntr = <FONT 
color=#0000ff>IIf(Master1,<FONT 
color=#0000ff>Sum(RABars,periods),0); 
//Sum of daily 
counts
<FONT color=#000000 
size=2> 
// 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;
 <FONT color=#000000 
size=2>
/*
// 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,-<FONT 
color=#ff00ff>1e10);<FONT color=#008000 
size=1>
 
// Graph the output<FONT 
size=1>
MaxGraph=2;<FONT 
color=#008000 size=1>
Graph0 = Close; Graph0Style = 1; Graph0Color 
=1;
Graph1 =LRLine; Graph1Style = <FONT 
color=#ff00ff>1;
 
//END
 
<BLOCKQUOTE 
>
----- Original Message ----- 
<DIV 
>From: 
<A title=hbahlool0542@xxxx 
href="">hmab1 
To: <A title=amibroker@xxxxxxxxxx 
href="">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,HBYour 
use of Yahoo! Groups is subject to the <A 
href="">Yahoo! Terms of Service.