PureBytes Links
Trading Reference Links
|
Here is EL code for detecting triangles. I wrote it today and used it to
scan 150 stocks which I track. The code could be improved a lot. But
here it is.
************************************************************
{ The basic idea was given by the book Using Easy Language Page 127 }
{ Drawing Objects - Drawing a trendline }
{ Basic Idea by Arthur G. Putt in his book, modified by Sudarshan
Sukhani }
{ I received the book just 2 days ago . The Drawing Objects chapter
contains a tutorial to draw a trendline. I modified this code to draw a
triangle . My first attempt at drawing objects and patterns }
{ Most of the initial code and variables are from Putt's book - a good
book to pick up EZ lang }
{ All the comments are mine. i added them as I tried to understand what
was happening }
{ The inputs in the next line are for strength, and color of tl }
inputs: SHStgth(1), DnTndClr("Blue"), UpTndClr("Red");
{ The following two variables contain the numeric color codes for the
colors specified as text in the inputs }
Vars: DnTndClrVar(0), UpTndClrVar(0);
Vars: SLStgth(0);
{ These arrays will save the swing high low values }
Arrays: SHArray[12,3](0), SLArray[12,3](0);
{ The following lines assign the numeric color codes to two vars by
converting the input colors. This is a one time task. Notice the " If
CurrentBar = 1 " condition }
If Currentbar = 1 then begin
DnTndClrVar = StrColorToNum(DnTndClr);
UpTndClrVar = StrColorToNum(UpTndClr);
SLStgth = SHStgth; { Putt used two vars for up and down lines. I want
both strength to be same }
end;
{ In this SwingHighBar, the 4rth parameter SHStgth+1 is the number of
bars over which the swing high occured }
{ Note : This function returns how many bars ago the swing occured }
If SwingHighBar(1,High,SHStgth,SHStgth + 1) = SHStgth then begin
{ found a swing high }
{ This For Next Loop shifts all array values 1 position/susbcript Up, so
that the first subscript 0 is now free to store the new swing point just
found }
For Value1 = 11 Downto 0 begin
For Value2 = 0 to 3 begin
SHArray[Value1+1,Value2] = SHArray[Value1,Value2];
End;
End;
SHArray[0,0] = Date[SHStgth];
SHArray[0,1] = Time[SHStgth];
SHArray[0,2] = High[SHStgth];
SHArray[0,3] = BarNumber[SHStgth];
End;
{ In this SwingHighBar, the 4rth parameter SHStgth+1 is the number of
bars over which the swing low occured }
{ Note : This function returns how many bars ago the swing occured }
If SwingLowBar(1,Low,SLStgth,SLStgth + 1) = SLStgth then begin
{ found a swing low }
{ This For Next Loop shifts all array values 1 position/susbcript Up, so
that the first subscript 0 is now free to store the new swing point just
found }
For Value1 = 11 Downto 0 begin
For Value2 = 0 to 3 begin
SLArray[Value1+1,Value2] = SLArray[Value1,Value2];
End;
End;
SLArray[0,0] = Date[SLStgth];
SLArray[0,1] = Time[SLStgth];
SLArray[0,2] = Low[SLStgth];
SLArray[0,3] = BarNumber[SLStgth];
End;
{ Now the following code is added by me to identify and draw the
triangle. The previous code was almost the same as in Putt's book }
If Date = LastCalcdate then begin
{ the last swing high should be less than the previous swing high
(SHArray[0,2] < SHArray[1,2]) and the last swing low should be more than
the previous swing low (SLArray[0,2] > SLArray[1,2]) , then we have
converging lines }
if SHArray[0,2] < SHArray[1,2] AND SLArray[0,2] > SLArray[1,2] then
begin
condition1 = false;
{ of the four points in a triangle, the first point is a high ? }
if SHArray[1,3] < SLArray[1,3] then begin { High starts first }
condition1 = SHArray[1,3] < SLArray[1,3] AND SLArray[1,3] <
SHArray[0,3];
{ Now we want high, low, and high - in this order }
end;
{ of the four points in a triangle, the first point is a low ? }
if SLArray[1,3] < SHArray[1,3] then begin { Low starts first }
condition1 = SLArray[1,3] < SHArray[1,3] AND SHArray[1,3] <
SLArray[0,3];
{ Now we want low, high, and low - in this order }
end;
{ If we have two converging lines, and alternate highs and lows then
condition1 will be true }
{ Now, draw a trendline using the last two swing points }
{ The TL_New returns a reference number if succesful. This number is
being stored in the value10 and Value11 vars , and is used later for
other work }
if condition1 then begin
Value10 = TL_New(SHArray[1,0],SHArray[1,1],SHArray[1,2],
SHArray[0,0],SHArray[0,1],SHArray[0,2]);
Value20 = TL_SetColor(Value10,DnTndClrVar);
Value30 = TL_SetSize(value10,2);
Value11 = TL_New(SLArray[1,0],SLArray[1,1],SLArray[1,2],
SLArray[0,0],SLArray[0,1],SLArray[0,2]);
Value21 = TL_SetColor(Value11,UpTndClrVar);
Value31 = TL_SetSize(value11,2); { I wanted the lines to be thick }
end;
end;
End;
if condition1 and checkalert = true then begin
alert = true;
Plot1(Close,"Close");
end;
{ The alert is required since I scan 150 stocks and would like to know,
how many of them have these close and tight triangles }
{ As the code is written now, the last occurence of a triangle will
trigger an alert, even if the triangle may have been completed many bars
earlier }
|