// Downloaded From https://www.WiseStockTrader.com // Amibroker AFL code by Edward Pottasch, Oct 2012 // Alternative ZIG type function based on the ATR and VSTOP functions // Added multiple timeframes. Maximum timeframe set to 1440 minutes x=xx=BarIndex(); tc=ParamList("Display Mode","ZIG|VSTOP|ZIG&VSTOP",0); disp0=ParamToggle("Display labels","Off|On",1); disp1=ParamToggle("Display value labels","Off|On",1); tf=Param("Time Frame (min)",60,1,1440,1);tfrm=in1Minute*tf; perBull=Param("perBull",20,1,150,1); perBear=Param("perBear",20,1,150,1); multBull=Param("multBull",2,0.05,4,0.05); multBear=Param("multBear",2,0.05,4,0.05); perc=Param("Percentage Range (S/R lines)",20,0.05,100,0.01); npiv=Param("N Pivots Used (S/R lines)",1,1,250,1); disp2=ParamToggle("Display S/R levels","Off|On",0); TimeFrameSet(tfrm); function vstop_func(trBull,trBear) { trailArray[0]=C[0]; for(i=1;iprev AND C[i-1]>prev) { trailArray[i]=Max(prev,C[i]-trBull[i]); } else if(C[i]prev) { trailArray[i]=C[i]-trBull[i]; } else { trailArray[i]=C[i]+trBear[i]; } } return trailArray; } trBull=multBull*ATR(perBull); trBear=multBear*ATR(perBear); trailArray = vstop_func(trBull,trBear); ts=IIf(trailArray>C,trailArray,Null); tl=IIf(trailArrayO,ParamColor("Candle Up Color", colorBrightGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey))); Plot(C,"Price",IIf(C>O,ParamColor("Shadow Up Color", ColorRGB(0,255,0)),IIf(C<=O,ParamColor("Shadow Color", ColorRGB(255,0,0)),colorLightGrey)),64,0,0,0,0,1); lll=LLV(L,BarsSince(!IsEmpty(tl)));lll=IIf(ts,lll,Null);llls=lll; ttt1=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1))) OR BarIndex()==BarCount-1,1,Null); ttt=ValueWhen(ttt1,lll,0);ttt=IIf(ts,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt); tr=L==ttt;lll=Sum(tr,BarsSince(!IsEmpty(tl))); qqq=ValueWhen(ttt1,lll,0);qqq=IIf(ts,qqq,Null);qqq=IIf(ttt1,Ref(qqq,-1),qqq);tr=tr AND lll==qqq; tr=IIf((!IsEmpty(ts) AND IsEmpty(Ref(ts,1)) AND IsEmpty(Ref(ts,-1))),1,tr);//exception hhh=HHV(H,BarsSince(!IsEmpty(ts)));hhh=IIf(tl,hhh,Null);hhhs=hhh; ttt1=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1))) OR BarIndex()==BarCount-1,1,Null); ttt=ValueWhen(ttt1,hhh,0);ttt=IIf(tl,ttt,Null);ttt=IIf(ttt1,Ref(ttt,-1),ttt); pk=H==ttt;hhh=Sum(pk,BarsSince(!IsEmpty(ts))); sss=ValueWhen(ttt1,hhh,0);sss=IIf(tl,sss,Null);sss=IIf(ttt1,Ref(sss,-1),sss);pk=pk AND hhh==sss; pk=IIf((!IsEmpty(tl) AND IsEmpty(Ref(tl,1)) AND IsEmpty(Ref(tl,-1))),1,pk);//exception 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); minipk=H>=Ref(HHV(H,1),-1) AND Ref(HHV(H,1),1)L; switch(tc) { case("ZIG"): aa1=IIf(px0>tx1,(ph0-tl1)/(px0-tx1),0);aa1=IIf(pk,Ref(aa1,-1),aa1);ls1=aa1*(xx-tx1)+tl1; bb1=IIf(px0>tx1 AND px1px1,(tl0-ph1)/(tx0-px1),0);aa1=IIf(tr,Ref(aa1,-1),aa1);ls1=aa1*(xx-px1)+ph1; bb1=IIf(tx0>px1 AND tx1tx1,(ph0-tl1)/(px0-tx1),0);aa1=IIf(pk,Ref(aa1,-1),aa1);ls1=aa1*(xx-tx1)+tl1; bb1=IIf(px0>tx1 AND px1px1,(tl0-ph1)/(tx0-px1),0);aa1=IIf(tr,Ref(aa1,-1),aa1);ls1=aa1*(xx-px1)+ph1; bb1=IIf(tx0>px1 AND tx1= 60 AND qq < 1440){tf=" hrs";tt=qq/60;} else if(qq >= 1440){tf=" days";tt=(qq/60)/24;} qq=Max(tfrm/60,Interval()/60); if(qq < 60){tfa=" min";tta=qq;} else if(qq >= 60 AND qq < 1440){tfa=" hrs";tta=qq/60;} else if(qq >= 1440){tfa=" days";tta=(qq/60)/24;} Title = Name() + "\nChart TF: " + tt + tf + "\nZig TF: " + tta + tfa; dxhm=14;dxlm=10;dxh=0;dxl=0;dyhm=5;dylm=3;dyh=18;dyl=29;hm=30;lm=30; dyl2=42;dylm2=16;dyhm2=18;dyh2=31; function GetVisibleBarCount() { lvb=Status("lastvisiblebar"); fvb=Status("firstvisiblebar"); return Min(lvb-fvb,BarCount-fvb); } function GfxConvertPixelsToBarX(Pixels) { lvb=Status("lastvisiblebar"); fvb=Status("firstvisiblebar"); pxchartleft=Status("pxchartleft"); pxchartwidth=Status("pxchartwidth"); fac=pxchartwidth/Pixels; bar=(lvb-fvb)/fac; return bar; } function GfxConvertPixelToValueY(Pixels) { local Miny,Maxy,pxchartbottom,pxchartheight; Miny=Status("axisminy"); Maxy=Status("axismaxy"); pxchartbottom=Status("pxchartbottom"); pxchartheight=Status("pxchartheight"); fac=pxchartheight/Pixels; Value=(Maxy-Miny)/fac; return Value; } ll=tr AND tl1tl2; hh=pk AND ph1>ph2; lh=pk AND ph1LowMargin) PlotText("LL",i+fvb+dxll,L[i+fvb]-dyll,colorWhite,colorDefault); if(ll[i+fvb] AND L[i+fvb]<=LowMargin) PlotText("LL",i+fvb+dxll+dxllm,L[i+fvb]-dyllm,colorWhite,colorDefault); if(hl[i+fvb] AND L[i+fvb]>LowMargin) PlotText("HL",i+fvb+dxll,L[i+fvb]-dyll,colorWhite,colorDefault); if(hl[i+fvb] AND L[i+fvb]<=LowMargin) PlotText("HL",i+fvb+dxll+dxllm,L[i+fvb]-dyllm,colorWhite,colorDefault); if(db[i+fvb] AND L[i+fvb]>LowMargin) PlotText("DB",i+fvb+dxll,L[i+fvb]-dyll,colorWhite,colorDefault); if(db[i+fvb] AND L[i+fvb]<=LowMargin) PlotText("DB",i+fvb+dxll+dxllm,L[i+fvb]-dyllm,colorWhite,colorDefault); if(hh[i+fvb] AND H[i+fvb]=HighMargin) PlotText("HH",i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm,colorWhite,colorDefault); if(lh[i+fvb] AND H[i+fvb]=HighMargin) PlotText("LH",i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm,colorWhite,colorDefault); if(dt[i+fvb] AND H[i+fvb]=HighMargin) PlotText("DT",i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm,colorWhite,colorDefault); } } if(disp1) { for(i=0;iLowMargin) PlotText(""+L[i+fvb],i+fvb+dxll,L[i+fvb]-dyll2,colorWhite,colorDefault); if(ll[i+fvb] AND L[i+fvb]<=LowMargin) PlotText(""+L[i+fvb],i+fvb+dxll+dxllm,L[i+fvb]-dyllm2,colorWhite,colorDefault); if(hl[i+fvb] AND L[i+fvb]>LowMargin) PlotText(""+L[i+fvb],i+fvb+dxll,L[i+fvb]-dyll2,colorWhite,colorDefault); if(hl[i+fvb] AND L[i+fvb]<=LowMargin) PlotText(""+L[i+fvb],i+fvb+dxll+dxllm,L[i+fvb]-dyllm2,colorWhite,colorDefault); if(db[i+fvb] AND L[i+fvb]>LowMargin) PlotText(""+L[i+fvb],i+fvb+dxll,L[i+fvb]-dyll2,colorWhite,colorDefault); if(db[i+fvb] AND L[i+fvb]<=LowMargin) PlotText(""+L[i+fvb],i+fvb+dxll+dxllm,L[i+fvb]-dyllm2,colorWhite,colorDefault); if(hh[i+fvb] AND H[i+fvb]=HighMargin) PlotText(""+H[i+fvb],i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm2,colorWhite,colorDefault); if(lh[i+fvb] AND H[i+fvb]=HighMargin) PlotText(""+H[i+fvb],i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm2,colorWhite,colorDefault); if(dt[i+fvb] AND H[i+fvb]=HighMargin) PlotText(""+H[i+fvb],i+fvb+dxhh+dxhhm,H[i+fvb]+dyhhm2,colorWhite,colorDefault); } } if(disp2) { miny=LastVisibleValue(C)-LastVisibleValue(C)/100*perc; maxy=LastVisibleValue(C)+LastVisibleValue(C)/100*perc; for (i=1;i<=npiv;i++) { rr=ValueWhen(pk,H,i); rr=IIf(rr>maxy OR rrmaxy OR ss