Plot(C,"Price",1,64,0,0,0,0);
_SECTION_BEGIN("Trendlines Multitimeframe");
// Amibroker AFL code by Edward Pottasch, 6/8/2011
// Using fractals to create automatic trendlines
// Using option to show multiple timeframes
xx=BarIndex();x=xx;Lx=LastValue(x);
nbar=Param("N Pivot Bars",3,2,50,1);
pds=Param(" No of Bar to plot?", 50,10,200,5);
fc=2;
pk=H>Ref(HHV(H,nbar*fc),-1) AND Ref(HHV(H,nbar),nbar)<=H;
tr=L<Ref(LLV(L,nbar*fc),-1) AND Ref(LLV(L,nbar),nbar)>=L;
px0=ValueWhen(pk,x,0); tx0=ValueWhen(tr,x,0);
px1=ValueWhen(pk,x,1); tx1=ValueWhen(tr,x,1);
px2=ValueWhen(pk,x,2); tx2=ValueWhen(tr,x,2);
ph0=ValueWhen(pk,H,0); tl0=ValueWhen(tr,L,0);
ph1=ValueWhen(pk,H,1); tl1=ValueWhen(tr,L,1);
ph2=ValueWhen(pk,H,2); tl2=ValueWhen(tr,L,2);
pkh=IIf(pk,H,Null);
trl=IIf(tr,L,Null);
y0=ValueWhen(tr,trl,0);
y1=ValueWhen(tr,trl,1);
x0=ValueWhen(tr,xx,0);
x1=ValueWhen(tr,xx,1);
aa=(Y0-Y1)/(X0-X1);
ls1=aa*(xx-X1)+Y1;
dls1=ls1-Ref(ls1,-1);
y0=ValueWhen(pk,pkh,0);
y1=ValueWhen(pk,pkh,1);
x0=ValueWhen(pk,xx,0);
x1=ValueWhen(pk,xx,1);
aa=(Y0-Y1)/(X0-X1);
hs1=aa*(xx-X1)+Y1;
dhs1=hs1-Ref(hs1,-1);
dd=0.0;
Vh=dd*(ValueWhen(pk,C)-ValueWhen(pk,Ref(C,-1)))/ValueWhen(pk,C);
Vl=dd*(ValueWhen(tr,C)-ValueWhen(tr,Ref(C,-1)))/ValueWhen(tr,C);
ls1=IIf(dls1>=Vl,ls1,Null);
hs1=IIf(dhs1<=Vh,hs1,Null);
bi = BarIndex();
Plot(IIf(bi< LastValue( bi )-pds ,Null,ls1),"\nLower Trendline",colorBrightGreen,styleLine|styleNoRescale,0,0,0,-1);
Plot(IIf(bi< LastValue( bi )-pds ,Null,hs1),"\nUpper Trendline",colorRed,styleLine|styleNoRescale,0,0,0,-1);
_SECTION_END();