I am developing a system where the average length of trades is about 5 or 6 days, and after examining the results, I found those trades that last longer than 5 or 6 days will be usually sold at a loss, so I would like to force those longer trades to exit at the Open of the 5th bar, ie.when barssince(buy) == 4. However it doesn't work, many Sell signals that are forced to be on the 5th bar don't get registered (with a few exceptions). And those Sell signals that are not forced earlier have no issues.
See the exploration of ALU where the Sell signal is not registered when forced to exit on 2020 Feb 27 instead of 2020 Mar 2 (when isSellTooLate is true), and this represents the majority of the case. However, there are a handful of exceptions see the result of ALQ. I am wondering why? See code excerpt and some results from running "Explore".
MaxNumBars = 4; //to set max length of trade by bars.
....................
....................
Sell0 = EXITTRIGGER; //the temporary sell signal according to the original rules.
Buy = ExRem(Buy,Sell0);
Sell0 = ExRem(Sell0,Buy);
SellPrice0 = ValueWhen(Sell0, IIf(O >= LIMITSELLPRICE,O,LIMITSELLPRICE)) ;
OnTheMaxBar = BarsSince(Buy)==MaxNumBars;
OpenPriceOfTheMaxBar = ValueWhen(OnTheMaxBar,O);
isSellTooLate = ValueWhen(Sell0, BarsSince(Buy) >= MaxNumBars);
Sell = IIf(isSellTooLate,OnTheMaxBar,sell0); //not sure why sell signal is not registered when before maxnumbars
SellPrice = IIf(isSellTooLate, OpenPriceOfTheMaxBar, SellPrice0);
LongInitialStop = ValueWhen(Buy,IIf(buyprice - Ref(6 * ATR(10),-1) <= 0, 0.01, BuyPrice - Ref(6 * ATR(10),-1)));
stopAmount=BuyPrice-LongInitialStop;
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, 1);
//the following part is for exploration
Filter=Buy OR Sell OR Sell0 OR OnTheMaxBar;
AddColumn(IIf(Buy,1,null),"Buy");
AddColumn(IIf(Sell,1,Null), "Sell");
AddColumn(IIf(Sell0,1,Null), "Sell0");
AddColumn(IIf(OntheMaxBar,1,Null), "OnTheMaxBar");
AddColumn(IIf(Buy,Ref(LimitBuyPrice,-1),Null),"Limit Buy Price");
AddColumn(IIf(Buy,BuyPrice,Null),"Buy Price");
AddColumn(IIF(Sell0,LimitSellprice,Null),"Limit Sell Price");
AddColumn(IIf(Sell0,SellPrice0,Null),"Sell0 Price");
AddColumn(IIf(Sell0,BarsSince(Buy),Null), "Bars Since Buy");
AddColumn(IIf(Sell0 AND isSellTooLate, 1,Null), "sold too late");
AddColumn(OpenPriceOfTheMaxBar, "Open Price of Max Day");
AddColumn(SellPrice,"Final sold price");
SetSortColumns(1,2);