If in case you want to add to your existing open positions to your systematic trading then assign sigScaleIn to the BUY/SHORT variable if you want to scale-in (increase the size of) LONG/SHORT position.
sigScaleIn is a special value assign to BUY/SHORT value to add to the existing positions. Here is a prototype AFL code that performs automated scale-in when the buy signal continues and also 39-EMA hook reversal happens.
What is 39-EMA hook reversal?
Is is a price zone where the previous candle breaks down below the 39-EMA followed by the candle which closes above 39EMA
buycontinue = Flip(Buy,Sell);
//Scalein Conditions
pyramid = buycontinue AND Ref(C,-1) < EMA(Close,39) AND Close > EMA(Close,39);
Position Sizing
Position Sizing needs to be done accordingly. If Buy = True then regular positionsize should happen For Example Purchasing 2,00,000 worth of shares and during scalein 1,00,000 worth of shares will be added to the existing position.
pyramidposition = IIf(Buy == sigScaleIn , 10000, 20000);
SetPositionSize( pyramidposition, spsValue);
Here is the complete Amibroker AFL Code
_SECTION_BEGIN("Scalein Positions at Hooked 39-EMA Reversals");
//Candle and Axis Plot
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
//Buy and Sell without pyramiding
Buy = Cross(EMA(Close,20), EMA(Close,50));
Sell = Cross(EMA(Close,50), EMA(Close,20));
buycontinue = Flip(Buy,Sell);
//Scalein Conditions
pyramid = buycontinue AND Ref(C,-1) < EMA(Close,39) AND Close > EMA(Close,39);
Plot(EMA(Close,20),"EMA20",colorRed,styleLine);
Plot(EMA(Close,50),"EMA50",colorblue,styleLine);
Plot(EMA(Close,39),"EMA39",coloryellow,styleLine);
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(pyramid, shapeCircle, shapeNone),colorGreen, 0, L, Offset=-40);
SetTradeDelays(0,0,0,0);
//Modified Buy Rules with ScaleIn at 39-EMA Hook Reversals
Buy = Buy + pyramid*sigScaleIn;
BuyPrice = Close;
SellPrice = Close;
pyramidposition = IIf(Buy == sigScaleIn , 10000, 20000);
SetPositionSize( pyramidposition, spsValue);
_SECTION_END();
References
Pyramiding (scaling in/out) and multiple currencies in the portfolio backtester