Scale-In / Scale-Out : How to implement in AmiBroker Auto-Trading like IB or the other auto-trading platform

Hi
I try to write code to scale-in and scale-out in backtest and exploration function as code below.

The code below, I study from this link and change slightly as my system trading logic.
http://www.amibroker.com/members/library/formula.php?id=1380


SetTradeDelays( 0, 0, 0, 0 );
RoundLotSize  = 1;
SetChartOptions( 0, chartShowArrows | chartShowDates );
SetOption( "initialequity", 1000000 );
//SetBarsRequired( -2, 0 );
SetOption( "AllowPositionShrinking", True );
SetOption( "AllowSameBarExit", False );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 0 );
SetOption( "FuturesMode", 1 );
SetOption( "InterestRate", 0 );
SetOption( "MaxOpenPositions", 1 );
SetOption( "MinPosValue", 0 );
SetOption( "PriceBoundChecking", False );
SetOption( "ReverseSignalForcesExit", True );
SetOption( "UsePrevBarEquityForPosSizing", False );
SetOption( "MinShares", RoundLotSize );
SetOption( "ActivateStopsImmediately", True );
TickSize      = 0.1;
PointValue    = 200;
MarginDeposit = 10000;
PositionSize  = MarginDeposit ;



//Initialize Signal Array
Buy = 0;
Sell = 0;
Short = 0;
Cover = 0;
BuyPrice = O;
SellPrice = O;
ShortPrice = O;
CoverPrice = O;



FirstScaleIn = 10;
SecondScaleIn = 6;
ThirdScaleIn = 4;


SetTradeDelays(0, 0, 0, 0);


Sell=EMA(C,10)<EMA(C,100) ;
BarsSinceSell = BarsSince(Sell);


FirstEntry=EMA(C,10)>EMA(C,100) ;// AND C> xMiddle ;
InFirstPos = Flip(FirstEntry, Sell);
FirstTrigger = ExRem(InFirstPos, Sell);
BarsSinceFirstTrigger = BarsSince(FirstTrigger);
FirstTriggerPrice = IIf(BarsSinceFirstTrigger < BarsSinceSell, Ref(C,-BarsSinceFirstTrigger), 0 );



SecondEntry= ( EMA(C,10)< EMA(C,25) AND EMA(C,10)>EMA(C,50) )  AND InFirstPos AND Ref(InFirstPos,-1);
InSecondPos = Flip(SecondEntry, Sell);
SecondTrigger = ExRem(InSecondPos, Sell);
BarsSinceSecondTrigger = BarsSince(SecondTrigger);
SecondTriggerPrice = IIf(BarsSinceSecondTrigger < BarsSinceSell, Ref(C,-BarsSinceSecondTrigger), 0);


ThirdEntry = ( EMA(C,10)< EMA(C,50) AND EMA(C,10)>EMA(C,100) )  AND InSecondPos AND Ref(InSecondPos,-1);
InThirdPos = Flip(ThirdEntry, Sell);
ThirdTrigger = ExRem(InThirdPos, Sell);
BarsSinceThirdTrigger = BarsSince(ThirdTrigger );
ThirdTriggerPrice = IIf(BarsSinceThirdTrigger < BarsSinceSell, Ref(C,-BarsSinceThirdTrigger), 0);



//if( FirstScaleIn + SecondScaleIn + ThirdScaleIn + FourthScaleIn == 1.0 )
if( FirstScaleIn + SecondScaleIn + ThirdScaleIn  == 20 )
{
  //PositionScore = PositionScore = 100 - CurrentRSI ;  // favour low RSI

  Buy = IIf( FirstTrigger, 1, 
             IIf( SecondTrigger OR ThirdTrigger, sigScaleIn, 0  ) );

  SetPositionSize(IIf( FirstTrigger, FirstScaleIn, 
                      IIf( SecondTrigger, SecondScaleIn, 
                         ThirdScaleIn ) ) , 
                  //IIf( Buy > 0, spsPercentOfEquity, spsNoChange ) );
                  IIf( Buy > 0, spsShares, spsNoChange ) );

  Sell = ExRem(Sell,Buy); //Most important in case sell at once buy buy multiple time
}

//Filter=Buy OR sell;
Filter=FirstTrigger or SecondTrigger or ThirdTrigger or Sell; 
//Filter=1;

AddColumn(FirstEntry, "1Entry", 1);
AddColumn(InFirstPos , "1Pos", 1);
AddColumn(FirstTrigger, "1Trg", 1);
AddColumn(BarsSinceFirstTrigger , "1Bar", 1);
AddColumn(FirstTriggerPrice , "1P", 1.2);


AddColumn(SecondEntry, "2Entry", 1);
AddColumn(InSecondPos , "2Pos", 1);
AddColumn(SecondTrigger, "2Trg", 1);
AddColumn(BarsSinceSecondTrigger , "2Bar", 1);
AddColumn(SecondTriggerPrice , "2P", 1.2);

AddColumn(ThirdEntry, "3Entry", 1);
AddColumn(InThirdPos , "3Pos", 1);
AddColumn(ThirdTrigger, "3Trg", 1);
AddColumn(BarsSinceThirdTrigger , "3Bar", 1);
AddColumn(ThirdTriggerPrice , "3P", 1.2);


AddColumn(Sell, "Sell", 1);
AddColumn(BarsSinceSell,"BarsSell",1);

The above is just coded in backtest function.
some code below I am not sure whether they are supported in any auto-trading platform or not


  Buy = IIf( FirstTrigger, 1, 
             IIf( SecondTrigger OR ThirdTrigger, sigScaleIn, 0  ) );


SetPositionSize(IIf( FirstTrigger, FirstScaleIn, 
                      IIf( SecondTrigger, SecondScaleIn, 
                         ThirdScaleIn ) ) , 
                  //IIf( Buy > 0, spsPercentOfEquity, spsNoChange ) );
                  IIf( Buy > 0, spsShares, spsNoChange ) );

How to implement in AmiBroker Auto-Trading like IB or the other auto-trading platform.
Please guide the solution for implementing in the real world, it would be better if anyone can show sample code or reference link for more research.

Thank you in advance.
Pongtorn