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

Re: EL support or resistance functions



PureBytes Links

Trading Reference Links

Adam Hardy wrote:
>I'm busy writing a function in TS4 to determine the point on a
>support line for tomorrow's bar underneath a range, using the Lows
>and gradients and a loop over the lows to determine the 2 lowest
>price points from which to extrapolate.
>
>Of course it's proving to be a complicated process and it occurred
>to me that it might have been provided in TS2000i by default.

Well, TS2000i has the Highest and Lowest functions, that tell you
the highest and lowest prices of the last N bars.  It's trivial to
modify that to give you the 2nd or 3rd highest or lowest bar.  I'll
do it here:

=======================================================
{Function WhichHighest(Price, Length, Order)}
{Order=1 means highest, 2 means 2nd highest, max 4}

Inputs: Price(NumericSeries), Length(NumericSimple),
   Order(NumericSimple);
Variables: counter(0), ii(0);
Array: hi[4](0); {stores highest 4 prices)

for ii=0 to 3 begin hi[ii] = Price[0]; end;
For counter = 1 To Length - 1 Begin
   If Price[counter] > hi1 Then begin
      for ii = 3 downto 1 begin
         hi[ii] = hi[ii-1];
      end;
      hi[0] = Price[Counter];
   End;
End;
WhichHighest = hi[order];
=======================================================

Above is from my head, untested.

If you want the highest or lowest swing points, that's a different
matter. Here's my code for that (tested, 'cause I actually use this):

=======================================================
{Function: _HighPoint
 by Alex Matulich, 11/16/2003
 Return the index of the most recent swing high to the left of index
 barsback.  A swing high is defined as a higher high followed by zero
 or more equal highs and then a lower high.}

Inputs:
   price(NumericSeries),   {data to search}
   barsback(NumericSimple),{where to start, >= 1}
   length(NumericSimple),  {search window will be 1 more than this}
   side(NumericSimple),    {side (1=left,-1=right) that can be equal}
   span(NumericRef);       {return number of bars having same low}

vars: j(0), k(0), rtn(0), maxidx(0);

rtn = 0;
if length >= 1 then begin
   maxidx = barsback + length - 1;
   j = barsback;

   {left side can be equal}

   if side > 0 then begin
       while j <= maxidx and rtn = 0 begin
         if price[j] > price[j-1] then begin
            k = j+1;
            while k < maxidx and price[k] = price[j] begin k = k+1; end;
            if price[j] > price[k] then begin
               rtn = j;
               span = k-j;
            end;
         end;
         j = j + 1;
      end;

   {right side can be equal}

   end else begin
       while j <= maxidx and rtn = 0 begin
         if price[j] > price[j+1] then begin
            k = j-1;
            while k > 0 and price[k] = price[j] begin k = k-1; end;
            if price[j] > price[k] then begin
               rtn = j;
               span = j-k;
            end;
         end;
         j = j + 1;
      end;
   end;
end;

_HighPoint = rtn;
=======================================================

The way you modify that to get the 2nd highest swing point is
similar to what I did in the first function above: store the current
highest in an array (in this case 'rtn' would be an array), and when
you find a higher one, bump the others down in the array.

-Alex