PureBytes Links
Trading Reference Links
|
Several list members requested that I adapt the Linear Regression
Channel indicator I posted a while ago to work for TS2k, so here it is.
This version also gives you more control over the color of a channel --
it now matches the color of the second "R".
For example, if you want up channels to be green, and down channels to
be red, then place the first "R" as your anchor (any color). If the
first trend is up, the place the second "R" in green. You'll see a green
channel (after clicking "Status" twice). Then place the third "R" in red
for the down channel, and you'll get a red channel.
Also, since the indicator uses Auto-Detect for the max bars back, be
sure to have sufficient bars to the left of your first point. Otherwise,
the channel won't show up.
See the "Usage" instructions below for more information.
As usual, I'll leave custom changes as an exercise for the student!
/Greg
**************************************************************
{ Indicator: Linear Regrsn Chanl2 }
input: price(c), labTL("R"), thickTL(0);
{ ======================================
This is my implementation of Linear Regression Channels. You are
welcome to use this code freely. You may redistribute it provided
you include this comment block, plus a description of any changes
you make.
If you extend it, or adapt it to other uses, I'd appreciate a look
at what you've done. Thanks.
Gregory Wood
SwissFranc@xxxxxxxx
01/26/97 v1.0 - Initial distribution
07/24/99 v1.1 - Adapt to TS2k, channel color depends on text color
========================================}
{
USAGE
Use the text tool to indicate the beginning and end of the
channel(s) you want. The default uses "R"s (upper case, no
quotes). Then to show the channel(s), go to Format Analysis
Techniques, select this indicator, and press the "Status"
button two times (this is not a double-click, really press
it two times).
The channel is based on Price, which defaults to Close, but
you can change it to High or Low.
You can set the thickness of the channels.
The color of the channel is the same as the color of the second
"R" in a pair.
You can show a series of channels by putting more "R"s on the
chart. Each pair of "R"s will get its own channel.
If you want separate channels, you can add the indicator multiple
times. Be sure to give each one a different labTL value.
}
array: dd[20](0),tt[20](0),vv[20](0),bb[20](0),cc[20](0);
var: iMax(20), ix(0), hh(0), ll(0), hhbb(0), llbb(0);
var: pvv1(0), pbb1(0), pvv2(0), pbb2(0), x1(0), x2(0), y1(0), y2(0);
var: ii(0), jj(0), kk(0), hval(0), lval(0), ij(-1);
var: handl(0), looper(25);
if currentbar = 1 then begin { examine all the text strings and save
some info about the ones we recognize }
ix = 0;
looper = 25;
handl = text_getfirst(0);
while handl >= 0 and looper > 0 begin
if text_getstring(handl) = labTL then begin { save the item's
date, time, value and handle }
if ix < iMax - 1 then begin
tt[ix] = text_gettime(handl);
dd[ix] = text_getdate(handl);
vv[ix] = text_getvalue(handl);
cc[ix] = text_getcolor(handl);
ix = ix + 1;
end;
end;
handl = text_getnext(handl,2); { IMPORTANT -- infinite loop if
this is missing! }
looper = looper - 1; { safety, just in case I introduce a bug }
end;
ij = -1;
end;
if ix > 0 then
for ii = 0 to ix - 1 begin { check each point }
if time = tt[ii] and date = dd[ii] then begin { we've found a
selected point }
bb[ii] = currentbar; { remember where we found it }
plot1(price,""); { show the user which point we used for the
calculations }
if ij > -1 then begin { need at least 1 point }
{ The regression line passes through (x1,y1) and (x2,y2) }
x1 = bb[ii];
x2 = bb[ij];
y1 = LinearRegValue(price, x1-x2 ,0);
y2 = LinearRegValue(price, x1-x2, x1-x2);
{ Draw the regression line }
handl = TL_New(date[currentbar-x2],time[currentbar-x2],y2,
date[currentbar-x1],time[currentbar-x1],y1 );
TL_SetExtRight(handl, true);
TL_SetExtLeft(handl, false);
TL_SetSize(handl, thickTL);
TL_SetColor(handl, cc[ii]);
{ find the max excursion on each side of the line }
hval = 0;
lval = 0;
if x2 < x1 then for kk = x2 to x1 begin
value1 = pnt2line(x1,y1,x2,y2, barnumber[currentbar-kk],
high[currentbar-kk]);
if value1 > 0 and hval < value1 then begin
hval = value1;
hh = h[currentbar-kk];
hhbb = kk;
end;
value2 = pnt2line(x1,y1,x2,y2, barnumber[currentbar-kk],
low[currentbar-kk]);
if value2 < 0 and lval > value2 then begin
lval = value2;
ll = l[currentbar-kk];
llbb = kk;
end;
end;
{ Now draw the channel lines }
plot2[currentbar-hhbb](hh,"");
pvv1 = TLValue(ll,llbb, ll-(y2-y1), llbb-(x2-x1), x1);
pvv2 = TLValue(ll,llbb, ll-(y2-y1), llbb-(x2-x1), x2);
handl = TL_New(date[currentbar-x2],time[currentbar-x2],pvv2,
date[currentbar-x1], time[currentbar-x1],pvv1);
TL_SetExtRight(handl, true);
TL_SetExtLeft(handl, false);
TL_SetSize(handl, thickTL);
TL_SetColor(handl, cc[ii]);
plot3[currentbar-llbb](ll,"");
pvv1 = TLValue(hh,hhbb, hh-(y2-y1), hhbb-(x2-x1), x1);
pvv2 = TLValue(hh,hhbb, hh-(y2-y1), hhbb-(x2-x1), x2);
handl = TL_New(date[currentbar-x2],time[currentbar-x2],pvv2,
date[currentbar-x1], time[currentbar-x1],pvv1);
TL_SetExtRight(handl, true);
TL_SetExtLeft(handl, false);
TL_SetSize(handl, thickTL);
TL_SetColor(handl, cc[ii]);
end;
ij = ii; { save for next iteration }
end;
end;
**************************************************************
{ Function: pnt2line }
input: x1(numericsimple), y1(numericsimple),
x2(numericsimple), y2(numericsimple),
x(numericsimple), y(numericsimple);
{ returns the distance from (x,y) to the line formed by (x1,y1) (x2, y2)
}
var: qq(0), m1(0), m2(0);
if x-x2 <> 0 then begin
m1 = (y1-y2) / (x1-x2);
m2 = (y-y2) / (x-x2);
qq = SquareRoot(Square(y-y2)+Square(x-x2));
value1 = qq * sine( arctangent( (m2-m1) / (1 + m1*m2)));
end else
value1 = 0;
pnt2line = value1;
|