AFL code to buy a stock multiple times if all buy conditions met along with ATR stop

I have a simple system to buy say macd > signal line and then using an ATR trailing stop loss

my problem is when i get multiple buy signals (on same stock; back to back), every 2nd signal is shown as a sell instead of adding to position. so if a stock has 2 buy signals before a big run up, i miss the entire run up cause the 2nd buy was treated as a sell.

I would like to add to the position on every buy signal and keep using the the SL highest since buy - (atr x 2) since the latest buy date.

i would really appreciate if someone could help with this last leg to complete my code.

sample code

//Initial Parameters

SetTradeDelays( 1,1,1,1);
SetOption("InitialEquity", 1000000); 
SetOption("MaxOpenPositions", 35 ); 	// no of open positions //
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("CommissionMode", 1); // comms mode
SetOption("CommissionAmount",0.15); // comms rate in %
SetOption("RefreshWhenCompleted",True); // Refresh when completed //
BuyPrice=Open;
Totalpositions = 35;
SetOption("MaxOpenPositions", Totalpositions );

Buy = "some buy parameters"
Sell = 0;

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

/* Plotting the Trailing Stop */
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);

thanks,

Gautam

@gautamlaungani I am not sure I understand what you want to accomplish but it looks like you need to learn about "Scale trading", Scale In and perhaps Scale Out. There were a lot of good links and learning examples and instruction in this previous forum post. Read through those instructions and code a few of those examples. By then you should be able to solve your issue but if not come back to the forum with your attempts and questions about why it is not working.

Hi Larry, thank you very much for your input. Think scale in should do the trick but since i am new to AFL, still grappling with the formula and usage. Will revert if i really cannot understand. thanks again.

Hi @portfoliobuilder , the video link you sent was very helpful. scale in function seems to be working and as expected, i am seeing better results too. Thanks again for your help. cheers