Help with Intrabar Exit

I am facing some problems with ExREM function. Whenever I use it, exit does not happen on the same bar. I am pretty sure it is a code issue but I am unable to figure out the root cause of my error. I have searched enough on web and forum, found some articles but cannot understand the issue

I have written a simple sample code to demonstrate the issue I am facing.

set a date to ensure there are no past signals
set entry/stoploss prices such that I have signals on the same bar

SetTradeDelays(0, 0, 0, 0);
SetOption("AllowSameBarExit", True );

entry = 62.1;
stoploss = 61.8;
target =65;

intrastart = 092959;
intraend = 132959;
intraExit = 151459;

NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;  

TimeCheck = TimeNum() >= intraStart AND TimeNum() <= intraEnd AND DateNum() == 1190828; printf("\nTimeCheck: " + TimeCheck);

Buy = (Cross(entry, L) OR L < entry) AND TimeCheck;	printf("\nBuy: " + Buy);
Buy = Buy AND Sum(Buy, BarsSince( newDay) +1 ) <= 1; printf("\nBuy: " + Buy);

Sell = (Cross(H, target) OR H > target) OR (Cross(stoploss, L)  OR L < stoploss) OR TimeNum() > intraExit; printf("\nSell: " + Sell);

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

printf("\nBuy: " + Buy + " Sell: " + Sell);

//--Plot Price Candle Chart
candlebordercolor = IIf(C>O,colorGreen,colorRed);
//SetBarFillColor( IIf(NewDay,IIf(C1 > H1-(0.25*CR1) AND C1>O1, colorGreen, IIf((L1+ 0.25*CR1 > C1) AND C1 < O1, colorRed,colorWhite)),colorblack));  
_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( Close, "Price", candlebordercolor, styleCandle );

PlotShapes(IIf(buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-20);
PlotShapes(IIf(buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-15);
PlotShapes(IIf(sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=10);
PlotShapes(IIf(sell, shapeSquare, shapeNone),colorOrange, 0, H, Offset=20);
PlotShapes(IIf(sell, shapeDownArrow, shapeNone),colorWhite, 0, H, Offset=-15);


I can see that if intrabar low goes below stoploss, Sell signal is generated. However it gets killed by ExRem. This only happens when Buy was also generated on the same bar

Where am I missing the plot

Remove ExRem function calls

If you are using Stops (ApplyStop) you should NOT be using ExRem. ApplyStop is backtester-only function and ExRem does not know anything about your stops.

Remove ExRems. If you want arrows, use built-in functionality (Show arrows after backtest).

Alternatively if you still want to draw arrows yourself AND evaluate stops properly, you could use Equity(1) IF ONLY this is single-symbol backtest.

Hi Tomasz,

Thanks for this. I am not sure I understand the recommendation clearly

I am not using ApplyStop. I exit on crossover over a certain value. The crossover may happen many times so to stop multiple signals, I am using Exrem.

The problem after above is that , exit happens on the next candle and not the immediate candle if price breaches the target in the same candle. If I use H > someSLValue condition (in case of shorts), it causes multiple Cover signals without Exrem.

Not sure if I have explained the problem clearly.

OK, your original post mentioned stop loss therefore I assumed that you were using built-in stop handling (ApplyStop OR stops settable in the settings).
Still advice on removing ExRem still applies.
ExRems are NOT needed by the backtester.
Backtester removes excess signals by itself:

One quick question. If in same bar, trade is triggered and so is stoploss (using crossover) - cover signal is removed when use ExRem. Would that be the case in real life as well?

It was written a number of times already: remove ExRems. They are not needed. ExRem() and Flip() logically are working like set-reset latch flip-flop device. So obviously the same time "set" and "reset" is not allowed. If you don't understand how ExRems work you should follow advice given here: How do I debug my formula? and specifically this