@fxshrat I have been working to understand this code and have expanded on it through my own trial and error. Thank you for your initial help 
I am running into an issue with stops not triggering correctly, and am hoping you might be able to point me in the right direction.
I will include the code here but the basic idea is building off of your inside bar clusters, taking positions in the direction of the trend on a break of the inside bar cluster. A points based stop loss is set to exceed the prior bar low as of the entry bar and a target stop is also points based, but calculates a percentage of the inside bar cluster size from high to low.
/// InsideBarClustersLongShort - CL
SetBacktestMode(backtestRegular);
SetOption("ActivateStopsImmediately", 0);
SetOption("AllowSameBarExit", True);
SetPositionSize(1, spsShares);
SetTradeDelays(0, 0, 0, 0);
TickSize = 0.01;
Slippage = 0.05;
InsideBarLong = High <= Ref(High, -1) AND Low >= Ref(Low, -1);
BuyPrice = Close;
Buy = InsideBarLong;
Sell = 0;
LongAmount1 = Ref(H,-1) - BuyPrice;
LongAmount2 = BuyPrice - Ref(L,-1);
ApplyStop(stopTypeProfit, stopModePoint, LongAmount1 + TickSize, 1);
ApplyStop(stopTypeLoss, stopModePoint, LongAmount2 + TickSize, 1);
eq = Equity(1,0);
intradeLong = Flip(Buy,Sell);
HHLong = IIf(intradeLong OR Sell>0, ValueWhen(Buy, Ref(H,-1)), Null);
LLLong = IIf(intradeLong OR Sell>0, ValueWhen(Buy, Ref(L,-1)), Null);
// Check Instrument Trend
IndexLongMAPeriods = 350; //Optimize("TrendMA", 350, 10, 400, 10);
IndexLongMA = Ref(MA(C, IndexLongMAPeriods), -1);
IndexTrendUp = Ref(C, -1) > IndexLongMA;
// Open in Balance
OpenInBalanceLong = O < HHLong;
// Cluster Percent
LongClusterSize = (HHLong - LLLong)/Ref(C,-1);
LongClusterPercentThreshold = 8; //Optimize("LongClusterPercent", 10, 5, 15, .5);
LongClusterSizeOkay = LongClusterSize <= (LongClusterPercentThreshold/100);
//Breakout
dn_breakLong = Sell==2;
up_breakLong = Sell==3;
LongEntryTrigger = IndexTrendUp AND OpenInBalanceLong AND LongClusterSizeOkay AND up_breakLong;
Buy = LongEntryTrigger;
BuyPrice = HHLong + Slippage;
Sell = 0;
LongTargetPercent = 90; //Optimize("LongTargetPercent", 90, 50, 110, 5);
LongTarget = (HHLong - LLLong) * (LongTargetPercent/100);
LongStopLevel = Ref(L, -1) - Slippage;
LongStopPoints = BuyPrice - LongStopLevel;
ApplyStop(stopTypeProfit, stopModePoint, LongTarget, 1);
ApplyStop(stopTypeLoss, stopModePoint, LongStopPoints, 1);
// SHORT RULES
InsideBarShort = High <= Ref(High, -1) AND Low >= Ref(Low, -1);
ShortPrice = Close;
Short = InsideBarShort;
Cover = 0;
ShortAmount1 = Ref(H,-1)- ShortPrice;
ShortAmount2 = ShortPrice - Ref(L,-1);
ApplyStop(stopTypeProfit, stopModePoint, ShortAmount2 + TickSize, 1);
ApplyStop(stopTypeLoss, stopModePoint, ShortAmount1 + TickSize, 1);
eq = Equity(1,0);
intradeShort = Flip(Short, Cover);
HHShort = IIf(intradeShort OR Cover > 0, ValueWhen(Short, Ref(H,-1)), Null);
LLShort = IIf(intradeShort OR Cover > 0, ValueWhen(Short, Ref(L,-1)), Null);
// Short Index Trend
IndexShortMAPeriods = 20; //Optimize("ShortMA", 20, 10, 400, 10);
IndexShortMA = Ref(MA(C, IndexShortMAPeriods), -1);
IndexTrendDown = Ref(C, -1) < IndexShortMA;
// Open in Balance
OpenInBalanceShort = O > LLShort;
// Short Cluster Percent
ShortClusterSize = (HHShort - LLShort)/Ref(C,-1);
ShortClusterPercentThreshold = 8; //Optimize("ShortClusterPercent", 10, 5, 15, .5);
ShortClusterSizeOkay = ShortClusterSize <= (ShortClusterPercentThreshold/100);
//Breakout
dn_breakShort = Cover==3;
up_breakShort = Cover==2;
ShortEntryTrigger = IndexTrendDown AND OpenInBalanceShort AND ShortClusterSizeOkay AND dn_breakShort;
Short = ShortEntryTrigger;
ShortPrice = LLShort - Slippage;
Cover = 0;
ShortTargetPercent = 90; //Optimize("ShortTargetPercent", 90, 50, 110, 5);
ShortTarget = (HHShort - LLShort) * (ShortTargetPercent/100);
ShortStopLevel = Ref(H, -1) + Slippage;
ShortStopPoints = ShortStopLevel - ShortPrice;
ApplyStop(stopTypeProfit, stopModePoint, ShortTarget, 1);
ApplyStop(stopTypeLoss, stopModePoint, ShortStopPoints, 1);
LongSetup = InsideBarLong AND IndexTrendUP;
ShortSetup = InsideBarShort AND IndexTrendDown;
Filter = LongSetup OR ShortSetup;
AddColumn(Iif(Ref(C, -1) > Ref(IndexLongMA, -1), 1, 0), "LONG", 1, colorGreen);
AddColumn(IIf(Ref(C, -1) < Ref(IndexShortMA, -1), 1, 0), "SHORT", 1, colorRed);
IIf(LongSetup, AddColumn(HHLong + TickSize, "Long - Buy Stop", 1.2, colorGreen), Null);
IIf(LongSetup, AddColumn(HHLong + TickSize + Slippage, "Long - Buy Limit", 1.2, colorGreen), Null);
IIf(LongSetup, AddColumn(HHLong + LongTarget, "Long - Target", 1.2, colorGreen), Null);
IIf(LongSetup, AddColumn(HHLong - LongStopPoints + (2 * Slippage) - TickSize, "Long - Stop (DAILY ADJUSTMENT)", 1.2, colorGreen), Null);
IIf(ShortSetup, AddColumn(LLShort - TickSize, "Short - Sell Stop", 1.2, colorRed), Null);
IIf(ShortSetup, AddColumn(LLShort - TickSize - Slippage, "Short - Sell Limit", 1.2, colorRed), Null);
IIf(ShortSetup, AddColumn(LLShort - ShortTarget, "Short - Target", 1.2, colorRed), Null);
IIf(ShortSetup, AddColumn(LLShort + ShortStopPoints - (2 * Slippage) + TickSize, "Short - Stop (DAILY ADJUSTMENT)", 1.2, colorRed), Null);
The code seems to work for me on a majority of trades but I have isolated one example that is not making sense to me.


Please see the trade on 6/21/2021. The entry price is correct, but the stop is triggered as a "(max loss)", despite the trade being profitable, and nowhere near the stop loss point. The trade is then exited on the following day's open...? The market continued higher in the days following and would have successfully reached the intended target. I am confused as a number of bars later, 7/28/2021, another long is entered and the code works correctly.
On a side note, I have also seen that if the entry bar low exceeds the prior bar low, it will trigger a stop which is filled the following day's open as a max loss, but that should not be the case here as the low of the entry bar did not exceed the prior bar low. I have unchecked the box for "allow same bar exit / entry signal" in backtester settings and this does not seem to resolve the issue.
Any help would be greatly appreciated.