Page 1 of 1

Trendline Finder Function

Posted: Sun Dec 30, 2007 7:34 pm
by drtrade
This function will find the closest support or resistance line of a specified color and give the value for it. Works great but is pretty slow.

Any input on how to optimize the code to process faster would be greatly appreciated.

Thanks

Code: Select all

         
inputs: 
    TrendLineID( numeric ),
    TLColor( Numeric);
 
Variables:
   DateTime(0),
   Distance(0),
   TLClose(0),
   BeginDate(0),
   EndDate(0),
   Extension(0);
Array: int PrevValue[1](1);

_TL_FindColorClosest = -1;
Distance = 999;
TLClose = 0;
Extension = 0;
If TrendLineID = -1 then Value1 = TL_GetNext( PrevValue[0], 3 ) ;
If TrendLineID = 1 then Value1 = TL_GetNext( PrevValue[1], 3 ) ;
DateTime = ELDateToDateTime(Date) + ELTimeToDateTime(Time);

if Value1 >= 0 then
   while Value1 >= 0 begin
      BeginDate = ELDateToDateTime(TL_GetBeginDate(Value1)) + ELTimeToDateTime(TL_GetBeginTime(Value1));
      EndDate = ELDateToDateTime(TL_GetEndDate(Value1)) + ELTimeToDateTime(TL_GetEndTime(Value1));
      If Date > CalcDate(CurrentDate,-10) then
         if TL_GetExtRight(Value1) = True then
            Extension = 1;
         if TL_GetColor( Value1 ) = TLColor and BeginDate <= DateTime AND (EndDate >= DateTime OR Extension = 1) then
         begin
      If TrendLineID = -1 AND TL_GetValue( Value1, D, T ) <= High then begin
      if AbsValue(Close - TL_GetValue( Value1, D, T )) < Distance then begin
      Distance = AbsValue(Close - TL_GetValue( Value1, D, T ));
      TLClose = Value1;
      end;
      end;
      If TrendLineID = 1 AND TL_GetValue( Value1, D, T ) >= Low then begin
      if AbsValue(Close - TL_GetValue( Value1, D, T )) < Distance then begin
      Distance = AbsValue(Close - TL_GetValue( Value1, D, T ));
      TLClose = Value1;
      end;
      end;
         end;
      if TL_GetColor( Value1 ) = TLColor AND BeginDate > DateTime then begin
         Value1 = -2 ;
      end
      else begin
         Value1 = TL_GetNext( Value1, 3 ) ;
       end;
   end ;

_TL_FindColorClosest = TLClose;
//PRINT (FILE ("c:\TsBars1.txt"), DATE, Time, "  Return Value   ", TLClose) ;
//Print(Date, Time, "Return Value   ", TLClose);
If TLClose > 20 AND TrendLineID = -1 then PrevValue[0] = TLClose - 20;
If TLClose > 20 AND TrendLineID = 1 then PrevValue[1] = TLClose - 20;


And here is the code to call the function

Code: Select all

Vars: FibResistance(0), FibSupport(0);
Value10 = _TL_FindColorClosest( -1, Blue);

if Value10 > 0 then 
FibSupport = TL_GetValue( Value10, D, T );

Value11 = _TL_FindColorClosest( 1, Red);

if Value11 > 0 then 
FibResistance = TL_GetValue( Value11, D, T );