PureBytes Links
Trading Reference Links
|
Below is the .ela code (incl function) for a very clever Linear Regression
Channel indicator (very similar to Advanced Get's channels).
This was written for TS4 several years ago, but for some reason it will not
work with TS2000.
Perhaps someone can figure out why?
Thank you.
{Indicator: Linear Regression Channel
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 color and thickness of the channels.
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 or different colors, you can add the indicator
multiple times. Be sure to give each one a different labTL value.}
input: price(c), labTL("R"), colorTL(tool_yellow), thickTL(0);
array: dd[20](0),tt[20](0),vv[20](0),bb[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);
var: handl(0);
if currentbar = 1 then begin { examine all the text strings and save some
info about the ones we recognize }
handl = text_getfirst(2);
while handl > 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);
ix = ix + 1;
end;
end;
handl = text_getnext(handl,2); { IMPORTANT -- infinite loop if this is
missing! }
end;
end;
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 ii > 0 then begin { need at least 1 point }
{ The regression line passes through (x1,y1) and (x2,y2) }
x1 = bb[ii];
x2 = bb[ii-1];
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, colorTL);
{ find the max excursion on each side of the line }
hval = 0;
lval = 0;
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, colorTL);
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, colorTL);
end;
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;
|