Scale in based in price

Good evening.
I'm trying to implement the scale in my strategy, I buy with PS and the scale in it will be triggered if the contract price is higher than 0.5 * ATR on the day it entered the BUY.
For example: Buy = U $ 11.00 and ATR of 0.50. If the price of the asset rises to 11.25, I call the scale in, recalculate the same method of PS, and pass the information to buy more contracts. I should now consider the entry price as 11.25 if it goes up 0.5 * ATR, that is, go to $ 11.50 the scale in is triggered again and so on.

I started thinking about low level implementation as follows, but there is something wrong with the code, you can give me a way to go, tips. Thank you !

SetOption("UseCustomBacktestProc", True ); 

if( Status("action") == actionPortfolio )
{
  bo = GetBacktesterObject();
 
  bo.PreProcess(); // Initialize backtester
 
  for(bar=0; bar < BarCount; bar++)
  {
   bo.ProcessTradeSignals( bar );
  
   CurEquity = bo.Equity;
  
   for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
   {
   
    price = pos.GetPrice( bar, "C" );
    symbolATR = StaticVarGet( pos.Symbol + "ATR" );
    price1 = pos.EntryPrice;
    positionrisk = 0.01; 
	investi = ((100*positionrisk*CurEquity)/symbolATR[bar]*pos.PointValue);
	investi1 = investi*pos.MarginDeposit;
    if( price > (price1 + 0.1*symbolATR[0]));
    {
     bo.ScaleTrade( bar, pos.Symbol, investi > 0, price, investi1, MarginDeposit);
    }
   }
  }
  bo.PostProcess(); // Finalize backtester
}

Why don't you just use this: http://www.amibroker.com/guide/h_pyramid.html
Low-level custom backtester is not really for beginners.

Thomas, good afternoon.

I started using these examples as a base but I have a problem that I need to store the value of the purchase of the first ScaleIn to determine if the next one will happen or not. Since it takes only the price of when the Buy occurred.

Follow the idea that I used and realize this problem, you have any suggestions that I can use!

PositionRisk = 0.01;
pct = (MarginDepositPositionRisk100 / (PointValue * ATR(20)));
AtrI = ValueWhen( Buy, ATR(20));
DoScaleIn = C > (ValueWhen(Buy, C) + 0.5*AtrI);// This value here needs to be fulfilled in every scale in which I take as my basis of decision, whether there will be another entry in the negotiation or not.
Buy = Buy + SigScaleIn * DoScaleIn;
SetPositionSize( pct, spsPercentOfEquity );
SetPositionSize( 10, spsShares * ( Buy == sigScaleIn ));

Did you end up figuring this out?