Thanks casoni.
I want to add one more condition for buy and sell in the above code as "AND if the bar closes above EMA(34) for buy and closes below of EMA(34) for sell. I tried the below code but it is giving error. Please help in correcting this.
Buy = Cross(C,trailarray) AND Cross(C,EMA(34));
Sell = Cross(trailarray,C) AND Cross(EMA(34),C);
//originally Posted by tools&tradingideas [ Edward.Pottasch ]
function vstop_func(tr)
{
trailArray[ 0 ] = C[ 0 ]; // initialize
for( i = 1; i < BarCount; i++ )
{
prev = trailArray[ i - 1 ];
if (C[ i ] > prev AND C[ i - 1 ] > prev)
{
trailArray[ i ] = Max(prev,C[ i ] - tr[ i ]);
}
else if (C[ i ] < prev AND C[ i - 1 ] < prev)
{
trailArray[ i ] = Min(prev,C[ i ] + tr[ i ]);
}
else if (C[ i ] > prev)
{
trailArray[ i ] = C[ i ] - tr[ i ];
}
else
{
trailArray[ i ] = C[ i ] + tr[ i ];
}
}
return trailArray;
}
per = Param("per",20, 5, 150, 1);
mult = Param("mult",2, 1, 4, 0.05);
tr = mult * ATR(per);
trailArray = vstop_func(tr);
//trailArray = Ref(trailArray,-1);
SetChartBkColor( ParamColor("ColorBG", ColorRGB( 0, 0, 0 ) ) );
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(IIf(trailArray > C,trailArray,Null),"\ntrailShort",ParamColor("Colo rTrailShort",ColorRGB(255,0,0)),styleStaircase);
Plot(IIf(trailArray < C,trailArray,Null),"\ntrailLong",ParamColor("Color TrailLong",ColorRGB(0,255,0)),styleStaircase);
Plot( C, "\nCandle",colorWhite, styleCandle );
// added
xx=EMA(C,34);
Plot(xx,"Ema34:",3,1);
//----------- 1
Buy1 = C>xx AND Cross(C,trailarray);
Sell1 = C<xx AND Cross(trailarray,C);
PlotShapes(Buy1*shapesmallUpTriangle+Sell1*shapesmallDownTriangle,IIf(Buy1,colorBlue,colorYellow),0,IIf(Buy1,L,H)) ;
//------------ 2
Buy2 = C>xx AND C>trailarray;
Sell2 = C<xx AND C<trailarray;
buy2=ExRem(buy2,sell2);
sell2=ExRem(sell2,buy2);
PlotShapes(Buy2*shapeUparrow+Sell2*shapeDownarrow,IIf(Buy2,colorgreen,colorred),0,IIf(Buy2,L-30,H+30)) ;
//------------ 3
Buy3 = Cross(C,trailarray) AND Cross(C,xx);
Sell3 = Cross(trailarray,C) AND Cross(xx,C);
PlotShapes(Buy3*shapeuparrow+Sell3*shapeDownarrow,IIf(Buy3,colorpalegreen,colororange),0,IIf(Buy3,L-40,H+40)) ;
Thank you