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.

How to use Amibroker to Parsing the Position Book…

This is a simple tutorial that explains how to access the position book using Amibroker and get various details about the position book details...
Rajandran R
2 min read

Mini Certification Course on Algorithmic Trading Strategies

If you are new to Automated trading? This certification course will help you to kickstart your automated trading with your broker and get to...
Rajandran R
1 min read

Algomojo Index Straddle/Strangle Execution Module with Intraday Stoploss

This tutorial focus on how to automate your index straddle/strangle strategy with intraday stop-loss levels with time-based entry and exits using Algomojo Platform and...
Rajandran R
7 min read

Leave a Reply

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