Hi, can some one pls help me with a scale in algo in amibroker ?

#1
Hi fellow traders, i am trying to work a basic bollinger breakout system using an ATR multiple as SL. What i want to do is scale in 3 times if i get multiple buy signals before my ATR SL is hit. below is a copy of my code, will appreciate if someone could help me get the last part, i.e. scaling in right... i want to scale in with 100% of my expected position size on first trade, 50% in second and only 25% in third. any other buys are to be ignored and Highest High since buy - (ATR * multiple ) as my Trailing SL.



//Initial Parameters
//SetBacktestMode( backtestRotational );
//SetBacktestMode( backtestRegularRawMulti );

//--------------------------------------------- TRADE DELAY ----------------------------------------------//
SetTradeDelays( 1,1,1,1); // Initiates on Next day Open & Stops on Next Day Open
BuyPrice=Open;
//--------------------------------------------------------------------------------------------------------//


//---------------------------------------- Initial Capital & Risk ----------------------------------------//
SetOption("InitialEquity", 100000);
SetOption( "UsePrevBarEquityForPosSizing", 1 ); //*set the use of last bars
SetOption("MinShares",1); // min no of shares to purchase //
RiskPerShare = 3 * ATR( 14 ); //Risk Per share //
PositionRisk = 5; // risk 5% of entire equity on single trade //
PctSize = PositionRisk * BuyPrice / RiskPerShare; // position size calculation - no of shares//
SetPositionSize( PctSize, spsPercentOfEquity ); //set position size)
SetOption( "AllowPositionShrinking", True ); // Shrink Position on Loss //
RoundLotSize = 1; // Lot Size //
SetOption("RefreshWhenCompleted",True); // Refresh when completed //
//--------------------------------------------------------------------------------------------------------//



//--------------------------------------------- COMMISSIONS ----------------------------------------------//
SetOption("CommissionMode", 1); // Percent Mode
SetOption("CommissionAmount",0.25); // 0.25% of Trade Value
//--------------------------------------------------------------------------------------------------------//



//-------------------------------------------- Max Positions ---------------------------------------------//
Totalpositions = 50; // Max no of positions allowed (just change here)//
SetOption("MaxOpenPositions", Totalpositions ); // formula for max open positions
//--------------------------------------------------------------------------------------------------------//



//-------------------------------------- System Buy/Sell Rules ------------------------------------------//

BuySignal = Close > BBandTop(Close, 14, 2)
AND RSI( 14 ) > 65;

SellSignal = 0;

//--------------------------------------------------------------------------------------------------------//




//-------------------------------------------- Trailing Stop ---------------------------------------------//

ApplyStop( stopTypeTrailing, stopModePoint, RiskPerShare, 2, True, 0);
//--------------------------------------------------------------------------------------------------------//




//---------------------------------------------- Scaling In ----------------------------------------------//

x = SumSince(ApplyStop( stopTypeTrailing, stopModePoint, RiskPerShare, 2, True, 0), BuySignal);
xSigs = x <=3;

Buy = IIf(BuySignal AND xSigs, sigScaleIn, 0);
Sell = SellSignal;
//--------------------------------------------------------------------------------------------------------//




//---------------------------------------------- Plotting ------------------------------------------------//

Equity (1,0);
inTrade = Flip(Buy, Sell); //True when Buy, False when Sell
SetOption("EveryBarNullCheck", True); // checks for null bars
stopline = IIf(intrade, HighestSince(Buy, H - RiskPerShare), Null);
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot( Close,"Price",coloryellow,styleBar);
Plot( stopline, "trailing stop line", colorRed );
//------------------------------------------------------------------------------------------------------**//