/////////////////
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
SetBarsRequired(10,10);
Factor1=Optimize("Factor1",0.5,5,10,2);
Pd1=Optimize("ATR Periods1",5,1,10,1);
Up1=(H+L)/2+(Factor1*ATR(Pd1));
Dn1=(H+L)/2-(Factor1*ATR(Pd1));
iATR1=ATR(Pd1);
TrendUp1=TrendDown1=Null;
trend1[0]=1;
changeOfTrend1=0;
flag1=flagh1=0;
for (i = 1; i <BarCount-1; i++) {
TrendUp1[i] = Null;
TrendDown1[i] = Null;
trend1[i]=1;
if (Close[i]>Up1[i-1]) {
trend1[i]=1;
if (trend1[i-1] == -1) changeOfTrend1 = 1;
}
else if (Close[i]<Dn1[i-1]) {
trend1[i]=-1;
if (trend1[i-1] == 1) changeOfTrend1 = 1;
}
else if (trend1[i-1]==1) {
trend1[i]=1;
changeOfTrend1 = 0;
}
else if (trend1[i-1]==-1) {
trend1[i]=-1;
changeOfTrend1 = 0;
}
if (trend1[i]<0 && trend1[i-1]>0) {
flag=1;
}
else {
flag=0;
}
if (trend1[i]>0 && trend1[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}
if (trend1[i]>0 && Dn1[i]<Dn1[i-1]){
Dn1[i]=Dn1[i-1];
}
if (trend1[i]<0 && Up1[i]>Up1[i-1])
{ Up1[i]=Up1[i-1];
}
if (flag==1)
{ Up1[i]=(H[i]+L[i])/2+(Factor1*iATR1[i]);;
}
if (flagh==1)
{ Dn1[i]=(H[i]+L[i])/2-(Factor1*iATR1[i]);;
}
if (trend1[i]==1) {
TrendUp1[i]=Dn1[i];
if (changeOfTrend1 == 1) {
TrendUp1[i-1] = TrendDown1[i-1];
changeOfTrend1 = 0;
}
}
else if (trend1[i]==-1) {
TrendDown1[i]=Up1[i];
if (changeOfTrend1 == 1) {
TrendDown1[i-1] = TrendUp1[i-1];
changeOfTrend1 = 0;
}
}
}
Plot(TrendUp1,"Trend1",colorWhite);
Plot(TrendDown1,"Down1",colorWhite);
Buyset = Cross(H,TrendUp1);
Sellset = Cross(TrendDown1,L);
Buy=Cross(H,ValueWhen(buyset,H)) AND C > O;
Sell=Cross(ValueWhen(sellset,L),L) AND C < O;
Buy[BarCount-1]=Null;
Sell[BarCount-1]=Null;
Cover=Buy;
Short=Sell;
shape = Buyset * shapehollowUpArrow + Sellset * shapehollowDownArrow;
PlotShapes( shape, IIf( Buyset, colorWhite, colorWhite ),0, IIf( Buyset, Low, High ) );