How to code reentry delay?

Hello!
My code isn't working as I expected. Maybe someone more experienced in coding might help.
Here is what I expect from the code:

Enter the trade - If Close of the current candle is lower than the low of previous 38 bars (LLV): enter Short next Open;
*Exit 1 (Stop Loss) - High of previous candle before the short position is opened;
*Exit 2 (Cover) - High of current candle is higher than highest (HHV) of previous 3 closed candles;

*If a short position is opened and cover is triggered in the same candle, the position must be closed. Therefore, option "Allow same bar exit/entry signal" is checked. Cover a position in the same candle is OK. But whenever a position is closed, no trade should opened in the same candle.

Here is the code:

LoBars = 38;
Lobreakout = Ref(LLV( L, LoBars ), -1);
ShortSignal = ( Close < Lobreakout );
Short = Ref(ShortSignal, -1);
ShortPrice = Open;

CoBars = 3;
Cobreakout = Ref(HHV( H, CoBars ), -1);
Cover = ( High >= Cobreakout );

StopAmount = Ref(High,-1) - ShortPrice;
ApplyStop( stopTypeLoss, stopModePoint, StopAmount, 1, False, 1);

ExRem(Short, Cover);
ExRem(Cover, Short);

Here is what I'm getting:
image

As you can see on the same candle a Short and Cover signal were triggered. How should I write the code to delay reentry to the next signal?

Thank you very much,
AK.

What I want is: if there is an open position when the candle opens, then no new trade is taken in this candle.
In settings, the checkbox “Allow same bar exit/entry signal” is marked, because I want same bar exit (but I don’t want same bar entry).

I managed to eliminate this specific unwanted behavior, by making some changes to the code. Although I’m still not sure if this is the best way to proceed.

SetTradeDelays(1, 0, 1, 0);

LoBars = 38;
Lobreakout = Ref(LLV( L, LoBars ), -1);

CoBars = 3;
Cobreakout = Ref(HHV( H, CoBars ), -1);

CurrentCoBreakout = ( HHV( H, CoBars ) );

Short = ( Close < Lobreakout ) AND Ref(( High < CurrentCoBreakout ), 1);
ShortPrice = Open;

Cover = ( High >= Cobreakout );
CoverPrice = Cobreakout;

StopAmount = Ref(High,-1) - ShortPrice;
ApplyStop( stopTypeLoss, stopModePoint, StopAmount, 1, False, 1);

ExRem(Short, Cover);
ExRem(Cover, Short);

It looks into the future to see if the Cover condition will be met in this candle. If condition is True, it won’t take another Short position.

Any thoughts on this issue?

The solution above didn’t get even close of solving the issue. The code was checking “if the Cover condition will be met in this candle”, but wasn’t checking if there was an open positon when the candle opened. All the changes made to the original code were deleted and a ‘for’ loop - to check if there was an open positon - solved this issue.

Thank you everyone who contributed for helping achieving the solution, very productive discussion we had!! :joy::rofl:

Hello! Would you be able to share the code for this loop?

Try this

SetTradeDelays( 1, 1, 1, 0 );
LoBars = 38;
Lobreakout = LLV( Ref(L, -1), LoBars );
Short = Cross( Lobreakout, Close );
ShortPrice = Open;

CoBars = 3;
Cobreakout = HHV( Ref(H, -1), CoBars );
Cover = Cross( H, Cobreakout ) OR H == Cobreakout;

stp = ValueWhen( Short, Ref(H, -1), 1 );

StopAmount = stp - ShortPrice;
ApplyStop( 0, 2, StopAmount, 1, False, 1, 0, -1 );

Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

Regards,