GraphXSpace=5;
SetChartOptions(0, chartShowDates);
SetBarFillColor(IIf(C>O,ParamColor("Candle Up Color", colorBrightGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"Price",IIf(C>O,ParamColor("Shadow Up Color", colorBrightGreen),IIf(C<=O,ParamColor("Shadow Color", colorRed),colorLightGrey)),64,0,0,0,0,1);
_SECTION_BEGIN("Zig");
x=xx=BarIndex();
tf=Param("Time Frame (min)",5,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);
TimeFrameSet(tfrm);
function vstop_func(trBull,trBear)
{
trailArray[0]=C[0];
for(i=1;i<BarCount;i++)
{
prev=trailArray[i-1];
if(C>prev AND C[i-1]>prev)
{
trailArray=Max(prev,C-trBull);
}
else if(C<prev AND C[i-1]< prev)
{
trailArray=Min(prev,C+trBear);
}
else if (C>prev)
{
trailArray=C-trBull;
}
else
{
trailArray=C+trBear;
}
}
return trailArray;
}
trBull=multBull*ATR(perBull);
trBear=multBear*ATR(perBear);
trailArray = vstop_func(trBull,trBear);
ts=IIf(trailArray>C,trailArray,Null);
tl=IIf(trailArray<C,trailArray,Null);
TimeFrameRestore();
ts=TimeFrameExpand(ts,tfrm,expandLast);
tl=TimeFrameExpand(tl,tfrm,expandLast);
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
Plot(ts,"\ntrailShort",colorCustom12,styleLine,0,0,0,1,1);
Plot(tl,"\ntrailLong",colorCustom11,styleLine,0,0,0,1,1);
PlotShapes(shapeSmallCircle*tr,colorGreen,0,L,-10);
PlotShapes(shapeSmallCircle*pk,colorRed,0,H,10);
_SECTION_END();
please chk this afl. You can create any signal with other parameter , which are above or below the SL line will give you tgt depends on TF.
This AFL is not developed by me , but somewhere in this forum.( May be Ed Pottash)