Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Scale-In Positions Explained – Amibroker AFL Code

1 min read

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

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

What is QuantZilla?

Quantzilla is 75+ hours of code mentoring program designed for noncoders who want to learn the coding program right from designing indicators, scanners, trading...
Rajandran R
58 sec read

Things to Consider While Building a Bracket Order Execution…

Building a Bracket Order Strategy is the most demanded request we got from most of the algomojo traders. This article explains the list of...
Rajandran R
12 min read

PlaceOrder – Multi Broker Exectuion Code Snippets for Amibroker

This tutorial explains how to build a button trading right from scratch and with a click of a button how you can send orders...
Rajandran R
7 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More