Help with Scale-in and Trailing Stop

Hello fellow traders,

I'm new to Amibroker and this is my first post! I've been learning about systematic trading systems and implemented the toy system below. Almost everything is working as expected (I believe), except the scale-in implementation. I'd like to scale-in the position whenever there is a breakeven - i.e. the trailing stop goes above the buy price or most recent scale-in buy price. So, it would be something like this. The Yellow arrow represents the scale-in trigger, and since there the buy delay is set to 1 at open, the shares would be acquired on the next trading day.



Also, the scale in should follow the position sizing rules already defined in the system. As you can see, both the entry point for scale in and the scaling itself are wrong. Not sure if the bug are. Tralling stop? Scale-in implementation? Both? After working on this for several hours (I'm a beginner), I came here to ask for help! Could you please provide me a code example on how to accomplish that? Also, if you spot additional problems/bugs/mistakes in my code, could you please comment/fix?

Additionally, could you please recommend a good book or tutorial on AFL?

Thank you very much!

// Backtest Setup

SetOption("InitialEquity", 5000);
SetOption("MaxOpenPositions", 1000);
SetOption("AccountMargin", 100);
SetOption("CommissionMode", 2);
SetOption("CommissionAmount", 0);
SetOption("MinShares", 1);
SetOption("AllowSameBarExit", True);

SetTradeDelays(1, 0, 1, 1);

BuyPrice = Open;

// Entry Triggers

EntryTrigger_1 = Cross(C, BBandTop(C, 20, 2));
EntryTrigger_2 = C - Ref(C, -1) > 3 * ATR(14);
EntryTrigger = EntryTrigger_1 OR EntryTrigger_2;

// Buy (Long)

Buy = EntryTrigger;
Sell = 0;

// Sell (Chandelier ATR Stop-Loss)

RiskPerShare = 3 * ATR(14);

// Chandelier ATR Stop-Loss plot

TrailStopArr = Null;
TrailStop = 0;

for(i = 1; i < BarCount; i++)
{
	if(TrailStop == 0 AND Buy[i])
		TrailStop = Close[i] - RiskPerShare[i];
	else
		Buy[i] = 0;
		
	if(TrailStop > 0 AND Low[i] < TrailStop)
	{
		Sell[i] = 1;
		SellPrice[i] = TrailStop;
		TrailStop = 0;
	}

	if(TrailStop > 0)
	{
		TrailStop = Max(Close[i] - RiskPerShare[i], TrailStop);
		TrailStopArr[i] = TrailStop;
	}
}

// Scale-in

Breakeven = 0;

for(i = 1; i < BarCount; i++)
{
	if(Buy[i])
		Breakeven = BuyPrice[i];
		
	if(TrailStopArr[i] >= Breakeven)
        {
		Buy[i] = Buy[i] + sigScaleIn;
                Breakeven = BuyPrice[i];
        }
}

// Plot arrows

PlotShapes(Buy * shapeUpArrow, colorGreen, 0, Low);
PlotShapes(Sell * shapeDownArrow, colorRed, 0, High);

// Plot trailing stop

Plot(TrailStopArr, "Chandelier ATR Stop-Loss", colorRed, styleDots);

// Position Sizing

PercentRiskPerTrade = 0.5;
PercentSize = PercentRiskPerTrade * BuyPrice / Ref(RiskPerShare, -1);

RoundLotSize = 1;
SetPositionSize(PercentSize, spsPercentOfEquity);

// Position Score

PositionScore = PercentSize;

// Exploration

Filter = Buy OR Sell;

AddColumn( IIf(Buy, 'B', IIf( Sell, 'S', 'N') ), "Which Signal", formatchar, IIf( Buy, colorGreen, IIf( Sell, colorRed, colorBlue)) ); 
AddColumn(Close, "Close Price");
AddColumn(TrailStopArr, "Initial Stop-Loss");

@angelo5d0 - see below links for your reference.

See Pyramiding (Scale In/Out) Doc.

Thank you for your help!