Static variable

#1
Dear Friends,

I am trying to remove excess signals in amibroker.
I am aware of exrem function but cannot use that as I have to set allowsamebarexit to true.
Thus, trying to achieve the same using static function.
However, not getting desired results. Below is my code:

_SECTION_BEGIN("Price");
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() );
_SECTION_END();


W = EMA(C,9);
X = EMA(C,21);

alreadyopenbuy = StaticVarGetText("alreadyopenbuy"+Name());
alreadyopenSell = StaticVarGetText("alreadyopensell"+Name());

Buy = W > X;
BuyF = Buy AND alreadyopenbuy == "";
BuyL = LastValue(BuyF);


if (BuyL)
{
StaticVarSetText("alreadyopenbuy"+Name() , "1");
StaticVarSetText("alreadyopensell"+Name() , "");
}

Sell = X > W;
SellF = Sell AND alreadyopenSell == "";
SellL = LastValue(SellF);

if (SellL)
{
StaticVarSetText("alreadyopenbuy"+Name() , "");
StaticVarSetText("alreadyopensell"+Name() , "1");
}

Plot(W, "ema9", colorRed,styleLine);
Plot(X, "ema21", colorBlueGrey,styleLine);

PlotShapes(BuyF*shapeUpTriangle,colorgreen,0,L,-20);
PlotShapes(SellF*shapeDownTriangle,colorred,0,H,-20);


Any help much appreciated.
Thanks!