@amiuser Thanks for reply. I tried using staticvariable like this in code below. May I know if I am using it right. Because this is not working. All signals disappears after using staticvar like this. Can you correct if there is a mistake?
_SECTION_BEGIN("EmaCrossovers with trail");
SetPositionSize(1*RoundLotSize,spsShares);
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
tsl = Param("% Trailing Stoploss",0.25,0.25,10,0.25);
Buy1 = Cross(EMA(Close,20),EMA(Close,50));
Buy2 = Cross(EMA(Close,9),EMA(Close,26));
Buy3 = Cross(MA(Close,8),EMA(Close,20));
Buy = Buy1 OR Buy2 OR Buy3 ;
Buy = Buy AND Nz(StaticVarGet("commonflag"))==0 ;
Sell1 = Cross(EMA(Close,50),EMA(Close,20));
Sell2 = Cross(EMA(Close,26),EMA(Close,9));
Sell3 = Cross(MA(Close,20),EMA(Close,8));
Sell4 = Cross(TimeNum(), 152800) ;
BuyPrice = ValueWhen(Buy,Close);
entryprice = BuyPrice;
stoplevel = (1 - tsl / 100); //initial stoploss
//initialize the variables before using it inside the forloop
Sell = 0;
trailstop = 0;
trailArray = Null;
for( i =1 ; i<BarCount; i++)
{
if(trailstop == 0 AND Buy[i] AND StaticVarGet("commonflag")==0)
{
trailstop = entryprice[i] - entryprice[i] * tsl/100;
StaticVarSet("commonflag",1) ;
}
else
{
Buy[i] = 0;
}
if(trailstop > 0 AND L[i] < trailstop)
{
Sell[i] = True;
SellPrice[i] = trailstop;
trailstop = 0;
StaticVarSet("commonflag",0) ;
}
if(trailstop > 0 AND sell1[i])
{
Sell[i] = True;
SellPrice[i] = sell1[i];
trailstop = 0;
StaticVarSet("commonflag",0) ;
}
if(trailstop > 0 AND sell2[i])
{
Sell[i] = True;
SellPrice[i] = sell2[i];
trailstop = 0;
StaticVarSet("commonflag",0) ;
}
if(trailstop > 0 AND sell3[i])
{
Sell[i] = True;
SellPrice[i] = sell3[i];
trailstop = 0;
StaticVarSet("commonflag",0) ;
}
if(trailstop > 0 AND sell4[i])
{
Sell[i] = True;
SellPrice[i] = Open[i];
trailstop = 0;
StaticVarSet("commonflag",0) ;
}
if(trailstop > 0)
{
trailstop = Max(High[i] * stoplevel, trailstop);
trailarray[i] = trailstop;
}
}//for loop
Plot(trailarray,"TSL Value",colorYellow,styleDashed | styleThick);
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();