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.
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.
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!!