{ Downloaded From https://www.WiseStockTrader.com }
{ 2-point plot, coding example v3.0
  Trendline choice 1: last High/Low in month.
  Trendline choice 2: lows at July/Dec 2006

  Copyright © 2003-2007 Jose Silva
  http://www.metastocktools.com }

{ User inputs }
extend:=Input("Extend trendline?  [1]Yes,  [0]No",0,1,1);
plot:=Input("choose event [1-2] trend",1,2,1);
choose:=Input("plot:  [1]Trendline,  [2]Event points",1,2,1);

{ Define events }
time1:=C=HHV(C,21);
time2:=C=LLV(C,21);
time2:=time2 AND time2<>time1;

{ Alternative events }
dateA:=Year()=2006 AND Month()=7;
dateA:=If(LastValue(Cum(dateA))=0,
 Year()=LastValue(Year())-1
 AND Month()=LastValue(Month()),dateA);
LoValA:=Lowest(ValueWhen(1,dateA,L));
timeA:=dateA AND L=LastValue(LoValA);

dateB:=Year()=2006 AND Month()=12;
dateB:=If(LastValue(Cum(dateB))=0,
 Year()=LastValue(Year())
 AND Month()=LastValue(Month()),dateB);
LoValB:=Lowest(ValueWhen(1,dateB,L));
timeB:=dateB AND L=LastValue(LoValB);
timeB:=timeB AND timeB<>timeA;

{ Choose events }
time1:=If(plot=1,time1,timeA);
time2:=If(plot=1,time2,timeB);

{ Restrict to last events }
time1:=time1
 AND Cum(time1)=LastValue(Cum(time1));
time2:=time2
 AND Cum(time2)=LastValue(Cum(time2));

{ Sort events }
t1pds:=LastValue(BarsSince(time1));
t2pds:=LastValue(BarsSince(time2));
x1:=If(t1pds>=t2pds,time1,time2);
x2:=If(t1pds>=t2pds,time2,time1);
y1:=If(plot=1,C,L);
y2:=If(plot=1,C,L);

{ Fix coordinates }
y1:=ValueWhen(1,x1,y1);
y2:=LastValue(ValueWhen(1,x2,y2));
b1:=LastValue(BarsSince(x1));
b2:=LastValue(BarsSince(x2));

{ Trendline definition/restriction/extension }
plot:=
 y1+BarsSince(x1)*(y2-y1)/Max(b1-b2,.000001);
b2:=LastValue(If(extend,0,b2));
plot:=Ref(Ref(plot,-b2),b2);

{ Plot trendline on price chart }
If(choose=1,plot,time1+time2)