Multiple Signals being generated on the previous bar

#1
I tried my strategy with the bar replay tool and found that I indeed was generating multiple signals .

I tried users ref(buy, -1), but I still get multiple signals.


Is there something else that I am missing to make the buy on prev bar work.
All I REALLY want to do is have a signal generated on the current bar, and then
act (open a trade) on that signal on the NEXT bar open.

here is my code below...you will see multiple traces showing multiple buy
signals.

Please help,thanks


Buy = Ref(Cross((Close),(EMA(Close,9))), -1);
Buystop = Ref(EMA(Close,9),-1);
BuyPrice = Max(Buystop,Low);

// not worried about delaying the sell signal until I get the buy signal working
Sell = Ref(Cross(EMA(Close,9),(Close)), -1);
Sellstop = Ref(EMA(Close,9),-1);
SellPrice = Min(sellstop,High);

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorBlue, colorRed ), 0, IIf( Buy, Low, High ) );
PlotShapes(IIf(Buy ==1, shapeUpArrow,shapeNone),colorBlue,0,0,10);
PlotShapes(IIf(Sell ==1, shapeHollowUpArrow,shapeNone),colorRed,0,0,10);