AFL Buy / Sell above / below signal bar

#1
I am trying to get a buy/sell signals on the chart on below conditions:
st = supertrend(10,3)
buysig= c>st
sellsig=c<st
Buy = Cross(H,Ref(H,BuySig))
Sell = Cross(Ref(L,SellSig),L)
however, sometimes I am getting correct and sometimes incorrect signals.
Please help in correcting the code.
Below is the entire code:
_SECTION_BEGIN(“Price”);
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN(“Try”);
function SuperTrend(lenATR, width)
{
nATR = ATR(lenATR);
pAvg = (H+L) / 2;
upperBand = pAvg + width * nATR;
lowerBand = pAvg - width * nATR;
isUpTrend = True;
dn = DateNum();

for (i=lenATR; i<BarCount; ++i)
{
if (C > upperBand[i-1])
isUpTrend = True;
else if (C < lowerBand[i-1])
isUpTrend = False;
else
isUpTrend = isUpTrend[i-1];

if (isUpTrend)
lowerBand = Max(lowerBand, lowerBand[i-1]);
else
upperBand = Min(upperBand, upperBand[i-1]);
}

super = IIf(isUpTrend, lowerBand, upperBand);
return super;

}
st = SuperTrend(10,3);
Plot(st, “STOP”, IIf(C>st,colorBrightGreen,colorRed),styleLine|styleStaircase|styleThick);
BuySig = C > st;
SellSig = C < st;
Buy = Cross(H,Ref(H,BuySig));
Sell = Cross(Ref(L,SellSig),L);
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);
Short = Sell;
Cover = Buy;
Cover = ExRem(Buy, Sell);
Short = ExRem(Sell, Buy);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBlueGrey, 0,L, offset=-80);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0,H, offset=-90);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone), colorGreen, 0,L, offset=-60);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone), colorOrange, 0,H, offset=-70);
_SECTION_END();
pls help
Thanks!